bioformats  5.1.3
Parse.h
1 /*
2  * #%L
3  * OME-XML C++ library for working with OME-XML metadata structures.
4  * %%
5  * Copyright © 2006 - 2015 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>
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 
235  }
236  }
237  }
238 }
239 
240 #endif // OME_XML_MODEL_DETAIL_PARSE_H
241 
242 /*
243  * Local Variables:
244  * mode:C++
245  * End:
246  */
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
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
Open Microscopy Environment C++ implementation.
Definition: CoreMetadata.cpp:40
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
Type trait for shared_ptr.
Definition: Parse.h:71
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
Xerces-C modern C++ wrapper.
Definition: Base.h:53