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 7 #ifndef BOOST_SPIRIT_KARMA_OPERATOR_OPTIONAL_HPP 8 #define BOOST_SPIRIT_KARMA_OPERATOR_OPTIONAL_HPP 9 10 #if defined(_MSC_VER) 11 #pragma once 12 #endif 13 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/proto/operators.hpp> 26 #include <boost/proto/tags.hpp> 27 #include <boost/type_traits/is_convertible.hpp> 28 29 namespace boost { namespace spirit 30 { 31 /////////////////////////////////////////////////////////////////////////// 32 // Enablers 33 /////////////////////////////////////////////////////////////////////////// 34 template <> 35 struct use_operator<karma::domain, proto::tag::negate> // enables -g 36 : mpl::true_ {}; 37 38 }} 39 40 /////////////////////////////////////////////////////////////////////////////// 41 namespace boost { namespace spirit { namespace karma 42 { 43 /////////////////////////////////////////////////////////////////////////// 44 template <typename Subject> 45 struct optional : unary_generator<optional<Subject> > 46 { 47 typedef Subject subject_type; 48 typedef typename subject_type::properties properties; 49 50 // Build a boost::optional from the subject's attribute. Note 51 // that boost::optional may return unused_type if the 52 // subject's attribute is an unused_type. 53 template <typename Context, typename Iterator = unused_type> 54 struct attribute 55 : traits::build_optional< 56 typename traits::attribute_of<Subject, Context, Iterator>::type 57 > 58 {}; 59 optionalboost::spirit::karma::optional60 optional(Subject const& subject) 61 : subject(subject) {} 62 63 template < 64 typename OutputIterator, typename Context, typename Delimiter 65 , typename Attribute> generateboost::spirit::karma::optional66 bool generate(OutputIterator& sink, Context& ctx 67 , Delimiter const& d, Attribute const& attr) const 68 { 69 if (traits::has_optional_value(attr)) 70 subject.generate(sink, ctx, d, traits::optional_value(attr)); 71 return sink_is_good(sink); 72 } 73 74 template <typename Context> whatboost::spirit::karma::optional75 info what(Context& context) const 76 { 77 return info("optional", subject.what(context)); 78 } 79 80 Subject subject; 81 }; 82 83 /////////////////////////////////////////////////////////////////////////// 84 // Generator generators: make_xxx function (objects) 85 /////////////////////////////////////////////////////////////////////////// 86 template <typename Elements, typename Modifiers> 87 struct make_composite<proto::tag::negate, Elements, Modifiers> 88 : make_unary_composite<Elements, optional> {}; 89 90 }}} 91 92 namespace boost { namespace spirit { namespace traits 93 { 94 /////////////////////////////////////////////////////////////////////////// 95 template <typename Subject> 96 struct has_semantic_action<karma::optional<Subject> > 97 : unary_has_semantic_action<Subject> {}; 98 99 /////////////////////////////////////////////////////////////////////////// 100 template <typename Subject, typename Attribute, typename Context 101 , typename Iterator> 102 struct handles_container<karma::optional<Subject>, Attribute, Context 103 , Iterator> 104 : mpl::true_ {}; 105 }}} 106 107 #endif 108