optional.hpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 handles_container = true;
  22. optional(Subject const& subject)
  23. : base_type(subject) {}
  24. using base_type::parse_subject;
  25. // Attribute is a container
  26. template <typename Iterator, typename Context
  27. , typename RContext, typename Attribute>
  28. bool parse_subject(Iterator& first, Iterator const& last
  29. , Context const& context, RContext& rcontext, Attribute& attr
  30. , traits::container_attribute) const
  31. {
  32. detail::parse_into_container(
  33. this->subject, first, last, context, rcontext, attr);
  34. return true;
  35. }
  36. // Attribute is an optional
  37. template <typename Iterator, typename Context
  38. , typename RContext, typename Attribute>
  39. bool parse_subject(Iterator& first, Iterator const& last
  40. , Context const& context, RContext& rcontext, Attribute& attr
  41. , traits::optional_attribute) const
  42. {
  43. typedef typename
  44. x3::traits::optional_value<Attribute>::type
  45. value_type;
  46. // create a local value
  47. value_type val{};
  48. if (this->subject.parse(first, last, context, rcontext, val))
  49. {
  50. // assign the parsed value into our attribute
  51. x3::traits::move_to(val, attr);
  52. }
  53. return true;
  54. }
  55. };
  56. template <typename Subject>
  57. inline optional<typename extension::as_parser<Subject>::value_type>
  58. operator-(Subject const& subject)
  59. {
  60. return { as_parser(subject) };
  61. }
  62. }}}
  63. namespace boost { namespace spirit { namespace x3 { namespace traits
  64. {
  65. template <typename Subject, typename Context>
  66. struct attribute_of<x3::optional<Subject>, Context>
  67. : build_optional<
  68. typename attribute_of<Subject, Context>::type> {};
  69. }}}}
  70. #endif