alternative_function.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  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. =============================================================================*/
  6. #ifndef BOOST_SPIRIT_QI_DETAIL_ALTERNATIVE_FUNCTION_HPP
  7. #define BOOST_SPIRIT_QI_DETAIL_ALTERNATIVE_FUNCTION_HPP
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/qi/domain.hpp>
  12. #include <boost/spirit/home/qi/detail/assign_to.hpp>
  13. #include <boost/spirit/home/support/unused.hpp>
  14. #include <boost/spirit/home/qi/detail/attributes.hpp>
  15. #include <boost/variant.hpp>
  16. #include <boost/mpl/bool.hpp>
  17. namespace boost { namespace spirit { namespace qi { namespace detail
  18. {
  19. template <typename Variant, typename T>
  20. struct find_substitute
  21. {
  22. // Get the type from the Variant that can be a substitute for T.
  23. // If none is found, just return T
  24. typedef Variant variant_type;
  25. typedef typename variant_type::types types;
  26. typedef typename mpl::end<types>::type end;
  27. typedef typename mpl::find<types, T>::type iter_1;
  28. typedef typename
  29. mpl::eval_if<
  30. is_same<iter_1, end>,
  31. mpl::find_if<types, traits::is_substitute<T, mpl::_1> >,
  32. mpl::identity<iter_1>
  33. >::type
  34. iter;
  35. typedef typename
  36. mpl::eval_if<
  37. is_same<iter, end>,
  38. mpl::identity<T>,
  39. mpl::deref<iter>
  40. >::type
  41. type;
  42. };
  43. #ifdef _MSC_VER
  44. # pragma warning(push)
  45. # pragma warning(disable: 4512) // assignment operator could not be generated.
  46. #endif
  47. template <typename Iterator, typename Context, typename Skipper,
  48. typename Attribute>
  49. struct alternative_function
  50. {
  51. alternative_function(
  52. Iterator& first_, Iterator const& last_, Context& context_,
  53. Skipper const& skipper_, Attribute& attr_)
  54. : first(first_), last(last_), context(context_), skipper(skipper_),
  55. attr(attr_)
  56. {
  57. }
  58. template <typename Component>
  59. bool call(Component const& component, mpl::true_) const
  60. {
  61. // if Attribute is not a variant, then pass it as-is
  62. return component.parse(first, last, context, skipper, attr);
  63. }
  64. template <typename Component>
  65. bool call_optional_or_variant(Component const& component, mpl::true_) const
  66. {
  67. // If Attribute is an optional, then create an attribute for the Component
  68. // with the type optional::value_type. If the expected attribute is unused type,
  69. // use it instead.
  70. typedef typename
  71. traits::attribute_of<Component, Context, Iterator>::type
  72. expected_type;
  73. typename mpl::if_<
  74. is_same<expected_type, unused_type>,
  75. unused_type,
  76. typename Attribute::value_type>::type
  77. val;
  78. if (component.parse(first, last, context, skipper, val))
  79. {
  80. traits::assign_to(val, attr);
  81. return true;
  82. }
  83. return false;
  84. }
  85. template <typename Component>
  86. bool call_variant(Component const& component, mpl::false_) const
  87. {
  88. // If Attribute is a variant, then search the variant types for a
  89. // suitable substitute type.
  90. typename
  91. find_substitute<Attribute,
  92. typename traits::attribute_of<Component, Context, Iterator>::type
  93. >::type
  94. val;
  95. if (component.parse(first, last, context, skipper, val))
  96. {
  97. traits::assign_to(val, attr);
  98. return true;
  99. }
  100. return false;
  101. }
  102. template <typename Component>
  103. bool call_variant(Component const& component, mpl::true_) const
  104. {
  105. // If Attribute is a variant and the expected attribute is
  106. // the same type (pass the variant as-is).
  107. return component.parse(first, last, context, skipper, attr);
  108. }
  109. template <typename Component>
  110. bool call_optional_or_variant(Component const& component, mpl::false_) const
  111. {
  112. // Attribute is a variant...
  113. typedef typename
  114. traits::attribute_of<Component, Context, Iterator>::type
  115. expected;
  116. return call_variant(component,
  117. is_same<Attribute, expected>());
  118. }
  119. template <typename Component>
  120. bool call(Component const& component, mpl::false_) const
  121. {
  122. return call_optional_or_variant(
  123. component, spirit::traits::not_is_variant<Attribute, qi::domain>());
  124. }
  125. template <typename Component>
  126. bool call_unused(Component const& component, mpl::true_) const
  127. {
  128. // return true if the parser succeeds
  129. return call(component,
  130. mpl::and_<
  131. spirit::traits::not_is_variant<Attribute, qi::domain>,
  132. spirit::traits::not_is_optional<Attribute, qi::domain>
  133. >());
  134. }
  135. template <typename Component>
  136. bool call_unused(Component const& component, mpl::false_) const
  137. {
  138. return component.parse(first, last, context, skipper, unused);
  139. }
  140. template <typename Component>
  141. bool operator()(Component const& component) const
  142. {
  143. // return true if the parser succeeds
  144. typedef typename traits::not_is_unused<
  145. typename traits::attribute_of<Component, Context, Iterator>::type
  146. >::type predicate;
  147. return call_unused(component, predicate());
  148. }
  149. Iterator& first;
  150. Iterator const& last;
  151. Context& context;
  152. Skipper const& skipper;
  153. Attribute& attr;
  154. };
  155. template <typename Iterator, typename Context, typename Skipper>
  156. struct alternative_function<Iterator, Context, Skipper, unused_type const>
  157. {
  158. alternative_function(
  159. Iterator& first_, Iterator const& last_, Context& context_,
  160. Skipper const& skipper_, unused_type)
  161. : first(first_), last(last_), context(context_), skipper(skipper_)
  162. {
  163. }
  164. template <typename Component>
  165. bool operator()(Component const& component) const
  166. {
  167. // return true if the parser succeeds
  168. return component.parse(first, last, context, skipper,
  169. unused);
  170. }
  171. Iterator& first;
  172. Iterator const& last;
  173. Context& context;
  174. Skipper const& skipper;
  175. };
  176. #ifdef _MSC_VER
  177. # pragma warning(pop)
  178. #endif
  179. }}}}
  180. #endif