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 #ifndef BOOST_SPIRIT_KARMA_DIRECTIVE_AS_HPP
8 #define BOOST_SPIRIT_KARMA_DIRECTIVE_AS_HPP
14 #include <boost/spirit/home/karma/meta_compiler.hpp>
15 #include <boost/spirit/home/karma/generator.hpp>
16 #include <boost/spirit/home/karma/delimit_out.hpp>
17 #include <boost/spirit/home/karma/domain.hpp>
18 #include <boost/spirit/home/karma/detail/output_iterator.hpp>
19 #include <boost/spirit/home/karma/detail/as.hpp>
20 #include <boost/spirit/home/support/unused.hpp>
21 #include <boost/spirit/home/support/info.hpp>
22 #include <boost/spirit/home/support/common_terminals.hpp>
23 #include <boost/spirit/home/support/has_semantic_action.hpp>
24 #include <boost/spirit/home/support/handles_container.hpp>
25 #include <boost/spirit/home/support/assert_msg.hpp>
26 #include <boost/spirit/home/support/container.hpp>
27 #include <boost/spirit/home/karma/detail/attributes.hpp>
29 namespace boost { namespace spirit { namespace karma
33 : stateful_tag_type<T, tag::as>
35 BOOST_SPIRIT_ASSERT_MSG(
36 (traits::is_container<T>::type::value),
37 error_type_must_be_a_container,
42 namespace boost { namespace spirit
44 ///////////////////////////////////////////////////////////////////////////
46 ///////////////////////////////////////////////////////////////////////////
47 // enables as_string[...]
49 struct use_directive<karma::domain, tag::as_string>
52 // enables as_wstring[...]
54 struct use_directive<karma::domain, tag::as_wstring>
59 struct use_directive<karma::domain, tag::stateful_tag<T, tag::as> >
64 namespace boost { namespace spirit { namespace karma
66 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
67 using spirit::as_string;
68 using spirit::as_wstring;
70 using spirit::as_string_type;
71 using spirit::as_wstring_type;
73 ///////////////////////////////////////////////////////////////////////////
74 // as_directive allows to hook custom conversions to string into the
75 // output generation process
76 ///////////////////////////////////////////////////////////////////////////
77 template <typename Subject, typename T>
79 : unary_generator<as_directive<Subject, T> >
81 typedef Subject subject_type;
82 typedef typename subject_type::properties properties;
84 as_directive(Subject const& subject)
87 template <typename Context, typename Iterator>
93 template <typename OutputIterator, typename Context, typename Delimiter
95 bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
96 , Attribute const& attr) const
98 if (!traits::valid_as<T>(attr))
101 return subject.generate(sink, ctx, d, traits::as<T>(attr)) &&
102 karma::delimit_out(sink, d); // always do post-delimiting
105 template <typename Context>
106 info what(Context& context) const
108 return info("as", subject.what(context));
114 ///////////////////////////////////////////////////////////////////////////
115 // Generator generators: make_xxx function (objects)
116 ///////////////////////////////////////////////////////////////////////////
117 template <typename Subject, typename Modifiers>
118 struct make_directive<tag::as_string, Subject, Modifiers>
120 typedef as_directive<Subject, std::string> result_type;
121 result_type operator()(unused_type, Subject const& subject
124 return result_type(subject);
128 template <typename Subject, typename Modifiers>
129 struct make_directive<tag::as_wstring, Subject, Modifiers>
131 typedef as_directive<Subject, std::basic_string<wchar_t> > result_type;
132 result_type operator()(unused_type, Subject const& subject
135 return result_type(subject);
139 template <typename T, typename Subject, typename Modifiers>
140 struct make_directive<tag::stateful_tag<T, tag::as>, Subject, Modifiers>
142 typedef as_directive<Subject, T> result_type;
143 result_type operator()(unused_type, Subject const& subject
146 return result_type(subject);
151 namespace boost { namespace spirit { namespace traits
153 ///////////////////////////////////////////////////////////////////////////
154 template <typename Subject, typename T>
155 struct has_semantic_action<karma::as_directive<Subject, T> >
156 : unary_has_semantic_action<Subject> {};
158 ///////////////////////////////////////////////////////////////////////////
159 template <typename Subject, typename T, typename Attribute
160 , typename Context, typename Iterator>
161 struct handles_container<karma::as_directive<Subject, T>, Attribute
163 : mpl::false_ {}; // always dereference attribute if used in sequences