• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  Copyright (c) 2001-2011 Hartmut Kaiser
2 //
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)
5 
6 #ifndef BOOST_SPIRIT_KARMA_DIRECTIVE_DUPLICATE_HPP
7 #define BOOST_SPIRIT_KARMA_DIRECTIVE_DUPLICATE_HPP
8 
9 #if defined(_MSC_VER)
10 #pragma once
11 #endif
12 
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>
29 
30 namespace boost { namespace spirit
31 {
32     ///////////////////////////////////////////////////////////////////////////
33     // Enablers
34     ///////////////////////////////////////////////////////////////////////////
35     template <>
36     struct use_directive<karma::domain, tag::duplicate> // enables duplicate
37       : mpl::true_ {};
38 }}
39 
40 namespace boost { namespace spirit { namespace karma
41 {
42 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
43     using spirit::duplicate;
44 #endif
45     using spirit::duplicate_type;
46 
47     ///////////////////////////////////////////////////////////////////////////
48     namespace detail
49     {
50         ///////////////////////////////////////////////////////////////////////
51         template <typename T
52           , bool IsSequence = fusion::traits::is_sequence<T>::value>
53         struct attribute_count
54           : fusion::result_of::size<T>
55         {};
56 
57         template <>
58         struct attribute_count<unused_type, false>
59           : mpl::int_<0>
60         {};
61 
62         template <typename T>
63         struct attribute_count<T, false>
64           : mpl::int_<1>
65         {};
66 
67         ///////////////////////////////////////////////////////////////////////
68         template <typename T
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>
72         {};
73 
74         template <typename T>
75         struct first_attribute_of_subject<T, false>
76           : mpl::identity<T>
77         {};
78 
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>
83         {};
84 
85         ///////////////////////////////////////////////////////////////////////
86         template <typename Attribute, typename T, int N>
87         struct duplicate_sequence_attribute
88         {
89             typedef typename fusion::result_of::make_cons<
90                 reference_wrapper<T const>
91               , typename duplicate_sequence_attribute<Attribute, T, N-1>::type
92             >::type type;
93 
callboost::spirit::karma::detail::duplicate_sequence_attribute94             static type call(T const& t)
95             {
96                 return fusion::make_cons(boost::cref(t)
97                   , duplicate_sequence_attribute<Attribute, T, N-1>::call(t));
98             }
99         };
100 
101         template <typename Attribute, typename T>
102         struct duplicate_sequence_attribute<Attribute, T, 1>
103         {
104             typedef typename fusion::result_of::make_cons<
105                 reference_wrapper<T const> >::type type;
106 
callboost::spirit::karma::detail::duplicate_sequence_attribute107             static type call(T const& t)
108             {
109                 return fusion::make_cons(boost::cref(t));
110             }
111         };
112 
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
118         {
119             BOOST_SPIRIT_ASSERT_MSG(N > 0, invalid_duplication_count, (Attribute));
120 
121             typedef typename duplicate_sequence_attribute<Attribute, T, N>::type
122                 cons_type;
123             typedef typename fusion::result_of::as_vector<cons_type>::type type;
124 
callboost::spirit::karma::detail::duplicate_attribute125             static type call(T const& t)
126             {
127                 return fusion::as_vector(
128                     duplicate_sequence_attribute<Attribute, T, N>::call(t));
129             }
130         };
131 
132         template <typename Attribute, typename T>
133         struct duplicate_attribute<Attribute, T, 0, false>
134         {
135             typedef unused_type type;
136 
callboost::spirit::karma::detail::duplicate_attribute137             static type call(T const&)
138             {
139                 return unused;
140             }
141         };
142 
143         template <typename Attribute, typename T, int N>
144         struct duplicate_attribute<Attribute, T, N, false>
145         {
146             typedef Attribute const& type;
147 
callboost::spirit::karma::detail::duplicate_attribute148             static type call(T const& t)
149             {
150                 return t;
151             }
152         };
153     }
154 
155     template <typename Attribute, typename T>
156     inline typename detail::duplicate_attribute<Attribute, T>::type
duplicate_attribute(T const & t)157     duplicate_attribute(T const& t)
158     {
159         return detail::duplicate_attribute<Attribute, T>::call(t);
160     }
161 
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> >
168     {
169         typedef Subject subject_type;
170         typedef typename subject_type::properties properties;
171 
duplicate_directiveboost::spirit::karma::duplicate_directive172         duplicate_directive(Subject const& subject)
173           : subject(subject) {}
174 
175         template <typename Context, typename Iterator = unused_type>
176         struct attribute
177           : detail::first_attribute_of<Subject, Context, Iterator>
178         {};
179 
180         template <typename OutputIterator, typename Context, typename Delimiter
181           , typename Attribute>
generateboost::spirit::karma::duplicate_directive182         bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
183           , Attribute const& attr) const
184         {
185             typedef typename traits::attribute_of<Subject, Context>::type
186                 subject_attr_type;
187             return subject.generate(sink, ctx, d
188               , duplicate_attribute<subject_attr_type>(attr));
189         }
190 
191         template <typename Context>
whatboost::spirit::karma::duplicate_directive192         info what(Context& context) const
193         {
194             return info("duplicate", subject.what(context));
195         }
196 
197         Subject subject;
198     };
199 
200     ///////////////////////////////////////////////////////////////////////////
201     // Generator generators: make_xxx function (objects)
202     ///////////////////////////////////////////////////////////////////////////
203     template <typename Subject, typename Modifiers>
204     struct make_directive<tag::duplicate, Subject, Modifiers>
205     {
206         typedef duplicate_directive<Subject> result_type;
operator ()boost::spirit::karma::make_directive207         result_type operator()(unused_type, Subject const& subject
208           , unused_type) const
209         {
210             return result_type(subject);
211         }
212     };
213 }}}
214 
215 namespace boost { namespace spirit { namespace traits
216 {
217     ///////////////////////////////////////////////////////////////////////////
218     template <typename Subject>
219     struct has_semantic_action<karma::duplicate_directive<Subject> >
220       : unary_has_semantic_action<Subject> {};
221 
222     ///////////////////////////////////////////////////////////////////////////
223     template <typename Subject, typename Attribute, typename Context
224         , typename Iterator>
225     struct handles_container<karma::duplicate_directive<Subject>, Attribute
226         , Context, Iterator>
227       : unary_handles_container<Subject, Attribute, Context, Iterator> {};
228 }}}
229 
230 #endif
231