1 // Copyright (c) 2001-2011 Hartmut Kaiser 2 // 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) 5 6 #if !defined(BOOST_SPIRIT_KARMA_BOOL_POLICIES_SEP_28_2009_1203PM) 7 #define BOOST_SPIRIT_KARMA_BOOL_POLICIES_SEP_28_2009_1203PM 8 9 #if defined(_MSC_VER) 10 #pragma once 11 #endif 12 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> 17 18 namespace boost { namespace spirit { namespace karma 19 { 20 /////////////////////////////////////////////////////////////////////////// 21 // 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 25 // 26 // struct special_bool_policy : karma::bool_policies<> 27 // { 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) 32 // { 33 // return string_inserter<CharEncoding, Tag>::call(sink, "eurt"); 34 // } 35 // }; 36 // 37 // typedef karma::bool_generator<special_bool_policy> backwards_bool; 38 // 39 // karma::generate(sink, backwards_bool(), false); // will output: eurt 40 // 41 /////////////////////////////////////////////////////////////////////////// 42 template <typename T = bool> 43 struct bool_policies 44 { 45 /////////////////////////////////////////////////////////////////////// 46 // Expose the data type the generator is targeted at 47 /////////////////////////////////////////////////////////////////////// 48 typedef T value_type; 49 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; 57 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`. 65 // 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> 72 static bool callboost::spirit::karma::bool_policies73 call (OutputIterator& sink, T n, Policies const& p) 74 { 75 return Inserter::call_n(sink, n, p); 76 } 77 78 /////////////////////////////////////////////////////////////////////// 79 // Print the textual representations of a true boolean value 80 // 81 // sink The output iterator to use for generation 82 // b The boolean value to convert. 83 // 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. 88 // 89 /////////////////////////////////////////////////////////////////////// 90 template <typename CharEncoding, typename Tag, typename OutputIterator> generate_trueboost::spirit::karma::bool_policies91 static bool generate_true(OutputIterator& sink, T) 92 { 93 return string_inserter<CharEncoding, Tag>::call(sink, "true"); 94 } 95 96 /////////////////////////////////////////////////////////////////////// 97 // Print the textual representations of a false boolean value 98 // 99 // sink The output iterator to use for generation 100 // b The boolean value to convert. 101 // 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. 106 // 107 /////////////////////////////////////////////////////////////////////// 108 template <typename CharEncoding, typename Tag, typename OutputIterator> generate_falseboost::spirit::karma::bool_policies109 static bool generate_false(OutputIterator& sink, T) 110 { 111 return string_inserter<CharEncoding, Tag>::call(sink, "false"); 112 } 113 }; 114 115 }}} 116 117 #endif 118