ome-xml  5.6.0
UnitsLengthConvert.h
1 /*
2  * #%L
3  * OME-XML C++ library for working with OME-XML metadata structures.
4  * %%
5  * Copyright © 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_ENUMS_UNITSLENGTHCONVERT_H
40 #define OME_XML_MODEL_ENUMS_UNITSLENGTHCONVERT_H
41 
42 #include <boost/preprocessor.hpp>
43 
45 
46 #include <ome/xml/model/enums/UnitsLength.h>
47 #include <ome/xml/model/primitives/Quantity.h>
48 
49 namespace ome
50 {
51  namespace xml
52  {
53  namespace model
54  {
55  namespace primitives
56  {
57  namespace detail
58  {
59 
60  using namespace ::ome::common::units;
61  using ::ome::xml::model::enums::UnitsLength;
62 
63 #define OME_XML_MODEL_ENUMS_UNITSLENGTH_PROPERTY_LIST \
64  ((YOTTAMETER)(yottameter_quantity)(0)) \
65  ((ZETTAMETER)(zettameter_quantity)(0)) \
66  ((EXAMETER)(exameter_quantity)(0)) \
67  ((PETAMETER)(petameter_quantity)(0)) \
68  ((TERAMETER)(terameter_quantity)(0)) \
69  ((GIGAMETER)(gigameter_quantity)(0)) \
70  ((MEGAMETER)(megameter_quantity)(0)) \
71  ((KILOMETER)(kilometer_quantity)(0)) \
72  ((HECTOMETER)(hectometer_quantity)(0)) \
73  ((DECAMETER)(decameter_quantity)(0)) \
74  ((METER)(meter_quantity)(0)) \
75  ((DECIMETER)(decimeter_quantity)(0)) \
76  ((CENTIMETER)(centimeter_quantity)(0)) \
77  ((MILLIMETER)(millimeter_quantity)(0)) \
78  ((MICROMETER)(micrometer_quantity)(0)) \
79  ((NANOMETER)(nanometer_quantity)(0)) \
80  ((PICOMETER)(picometer_quantity)(0)) \
81  ((FEMTOMETER)(femtometer_quantity)(0)) \
82  ((ATTOMETER)(attometer_quantity)(0)) \
83  ((ZEPTOMETER)(zeptometer_quantity)(0)) \
84  ((YOCTOMETER)(yoctometer_quantity)(0)) \
85  ((ANGSTROM)(angstrom_quantity)(0)) \
86  ((THOU)(thou_quantity)(0)) \
87  ((LINE)(line_quantity)(0)) \
88  ((INCH)(inch_quantity)(0)) \
89  ((FOOT)(foot_quantity)(0)) \
90  ((YARD)(yard_quantity)(0)) \
91  ((MILE)(mile_quantity)(0)) \
92  ((ASTRONOMICALUNIT)(astronomical_unit_quantity)(0)) \
93  ((LIGHTYEAR)(light_year_quantity)(0)) \
94  ((PARSEC)(parsec_quantity)(0)) \
95  ((POINT)(point_quantity)(0)) \
96  ((PIXEL)(pixel_quantity)(1)) \
97  ((REFERENCEFRAME)(reference_frame_quantity)(2))
98 
102  template<int>
104 
105 #define OME_XML_MODEL_ENUMS_UNITSLENGTH_UNIT_CASE(maR, maProperty, maType) \
106  template<> \
107  struct LengthProperties<UnitsLength::BOOST_PP_SEQ_ELEM(0, maType)> \
108  { \
109  typedef BOOST_PP_SEQ_ELEM(1, maType) quantity_type; \
110  static const int system = BOOST_PP_SEQ_ELEM(2, maType); \
111  };
112 
113  BOOST_PP_SEQ_FOR_EACH(OME_XML_MODEL_ENUMS_UNITSLENGTH_UNIT_CASE, %%, OME_XML_MODEL_ENUMS_UNITSLENGTH_PROPERTY_LIST)
114 
115 #undef OME_XML_MODEL_ENUMS_UNITSLENGTH_UNIT_CASE
116 #undef OME_XML_MODEL_ENUMS_UNITSLENGTH_PROPERTY_LIST
117 
121  template<int U1, int U2>
123  {
126  };
127 
128  // Convert two units
129  template<typename Q, int Src, int Dest>
130  inline
131  typename boost::enable_if_c<
133  Q
134  >::type
135  length_convert_src_dest(typename Q::value_type v,
136  typename Q::unit_type dest)
137  {
139  return Q(quantity_cast<typename Q::value_type>(d), dest);
140  }
141 
142  // Fail conversion: incompatible unit systems
143  template<typename Q, int Src, int Dest>
144  inline
145  typename boost::disable_if_c<
146  LengthConversion<Src, Dest>::valid,
147  Q
148  >::type
149  length_convert_src_dest(typename Q::value_type /* v */,
150  typename Q::unit_type /*dest */)
151  {
152  throw std::logic_error("Unit conversion failed: incompatible unit systems");
153  }
154 
155  // No switch default to avoid -Wunreachable-code errors.
156  // However, this then makes -Wswitch-default complain. Disable
157  // temporarily.
158 #ifdef __GNUC__
159 # pragma GCC diagnostic push
160 # pragma GCC diagnostic ignored "-Wswitch-default"
161 #endif
162 
163 #define OME_XML_MODEL_ENUMS_UNITSLENGTH_DEST_UNIT_CASE(maR, maProperty, maType) \
164  case UnitsLength::maType: \
165  { \
166  maProperty = length_convert_src_dest<Q, Src, UnitsLength::maType>(value, dest); \
167  } \
168  break;
169 
170  template<typename Q, int Src>
171  Q
172  length_convert_dest(typename Q::value_type value,
173  typename Q::unit_type dest)
174  {
175  Q q;
176 
177  switch(dest)
178  {
179  BOOST_PP_SEQ_FOR_EACH(OME_XML_MODEL_ENUMS_UNITSLENGTH_DEST_UNIT_CASE, q, OME_XML_MODEL_ENUMS_UNITSLENGTH_VALUES);
180  }
181 
182  return q;
183  }
184 
185 #undef OME_XML_MODEL_ENUMS_UNITSLENGTH_DEST_UNIT_CASE
186 
187 #ifdef __GNUC__
188 # pragma GCC diagnostic pop
189 #endif
190 
191  }
192 
193  // No switch default to avoid -Wunreachable-code errors.
194  // However, this then makes -Wswitch-default complain. Disable
195  // temporarily.
196 #ifdef __GNUC__
197 # pragma GCC diagnostic push
198 # pragma GCC diagnostic ignored "-Wswitch-default"
199 #endif
200 
201 #define OME_XML_MODEL_ENUMS_UNITSLENGTH_SRC_UNIT_CASE(maR, maProperty, maType) \
202  case ome::xml::model::enums::UnitsLength::maType: \
203  maProperty = detail::length_convert_dest<Quantity<ome::xml::model::enums::UnitsLength>, ome::xml::model::enums::UnitsLength::maType>(quantity.getValue(), unit); \
204  break;
205 
207  template<typename Value>
209  {
211  inline
215  {
217 
218  switch(quantity.getUnit())
219  {
220  BOOST_PP_SEQ_FOR_EACH(OME_XML_MODEL_ENUMS_UNITSLENGTH_SRC_UNIT_CASE, q, OME_XML_MODEL_ENUMS_UNITSLENGTH_VALUES);
221  }
222 
223  return q;
224  }
225  };
226 
227 #undef OME_XML_MODEL_ENUMS_UNITSLENGTH_SRC_UNIT_CASE
228 
229 #ifdef __GNUC__
230 # pragma GCC diagnostic pop
231 #endif
232 
233  }
234  }
235  }
236 }
237 
238 #endif // OME_XML_MODEL_ENUMS_UNITSLENGTHCONVERT_H
239 
240 /*
241  * Local Variables:
242  * mode:C++
243  * End:
244  */
UnitsElectricPotential enumeration.
Definition: UnitsElectricPotential.h:74
static const bool valid
Is the conversion valid?
Definition: UnitsLengthConvert.h:125
Open Microscopy Environment C++ implementation.
Test if a given conversion is valid.
Definition: UnitsLengthConvert.h:122
Convert a quantity to a different unit.
Definition: Quantity.h:267
Map a given UnitsLength enum to the corresponding language types.
Definition: UnitsLengthConvert.h:103
A quantity of a defined unit.
Definition: Quantity.h:57
UnitsLength enumeration.
Definition: UnitsLength.h:74
unit_type getUnit() const
Get the unit for this quantity.
Definition: Quantity.h:131