bool_policies.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #if !defined(BOOST_SPIRIT_KARMA_BOOL_POLICIES_SEP_28_2009_1203PM)
  6. #define BOOST_SPIRIT_KARMA_BOOL_POLICIES_SEP_28_2009_1203PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/spirit/home/support/char_class.hpp>
  11. #include <boost/spirit/home/karma/generator.hpp>
  12. #include <boost/spirit/home/karma/char.hpp>
  13. #include <boost/spirit/home/karma/numeric/detail/numeric_utils.hpp>
  14. namespace boost { namespace spirit { namespace karma
  15. {
  16. ///////////////////////////////////////////////////////////////////////////
  17. //
  18. // bool_policies, if you need special handling of your boolean output
  19. // just overload this policy class and use it as a template
  20. // parameter to the karma::bool_generator boolean generator
  21. //
  22. // struct special_bool_policy : karma::bool_policies<>
  23. // {
  24. // // we want to spell the names of false as eurt (true backwards)
  25. // template <typename CharEncoding, typename Tag
  26. // , typename OutputIterator>
  27. // static bool generate_false(OutputIterator& sink, bool)
  28. // {
  29. // return string_inserter<CharEncoding, Tag>::call(sink, "eurt");
  30. // }
  31. // };
  32. //
  33. // typedef karma::bool_generator<special_bool_policy> backwards_bool;
  34. //
  35. // karma::generate(sink, backwards_bool(), false); // will output: eurt
  36. //
  37. ///////////////////////////////////////////////////////////////////////////
  38. template <typename T = bool>
  39. struct bool_policies
  40. {
  41. ///////////////////////////////////////////////////////////////////////
  42. // Expose the data type the generator is targeted at
  43. ///////////////////////////////////////////////////////////////////////
  44. typedef T value_type;
  45. ///////////////////////////////////////////////////////////////////////
  46. // By default the policy doesn't require any special iterator
  47. // functionality. The boolean generator exposes its properties
  48. // from here, so this needs to be updated in case other properties
  49. // need to be implemented.
  50. ///////////////////////////////////////////////////////////////////////
  51. typedef mpl::int_<generator_properties::no_properties> properties;
  52. ///////////////////////////////////////////////////////////////////////
  53. // This is the main function used to generate the output for a
  54. // boolean. It is called by the boolean generator in order
  55. // to perform the conversion. In theory all of the work can be
  56. // implemented here, but it is the easiest to use existing
  57. // functionality provided by the type specified by the template
  58. // parameter `Inserter`.
  59. //
  60. // sink: the output iterator to use for generation
  61. // n: the floating point number to convert
  62. // p: the instance of the policy type used to instantiate this
  63. // floating point generator.
  64. ///////////////////////////////////////////////////////////////////////
  65. template <typename Inserter, typename OutputIterator, typename Policies>
  66. static bool
  67. call (OutputIterator& sink, T n, Policies const& p)
  68. {
  69. return Inserter::call_n(sink, n, p);
  70. }
  71. ///////////////////////////////////////////////////////////////////////
  72. // Print the textual representations of a true boolean value
  73. //
  74. // sink The output iterator to use for generation
  75. // b The boolean value to convert.
  76. //
  77. // The CharEncoding and Tag template parameters are either of the type
  78. // unused_type or describes the character class and conversion to be
  79. // applied to any output possibly influenced by either the lower[...]
  80. // or upper[...] directives.
  81. //
  82. ///////////////////////////////////////////////////////////////////////
  83. template <typename CharEncoding, typename Tag, typename OutputIterator>
  84. static bool generate_true(OutputIterator& sink, T)
  85. {
  86. return string_inserter<CharEncoding, Tag>::call(sink, "true");
  87. }
  88. ///////////////////////////////////////////////////////////////////////
  89. // Print the textual representations of a false boolean value
  90. //
  91. // sink The output iterator to use for generation
  92. // b The boolean value to convert.
  93. //
  94. // The CharEncoding and Tag template parameters are either of the type
  95. // unused_type or describes the character class and conversion to be
  96. // applied to any output possibly influenced by either the lower[...]
  97. // or upper[...] directives.
  98. //
  99. ///////////////////////////////////////////////////////////////////////
  100. template <typename CharEncoding, typename Tag, typename OutputIterator>
  101. static bool generate_false(OutputIterator& sink, T)
  102. {
  103. return string_inserter<CharEncoding, Tag>::call(sink, "false");
  104. }
  105. };
  106. }}}
  107. #endif