ome-xml 5.2.3

Parse.h
1 /*
2 * #%L
3 * OME-XML C++ library for working with OME-XML metadata structures.
4 * %%
5 * Copyright © 2006 - 2016 Open Microscopy Environment:
6 * - Massachusetts Institute of Technology
7 * - National Institutes of Health
8 * - University of Dundee
9 * - Board of Regents of the University of Wisconsin-Madison
10 * - Glencoe Software, Inc.
11 * %%
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 * The views and conclusions contained in the software and documentation are
34 * those of the authors and should not be interpreted as representing official
35 * policies, either expressed or implied, of any organization.
36 * #L%
37 */
38
39 #ifndef OME_XML_MODEL_DETAIL_PARSE_H
40 #define OME_XML_MODEL_DETAIL_PARSE_H
41
42 #include <ome/xml/model/OMEModelObject.h>
43
44 #include <boost/mpl/bool.hpp>
45 #include <boost/type_traits.hpp>
46
47 namespace ome
48 {
49 namespace xml
50 {
51 namespace model
52 {
53 namespace detail
54 {
55
64 void
65 parse_value_fail(const std::string& text,
66 const std::string& klass,
67 const std::string& property);
68
70 template <class T>
71 struct is_shared_ptr
72 : boost::false_type {};
73
75 template <class T>
76 struct is_shared_ptr<ome::compat::shared_ptr<T> >
77 : boost::true_type {};
78
88 template<typename T>
89 inline void
90 parse_value(const std::string& text,
91 T& value,
92 const std::string& klass,
93 const std::string& property)
94 {
95 std::istringstream is(text);
96 is.imbue(std::locale::classic());
97
98 if (!(is >> value))
99 parse_value_fail(text, klass, property);
100 }
101
111 template<>
112 inline void
113 parse_value<bool>(const std::string& text,
114 bool& value,
115 const std::string& klass,
116 const std::string& property)
117 {
118 std::istringstream is(text);
119 is.imbue(std::locale::classic());
120
121 if (!(is >> std::boolalpha >> value))
122 parse_value_fail(text, klass, property);
123 }
124
125 // Disable -Wunused-parameter temporarily. We can't comment out
126 // the names since they are needed for docstrings when used
127 // inline.
128 #ifdef __GNUC__
129 # pragma GCC diagnostic push
130 # pragma GCC diagnostic ignored "-Wunused-parameter"
131 #endif
132
142 template<>
143 inline void
144 parse_value<std::string>(const std::string& text,
145 std::string& value,
146 const std::string& klass,
147 const std::string& property)
148 {
149 value = text;
150 }
151
152 #ifdef __GNUC__
153 # pragma GCC diagnostic pop
154 #endif
155
165 template<typename T>
166 inline typename boost::disable_if_c<
167 is_shared_ptr<T>::value,
168 typename boost::remove_const<typename boost::remove_reference<T>::type>::type
169 >::type
170 parse_value(const std::string& text,
171 const std::string& klass,
172 const std::string& property)
173 {
174 typedef typename boost::remove_const<typename boost::remove_reference<T>::type>::type raw_type;
175
176 raw_type attr;
177 parse_value(text, attr, klass, property);
178 return attr;
179 }
180
190 template<typename T>
191 inline typename boost::enable_if_c<
192 is_shared_ptr<T>::value,
193 typename boost::remove_const<typename boost::remove_reference<T>::type>::type
194 >::type
195 parse_value(const std::string& text,
196 const std::string& klass,
197 const std::string& property)
198 {
199 typedef typename boost::remove_const<typename boost::remove_reference<T>::type>::type raw_type;
200
201 raw_type attr(ome::compat::make_shared<typename raw_type::element_type>());
202 parse_value(text, *attr, klass, property);
203 return attr;
204 }
205
220 template<class C, typename T>
221 inline void
222 set_value(const std::string& text,
223 C& object,
224 void (C::* setter)(T value),
225 const std::string& klass,
226 const std::string& property)
227 {
228 typedef typename boost::remove_const<typename boost::remove_reference<T>::type>::type raw_type;
229
230 raw_type attr(parse_value<raw_type>(text, klass, property));
231
232 (object.*setter)(attr);
233 }
234
245 template<typename T>
246 inline typename boost::disable_if_c<
247 is_shared_ptr<T>::value,
248 typename boost::remove_const<typename boost::remove_reference<T>::type>::type
249 >::type
250 parse_quantity(const std::string& text,
251 const std::string& unit,
252 const std::string& klass,
253 const std::string& property)
254 {
255 typedef typename boost::remove_const<typename boost::remove_reference<T>::type>::type raw_type;
256
257 typename raw_type::element_type::value_type v;
258 parse_value(text, v, klass, property);
259 typename raw_type::element_type::unit_type u(unit);
260 raw_type attr(v, u);
261 return attr;
262 }
263
274 template<typename T>
275 inline typename boost::enable_if_c<
276 is_shared_ptr<T>::value,
277 typename boost::remove_const<typename boost::remove_reference<T>::type>::type
278 >::type
279 parse_quantity(const std::string& text,
280 const std::string& unit,
281 const std::string& klass,
282 const std::string& property)
283 {
284 typedef typename boost::remove_const<typename boost::remove_reference<T>::type>::type raw_type;
285
286 typename raw_type::element_type::value_type v;
287 parse_value(text, v, klass, property);
288 typename raw_type::element_type::unit_type u(unit);
289 raw_type attr(ome::compat::make_shared<typename raw_type::element_type>(v, u));
290 return attr;
291 }
292
308 template<class C, typename T>
309 inline void
310 set_quantity(const std::string& text,
311 const std::string& unit,
312 C& object,
313 void (C::* setter)(T value),
314 const std::string& klass,
315 const std::string& property)
316 {
317 typedef typename boost::remove_const<typename boost::remove_reference<T>::type>::type raw_type;
318
319 raw_type attr(parse_quantity<raw_type>(text, unit, klass, property));
320
321 (object.*setter)(attr);
322 }
323
324 }
325 }
326 }
327 }
328
329 #endif // OME_XML_MODEL_DETAIL_PARSE_H
330
331 /*
332 * Local Variables:
333 * mode:C++
334 * End:
335 */
ome::xml::model::detail::parse_value< bool >
void parse_value< bool >(const std::string &text, bool &value, const std::string &klass, const std::string &property)
Parse a Boolean value.
Definition: Parse.h:113
ome::xml::model::detail::parse_quantity
boost::disable_if_c< is_shared_ptr< T >::value, typename boost::remove_const< typename boost::remove_reference< T >::type >::type >::type parse_quantity(const std::string &text, const std::string &unit, const std::string &klass, const std::string &property)
Parse an arbitrary quantity.
Definition: Parse.h:250
ome::xml::model::detail::parse_value
void parse_value(const std::string &text, T &value, const std::string &klass, const std::string &property)
Parse an arbitrary value.
Definition: Parse.h:90
ome
Open Microscopy Environment C++ implementation.
ome::xml::model::detail::set_value
void set_value(const std::string &text, C &object, void(C::*setter)(T value), const std::string &klass, const std::string &property)
Set an arbitrary value.
Definition: Parse.h:222
ome::xml::model::detail::set_quantity
void set_quantity(const std::string &text, const std::string &unit, C &object, void(C::*setter)(T value), const std::string &klass, const std::string &property)
Set an arbitrary quantity.
Definition: Parse.h:310
ome::xml::model::detail::is_shared_ptr
Type trait for shared_ptr.
Definition: Parse.h:71
ome::xml::model::detail::parse_value_fail
void parse_value_fail(const std::string &text, const std::string &klass, const std::string &property)
Throw a model exception with an informative message.
Definition: Parse.cpp:54