optional.hpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  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. =============================================================================*/
  7. #if !defined(BOOST_SPIRIT_X3_OPTIONAL_MARCH_23_2007_1117PM)
  8. #define BOOST_SPIRIT_X3_OPTIONAL_MARCH_23_2007_1117PM
  9. #include <boost/spirit/home/x3/core/proxy.hpp>
  10. #include <boost/spirit/home/x3/core/detail/parse_into_container.hpp>
  11. #include <boost/spirit/home/x3/support/traits/attribute_of.hpp>
  12. #include <boost/spirit/home/x3/support/traits/move_to.hpp>
  13. #include <boost/spirit/home/x3/support/traits/optional_traits.hpp>
  14. #include <boost/spirit/home/x3/support/traits/attribute_category.hpp>
  15. namespace boost { namespace spirit { namespace x3
  16. {
  17. template <typename Subject>
  18. struct optional : proxy<Subject, optional<Subject>>
  19. {
  20. typedef proxy<Subject, optional<Subject>> base_type;
  21. static bool const is_pass_through_unary = false;
  22. static bool const handles_container = true;
  23. constexpr optional(Subject const& subject)
  24. : base_type(subject) {}
  25. using base_type::parse_subject;
  26. // Attribute is a container
  27. template <typename Iterator, typename Context
  28. , typename RContext, typename Attribute>
  29. bool parse_subject(Iterator& first, Iterator const& last
  30. , Context const& context, RContext& rcontext, Attribute& attr
  31. , traits::container_attribute) const
  32. {
  33. detail::parse_into_container(
  34. this->subject, first, last, context, rcontext, attr);
  35. return true;
  36. }
  37. // Attribute is an optional
  38. template <typename Iterator, typename Context
  39. , typename RContext, typename Attribute>
  40. bool parse_subject(Iterator& first, Iterator const& last
  41. , Context const& context, RContext& rcontext, Attribute& attr
  42. , traits::optional_attribute) const
  43. {
  44. typedef typename
  45. x3::traits::optional_value<Attribute>::type
  46. value_type;
  47. // create a local value
  48. value_type val{};
  49. if (this->subject.parse(first, last, context, rcontext, val))
  50. {
  51. // assign the parsed value into our attribute
  52. x3::traits::move_to(val, attr);
  53. }
  54. return true;
  55. }
  56. };
  57. template <typename Subject>
  58. constexpr optional<typename extension::as_parser<Subject>::value_type>
  59. operator-(Subject const& subject)
  60. {
  61. return { as_parser(subject) };
  62. }
  63. }}}
  64. namespace boost { namespace spirit { namespace x3 { namespace traits
  65. {
  66. template <typename Subject, typename Context>
  67. struct attribute_of<x3::optional<Subject>, Context>
  68. : build_optional<
  69. typename attribute_of<Subject, Context>::type> {};
  70. }}}}
  71. #endif