9
3

optional.hpp 3.7 KB

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