duplicate.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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(SPIRIT_KARMA_DUPLICATE_JUL_11_2010_0954AM)
  6. #define SPIRIT_KARMA_DUPLICATE_JUL_11_2010_0954AM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/spirit/home/karma/meta_compiler.hpp>
  11. #include <boost/spirit/home/karma/generator.hpp>
  12. #include <boost/spirit/home/karma/domain.hpp>
  13. #include <boost/spirit/home/karma/detail/attributes.hpp>
  14. #include <boost/spirit/home/support/unused.hpp>
  15. #include <boost/spirit/home/support/info.hpp>
  16. #include <boost/spirit/home/support/common_terminals.hpp>
  17. #include <boost/spirit/home/support/assert_msg.hpp>
  18. #include <boost/spirit/home/support/has_semantic_action.hpp>
  19. #include <boost/spirit/home/support/handles_container.hpp>
  20. #include <boost/fusion/include/cons.hpp>
  21. #include <boost/fusion/include/make_cons.hpp>
  22. #include <boost/fusion/include/vector.hpp>
  23. #include <boost/fusion/include/at_c.hpp>
  24. #include <boost/mpl/identity.hpp>
  25. #include <boost/mpl/bool.hpp>
  26. namespace boost { namespace spirit
  27. {
  28. ///////////////////////////////////////////////////////////////////////////
  29. // Enablers
  30. ///////////////////////////////////////////////////////////////////////////
  31. template <>
  32. struct use_directive<karma::domain, tag::duplicate> // enables duplicate
  33. : mpl::true_ {};
  34. }}
  35. namespace boost { namespace spirit { namespace karma
  36. {
  37. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  38. using spirit::duplicate;
  39. #endif
  40. using spirit::duplicate_type;
  41. ///////////////////////////////////////////////////////////////////////////
  42. namespace detail
  43. {
  44. ///////////////////////////////////////////////////////////////////////
  45. template <typename T
  46. , bool IsSequence = fusion::traits::is_sequence<T>::value>
  47. struct attribute_count
  48. : fusion::result_of::size<T>
  49. {};
  50. template <>
  51. struct attribute_count<unused_type, false>
  52. : mpl::int_<0>
  53. {};
  54. template <typename T>
  55. struct attribute_count<T, false>
  56. : mpl::int_<1>
  57. {};
  58. ///////////////////////////////////////////////////////////////////////
  59. template <typename T
  60. , bool IsSequence = fusion::traits::is_sequence<T>::value>
  61. struct first_attribute_of_subject
  62. : remove_reference<typename fusion::result_of::at_c<T, 0>::type>
  63. {};
  64. template <typename T>
  65. struct first_attribute_of_subject<T, false>
  66. : mpl::identity<T>
  67. {};
  68. template <typename T, typename Context, typename Iterator>
  69. struct first_attribute_of
  70. : first_attribute_of_subject<
  71. typename traits::attribute_of<T, Context, Iterator>::type>
  72. {};
  73. ///////////////////////////////////////////////////////////////////////
  74. template <typename Attribute, typename T, int N>
  75. struct duplicate_sequence_attribute
  76. {
  77. typedef typename fusion::result_of::make_cons<
  78. reference_wrapper<T const>
  79. , typename duplicate_sequence_attribute<Attribute, T, N-1>::type
  80. >::type type;
  81. static type call(T const& t)
  82. {
  83. return fusion::make_cons(boost::cref(t)
  84. , duplicate_sequence_attribute<Attribute, T, N-1>::call(t));
  85. }
  86. };
  87. template <typename Attribute, typename T>
  88. struct duplicate_sequence_attribute<Attribute, T, 1>
  89. {
  90. typedef typename fusion::result_of::make_cons<
  91. reference_wrapper<T const> >::type type;
  92. static type call(T const& t)
  93. {
  94. return fusion::make_cons(boost::cref(t));
  95. }
  96. };
  97. ///////////////////////////////////////////////////////////////////////
  98. template <typename Attribute, typename T
  99. , int N = attribute_count<Attribute>::value
  100. , bool IsSequence = fusion::traits::is_sequence<Attribute>::value>
  101. struct duplicate_attribute
  102. {
  103. BOOST_SPIRIT_ASSERT_MSG(N > 0, invalid_duplication_count, (Attribute));
  104. typedef typename duplicate_sequence_attribute<Attribute, T, N>::type
  105. cons_type;
  106. typedef typename fusion::result_of::as_vector<cons_type>::type type;
  107. static type call(T const& t)
  108. {
  109. return fusion::as_vector(
  110. duplicate_sequence_attribute<Attribute, T, N>::call(t));
  111. }
  112. };
  113. template <typename Attribute, typename T>
  114. struct duplicate_attribute<Attribute, T, 0, false>
  115. {
  116. typedef unused_type type;
  117. static type call(T const&)
  118. {
  119. return unused;
  120. }
  121. };
  122. template <typename Attribute, typename T, int N>
  123. struct duplicate_attribute<Attribute, T, N, false>
  124. {
  125. typedef Attribute const& type;
  126. static type call(T const& t)
  127. {
  128. return t;
  129. }
  130. };
  131. }
  132. template <typename Attribute, typename T>
  133. inline typename detail::duplicate_attribute<Attribute, T>::type
  134. duplicate_attribute(T const& t)
  135. {
  136. return detail::duplicate_attribute<Attribute, T>::call(t);
  137. }
  138. ///////////////////////////////////////////////////////////////////////////
  139. // duplicate_directive duplicate its attribute for all elements of the
  140. // subject generator without generating anything itself
  141. ///////////////////////////////////////////////////////////////////////////
  142. template <typename Subject>
  143. struct duplicate_directive : unary_generator<duplicate_directive<Subject> >
  144. {
  145. typedef Subject subject_type;
  146. typedef typename subject_type::properties properties;
  147. duplicate_directive(Subject const& subject)
  148. : subject(subject) {}
  149. template <typename Context, typename Iterator = unused_type>
  150. struct attribute
  151. : detail::first_attribute_of<Subject, Context, Iterator>
  152. {};
  153. template <typename OutputIterator, typename Context, typename Delimiter
  154. , typename Attribute>
  155. bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
  156. , Attribute const& attr) const
  157. {
  158. typedef typename traits::attribute_of<Subject, Context>::type
  159. subject_attr_type;
  160. return subject.generate(sink, ctx, d
  161. , duplicate_attribute<subject_attr_type>(attr));
  162. }
  163. template <typename Context>
  164. info what(Context& context) const
  165. {
  166. return info("duplicate", subject.what(context));
  167. }
  168. Subject subject;
  169. };
  170. ///////////////////////////////////////////////////////////////////////////
  171. // Generator generators: make_xxx function (objects)
  172. ///////////////////////////////////////////////////////////////////////////
  173. template <typename Subject, typename Modifiers>
  174. struct make_directive<tag::duplicate, Subject, Modifiers>
  175. {
  176. typedef duplicate_directive<Subject> result_type;
  177. result_type operator()(unused_type, Subject const& subject
  178. , unused_type) const
  179. {
  180. return result_type(subject);
  181. }
  182. };
  183. }}}
  184. namespace boost { namespace spirit { namespace traits
  185. {
  186. ///////////////////////////////////////////////////////////////////////////
  187. template <typename Subject>
  188. struct has_semantic_action<karma::duplicate_directive<Subject> >
  189. : unary_has_semantic_action<Subject> {};
  190. ///////////////////////////////////////////////////////////////////////////
  191. template <typename Subject, typename Attribute, typename Context
  192. , typename Iterator>
  193. struct handles_container<karma::duplicate_directive<Subject>, Attribute
  194. , Context, Iterator>
  195. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  196. }}}
  197. #endif