/Src/Dependencies/Boost/boost/spirit/home/karma/directive/duplicate.hpp

http://hadesmem.googlecode.com/ · C++ Header · 229 lines · 175 code · 33 blank · 21 comment · 0 complexity · 9562fb29c332d7167d1cc6097ef89a51 MD5 · raw file

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