list.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. // Copyright (c) 2001-2011 Joel de Guzman
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_SPIRIT_KARMA_OPERATOR_LIST_HPP
  7. #define BOOST_SPIRIT_KARMA_OPERATOR_LIST_HPP
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/karma/domain.hpp>
  12. #include <boost/spirit/home/karma/generator.hpp>
  13. #include <boost/spirit/home/karma/meta_compiler.hpp>
  14. #include <boost/spirit/home/karma/detail/output_iterator.hpp>
  15. #include <boost/spirit/home/karma/detail/indirect_iterator.hpp>
  16. #include <boost/spirit/home/karma/detail/get_stricttag.hpp>
  17. #include <boost/spirit/home/karma/detail/pass_container.hpp>
  18. #include <boost/spirit/home/karma/detail/fail_function.hpp>
  19. #include <boost/spirit/home/support/info.hpp>
  20. #include <boost/spirit/home/support/unused.hpp>
  21. #include <boost/spirit/home/support/container.hpp>
  22. #include <boost/spirit/home/support/handles_container.hpp>
  23. #include <boost/spirit/home/karma/detail/attributes.hpp>
  24. #include <boost/proto/operators.hpp>
  25. #include <boost/proto/tags.hpp>
  26. namespace boost { namespace spirit
  27. {
  28. ///////////////////////////////////////////////////////////////////////////
  29. // Enablers
  30. ///////////////////////////////////////////////////////////////////////////
  31. template <>
  32. struct use_operator<karma::domain, proto::tag::modulus> // enables g % d
  33. : mpl::true_ {};
  34. }}
  35. ///////////////////////////////////////////////////////////////////////////////
  36. namespace boost { namespace spirit { namespace karma
  37. {
  38. template <typename Left, typename Right, typename Strict, typename Derived>
  39. struct base_list : binary_generator<Derived>
  40. {
  41. private:
  42. // iterate over the given container until its exhausted or the embedded
  43. // (left) generator succeeds
  44. template <typename F, typename Attribute>
  45. bool generate_left(F f, Attribute const&, mpl::false_) const
  46. {
  47. // Failing subject generators are just skipped. This allows to
  48. // selectively generate items in the provided attribute.
  49. while (!f.is_at_end())
  50. {
  51. bool r = !f(left);
  52. if (r)
  53. return true;
  54. if (!f.is_at_end())
  55. f.next();
  56. }
  57. return false;
  58. }
  59. template <typename F, typename Attribute>
  60. bool generate_left(F f, Attribute const&, mpl::true_) const
  61. {
  62. return !f(left);
  63. }
  64. // There is no way to distinguish a failed generator from a
  65. // generator to be skipped. We assume the user takes responsibility
  66. // for ending the loop if no attribute is specified.
  67. template <typename F>
  68. bool generate_left(F f, unused_type, mpl::false_) const
  69. {
  70. return !f(left);
  71. }
  72. public:
  73. typedef Left left_type;
  74. typedef Right right_type;
  75. typedef mpl::int_<
  76. left_type::properties::value
  77. | right_type::properties::value
  78. | generator_properties::buffering
  79. | generator_properties::counting
  80. > properties;
  81. // Build a std::vector from the LHS's attribute. Note
  82. // that build_std_vector may return unused_type if the
  83. // subject's attribute is an unused_type.
  84. template <typename Context, typename Iterator>
  85. struct attribute
  86. : traits::build_std_vector<
  87. typename traits::attribute_of<Left, Context, Iterator>::type>
  88. {};
  89. base_list(Left const& left, Right const& right)
  90. : left(left), right(right)
  91. {}
  92. template <
  93. typename OutputIterator, typename Context, typename Delimiter
  94. , typename Attribute>
  95. bool generate(OutputIterator& sink, Context& ctx
  96. , Delimiter const& d, Attribute const& attr) const
  97. {
  98. typedef detail::fail_function<
  99. OutputIterator, Context, Delimiter
  100. > fail_function;
  101. typedef typename traits::container_iterator<
  102. typename add_const<Attribute>::type
  103. >::type iterator_type;
  104. typedef
  105. typename traits::make_indirect_iterator<iterator_type>::type
  106. indirect_iterator_type;
  107. typedef detail::pass_container<
  108. fail_function, Attribute, indirect_iterator_type, mpl::false_>
  109. pass_container;
  110. iterator_type it = traits::begin(attr);
  111. iterator_type end = traits::end(attr);
  112. pass_container pass(fail_function(sink, ctx, d),
  113. indirect_iterator_type(it), indirect_iterator_type(end));
  114. if (generate_left(pass, attr, Strict()))
  115. {
  116. while (!pass.is_at_end())
  117. {
  118. // wrap the given output iterator as generate_left might fail
  119. detail::enable_buffering<OutputIterator> buffering(sink);
  120. {
  121. detail::disable_counting<OutputIterator> nocounting(sink);
  122. if (!right.generate(sink, ctx, d, unused))
  123. return false; // shouldn't happen
  124. if (!generate_left(pass, attr, Strict()))
  125. break; // return true as one item succeeded
  126. }
  127. buffering.buffer_copy();
  128. }
  129. return detail::sink_is_good(sink);
  130. }
  131. return false;
  132. }
  133. template <typename Context>
  134. info what(Context& context) const
  135. {
  136. return info("list",
  137. std::make_pair(left.what(context), right.what(context)));
  138. }
  139. Left left;
  140. Right right;
  141. };
  142. template <typename Left, typename Right>
  143. struct list
  144. : base_list<Left, Right, mpl::false_, list<Left, Right> >
  145. {
  146. typedef base_list<Left, Right, mpl::false_, list> base_list_;
  147. list(Left const& left, Right const& right)
  148. : base_list_(left, right) {}
  149. };
  150. template <typename Left, typename Right>
  151. struct strict_list
  152. : base_list<Left, Right, mpl::true_, strict_list<Left, Right> >
  153. {
  154. typedef base_list<Left, Right, mpl::true_, strict_list> base_list_;
  155. strict_list (Left const& left, Right const& right)
  156. : base_list_(left, right) {}
  157. };
  158. ///////////////////////////////////////////////////////////////////////////
  159. // Generator generators: make_xxx function (objects)
  160. ///////////////////////////////////////////////////////////////////////////
  161. namespace detail
  162. {
  163. template <typename Subject, bool strict_mode = false>
  164. struct make_list
  165. : make_binary_composite<Subject, list>
  166. {};
  167. template <typename Subject>
  168. struct make_list<Subject, true>
  169. : make_binary_composite<Subject, strict_list>
  170. {};
  171. }
  172. template <typename Subject, typename Modifiers>
  173. struct make_composite<proto::tag::modulus, Subject, Modifiers>
  174. : detail::make_list<Subject, detail::get_stricttag<Modifiers>::value>
  175. {};
  176. }}}
  177. namespace boost { namespace spirit { namespace traits
  178. {
  179. ///////////////////////////////////////////////////////////////////////////
  180. template <typename Left, typename Right>
  181. struct has_semantic_action<karma::list<Left, Right> >
  182. : binary_has_semantic_action<Left, Right> {};
  183. template <typename Left, typename Right>
  184. struct has_semantic_action<karma::strict_list<Left, Right> >
  185. : binary_has_semantic_action<Left, Right> {};
  186. ///////////////////////////////////////////////////////////////////////////
  187. template <typename Left, typename Right, typename Attribute
  188. , typename Context, typename Iterator>
  189. struct handles_container<karma::list<Left, Right>, Attribute
  190. , Context, Iterator>
  191. : mpl::true_ {};
  192. template <typename Left, typename Right, typename Attribute
  193. , typename Context, typename Iterator>
  194. struct handles_container<karma::strict_list<Left, Right>, Attribute
  195. , Context, Iterator>
  196. : mpl::true_ {};
  197. }}}
  198. #endif