format_manip.hpp 4.5 KB

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