plus.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright (c) 2001-2011 Joel de Guzman
  2. // Copyright (c) 2001-2011 Hartmut Kaiser
  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. #if !defined(BOOST_SPIRIT_KARMA_POSITIVE_MAR_03_2007_0945PM)
  7. #define BOOST_SPIRIT_KARMA_POSITIVE_MAR_03_2007_0945PM
  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/indirect_iterator.hpp>
  15. #include <boost/spirit/home/karma/detail/output_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/has_semantic_action.hpp>
  23. #include <boost/spirit/home/support/handles_container.hpp>
  24. #include <boost/spirit/home/karma/detail/attributes.hpp>
  25. #include <boost/proto/operators.hpp>
  26. #include <boost/proto/tags.hpp>
  27. #include <boost/type_traits/add_const.hpp>
  28. namespace boost { namespace spirit
  29. {
  30. ///////////////////////////////////////////////////////////////////////////
  31. // Enablers
  32. ///////////////////////////////////////////////////////////////////////////
  33. template <>
  34. struct use_operator<karma::domain, proto::tag::unary_plus> // enables +g
  35. : mpl::true_ {};
  36. }}
  37. ///////////////////////////////////////////////////////////////////////////////
  38. namespace boost { namespace spirit { namespace karma
  39. {
  40. template <typename Subject, typename Strict, typename Derived>
  41. struct base_plus : unary_generator<Derived>
  42. {
  43. private:
  44. // Ignore return value in relaxed mode (failing subject generators
  45. // are just skipped). This allows to selectively generate items in
  46. // the provided attribute.
  47. template <typename F, typename Attribute>
  48. bool generate_subject(F f, Attribute const&, bool& result, mpl::false_) const
  49. {
  50. bool r = !f(subject);
  51. if (r)
  52. result = true;
  53. else if (!f.is_at_end())
  54. f.next();
  55. return true;
  56. }
  57. template <typename F, typename Attribute>
  58. bool generate_subject(F f, Attribute const&, bool& result, mpl::true_) const
  59. {
  60. bool r = !f(subject);
  61. if (r)
  62. result = true;
  63. return r;
  64. }
  65. // There is no way to distinguish a failed generator from a
  66. // generator to be skipped. We assume the user takes responsibility
  67. // for ending the loop if no attribute is specified.
  68. template <typename F>
  69. bool generate_subject(F f, unused_type, bool& result, mpl::false_) const
  70. {
  71. bool r = f(subject);
  72. if (!r)
  73. result = true;
  74. return !r;
  75. }
  76. // template <typename F>
  77. // bool generate_subject(F f, unused_type, bool& result, mpl::true_) const
  78. // {
  79. // bool r = f(subject);
  80. // if (!r)
  81. // result = true;
  82. // return !r;
  83. // }
  84. public:
  85. typedef Subject subject_type;
  86. typedef typename subject_type::properties properties;
  87. // Build a std::vector from the subjects attribute. Note
  88. // that build_std_vector may return unused_type if the
  89. // subject's attribute is an unused_type.
  90. template <typename Context, typename Iterator>
  91. struct attribute
  92. : traits::build_std_vector<
  93. typename traits::attribute_of<subject_type, Context, Iterator>::type
  94. >
  95. {};
  96. base_plus(Subject const& subject)
  97. : subject(subject) {}
  98. template <
  99. typename OutputIterator, typename Context, typename Delimiter
  100. , typename Attribute>
  101. bool generate(OutputIterator& sink, Context& ctx
  102. , Delimiter const& d, Attribute const& attr) const
  103. {
  104. typedef detail::fail_function<
  105. OutputIterator, Context, Delimiter> fail_function;
  106. typedef typename traits::container_iterator<
  107. typename add_const<Attribute>::type
  108. >::type iterator_type;
  109. typedef
  110. typename traits::make_indirect_iterator<iterator_type>::type
  111. indirect_iterator_type;
  112. typedef detail::pass_container<
  113. fail_function, Attribute, indirect_iterator_type, mpl::false_>
  114. pass_container;
  115. iterator_type it = traits::begin(attr);
  116. iterator_type end = traits::end(attr);
  117. // plus fails if the parameter is empty
  118. if (traits::compare(it, end))
  119. return false;
  120. pass_container pass(fail_function(sink, ctx, d),
  121. indirect_iterator_type(it), indirect_iterator_type(end));
  122. // from now on plus fails if the underlying output fails or overall
  123. // no subject generators succeeded
  124. bool result = false;
  125. while (!pass.is_at_end())
  126. {
  127. if (!generate_subject(pass, attr, result, Strict()))
  128. break;
  129. }
  130. return result && detail::sink_is_good(sink);
  131. }
  132. template <typename Context>
  133. info what(Context& context) const
  134. {
  135. return info("plus", subject.what(context));
  136. }
  137. Subject subject;
  138. };
  139. template <typename Subject>
  140. struct plus
  141. : base_plus<Subject, mpl::false_, plus<Subject> >
  142. {
  143. typedef base_plus<Subject, mpl::false_, plus> base_plus_;
  144. plus(Subject const& subject)
  145. : base_plus_(subject) {}
  146. };
  147. template <typename Subject>
  148. struct strict_plus
  149. : base_plus<Subject, mpl::true_, strict_plus<Subject> >
  150. {
  151. typedef base_plus<Subject, mpl::true_, strict_plus> base_plus_;
  152. strict_plus(Subject const& subject)
  153. : base_plus_(subject) {}
  154. };
  155. ///////////////////////////////////////////////////////////////////////////
  156. // Generator generators: make_xxx function (objects)
  157. ///////////////////////////////////////////////////////////////////////////
  158. namespace detail
  159. {
  160. template <typename Elements, bool strict_mode = false>
  161. struct make_plus
  162. : make_unary_composite<Elements, plus>
  163. {};
  164. template <typename Elements>
  165. struct make_plus<Elements, true>
  166. : make_unary_composite<Elements, strict_plus>
  167. {};
  168. }
  169. template <typename Elements, typename Modifiers>
  170. struct make_composite<proto::tag::unary_plus, Elements, Modifiers>
  171. : detail::make_plus<Elements, detail::get_stricttag<Modifiers>::value>
  172. {};
  173. }}}
  174. namespace boost { namespace spirit { namespace traits
  175. {
  176. ///////////////////////////////////////////////////////////////////////////
  177. template <typename Subject>
  178. struct has_semantic_action<karma::plus<Subject> >
  179. : unary_has_semantic_action<Subject> {};
  180. template <typename Subject>
  181. struct has_semantic_action<karma::strict_plus<Subject> >
  182. : unary_has_semantic_action<Subject> {};
  183. ///////////////////////////////////////////////////////////////////////////
  184. template <typename Subject, typename Attribute, typename Context
  185. , typename Iterator>
  186. struct handles_container<karma::plus<Subject>, Attribute
  187. , Context, Iterator>
  188. : mpl::true_ {};
  189. template <typename Subject, typename Attribute, typename Context
  190. , typename Iterator>
  191. struct handles_container<karma::strict_plus<Subject>, Attribute
  192. , Context, Iterator>
  193. : mpl::true_ {};
  194. }}}
  195. #endif