ome-xml  5.5.0
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 <sstream>
43 #include <type_traits>
44 
45 #include <ome/xml/model/OMEModelObject.h>
46 
47 #include <boost/mpl/bool.hpp>
48 
49 namespace ome
50 {
51  namespace xml
52  {
53  namespace model
54  {
55  namespace detail
56  {
57 
66  void
67  parse_value_fail(const std::string& text,
68  const std::string& klass,
69  const std::string& property);
70 
72  template <class T>
74  : boost::false_type {};
75 
77  template <class T>
78  struct is_shared_ptr<std::shared_ptr<T>>
79  : boost::true_type {};
80 
90  template<typename T>
91  inline void
92  parse_value(const std::string& text,
93  T& value,
94  const std::string& klass,
95  const std::string& property)
96  {
97  std::istringstream is(text);
98  is.imbue(std::locale::classic());
99 
100  if (!(is >> value))
101  parse_value_fail(text, klass, property);
102  }
103 
113  template<>
114  inline void
115  parse_value<bool>(const std::string& text,
116  bool& value,
117  const std::string& klass,
118  const std::string& property)
119  {
120  std::istringstream is(text);
121  is.imbue(std::locale::classic());
122 
123  if (!(is >> std::boolalpha >> value))
124  parse_value_fail(text, klass, property);
125  }
126 
127  // Disable -Wunused-parameter temporarily. We can't comment out
128  // the names since they are needed for docstrings when used
129  // inline.
130 #ifdef __GNUC__
131 # pragma GCC diagnostic push
132 # pragma GCC diagnostic ignored "-Wunused-parameter"
133 #endif
134 
144  template<>
145  inline void
146  parse_value<std::string>(const std::string& text,
147  std::string& value,
148  const std::string& klass,
149  const std::string& property)
150  {
151  value = text;
152  }
153 
154 #ifdef __GNUC__
155 # pragma GCC diagnostic pop
156 #endif
157 
167  template<typename T>
168  inline typename std::enable_if<
170  typename std::remove_const<typename boost::remove_reference<T>::type>::type
171  >::type
172  parse_value(const std::string& text,
173  const std::string& klass,
174  const std::string& property)
175  {
176  typedef typename boost::remove_const<typename boost::remove_reference<T>::type>::type raw_type;
177 
178  raw_type attr;
179  parse_value(text, attr, klass, property);
180  return attr;
181  }
182 
192  template<typename T>
193  inline typename std::enable_if<
195  typename std::remove_const<typename boost::remove_reference<T>::type>::type
196  >::type
197  parse_value(const std::string& text,
198  const std::string& klass,
199  const std::string& property)
200  {
201  typedef typename boost::remove_const<typename boost::remove_reference<T>::type>::type raw_type;
202 
203  raw_type attr(std::make_shared<typename raw_type::element_type>());
204  parse_value(text, *attr, klass, property);
205  return attr;
206  }
207 
222  template<class C, typename T>
223  inline void
224  set_value(const std::string& text,
225  C& object,
226  void (C::* setter)(T value),
227  const std::string& klass,
228  const std::string& property)
229  {
230  typedef typename boost::remove_const<typename boost::remove_reference<T>::type>::type raw_type;
231 
232  raw_type attr(parse_value<raw_type>(text, klass, property));
233 
234  (object.*setter)(attr);
235  }
236 
247  template<typename T>
248  inline typename std::enable_if<
249  !is_shared_ptr<T>::value,
250  typename std::remove_const<typename boost::remove_reference<T>::type>::type
251  >::type
252  parse_quantity(const std::string& text,
253  const std::string& unit,
254  const std::string& klass,
255  const std::string& property)
256  {
257  typedef typename boost::remove_const<typename boost::remove_reference<T>::type>::type raw_type;
258 
259  typename raw_type::element_type::value_type v;
260  parse_value(text, v, klass, property);
261  typename raw_type::element_type::unit_type u(unit);
262  raw_type attr(v, u);
263  return attr;
264  }
265 
276  template<typename T>
277  inline typename std::enable_if<
278  is_shared_ptr<T>::value,
279  typename std::remove_const<typename boost::remove_reference<T>::type>::type
280  >::type
281  parse_quantity(const std::string& text,
282  const std::string& unit,
283  const std::string& klass,
284  const std::string& property)
285  {
286  typedef typename boost::remove_const<typename boost::remove_reference<T>::type>::type raw_type;
287 
288  typename raw_type::element_type::value_type v;
289  parse_value(text, v, klass, property);
290  typename raw_type::element_type::unit_type u(unit);
291  raw_type attr(std::make_shared<typename raw_type::element_type>(v, u));
292  return attr;
293  }
294 
310  template<class C, typename T>
311  inline void
312  set_quantity(const std::string& text,
313  const std::string& unit,
314  C& object,
315  void (C::* setter)(T value),
316  const std::string& klass,
317  const std::string& property)
318  {
319  typedef typename boost::remove_const<typename boost::remove_reference<T>::type>::type raw_type;
320 
321  raw_type attr(parse_quantity<raw_type>(text, unit, klass, property));
322 
323  (object.*setter)(attr);
324  }
325 
326  }
327  }
328  }
329 }
330 
331 #endif // OME_XML_MODEL_DETAIL_PARSE_H
332 
333 /*
334  * Local Variables:
335  * mode:C++
336  * End:
337  */
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:115
void parse_value(const std::string &text, T &value, const std::string &klass, const std::string &property)
Parse an arbitrary value.
Definition: Parse.h:92
STL namespace.
Open Microscopy Environment C++ implementation.
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:224
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:312
Type trait for shared_ptr.
Definition: Parse.h:73
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
std::enable_if< !is_shared_ptr< T >::value, typename std::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:252