1 // Copyright (c) 2001-2011 Hartmut Kaiser
2 // Copyright (c) 2010 Bryce Lelbach
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_AS_DEC_18_0510PM)
8 #define SPIRIT_KARMA_AS_DEC_18_0510PM
14 #include <boost/spirit/home/karma/meta_compiler.hpp>
15 #include <boost/spirit/home/karma/generator.hpp>
16 #include <boost/spirit/home/karma/domain.hpp>
17 #include <boost/spirit/home/karma/detail/output_iterator.hpp>
18 #include <boost/spirit/home/karma/detail/as.hpp>
19 #include <boost/spirit/home/support/unused.hpp>
20 #include <boost/spirit/home/support/info.hpp>
21 #include <boost/spirit/home/support/common_terminals.hpp>
22 #include <boost/spirit/home/support/has_semantic_action.hpp>
23 #include <boost/spirit/home/support/handles_container.hpp>
24 #include <boost/spirit/home/support/assert_msg.hpp>
25 #include <boost/spirit/home/support/container.hpp>
26 #include <boost/spirit/home/karma/detail/attributes.hpp>
28 namespace boost { namespace spirit { namespace karma
32 : stateful_tag_type<T, tag::as>
34 BOOST_SPIRIT_ASSERT_MSG(
35 (traits::is_container<T>::type::value),
36 error_type_must_be_a_container,
41 namespace boost { namespace spirit
43 ///////////////////////////////////////////////////////////////////////////
45 ///////////////////////////////////////////////////////////////////////////
46 // enables as_string[...]
48 struct use_directive<karma::domain, tag::as_string>
51 // enables as_wstring[...]
53 struct use_directive<karma::domain, tag::as_wstring>
58 struct use_directive<karma::domain, tag::stateful_tag<T, tag::as> >
63 namespace boost { namespace spirit { namespace karma
65 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
66 using spirit::as_string;
67 using spirit::as_wstring;
69 using spirit::as_string_type;
70 using spirit::as_wstring_type;
72 ///////////////////////////////////////////////////////////////////////////
73 // as_directive allows to hook custom conversions to string into the
74 // output generation process
75 ///////////////////////////////////////////////////////////////////////////
76 template <typename Subject, typename T>
78 : unary_generator<as_directive<Subject, T> >
80 typedef Subject subject_type;
81 typedef typename subject_type::properties properties;
83 as_directive(Subject const& subject)
86 template <typename Context, typename Iterator>
92 template <typename OutputIterator, typename Context, typename Delimiter
94 bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
95 , Attribute const& attr) const
97 if (!traits::valid_as<T>(attr))
100 return subject.generate(sink, ctx, d, traits::as<T>(attr)) &&
101 karma::delimit_out(sink, d); // always do post-delimiting
104 template <typename Context>
105 info what(Context& context) const
107 return info("as", subject.what(context));
113 ///////////////////////////////////////////////////////////////////////////
114 // Generator generators: make_xxx function (objects)
115 ///////////////////////////////////////////////////////////////////////////
116 template <typename Subject, typename Modifiers>
117 struct make_directive<tag::as_string, Subject, Modifiers>
119 typedef as_directive<Subject, std::string> result_type;
120 result_type operator()(unused_type, Subject const& subject
123 return result_type(subject);
127 template <typename Subject, typename Modifiers>
128 struct make_directive<tag::as_wstring, Subject, Modifiers>
130 typedef as_directive<Subject, std::basic_string<wchar_t> > result_type;
131 result_type operator()(unused_type, Subject const& subject
134 return result_type(subject);
138 template <typename T, typename Subject, typename Modifiers>
139 struct make_directive<tag::stateful_tag<T, tag::as>, Subject, Modifiers>
141 typedef as_directive<Subject, T> result_type;
142 result_type operator()(unused_type, Subject const& subject
145 return result_type(subject);
150 namespace boost { namespace spirit { namespace traits
152 ///////////////////////////////////////////////////////////////////////////
153 template <typename Subject, typename T>
154 struct has_semantic_action<karma::as_directive<Subject, T> >
155 : unary_has_semantic_action<Subject> {};
157 ///////////////////////////////////////////////////////////////////////////
158 template <typename Subject, typename T, typename Attribute
159 , typename Context, typename Iterator>
160 struct handles_container<karma::as_directive<Subject, T>, Attribute
162 : mpl::false_ {}; // always dereference attribute if used in sequences