Chris@16
|
1 // Copyright (c) 2001-2011 Hartmut Kaiser
|
Chris@16
|
2 // Copyright (c) 2001-2011 Joel de Guzman
|
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_QI_DETAIL_ATTRIBUTES_APR_18_2010_0458PM)
|
Chris@16
|
8 #define SPIRIT_QI_DETAIL_ATTRIBUTES_APR_18_2010_0458PM
|
Chris@16
|
9
|
Chris@16
|
10 #include <boost/spirit/home/qi/domain.hpp>
|
Chris@16
|
11 #include <boost/spirit/home/support/attributes_fwd.hpp>
|
Chris@16
|
12 #include <boost/spirit/home/support/attributes.hpp>
|
Chris@16
|
13 #include <boost/spirit/home/support/utree/utree_traits_fwd.hpp>
|
Chris@16
|
14
|
Chris@16
|
15 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
16 namespace boost { namespace spirit { namespace qi
|
Chris@16
|
17 {
|
Chris@16
|
18 template <typename Exposed, typename Transformed>
|
Chris@16
|
19 struct default_transform_attribute
|
Chris@16
|
20 {
|
Chris@16
|
21 typedef Transformed type;
|
Chris@16
|
22
|
Chris@16
|
23 static Transformed pre(Exposed&) { return Transformed(); }
|
Chris@16
|
24
|
Chris@16
|
25 static void post(Exposed& val, Transformed const& attr)
|
Chris@16
|
26 {
|
Chris@16
|
27 traits::assign_to(attr, val);
|
Chris@16
|
28 }
|
Chris@16
|
29
|
Chris@16
|
30 // fail() will be called by Qi rule's if the rhs failed parsing
|
Chris@16
|
31 static void fail(Exposed&) {}
|
Chris@16
|
32 };
|
Chris@16
|
33
|
Chris@16
|
34 // handle case where no transformation is required as the types are the same
|
Chris@16
|
35 template <typename Attribute>
|
Chris@16
|
36 struct default_transform_attribute<Attribute, Attribute>
|
Chris@16
|
37 {
|
Chris@16
|
38 typedef Attribute& type;
|
Chris@16
|
39 static Attribute& pre(Attribute& val) { return val; }
|
Chris@16
|
40 static void post(Attribute&, Attribute const&) {}
|
Chris@16
|
41 static void fail(Attribute&) {}
|
Chris@16
|
42 };
|
Chris@16
|
43
|
Chris@16
|
44 template <typename Exposed, typename Transformed>
|
Chris@16
|
45 struct proxy_transform_attribute
|
Chris@16
|
46 {
|
Chris@16
|
47 typedef Transformed type;
|
Chris@16
|
48
|
Chris@16
|
49 static Transformed pre(Exposed& val) { return Transformed(val); }
|
Chris@16
|
50 static void post(Exposed&, Transformed const&) { /* no-op */ }
|
Chris@16
|
51
|
Chris@16
|
52 // fail() will be called by Qi rule's if the rhs failed parsing
|
Chris@16
|
53 static void fail(Exposed&) {}
|
Chris@16
|
54 };
|
Chris@16
|
55
|
Chris@16
|
56 // handle case where no transformation is required as the types are the same
|
Chris@16
|
57 template <typename Attribute>
|
Chris@16
|
58 struct proxy_transform_attribute<Attribute, Attribute>
|
Chris@16
|
59 {
|
Chris@16
|
60 typedef Attribute& type;
|
Chris@16
|
61 static Attribute& pre(Attribute& val) { return val; }
|
Chris@16
|
62 static void post(Attribute&, Attribute const&) {}
|
Chris@16
|
63 static void fail(Attribute&) {}
|
Chris@16
|
64 };
|
Chris@16
|
65
|
Chris@16
|
66 // main specialization for Qi
|
Chris@16
|
67 template <typename Exposed, typename Transformed, typename Enable = void>
|
Chris@16
|
68 struct transform_attribute
|
Chris@16
|
69 : mpl::if_<
|
Chris@16
|
70 mpl::and_<
|
Chris@16
|
71 mpl::not_<is_const<Exposed> >
|
Chris@16
|
72 , mpl::not_<is_reference<Exposed> >
|
Chris@16
|
73 , traits::is_proxy<Transformed> >
|
Chris@16
|
74 , proxy_transform_attribute<Exposed, Transformed>
|
Chris@16
|
75 , default_transform_attribute<Exposed, Transformed>
|
Chris@16
|
76 >::type
|
Chris@16
|
77 {};
|
Chris@16
|
78
|
Chris@16
|
79 template <typename Exposed, typename Transformed>
|
Chris@16
|
80 struct transform_attribute<boost::optional<Exposed>, Transformed
|
Chris@16
|
81 , typename disable_if<is_same<boost::optional<Exposed>, Transformed> >::type>
|
Chris@16
|
82 {
|
Chris@16
|
83 typedef Transformed& type;
|
Chris@16
|
84 static Transformed& pre(boost::optional<Exposed>& val)
|
Chris@16
|
85 {
|
Chris@16
|
86 if (!val)
|
Chris@16
|
87 val = Transformed();
|
Chris@16
|
88 return boost::get<Transformed>(val);
|
Chris@16
|
89 }
|
Chris@16
|
90 static void post(boost::optional<Exposed>&, Transformed const&) {}
|
Chris@16
|
91 static void fail(boost::optional<Exposed>& val)
|
Chris@16
|
92 {
|
Chris@16
|
93 val = none_t(); // leave optional uninitialized if rhs failed
|
Chris@16
|
94 }
|
Chris@16
|
95 };
|
Chris@16
|
96
|
Chris@16
|
97 // reference types need special handling
|
Chris@16
|
98 template <typename Attribute>
|
Chris@16
|
99 struct transform_attribute<Attribute&, Attribute>
|
Chris@16
|
100 {
|
Chris@16
|
101 typedef Attribute& type;
|
Chris@16
|
102 static Attribute& pre(Attribute& val) { return val; }
|
Chris@16
|
103 static void post(Attribute&, Attribute const&) {}
|
Chris@16
|
104 static void fail(Attribute&) {}
|
Chris@16
|
105 };
|
Chris@16
|
106
|
Chris@16
|
107 // unused_type needs some special handling as well
|
Chris@16
|
108 template <>
|
Chris@16
|
109 struct transform_attribute<unused_type, unused_type>
|
Chris@16
|
110 {
|
Chris@16
|
111 typedef unused_type type;
|
Chris@16
|
112 static unused_type pre(unused_type) { return unused; }
|
Chris@16
|
113 static void post(unused_type, unused_type) {}
|
Chris@16
|
114 static void fail(unused_type) {}
|
Chris@16
|
115 };
|
Chris@16
|
116
|
Chris@16
|
117 template <>
|
Chris@16
|
118 struct transform_attribute<unused_type const, unused_type>
|
Chris@16
|
119 : transform_attribute<unused_type, unused_type>
|
Chris@16
|
120 {};
|
Chris@16
|
121
|
Chris@16
|
122 template <typename Attribute>
|
Chris@16
|
123 struct transform_attribute<unused_type, Attribute>
|
Chris@16
|
124 : transform_attribute<unused_type, unused_type>
|
Chris@16
|
125 {};
|
Chris@16
|
126
|
Chris@16
|
127 template <typename Attribute>
|
Chris@16
|
128 struct transform_attribute<unused_type const, Attribute>
|
Chris@16
|
129 : transform_attribute<unused_type, unused_type>
|
Chris@16
|
130 {};
|
Chris@16
|
131
|
Chris@16
|
132 template <typename Attribute>
|
Chris@16
|
133 struct transform_attribute<Attribute, unused_type>
|
Chris@16
|
134 : transform_attribute<unused_type, unused_type>
|
Chris@16
|
135 {};
|
Chris@16
|
136
|
Chris@16
|
137 template <typename Attribute>
|
Chris@16
|
138 struct transform_attribute<Attribute const, unused_type>
|
Chris@16
|
139 : transform_attribute<unused_type, unused_type>
|
Chris@16
|
140 {};
|
Chris@16
|
141 }}}
|
Chris@16
|
142
|
Chris@16
|
143 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
144 namespace boost { namespace spirit { namespace traits
|
Chris@16
|
145 {
|
Chris@16
|
146 template <typename Exposed, typename Transformed>
|
Chris@16
|
147 struct transform_attribute<Exposed, Transformed, qi::domain>
|
Chris@16
|
148 : qi::transform_attribute<Exposed, Transformed>
|
Chris@16
|
149 {};
|
Chris@16
|
150
|
Chris@16
|
151 template <typename Exposed, typename Transformed>
|
Chris@16
|
152 struct transform_attribute<Exposed&, Transformed, qi::domain>
|
Chris@16
|
153 : transform_attribute<Exposed, Transformed, qi::domain>
|
Chris@16
|
154 {};
|
Chris@16
|
155
|
Chris@16
|
156 template <typename Attribute>
|
Chris@16
|
157 struct transform_attribute<Attribute&, Attribute, qi::domain>
|
Chris@16
|
158 : qi::transform_attribute<Attribute&, Attribute>
|
Chris@16
|
159 {};
|
Chris@16
|
160
|
Chris@16
|
161 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
162 template <typename Exposed, typename Transformed>
|
Chris@16
|
163 void post_transform(Exposed& dest, Transformed const& attr)
|
Chris@16
|
164 {
|
Chris@16
|
165 return transform_attribute<Exposed, Transformed, qi::domain>::post(dest, attr);
|
Chris@16
|
166 }
|
Chris@16
|
167
|
Chris@16
|
168 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
169 template <typename Exposed, typename Transformed>
|
Chris@16
|
170 void fail_transform(Exposed& dest, Transformed const&)
|
Chris@16
|
171 {
|
Chris@16
|
172 return transform_attribute<Exposed, Transformed, qi::domain>::fail(dest);
|
Chris@16
|
173 }
|
Chris@16
|
174 }}}
|
Chris@16
|
175
|
Chris@16
|
176 #endif
|