optional.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifndef BOOST_SPIRIT_KARMA_OPERATOR_OPTIONAL_HPP
  7. #define BOOST_SPIRIT_KARMA_OPERATOR_OPTIONAL_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/support/info.hpp>
  15. #include <boost/spirit/home/support/unused.hpp>
  16. #include <boost/spirit/home/karma/detail/attributes.hpp>
  17. #include <boost/spirit/home/support/container.hpp>
  18. #include <boost/spirit/home/support/has_semantic_action.hpp>
  19. #include <boost/spirit/home/support/handles_container.hpp>
  20. #include <boost/mpl/assert.hpp>
  21. #include <boost/optional.hpp>
  22. #include <boost/proto/operators.hpp>
  23. #include <boost/proto/tags.hpp>
  24. #include <boost/type_traits/is_convertible.hpp>
  25. namespace boost { namespace spirit
  26. {
  27. ///////////////////////////////////////////////////////////////////////////
  28. // Enablers
  29. ///////////////////////////////////////////////////////////////////////////
  30. template <>
  31. struct use_operator<karma::domain, proto::tag::negate> // enables -g
  32. : mpl::true_ {};
  33. }}
  34. ///////////////////////////////////////////////////////////////////////////////
  35. namespace boost { namespace spirit { namespace karma
  36. {
  37. ///////////////////////////////////////////////////////////////////////////
  38. template <typename Subject>
  39. struct optional : unary_generator<optional<Subject> >
  40. {
  41. typedef Subject subject_type;
  42. typedef typename subject_type::properties properties;
  43. // Build a boost::optional from the subject's attribute. Note
  44. // that boost::optional may return unused_type if the
  45. // subject's attribute is an unused_type.
  46. template <typename Context, typename Iterator = unused_type>
  47. struct attribute
  48. : traits::build_optional<
  49. typename traits::attribute_of<Subject, Context, Iterator>::type
  50. >
  51. {};
  52. optional(Subject const& subject)
  53. : subject(subject) {}
  54. template <
  55. typename OutputIterator, typename Context, typename Delimiter
  56. , typename Attribute>
  57. bool generate(OutputIterator& sink, Context& ctx
  58. , Delimiter const& d, Attribute const& attr) const
  59. {
  60. if (traits::has_optional_value(attr))
  61. subject.generate(sink, ctx, d, traits::optional_value(attr));
  62. return sink_is_good(sink);
  63. }
  64. template <typename Context>
  65. info what(Context& context) const
  66. {
  67. return info("optional", subject.what(context));
  68. }
  69. Subject subject;
  70. };
  71. ///////////////////////////////////////////////////////////////////////////
  72. // Generator generators: make_xxx function (objects)
  73. ///////////////////////////////////////////////////////////////////////////
  74. template <typename Elements, typename Modifiers>
  75. struct make_composite<proto::tag::negate, Elements, Modifiers>
  76. : make_unary_composite<Elements, optional> {};
  77. }}}
  78. namespace boost { namespace spirit { namespace traits
  79. {
  80. ///////////////////////////////////////////////////////////////////////////
  81. template <typename Subject>
  82. struct has_semantic_action<karma::optional<Subject> >
  83. : unary_has_semantic_action<Subject> {};
  84. ///////////////////////////////////////////////////////////////////////////
  85. template <typename Subject, typename Attribute, typename Context
  86. , typename Iterator>
  87. struct handles_container<karma::optional<Subject>, Attribute, Context
  88. , Iterator>
  89. : mpl::true_ {};
  90. }}}
  91. #endif