auto.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_AUTO_NOV_29_2009_0339PM)
  6. #define BOOST_SPIRIT_KARMA_AUTO_NOV_29_2009_0339PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/spirit/home/support/common_terminals.hpp>
  11. #include <boost/spirit/home/support/info.hpp>
  12. #include <boost/spirit/home/support/container.hpp>
  13. #include <boost/spirit/home/support/assert_msg.hpp>
  14. #include <boost/spirit/home/support/detail/hold_any.hpp>
  15. #include <boost/spirit/home/karma/domain.hpp>
  16. #include <boost/spirit/home/karma/meta_compiler.hpp>
  17. #include <boost/spirit/home/karma/delimit_out.hpp>
  18. #include <boost/spirit/home/karma/generator.hpp>
  19. #include <boost/spirit/home/karma/auto/create_generator.hpp>
  20. #include <boost/mpl/bool.hpp>
  21. ///////////////////////////////////////////////////////////////////////////////
  22. namespace boost { namespace spirit
  23. {
  24. ///////////////////////////////////////////////////////////////////////////
  25. // Enablers
  26. ///////////////////////////////////////////////////////////////////////////
  27. template <>
  28. struct use_terminal<karma::domain, tag::auto_> // enables auto_
  29. : mpl::true_ {};
  30. template <typename A0>
  31. struct use_terminal<karma::domain // enables auto_(...)
  32. , terminal_ex<tag::auto_, fusion::vector1<A0> >
  33. > : mpl::true_ {};
  34. template <> // enables auto_(f)
  35. struct use_lazy_terminal<
  36. karma::domain, tag::auto_, 1 /*arity*/
  37. > : mpl::true_ {};
  38. }}
  39. ///////////////////////////////////////////////////////////////////////////////
  40. namespace boost { namespace spirit { namespace karma
  41. {
  42. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  43. using spirit::auto_;
  44. #endif
  45. using spirit::auto_type;
  46. ///////////////////////////////////////////////////////////////////////////
  47. template <typename Modifiers>
  48. struct auto_generator
  49. : generator<auto_generator<Modifiers> >
  50. {
  51. typedef mpl::int_<generator_properties::all_properties> properties;
  52. template <typename Context, typename Unused>
  53. struct attribute
  54. {
  55. typedef spirit::basic_hold_any<char> type;
  56. };
  57. auto_generator(Modifiers const& modifiers)
  58. : modifiers_(modifiers) {}
  59. // auto_generator has an attached attribute
  60. template <
  61. typename OutputIterator, typename Context, typename Delimiter
  62. , typename Attribute>
  63. bool generate(OutputIterator& sink, Context& context
  64. , Delimiter const& d, Attribute const& attr) const
  65. {
  66. return compile<karma::domain>(create_generator<Attribute>(), modifiers_)
  67. .generate(sink, context, d, attr);
  68. }
  69. // this auto_generator has no attribute attached, it needs to have been
  70. // initialized from a value/variable
  71. template <typename OutputIterator, typename Context
  72. , typename Delimiter>
  73. static bool
  74. generate(OutputIterator&, Context&, Delimiter const&, unused_type)
  75. {
  76. // It is not possible (doesn't make sense) to use auto_ generators
  77. // without providing any attribute, as the generator doesn't 'know'
  78. // what to output. The following assertion fires if this situation
  79. // is detected in your code.
  80. BOOST_SPIRIT_ASSERT_FAIL(OutputIterator, auto_not_usable_without_attribute, ());
  81. return false;
  82. }
  83. template <typename Context>
  84. info what(Context& /*context*/) const
  85. {
  86. return info("auto_");
  87. }
  88. Modifiers modifiers_;
  89. };
  90. ///////////////////////////////////////////////////////////////////////////
  91. template <typename T, typename Modifiers>
  92. struct lit_auto_generator
  93. : generator<lit_auto_generator<T, Modifiers> >
  94. {
  95. typedef mpl::int_<generator_properties::all_properties> properties;
  96. template <typename Context, typename Unused>
  97. struct attribute
  98. {
  99. typedef unused_type type;
  100. };
  101. lit_auto_generator(typename add_reference<T>::type t, Modifiers const& modifiers)
  102. : t_(t)
  103. , generator_(compile<karma::domain>(create_generator<T>(), modifiers))
  104. {}
  105. // auto_generator has an attached attribute
  106. template <
  107. typename OutputIterator, typename Context, typename Delimiter
  108. , typename Attribute>
  109. bool generate(OutputIterator& sink, Context& context
  110. , Delimiter const& d, Attribute const&) const
  111. {
  112. return generator_.generate(sink, context, d, t_);
  113. }
  114. template <typename Context>
  115. info what(Context& /*context*/) const
  116. {
  117. return info("auto_");
  118. }
  119. typedef typename spirit::result_of::create_generator<T>::type
  120. generator_type;
  121. typedef typename spirit::result_of::compile<
  122. karma::domain, generator_type, Modifiers>::type generator_impl_type;
  123. T t_;
  124. generator_impl_type generator_;
  125. // silence MSVC warning C4512: assignment operator could not be generated
  126. BOOST_DELETED_FUNCTION(lit_auto_generator& operator= (lit_auto_generator const&))
  127. };
  128. ///////////////////////////////////////////////////////////////////////////
  129. // Generator generators: make_xxx function (objects)
  130. ///////////////////////////////////////////////////////////////////////////
  131. // auto_
  132. template <typename Modifiers>
  133. struct make_primitive<tag::auto_, Modifiers>
  134. {
  135. typedef auto_generator<Modifiers> result_type;
  136. result_type operator()(unused_type, Modifiers const& modifiers) const
  137. {
  138. return result_type(modifiers);
  139. }
  140. };
  141. // auto_(...)
  142. template <typename Modifiers, typename A0>
  143. struct make_primitive<
  144. terminal_ex<tag::auto_, fusion::vector1<A0> >, Modifiers>
  145. {
  146. typedef typename add_const<A0>::type const_attribute;
  147. typedef lit_auto_generator<const_attribute, Modifiers> result_type;
  148. template <typename Terminal>
  149. result_type operator()(Terminal const& term, Modifiers const& modifiers) const
  150. {
  151. return result_type(fusion::at_c<0>(term.args), modifiers);
  152. }
  153. };
  154. }}}
  155. #endif