format_manip.hpp 4.4 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_FORMAT_MANIP_MAY_01_2007_1211PM)
  6. #define BOOST_SPIRIT_KARMA_FORMAT_MANIP_MAY_01_2007_1211PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/spirit/home/karma/generate.hpp>
  11. #include <boost/spirit/home/karma/generator.hpp>
  12. #include <boost/spirit/home/karma/detail/output_iterator.hpp>
  13. #include <boost/spirit/home/karma/stream/detail/format_manip.hpp>
  14. #include <boost/spirit/home/support/unused.hpp>
  15. ///////////////////////////////////////////////////////////////////////////////
  16. namespace boost { namespace spirit { namespace karma
  17. {
  18. ///////////////////////////////////////////////////////////////////////////
  19. template <typename Expr>
  20. inline typename detail::format<Expr>::type
  21. format(Expr const& expr)
  22. {
  23. return detail::format<Expr>::call(expr);
  24. }
  25. template <typename Expr, typename Attribute>
  26. inline detail::format_manip<Expr, mpl::false_, mpl::false_, unused_type, Attribute>
  27. format(
  28. Expr const& expr
  29. , Attribute const& attr)
  30. {
  31. using karma::detail::format_manip;
  32. // Report invalid expression error as early as possible.
  33. // If you got an error_invalid_expression error message here,
  34. // then the expression (expr) is not a valid spirit karma expression.
  35. BOOST_SPIRIT_ASSERT_MATCH(karma::domain, Expr);
  36. return format_manip<Expr, mpl::false_, mpl::false_, unused_type, Attribute>(
  37. expr, unused, attr);
  38. }
  39. ///////////////////////////////////////////////////////////////////////////
  40. template <typename Expr, typename Delimiter>
  41. inline typename detail::format_delimited<Expr, Delimiter>::type
  42. format_delimited(
  43. Expr const& expr
  44. , Delimiter const& d
  45. , BOOST_SCOPED_ENUM(delimit_flag) pre_delimit =
  46. delimit_flag::dont_predelimit)
  47. {
  48. return detail::format_delimited<Expr, Delimiter>::call(expr, d, pre_delimit);
  49. }
  50. template <typename Expr, typename Delimiter, typename Attribute>
  51. inline detail::format_manip<Expr, mpl::false_, mpl::false_, Delimiter, Attribute>
  52. format_delimited(
  53. Expr const& xpr
  54. , Delimiter const& d
  55. , BOOST_SCOPED_ENUM(delimit_flag) pre_delimit
  56. , Attribute const& attr)
  57. {
  58. using karma::detail::format_manip;
  59. // Report invalid expression error as early as possible.
  60. // If you got an error_invalid_expression error message here,
  61. // then the expression (expr) is not a valid spirit karma expression.
  62. BOOST_SPIRIT_ASSERT_MATCH(karma::domain, Expr);
  63. BOOST_SPIRIT_ASSERT_MATCH(karma::domain, Delimiter);
  64. return format_manip<Expr, mpl::false_, mpl::false_, Delimiter, Attribute>(
  65. xpr, d, pre_delimit, attr);
  66. }
  67. template <typename Expr, typename Delimiter, typename Attribute>
  68. inline detail::format_manip<Expr, mpl::false_, mpl::false_, Delimiter, Attribute>
  69. format_delimited(
  70. Expr const& xpr
  71. , Delimiter const& d
  72. , Attribute const& attr)
  73. {
  74. using karma::detail::format_manip;
  75. // Report invalid expression error as early as possible.
  76. // If you got an error_invalid_expression error message here,
  77. // then the expression (expr) is not a valid spirit karma expression.
  78. BOOST_SPIRIT_ASSERT_MATCH(karma::domain, Expr);
  79. BOOST_SPIRIT_ASSERT_MATCH(karma::domain, Delimiter);
  80. return format_manip<Expr, mpl::false_, mpl::false_, Delimiter, Attribute>(
  81. xpr, d, delimit_flag::dont_predelimit, attr);
  82. }
  83. ///////////////////////////////////////////////////////////////////////////
  84. template<typename Char, typename Traits, typename Derived>
  85. inline std::basic_ostream<Char, Traits> &
  86. operator<< (std::basic_ostream<Char, Traits> &os, generator<Derived> const& g)
  87. {
  88. typedef traits::properties_of<
  89. typename result_of::compile<karma::domain, Derived>::type
  90. > properties;
  91. typedef karma::ostream_iterator<Char, Char, Traits> outiter_type;
  92. outiter_type target_sink(os);
  93. karma::detail::output_iterator<outiter_type, properties> sink(target_sink);
  94. if (!g.derived().generate(sink, unused, unused, unused))
  95. {
  96. os.setstate(std::ios_base::failbit);
  97. }
  98. return os;
  99. }
  100. }}}
  101. #endif