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

source code of boost/libs/spirit/include/boost/spirit/home/karma/directive/as.hpp