Chris@16
|
1 // Copyright (c) 2001-2011 Hartmut Kaiser
|
Chris@16
|
2 // Copyright (c) 2010 Bryce Lelbach
|
Chris@16
|
3 //
|
Chris@16
|
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6
|
Chris@16
|
7 #if !defined(SPIRIT_KARMA_AS_DEC_18_0510PM)
|
Chris@16
|
8 #define SPIRIT_KARMA_AS_DEC_18_0510PM
|
Chris@16
|
9
|
Chris@16
|
10 #if defined(_MSC_VER)
|
Chris@16
|
11 #pragma once
|
Chris@16
|
12 #endif
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/spirit/home/karma/meta_compiler.hpp>
|
Chris@16
|
15 #include <boost/spirit/home/karma/generator.hpp>
|
Chris@16
|
16 #include <boost/spirit/home/karma/domain.hpp>
|
Chris@16
|
17 #include <boost/spirit/home/karma/detail/output_iterator.hpp>
|
Chris@16
|
18 #include <boost/spirit/home/karma/detail/as.hpp>
|
Chris@16
|
19 #include <boost/spirit/home/support/unused.hpp>
|
Chris@16
|
20 #include <boost/spirit/home/support/info.hpp>
|
Chris@16
|
21 #include <boost/spirit/home/support/common_terminals.hpp>
|
Chris@16
|
22 #include <boost/spirit/home/support/has_semantic_action.hpp>
|
Chris@16
|
23 #include <boost/spirit/home/support/handles_container.hpp>
|
Chris@16
|
24 #include <boost/spirit/home/support/assert_msg.hpp>
|
Chris@16
|
25 #include <boost/spirit/home/support/container.hpp>
|
Chris@16
|
26 #include <boost/spirit/home/karma/detail/attributes.hpp>
|
Chris@16
|
27
|
Chris@16
|
28 namespace boost { namespace spirit { namespace karma
|
Chris@16
|
29 {
|
Chris@16
|
30 template <typename T>
|
Chris@16
|
31 struct as
|
Chris@16
|
32 : stateful_tag_type<T, tag::as>
|
Chris@16
|
33 {
|
Chris@16
|
34 BOOST_SPIRIT_ASSERT_MSG(
|
Chris@16
|
35 (traits::is_container<T>::type::value),
|
Chris@16
|
36 error_type_must_be_a_container,
|
Chris@16
|
37 (T));
|
Chris@16
|
38 };
|
Chris@16
|
39 }}}
|
Chris@16
|
40
|
Chris@16
|
41 namespace boost { namespace spirit
|
Chris@16
|
42 {
|
Chris@16
|
43 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
44 // Enablers
|
Chris@16
|
45 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
46 // enables as_string[...]
|
Chris@16
|
47 template <>
|
Chris@16
|
48 struct use_directive<karma::domain, tag::as_string>
|
Chris@16
|
49 : mpl::true_ {};
|
Chris@16
|
50
|
Chris@16
|
51 // enables as_wstring[...]
|
Chris@16
|
52 template <>
|
Chris@16
|
53 struct use_directive<karma::domain, tag::as_wstring>
|
Chris@16
|
54 : mpl::true_ {};
|
Chris@16
|
55
|
Chris@16
|
56 // enables as<T>[...]
|
Chris@16
|
57 template <typename T>
|
Chris@16
|
58 struct use_directive<karma::domain, tag::stateful_tag<T, tag::as> >
|
Chris@16
|
59 : mpl::true_
|
Chris@16
|
60 {};
|
Chris@16
|
61 }}
|
Chris@16
|
62
|
Chris@16
|
63 namespace boost { namespace spirit { namespace karma
|
Chris@16
|
64 {
|
Chris@16
|
65 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
|
Chris@16
|
66 using spirit::as_string;
|
Chris@16
|
67 using spirit::as_wstring;
|
Chris@16
|
68 #endif
|
Chris@16
|
69 using spirit::as_string_type;
|
Chris@16
|
70 using spirit::as_wstring_type;
|
Chris@16
|
71
|
Chris@16
|
72 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
73 // as_directive allows to hook custom conversions to string into the
|
Chris@16
|
74 // output generation process
|
Chris@16
|
75 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
76 template <typename Subject, typename T>
|
Chris@16
|
77 struct as_directive
|
Chris@16
|
78 : unary_generator<as_directive<Subject, T> >
|
Chris@16
|
79 {
|
Chris@16
|
80 typedef Subject subject_type;
|
Chris@16
|
81 typedef typename subject_type::properties properties;
|
Chris@16
|
82
|
Chris@16
|
83 as_directive(Subject const& subject)
|
Chris@16
|
84 : subject(subject) {}
|
Chris@16
|
85
|
Chris@16
|
86 template <typename Context, typename Iterator>
|
Chris@16
|
87 struct attribute
|
Chris@16
|
88 {
|
Chris@16
|
89 typedef T type;
|
Chris@16
|
90 };
|
Chris@16
|
91
|
Chris@16
|
92 template <typename OutputIterator, typename Context, typename Delimiter
|
Chris@16
|
93 , typename Attribute>
|
Chris@16
|
94 bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
|
Chris@16
|
95 , Attribute const& attr) const
|
Chris@16
|
96 {
|
Chris@16
|
97 if (!traits::valid_as<T>(attr))
|
Chris@16
|
98 return false;
|
Chris@16
|
99
|
Chris@16
|
100 return subject.generate(sink, ctx, d, traits::as<T>(attr)) &&
|
Chris@16
|
101 karma::delimit_out(sink, d); // always do post-delimiting
|
Chris@16
|
102 }
|
Chris@16
|
103
|
Chris@16
|
104 template <typename Context>
|
Chris@16
|
105 info what(Context& context) const
|
Chris@16
|
106 {
|
Chris@16
|
107 return info("as", subject.what(context));
|
Chris@16
|
108 }
|
Chris@16
|
109
|
Chris@16
|
110 Subject subject;
|
Chris@16
|
111 };
|
Chris@16
|
112
|
Chris@16
|
113 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
114 // Generator generators: make_xxx function (objects)
|
Chris@16
|
115 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
116 template <typename Subject, typename Modifiers>
|
Chris@16
|
117 struct make_directive<tag::as_string, Subject, Modifiers>
|
Chris@16
|
118 {
|
Chris@16
|
119 typedef as_directive<Subject, std::string> result_type;
|
Chris@16
|
120 result_type operator()(unused_type, Subject const& subject
|
Chris@16
|
121 , unused_type) const
|
Chris@16
|
122 {
|
Chris@16
|
123 return result_type(subject);
|
Chris@16
|
124 }
|
Chris@16
|
125 };
|
Chris@16
|
126
|
Chris@16
|
127 template <typename Subject, typename Modifiers>
|
Chris@16
|
128 struct make_directive<tag::as_wstring, Subject, Modifiers>
|
Chris@16
|
129 {
|
Chris@16
|
130 typedef as_directive<Subject, std::basic_string<wchar_t> > result_type;
|
Chris@16
|
131 result_type operator()(unused_type, Subject const& subject
|
Chris@16
|
132 , unused_type) const
|
Chris@16
|
133 {
|
Chris@16
|
134 return result_type(subject);
|
Chris@16
|
135 }
|
Chris@16
|
136 };
|
Chris@16
|
137
|
Chris@16
|
138 template <typename T, typename Subject, typename Modifiers>
|
Chris@16
|
139 struct make_directive<tag::stateful_tag<T, tag::as>, Subject, Modifiers>
|
Chris@16
|
140 {
|
Chris@16
|
141 typedef as_directive<Subject, T> result_type;
|
Chris@16
|
142 result_type operator()(unused_type, Subject const& subject
|
Chris@16
|
143 , unused_type) const
|
Chris@16
|
144 {
|
Chris@16
|
145 return result_type(subject);
|
Chris@16
|
146 }
|
Chris@16
|
147 };
|
Chris@16
|
148 }}}
|
Chris@16
|
149
|
Chris@16
|
150 namespace boost { namespace spirit { namespace traits
|
Chris@16
|
151 {
|
Chris@16
|
152 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
153 template <typename Subject, typename T>
|
Chris@16
|
154 struct has_semantic_action<karma::as_directive<Subject, T> >
|
Chris@16
|
155 : unary_has_semantic_action<Subject> {};
|
Chris@16
|
156
|
Chris@16
|
157 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
158 template <typename Subject, typename T, typename Attribute
|
Chris@16
|
159 , typename Context, typename Iterator>
|
Chris@16
|
160 struct handles_container<karma::as_directive<Subject, T>, Attribute
|
Chris@16
|
161 , Context, Iterator>
|
Chris@16
|
162 : mpl::false_ {}; // always dereference attribute if used in sequences
|
Chris@16
|
163 }}}
|
Chris@16
|
164
|
Chris@16
|
165 #endif
|