format_manip.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_FORMAT_MANIP_MAY_03_2007_1424PM)
  6. #define BOOST_SPIRIT_KARMA_FORMAT_MANIP_MAY_03_2007_1424PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <iosfwd>
  11. #include <iterator>
  12. #include <string>
  13. #include <boost/spirit/home/karma/generate.hpp>
  14. #include <boost/spirit/home/support/iterators/ostream_iterator.hpp>
  15. #include <boost/core/scoped_enum.hpp>
  16. #include <boost/mpl/bool.hpp>
  17. ///////////////////////////////////////////////////////////////////////////////
  18. namespace boost { namespace spirit { namespace karma { namespace detail
  19. {
  20. ///////////////////////////////////////////////////////////////////////////
  21. #ifdef _MSC_VER
  22. # pragma warning(push)
  23. # pragma warning(disable: 4512) // assignment operator could not be generated.
  24. #endif
  25. template <typename Expr
  26. , typename CopyExpr = mpl::false_, typename CopyAttr = mpl::false_
  27. , typename Delimiter = unused_type, typename Attribute = unused_type>
  28. struct format_manip
  29. {
  30. // This assertion makes sure we don't hit the only code path which is
  31. // not implemented (because it isn't needed), where both, the
  32. // expression and the attribute need to be held as a copy.
  33. BOOST_SPIRIT_ASSERT_MSG(!CopyExpr::value || !CopyAttr::value
  34. , error_invalid_should_not_happen, ());
  35. format_manip(Expr const& xpr, Delimiter const& d, Attribute const& a)
  36. : expr(xpr), delim(d), pre(delimit_flag::dont_predelimit), attr(a) {}
  37. format_manip(Expr const& xpr, Delimiter const& d
  38. , BOOST_SCOPED_ENUM(delimit_flag) pre_delimit, Attribute const& a)
  39. : expr(xpr), delim(d), pre(pre_delimit), attr(a) {}
  40. Expr const& expr;
  41. Delimiter const& delim;
  42. BOOST_SCOPED_ENUM(delimit_flag) const pre;
  43. Attribute const& attr;
  44. };
  45. template <typename Expr, typename Delimiter, typename Attribute>
  46. struct format_manip<Expr, mpl::false_, mpl::true_, Delimiter, Attribute>
  47. {
  48. format_manip(Expr const& xpr, Delimiter const& d, Attribute const& a)
  49. : expr(xpr), delim(d), pre(delimit_flag::dont_predelimit), attr(a) {}
  50. format_manip(Expr const& xpr, Delimiter const& d
  51. , BOOST_SCOPED_ENUM(delimit_flag) pre_delimit, Attribute const& a)
  52. : expr(xpr), delim(d), pre(pre_delimit), attr(a) {}
  53. Expr const& expr;
  54. Delimiter const& delim;
  55. BOOST_SCOPED_ENUM(delimit_flag) const pre;
  56. Attribute attr;
  57. };
  58. template <typename Expr, typename Delimiter, typename Attribute>
  59. struct format_manip<Expr, mpl::true_, mpl::false_, Delimiter, Attribute>
  60. {
  61. format_manip(Expr const& xpr, Delimiter const& d, Attribute const& a)
  62. : expr(xpr), delim(d), pre(delimit_flag::dont_predelimit), attr(a) {}
  63. format_manip(Expr const& xpr, Delimiter const& d
  64. , BOOST_SCOPED_ENUM(delimit_flag) pre_delimit, Attribute const& a)
  65. : expr(xpr), delim(d), pre(pre_delimit), attr(a) {}
  66. Expr expr;
  67. Delimiter const& delim;
  68. BOOST_SCOPED_ENUM(delimit_flag) const pre;
  69. Attribute const& attr;
  70. };
  71. #ifdef _MSC_VER
  72. # pragma warning(pop)
  73. #endif
  74. ///////////////////////////////////////////////////////////////////////////
  75. template <typename Expr, typename Enable = void>
  76. struct format
  77. {
  78. // Report invalid expression error as early as possible.
  79. // If you got an error_invalid_expression error message here,
  80. // then the expression (Expr) is not a valid spirit karma expression.
  81. // Did you intend to use the auto_ facilities while forgetting to
  82. // #include <boost/spirit/include/karma_format_auto.hpp>?
  83. BOOST_SPIRIT_ASSERT_MATCH(karma::domain, Expr);
  84. };
  85. template <typename Expr>
  86. struct format<Expr
  87. , typename enable_if<traits::matches<karma::domain, Expr> >::type>
  88. {
  89. typedef format_manip<Expr> type;
  90. static type call(Expr const& expr)
  91. {
  92. return type(expr, unused, unused);
  93. }
  94. };
  95. ///////////////////////////////////////////////////////////////////////////
  96. template <typename Expr, typename Delimiter, typename Enable = void>
  97. struct format_delimited
  98. {
  99. // Report invalid expression error as early as possible.
  100. // If you got an error_invalid_expression error message here,
  101. // then the expression (Expr) is not a valid spirit karma expression.
  102. // Did you intend to use the auto_ facilities while forgetting to
  103. // #include <boost/spirit/include/karma_format_auto.hpp>?
  104. BOOST_SPIRIT_ASSERT_MATCH(karma::domain, Expr);
  105. };
  106. template <typename Expr, typename Delimiter>
  107. struct format_delimited<Expr, Delimiter
  108. , typename enable_if<traits::matches<karma::domain, Expr> >::type>
  109. {
  110. typedef format_manip<Expr, mpl::false_, mpl::false_, Delimiter> type;
  111. static type call(
  112. Expr const& expr
  113. , Delimiter const& delimiter
  114. , BOOST_SCOPED_ENUM(delimit_flag) pre_delimit)
  115. {
  116. // Report invalid expression error as early as possible.
  117. // If you got an error_invalid_expression error message here,
  118. // then the delimiter is not a valid spirit karma expression.
  119. BOOST_SPIRIT_ASSERT_MATCH(karma::domain, Delimiter);
  120. return type(expr, delimiter, pre_delimit, unused);
  121. }
  122. };
  123. ///////////////////////////////////////////////////////////////////////////
  124. template<typename Char, typename Traits, typename Expr
  125. , typename CopyExpr, typename CopyAttr>
  126. inline std::basic_ostream<Char, Traits> &
  127. operator<< (std::basic_ostream<Char, Traits> &os
  128. , format_manip<Expr, CopyExpr, CopyAttr> const& fm)
  129. {
  130. karma::ostream_iterator<Char, Char, Traits> sink(os);
  131. if (!karma::generate (sink, fm.expr))
  132. {
  133. os.setstate(std::basic_ostream<Char, Traits>::failbit);
  134. }
  135. return os;
  136. }
  137. ///////////////////////////////////////////////////////////////////////////
  138. template<typename Char, typename Traits, typename Expr
  139. , typename CopyExpr, typename CopyAttr, typename Attribute>
  140. inline std::basic_ostream<Char, Traits> &
  141. operator<< (std::basic_ostream<Char, Traits> &os
  142. , format_manip<Expr, CopyExpr, CopyAttr, unused_type, Attribute> const& fm)
  143. {
  144. karma::ostream_iterator<Char, Char, Traits> sink(os);
  145. if (!karma::generate(sink, fm.expr, fm.attr))
  146. {
  147. os.setstate(std::basic_ostream<Char, Traits>::failbit);
  148. }
  149. return os;
  150. }
  151. template<typename Char, typename Traits, typename Expr
  152. , typename CopyExpr, typename CopyAttr, typename Delimiter>
  153. inline std::basic_ostream<Char, Traits> &
  154. operator<< (std::basic_ostream<Char, Traits> &os
  155. , format_manip<Expr, CopyExpr, CopyAttr, Delimiter> const& fm)
  156. {
  157. karma::ostream_iterator<Char, Char, Traits> sink(os);
  158. if (!karma::generate_delimited(sink, fm.expr, fm.delim, fm.pre))
  159. {
  160. os.setstate(std::basic_ostream<Char, Traits>::failbit);
  161. }
  162. return os;
  163. }
  164. ///////////////////////////////////////////////////////////////////////////
  165. template<typename Char, typename Traits, typename Expr
  166. , typename CopyExpr, typename CopyAttr, typename Delimiter
  167. , typename Attribute>
  168. inline std::basic_ostream<Char, Traits> &
  169. operator<< (std::basic_ostream<Char, Traits> &os
  170. , format_manip<Expr, CopyExpr, CopyAttr, Delimiter, Attribute> const& fm)
  171. {
  172. karma::ostream_iterator<Char, Char, Traits> sink(os);
  173. if (!karma::generate_delimited(sink, fm.expr, fm.delim, fm.pre, fm.attr))
  174. {
  175. os.setstate(std::basic_ostream<Char, Traits>::failbit);
  176. }
  177. return os;
  178. }
  179. }}}}
  180. #endif