1 // Copyright (c) 2001-2011 Hartmut Kaiser
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #if !defined(BOOST_SPIRIT_KARMA_BOOL_POLICIES_SEP_28_2009_1203PM)
7 #define BOOST_SPIRIT_KARMA_BOOL_POLICIES_SEP_28_2009_1203PM
13 #include <boost/spirit/home/support/char_class.hpp>
14 #include <boost/spirit/home/karma/generator.hpp>
15 #include <boost/spirit/home/karma/char.hpp>
16 #include <boost/spirit/home/karma/numeric/detail/numeric_utils.hpp>
18 namespace boost { namespace spirit { namespace karma
20 ///////////////////////////////////////////////////////////////////////////
22 // bool_policies, if you need special handling of your boolean output
23 // just overload this policy class and use it as a template
24 // parameter to the karma::bool_generator boolean generator
26 // struct special_bool_policy : karma::bool_policies<>
28 // // we want to spell the names of false as eurt (true backwards)
29 // template <typename CharEncoding, typename Tag
30 // , typename OutputIterator>
31 // static bool generate_false(OutputIterator& sink, bool)
33 // return string_inserter<CharEncoding, Tag>::call(sink, "eurt");
37 // typedef karma::bool_generator<special_bool_policy> backwards_bool;
39 // karma::generate(sink, backwards_bool(), false); // will output: eurt
41 ///////////////////////////////////////////////////////////////////////////
42 template <typename T = bool>
45 ///////////////////////////////////////////////////////////////////////
46 // Expose the data type the generator is targeted at
47 ///////////////////////////////////////////////////////////////////////
50 ///////////////////////////////////////////////////////////////////////
51 // By default the policy doesn't require any special iterator
52 // functionality. The boolean generator exposes its properties
53 // from here, so this needs to be updated in case other properties
54 // need to be implemented.
55 ///////////////////////////////////////////////////////////////////////
56 typedef mpl::int_<generator_properties::no_properties> properties;
58 ///////////////////////////////////////////////////////////////////////
59 // This is the main function used to generate the output for a
60 // boolean. It is called by the boolean generator in order
61 // to perform the conversion. In theory all of the work can be
62 // implemented here, but it is the easiest to use existing
63 // functionality provided by the type specified by the template
64 // parameter `Inserter`.
66 // sink: the output iterator to use for generation
67 // n: the floating point number to convert
68 // p: the instance of the policy type used to instantiate this
69 // floating point generator.
70 ///////////////////////////////////////////////////////////////////////
71 template <typename Inserter, typename OutputIterator, typename Policies>
73 call (OutputIterator& sink, T n, Policies const& p)
75 return Inserter::call_n(sink, n, p);
78 ///////////////////////////////////////////////////////////////////////
79 // Print the textual representations of a true boolean value
81 // sink The output iterator to use for generation
82 // b The boolean value to convert.
84 // The CharEncoding and Tag template parameters are either of the type
85 // unused_type or describes the character class and conversion to be
86 // applied to any output possibly influenced by either the lower[...]
87 // or upper[...] directives.
89 ///////////////////////////////////////////////////////////////////////
90 template <typename CharEncoding, typename Tag, typename OutputIterator>
91 static bool generate_true(OutputIterator& sink, T)
93 return string_inserter<CharEncoding, Tag>::call(sink, "true");
96 ///////////////////////////////////////////////////////////////////////
97 // Print the textual representations of a false boolean value
99 // sink The output iterator to use for generation
100 // b The boolean value to convert.
102 // The CharEncoding and Tag template parameters are either of the type
103 // unused_type or describes the character class and conversion to be
104 // applied to any output possibly influenced by either the lower[...]
105 // or upper[...] directives.
107 ///////////////////////////////////////////////////////////////////////
108 template <typename CharEncoding, typename Tag, typename OutputIterator>
109 static bool generate_false(OutputIterator& sink, T)
111 return string_inserter<CharEncoding, Tag>::call(sink, "false");