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 #if !defined(BOOST_SPIRIT_ADAPT_ADT_ATTRIBUTES_SEP_15_2010_1219PM)
7 #define BOOST_SPIRIT_ADAPT_ADT_ATTRIBUTES_SEP_15_2010_1219PM
13 #include <boost/spirit/home/support/attributes.hpp>
14 #include <boost/spirit/home/support/container.hpp>
15 #include <boost/spirit/home/support/numeric_traits.hpp>
16 #include <boost/fusion/include/adapt_adt.hpp>
17 #include <boost/utility/enable_if.hpp>
19 ///////////////////////////////////////////////////////////////////////////////
20 // customization points allowing to use adapted classes with spirit
21 namespace boost { namespace spirit { namespace traits
23 ///////////////////////////////////////////////////////////////////////////
24 template <typename T, int N, bool Const, typename Domain>
25 struct not_is_variant<
26 fusion::extension::adt_attribute_proxy<T, N, Const>, Domain>
28 typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
32 template <typename T, int N, bool Const, typename Domain>
33 struct not_is_optional<
34 fusion::extension::adt_attribute_proxy<T, N, Const>, Domain>
36 typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
40 ///////////////////////////////////////////////////////////////////////////
41 template <typename T, int N, bool Const>
42 struct is_container<fusion::extension::adt_attribute_proxy<T, N, Const> >
44 typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
48 template <typename T, int N, bool Const>
49 struct container_value<fusion::extension::adt_attribute_proxy<T, N, Const> >
51 typename remove_reference<
52 typename fusion::extension::adt_attribute_proxy<
59 template <typename T, int N, bool Const>
60 struct container_value<
61 fusion::extension::adt_attribute_proxy<T, N, Const> const>
64 typename remove_reference<
65 typename fusion::extension::adt_attribute_proxy<
73 template <typename T, int N, typename Val>
74 struct push_back_container<
75 fusion::extension::adt_attribute_proxy<T, N, false>
77 , typename enable_if<is_reference<
78 typename fusion::extension::adt_attribute_proxy<T, N, false>::type
82 fusion::extension::adt_attribute_proxy<T, N, false>& p
86 fusion::extension::adt_attribute_proxy<T, N, false>::type
88 return push_back(type(p), val);
92 template <typename T, int N, bool Const>
93 struct container_iterator<
94 fusion::extension::adt_attribute_proxy<T, N, Const> >
96 typename remove_reference<
97 typename fusion::extension::adt_attribute_proxy<
104 template <typename T, int N, bool Const>
105 struct container_iterator<
106 fusion::extension::adt_attribute_proxy<T, N, Const> const>
107 : container_iterator<
109 typename remove_reference<
110 typename fusion::extension::adt_attribute_proxy<
118 template <typename T, int N, bool Const>
119 struct begin_container<fusion::extension::adt_attribute_proxy<T, N, Const> >
121 typedef typename remove_reference<
122 typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
123 >::type container_type;
125 static typename container_iterator<container_type>::type
126 call(fusion::extension::adt_attribute_proxy<T, N, Const>& c)
128 return c.get().begin();
132 template <typename T, int N, bool Const>
133 struct begin_container<fusion::extension::adt_attribute_proxy<T, N, Const> const>
135 typedef typename add_const<
136 typename remove_reference<
137 typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
139 >::type container_type;
141 static typename container_iterator<container_type>::type
142 call(fusion::extension::adt_attribute_proxy<T, N, Const> const& c)
144 return c.get().begin();
148 template <typename T, int N, bool Const>
149 struct end_container<fusion::extension::adt_attribute_proxy<T, N, Const> >
151 typedef typename remove_reference<
152 typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
153 >::type container_type;
155 static typename container_iterator<container_type>::type
156 call(fusion::extension::adt_attribute_proxy<T, N, Const>& c)
158 return c.get().end();
162 template <typename T, int N, bool Const>
163 struct end_container<fusion::extension::adt_attribute_proxy<T, N, Const> const>
165 typedef typename add_const<
166 typename remove_reference<
167 typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
169 >::type container_type;
171 static typename container_iterator<container_type>::type
172 call(fusion::extension::adt_attribute_proxy<T, N, Const> const& c)
174 return c.get().end();
178 ///////////////////////////////////////////////////////////////////////////
179 template <typename T, int N, typename Val>
180 struct assign_to_attribute_from_value<
181 fusion::extension::adt_attribute_proxy<T, N, false>
186 , fusion::extension::adt_attribute_proxy<T, N, false>& attr)
192 template <typename T, int N, bool Const, typename Exposed>
193 struct extract_from_attribute<
194 fusion::extension::adt_attribute_proxy<T, N, Const>, Exposed>
197 typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
199 typedef typename remove_const<
200 typename remove_reference<
203 >::type embedded_type;
205 typename spirit::result_of::extract_from<Exposed, embedded_type>::type
208 // If adt_attribute_proxy returned a value we must pass the attribute
209 // by value, otherwise we will end up with a reference to a temporary
210 // that will expire out of scope of the function call.
211 typedef typename mpl::if_c<is_reference<get_return_type>::value
213 , typename remove_reference<extracted_type>::type
216 template <typename Context>
218 call(fusion::extension::adt_attribute_proxy<T, N, Const> const& val, Context& ctx)
220 return extract_from<Exposed>(val.get(), ctx);
224 ///////////////////////////////////////////////////////////////////////////
225 template <typename T, int N, bool Const>
226 struct attribute_type<fusion::extension::adt_attribute_proxy<T, N, Const> >
227 : fusion::extension::adt_attribute_proxy<T, N, Const>
230 ///////////////////////////////////////////////////////////////////////////
231 template <typename T, int N, bool Const>
232 struct optional_attribute<
233 fusion::extension::adt_attribute_proxy<T, N, Const> >
235 typedef typename result_of::optional_value<
236 typename remove_reference<
237 typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
242 call(fusion::extension::adt_attribute_proxy<T, N, Const> const& val)
244 return optional_value(val.get());
248 is_valid(fusion::extension::adt_attribute_proxy<T, N, Const> const& val)
250 return has_optional_value(val.get());
254 ///////////////////////////////////////////////////////////////////////////
255 template <typename T, int N, typename Attribute, typename Domain>
256 struct transform_attribute<
257 fusion::extension::adt_attribute_proxy<T, N, false>
260 , typename disable_if<is_reference<
261 typename fusion::extension::adt_attribute_proxy<T, N, false>::type
264 typedef Attribute type;
267 pre(fusion::extension::adt_attribute_proxy<T, N, false>& val)
273 fusion::extension::adt_attribute_proxy<T, N, false>& val
274 , Attribute const& attr)
279 fail(fusion::extension::adt_attribute_proxy<T, N, false>&)
285 typename T, int N, bool Const, typename Attribute, typename Domain>
286 struct transform_attribute<
287 fusion::extension::adt_attribute_proxy<T, N, Const>
290 , typename enable_if<is_reference<
291 typename fusion::extension::adt_attribute_proxy<
296 typedef Attribute& type;
299 pre(fusion::extension::adt_attribute_proxy<T, N, Const>& val)
305 fusion::extension::adt_attribute_proxy<T, N, Const>&
310 fail(fusion::extension::adt_attribute_proxy<T, N, Const>&)
315 template <typename T, int N, bool Const>
316 struct clear_value<fusion::extension::adt_attribute_proxy<T, N, Const> >
319 fusion::extension::adt_attribute_proxy<T, N, Const>& val)
322 fusion::extension::adt_attribute_proxy<T, N, Const>::type
328 template <typename T, int N, bool Const>
329 struct attribute_size<fusion::extension::adt_attribute_proxy<T, N, Const> >
331 typedef typename remove_const<
332 typename remove_reference<
333 typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
335 >::type embedded_type;
337 typedef typename attribute_size<embedded_type>::type type;
340 call(fusion::extension::adt_attribute_proxy<T, N, Const> const& val)
342 return attribute_size<embedded_type>::call(val.get());
346 ///////////////////////////////////////////////////////////////////////////
347 // customization point specializations for numeric generators
348 template <typename T, int N, bool Const>
349 struct absolute_value<fusion::extension::adt_attribute_proxy<T, N, Const> >
352 fusion::extension::adt_attribute_proxy<T, N, Const>::type
356 call (fusion::extension::adt_attribute_proxy<T, N, Const> const& val)
358 return get_absolute_value(val.get());
362 template <typename T, int N, bool Const>
363 struct is_negative<fusion::extension::adt_attribute_proxy<T, N, Const> >
366 call(fusion::extension::adt_attribute_proxy<T, N, Const> const& val)
368 return test_negative(val.get());
372 template <typename T, int N, bool Const>
373 struct is_zero<fusion::extension::adt_attribute_proxy<T, N, Const> >
376 call(fusion::extension::adt_attribute_proxy<T, N, Const> const& val)
378 return test_zero(val.get());
382 template <typename T, int N, bool Const>
383 struct is_nan<fusion::extension::adt_attribute_proxy<T, N, Const> >
386 call(fusion::extension::adt_attribute_proxy<T, N, Const> const& val)
388 return test_nan(val.get());
392 template <typename T, int N, bool Const>
393 struct is_infinite<fusion::extension::adt_attribute_proxy<T, N, Const> >
396 call(fusion::extension::adt_attribute_proxy<T, N, Const> const& val)
398 return test_infinite(val.get());
403 ///////////////////////////////////////////////////////////////////////////////
404 namespace boost { namespace spirit { namespace result_of
406 template <typename T, int N, bool Const>
407 struct optional_value<fusion::extension::adt_attribute_proxy<T, N, Const> >
408 : result_of::optional_value<
409 typename remove_const<
410 typename remove_reference<
411 typename fusion::extension::adt_attribute_proxy<T, N, Const>::type