1 // Copyright (c) 2001-2011 Joel de Guzman
2 // 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)
7 #if !defined(SPIRIT_KARMA_OPTIONAL_MARCH_31_2007_0852AM)
8 #define SPIRIT_KARMA_OPTIONAL_MARCH_31_2007_0852AM
14 #include <boost/spirit/home/karma/domain.hpp>
15 #include <boost/spirit/home/karma/generator.hpp>
16 #include <boost/spirit/home/karma/meta_compiler.hpp>
17 #include <boost/spirit/home/support/info.hpp>
18 #include <boost/spirit/home/support/unused.hpp>
19 #include <boost/spirit/home/karma/detail/attributes.hpp>
20 #include <boost/spirit/home/support/container.hpp>
21 #include <boost/spirit/home/support/has_semantic_action.hpp>
22 #include <boost/spirit/home/support/handles_container.hpp>
23 #include <boost/mpl/assert.hpp>
24 #include <boost/optional.hpp>
25 #include <boost/type_traits/is_convertible.hpp>
27 namespace boost { namespace spirit
29 ///////////////////////////////////////////////////////////////////////////
31 ///////////////////////////////////////////////////////////////////////////
33 struct use_operator<karma::domain, proto::tag::negate> // enables -g
38 ///////////////////////////////////////////////////////////////////////////////
39 namespace boost { namespace spirit { namespace karma
41 ///////////////////////////////////////////////////////////////////////////
42 template <typename Subject>
43 struct optional : unary_generator<optional<Subject> >
45 typedef Subject subject_type;
46 typedef typename subject_type::properties properties;
48 // Build a boost::optional from the subject's attribute. Note
49 // that boost::optional may return unused_type if the
50 // subject's attribute is an unused_type.
51 template <typename Context, typename Iterator = unused_type>
53 : traits::build_optional<
54 typename traits::attribute_of<Subject, Context, Iterator>::type
58 optional(Subject const& subject)
62 typename OutputIterator, typename Context, typename Delimiter
64 bool generate(OutputIterator& sink, Context& ctx
65 , Delimiter const& d, Attribute const& attr) const
67 if (traits::has_optional_value(attr))
68 subject.generate(sink, ctx, d, traits::optional_value(attr));
69 return sink_is_good(sink);
72 template <typename Context>
73 info what(Context& context) const
75 return info("optional", subject.what(context));
81 ///////////////////////////////////////////////////////////////////////////
82 // Generator generators: make_xxx function (objects)
83 ///////////////////////////////////////////////////////////////////////////
84 template <typename Elements, typename Modifiers>
85 struct make_composite<proto::tag::negate, Elements, Modifiers>
86 : make_unary_composite<Elements, optional> {};
90 namespace boost { namespace spirit { namespace traits
92 ///////////////////////////////////////////////////////////////////////////
93 template <typename Subject>
94 struct has_semantic_action<karma::optional<Subject> >
95 : unary_has_semantic_action<Subject> {};
97 ///////////////////////////////////////////////////////////////////////////
98 template <typename Subject, typename Attribute, typename Context
100 struct handles_container<karma::optional<Subject>, Attribute, Context