1 // Copyright (c) 2001-2011 Hartmut Kaiser
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_SPIRIT_KARMA_DIRECTIVE_DUPLICATE_HPP
7 #define BOOST_SPIRIT_KARMA_DIRECTIVE_DUPLICATE_HPP
13 #include <boost/spirit/home/karma/meta_compiler.hpp>
14 #include <boost/spirit/home/karma/generator.hpp>
15 #include <boost/spirit/home/karma/domain.hpp>
16 #include <boost/spirit/home/karma/detail/attributes.hpp>
17 #include <boost/spirit/home/support/unused.hpp>
18 #include <boost/spirit/home/support/info.hpp>
19 #include <boost/spirit/home/support/common_terminals.hpp>
20 #include <boost/spirit/home/support/assert_msg.hpp>
21 #include <boost/spirit/home/support/has_semantic_action.hpp>
22 #include <boost/spirit/home/support/handles_container.hpp>
23 #include <boost/fusion/include/cons.hpp>
24 #include <boost/fusion/include/make_cons.hpp>
25 #include <boost/fusion/include/vector.hpp>
26 #include <boost/fusion/include/at_c.hpp>
27 #include <boost/mpl/identity.hpp>
28 #include <boost/mpl/bool.hpp>
30 namespace boost { namespace spirit
32 ///////////////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////////////
36 struct use_directive<karma::domain, tag::duplicate> // enables duplicate
40 namespace boost { namespace spirit { namespace karma
42 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
43 using spirit::duplicate;
45 using spirit::duplicate_type;
47 ///////////////////////////////////////////////////////////////////////////
50 ///////////////////////////////////////////////////////////////////////
52 , bool IsSequence = fusion::traits::is_sequence<T>::value>
53 struct attribute_count
54 : fusion::result_of::size<T>
58 struct attribute_count<unused_type, false>
63 struct attribute_count<T, false>
67 ///////////////////////////////////////////////////////////////////////
69 , bool IsSequence = fusion::traits::is_sequence<T>::value>
70 struct first_attribute_of_subject
71 : remove_reference<typename fusion::result_of::at_c<T, 0>::type>
75 struct first_attribute_of_subject<T, false>
79 template <typename T, typename Context, typename Iterator>
80 struct first_attribute_of
81 : first_attribute_of_subject<
82 typename traits::attribute_of<T, Context, Iterator>::type>
85 ///////////////////////////////////////////////////////////////////////
86 template <typename Attribute, typename T, int N>
87 struct duplicate_sequence_attribute
89 typedef typename fusion::result_of::make_cons<
90 reference_wrapper<T const>
91 , typename duplicate_sequence_attribute<Attribute, T, N-1>::type
94 static type call(T const& t)
96 return fusion::make_cons(boost::cref(t)
97 , duplicate_sequence_attribute<Attribute, T, N-1>::call(t));
101 template <typename Attribute, typename T>
102 struct duplicate_sequence_attribute<Attribute, T, 1>
104 typedef typename fusion::result_of::make_cons<
105 reference_wrapper<T const> >::type type;
107 static type call(T const& t)
109 return fusion::make_cons(boost::cref(t));
113 ///////////////////////////////////////////////////////////////////////
114 template <typename Attribute, typename T
115 , int N = attribute_count<Attribute>::value
116 , bool IsSequence = fusion::traits::is_sequence<Attribute>::value>
117 struct duplicate_attribute
119 BOOST_SPIRIT_ASSERT_MSG(N > 0, invalid_duplication_count, (Attribute));
121 typedef typename duplicate_sequence_attribute<Attribute, T, N>::type
123 typedef typename fusion::result_of::as_vector<cons_type>::type type;
125 static type call(T const& t)
127 return fusion::as_vector(
128 duplicate_sequence_attribute<Attribute, T, N>::call(t));
132 template <typename Attribute, typename T>
133 struct duplicate_attribute<Attribute, T, 0, false>
135 typedef unused_type type;
137 static type call(T const&)
143 template <typename Attribute, typename T, int N>
144 struct duplicate_attribute<Attribute, T, N, false>
146 typedef Attribute const& type;
148 static type call(T const& t)
155 template <typename Attribute, typename T>
156 inline typename detail::duplicate_attribute<Attribute, T>::type
157 duplicate_attribute(T const& t)
159 return detail::duplicate_attribute<Attribute, T>::call(t);
162 ///////////////////////////////////////////////////////////////////////////
163 // duplicate_directive duplicate its attribute for all elements of the
164 // subject generator without generating anything itself
165 ///////////////////////////////////////////////////////////////////////////
166 template <typename Subject>
167 struct duplicate_directive : unary_generator<duplicate_directive<Subject> >
169 typedef Subject subject_type;
170 typedef typename subject_type::properties properties;
172 duplicate_directive(Subject const& subject)
173 : subject(subject) {}
175 template <typename Context, typename Iterator = unused_type>
177 : detail::first_attribute_of<Subject, Context, Iterator>
180 template <typename OutputIterator, typename Context, typename Delimiter
181 , typename Attribute>
182 bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
183 , Attribute const& attr) const
185 typedef typename traits::attribute_of<Subject, Context>::type
187 return subject.generate(sink, ctx, d
188 , duplicate_attribute<subject_attr_type>(attr));
191 template <typename Context>
192 info what(Context& context) const
194 return info("duplicate", subject.what(context));
200 ///////////////////////////////////////////////////////////////////////////
201 // Generator generators: make_xxx function (objects)
202 ///////////////////////////////////////////////////////////////////////////
203 template <typename Subject, typename Modifiers>
204 struct make_directive<tag::duplicate, Subject, Modifiers>
206 typedef duplicate_directive<Subject> result_type;
207 result_type operator()(unused_type, Subject const& subject
210 return result_type(subject);
215 namespace boost { namespace spirit { namespace traits
217 ///////////////////////////////////////////////////////////////////////////
218 template <typename Subject>
219 struct has_semantic_action<karma::duplicate_directive<Subject> >
220 : unary_has_semantic_action<Subject> {};
222 ///////////////////////////////////////////////////////////////////////////
223 template <typename Subject, typename Attribute, typename Context
225 struct handles_container<karma::duplicate_directive<Subject>, Attribute
227 : unary_handles_container<Subject, Attribute, Context, Iterator> {};