ome-xml 5.2.3

UnitsTimeConvert.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_UNITSTIMECONVERT_H
40 #define OME_XML_MODEL_ENUMS_UNITSTIMECONVERT_H
41
42 #include <boost/preprocessor.hpp>
43
44 #include <ome/common/units/time.h>
45
46 #include <ome/xml/model/enums/UnitsTime.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::UnitsTime;
62
63 #define OME_XML_MODEL_ENUMS_UNITSTIME_PROPERTY_LIST \
64 ((YOTTASECOND)(yottasecond_quantity)) \
65 ((ZETTASECOND)(zettasecond_quantity)) \
66 ((EXASECOND)(exasecond_quantity)) \
67 ((PETASECOND)(petasecond_quantity)) \
68 ((TERASECOND)(terasecond_quantity)) \
69 ((GIGASECOND)(gigasecond_quantity)) \
70 ((MEGASECOND)(megasecond_quantity)) \
71 ((KILOSECOND)(kilosecond_quantity)) \
72 ((HECTOSECOND)(hectosecond_quantity)) \
73 ((DECASECOND)(decasecond_quantity)) \
74 ((SECOND)(second_quantity)) \
75 ((DECISECOND)(decisecond_quantity)) \
76 ((CENTISECOND)(centisecond_quantity)) \
77 ((MILLISECOND)(millisecond_quantity)) \
78 ((MICROSECOND)(microsecond_quantity)) \
79 ((NANOSECOND)(nanosecond_quantity)) \
80 ((PICOSECOND)(picosecond_quantity)) \
81 ((FEMTOSECOND)(femtosecond_quantity)) \
82 ((ATTOSECOND)(attosecond_quantity)) \
83 ((ZEPTOSECOND)(zeptosecond_quantity)) \
84 ((YOCTOSECOND)(yoctosecond_quantity)) \
85 ((MINUTE)(minute_quantity)) \
86 ((HOUR)(hour_quantity)) \
87 ((DAY)(day_quantity))
88
92 template<int>
93 struct TimeProperties;
94
95 #define OME_XML_MODEL_ENUMS_UNITSTIME_UNIT_CASE(maR, maProperty, maType) \
96 template<> \
97 struct TimeProperties<UnitsTime::BOOST_PP_SEQ_ELEM(0, maType)> \
98 { \
99 typedef BOOST_PP_SEQ_ELEM(1, maType) quantity_type; \
100 };
101
102 BOOST_PP_SEQ_FOR_EACH(OME_XML_MODEL_ENUMS_UNITSTIME_UNIT_CASE, %%, OME_XML_MODEL_ENUMS_UNITSTIME_PROPERTY_LIST)
103
104 #undef OME_XML_MODEL_ENUMS_UNITSTIME_UNIT_CASE
105 #undef OME_XML_MODEL_ENUMS_UNITSTIME_PROPERTY_LIST
106
107 // Convert two units
108 template<typename Q, int Src, int Dest>
109 Q
110 time_convert_src_dest(typename Q::value_type v,
111 typename Q::unit_type dest)
112 {
113 typename TimeProperties<Dest>::quantity_type d(TimeProperties<Src>::quantity_type::from_value(v));
114 return Q(quantity_cast<typename Q::value_type>(d), dest);
115 }
116
117 // No switch default to avoid -Wunreachable-code errors.
118 // However, this then makes -Wswitch-default complain. Disable
119 // temporarily.
120 #ifdef __GNUC__
121 # pragma GCC diagnostic push
122 # pragma GCC diagnostic ignored "-Wswitch-default"
123 #endif
124
125 #define OME_XML_MODEL_ENUMS_UNITSTIME_DEST_UNIT_CASE(maR, maProperty, maType) \
126 case UnitsTime::maType: \
127 { \
128 maProperty = time_convert_src_dest<Q, Src, UnitsTime::maType>(value, dest); \
129 } \
130 break;
131
132 template<typename Q, int Src>
133 Q
134 time_convert_dest(typename Q::value_type value,
135 typename Q::unit_type dest)
136 {
137 Q q;
138
139 switch(dest)
140 {
141 BOOST_PP_SEQ_FOR_EACH(OME_XML_MODEL_ENUMS_UNITSTIME_DEST_UNIT_CASE, q, OME_XML_MODEL_ENUMS_UNITSTIME_VALUES);
142 }
143
144 return q;
145 }
146
147 #undef OME_XML_MODEL_ENUMS_UNITSTIME_DEST_UNIT_CASE
148
149 #ifdef __GNUC__
150 # pragma GCC diagnostic pop
151 #endif
152
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_UNITSTIME_SRC_UNIT_CASE(maR, maProperty, maType) \
164 case ome::xml::model::enums::UnitsTime::maType: \
165 maProperty = detail::time_convert_dest<Quantity<ome::xml::model::enums::UnitsTime>, ome::xml::model::enums::UnitsTime::maType>(quantity.getValue(), unit); \
166 break;
167
169 template<typename Value>
170 struct QuantityConverter<ome::xml::model::enums::UnitsTime, Value>
171 {
173 inline
174 Quantity<ome::xml::model::enums::UnitsTime, Value>
175 operator() (const Quantity<ome::xml::model::enums::UnitsTime, Value>& quantity,
176 typename Quantity<ome::xml::model::enums::UnitsTime, Value>::unit_type unit) const
177 {
178 Quantity<ome::xml::model::enums::UnitsTime, Value> q;
179
180 switch(quantity.getUnit())
181 {
182 BOOST_PP_SEQ_FOR_EACH(OME_XML_MODEL_ENUMS_UNITSTIME_SRC_UNIT_CASE, q, OME_XML_MODEL_ENUMS_UNITSTIME_VALUES);
183 }
184
185 return q;
186 }
187 };
188
189 #undef OME_XML_MODEL_ENUMS_UNITSTIME_SRC_UNIT_CASE
190
191 #ifdef __GNUC__
192 # pragma GCC diagnostic pop
193 #endif
194
195 }
196 }
197 }
198 }
199
200 #endif // OME_XML_MODEL_ENUMS_UNITSTIMECONVERT_H
201
202 /*
203 * Local Variables:
204 * mode:C++
205 * End:
206 */
ome::xml::model::primitives::detail::TimeProperties
Map a given UnitsTime enum to the corresponding language types.
Definition: UnitsTimeConvert.h:93
time.h
ome::xml::model::enums::UnitsElectricPotential
UnitsElectricPotential enumeration.
Definition: UnitsElectricPotential.h:74
ome
Open Microscopy Environment C++ implementation.
ome::xml::model::primitives::QuantityConverter
Convert a quantity to a different unit.
Definition: Quantity.h:267
ome::xml::model::enums::UnitsTime
UnitsTime enumeration.
Definition: UnitsTime.h:74
ome::xml::model::primitives::Quantity
A quantity of a defined unit.
Definition: Quantity.h:57
ome::common::units
ome::xml::model::primitives::Quantity::getUnit
unit_type getUnit() const
Get the unit for this quantity.
Definition: Quantity.h:131