ome-xml 5.2.3

UnitsPowerConvert.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_UNITSPOWERCONVERT_H
40 #define OME_XML_MODEL_ENUMS_UNITSPOWERCONVERT_H
41
42 #include <boost/preprocessor.hpp>
43
44 #include <ome/common/units/power.h>
45
46 #include <ome/xml/model/enums/UnitsPower.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::UnitsPower;
62
63 #define OME_XML_MODEL_ENUMS_UNITSPOWER_PROPERTY_LIST \
64 ((YOTTAWATT)(yottawatt_quantity)) \
65 ((ZETTAWATT)(zettawatt_quantity)) \
66 ((EXAWATT)(exawatt_quantity)) \
67 ((PETAWATT)(petawatt_quantity)) \
68 ((TERAWATT)(terawatt_quantity)) \
69 ((GIGAWATT)(gigawatt_quantity)) \
70 ((MEGAWATT)(megawatt_quantity)) \
71 ((KILOWATT)(kilowatt_quantity)) \
72 ((HECTOWATT)(hectowatt_quantity)) \
73 ((DECAWATT)(decawatt_quantity)) \
74 ((WATT)(watt_quantity)) \
75 ((DECIWATT)(deciwatt_quantity)) \
76 ((CENTIWATT)(centiwatt_quantity)) \
77 ((MILLIWATT)(milliwatt_quantity)) \
78 ((MICROWATT)(microwatt_quantity)) \
79 ((NANOWATT)(nanowatt_quantity)) \
80 ((PICOWATT)(picowatt_quantity)) \
81 ((FEMTOWATT)(femtowatt_quantity)) \
82 ((ATTOWATT)(attowatt_quantity)) \
83 ((ZEPTOWATT)(zeptowatt_quantity)) \
84 ((YOCTOWATT)(yoctowatt_quantity))
85
89 template<int>
90 struct PowerProperties;
91
92 #define OME_XML_MODEL_ENUMS_UNITSPOWER_UNIT_CASE(maR, maProperty, maType) \
93 template<> \
94 struct PowerProperties<UnitsPower::BOOST_PP_SEQ_ELEM(0, maType)> \
95 { \
96 typedef BOOST_PP_SEQ_ELEM(1, maType) quantity_type; \
97 };
98
99 BOOST_PP_SEQ_FOR_EACH(OME_XML_MODEL_ENUMS_UNITSPOWER_UNIT_CASE, %%, OME_XML_MODEL_ENUMS_UNITSPOWER_PROPERTY_LIST)
100
101 #undef OME_XML_MODEL_ENUMS_UNITSPOWER_UNIT_CASE
102 #undef OME_XML_MODEL_ENUMS_UNITSPOWER_PROPERTY_LIST
103
104 // Convert two units
105 template<typename Q, int Src, int Dest>
106 Q
107 power_convert_src_dest(typename Q::value_type v,
108 typename Q::unit_type dest)
109 {
110 typename PowerProperties<Dest>::quantity_type d(PowerProperties<Src>::quantity_type::from_value(v));
111 return Q(quantity_cast<typename Q::value_type>(d), dest);
112 }
113
114 // No switch default to avoid -Wunreachable-code errors.
115 // However, this then makes -Wswitch-default complain. Disable
116 // temporarily.
117 #ifdef __GNUC__
118 # pragma GCC diagnostic push
119 # pragma GCC diagnostic ignored "-Wswitch-default"
120 #endif
121
122 #define OME_XML_MODEL_ENUMS_UNITSPOWER_DEST_UNIT_CASE(maR, maProperty, maType) \
123 case UnitsPower::maType: \
124 { \
125 maProperty = power_convert_src_dest<Q, Src, UnitsPower::maType>(value, dest); \
126 } \
127 break;
128
129 template<typename Q, int Src>
130 Q
131 power_convert_dest(typename Q::value_type value,
132 typename Q::unit_type dest)
133 {
134 Q q;
135
136 switch(dest)
137 {
138 BOOST_PP_SEQ_FOR_EACH(OME_XML_MODEL_ENUMS_UNITSPOWER_DEST_UNIT_CASE, q, OME_XML_MODEL_ENUMS_UNITSPOWER_VALUES);
139 }
140
141 return q;
142 }
143
144 #undef OME_XML_MODEL_ENUMS_UNITSPOWER_DEST_UNIT_CASE
145
146 #ifdef __GNUC__
147 # pragma GCC diagnostic pop
148 #endif
149
150 }
151
152 // No switch default to avoid -Wunreachable-code errors.
153 // However, this then makes -Wswitch-default complain. Disable
154 // temporarily.
155 #ifdef __GNUC__
156 # pragma GCC diagnostic push
157 # pragma GCC diagnostic ignored "-Wswitch-default"
158 #endif
159
160 #define OME_XML_MODEL_ENUMS_UNITSPOWER_SRC_UNIT_CASE(maR, maProperty, maType) \
161 case ome::xml::model::enums::UnitsPower::maType: \
162 maProperty = detail::power_convert_dest<Quantity<ome::xml::model::enums::UnitsPower>, ome::xml::model::enums::UnitsPower::maType>(quantity.getValue(), unit); \
163 break;
164
166 template<typename Value>
167 struct QuantityConverter<ome::xml::model::enums::UnitsPower, Value>
168 {
170 inline
171 Quantity<ome::xml::model::enums::UnitsPower, Value>
172 operator() (const Quantity<ome::xml::model::enums::UnitsPower, Value>& quantity,
173 typename Quantity<ome::xml::model::enums::UnitsPower, Value>::unit_type unit) const
174 {
175 Quantity<ome::xml::model::enums::UnitsPower, Value> q;
176
177 switch(quantity.getUnit())
178 {
179 BOOST_PP_SEQ_FOR_EACH(OME_XML_MODEL_ENUMS_UNITSPOWER_SRC_UNIT_CASE, q, OME_XML_MODEL_ENUMS_UNITSPOWER_VALUES);
180 }
181
182 return q;
183 }
184 };
185
186 #undef OME_XML_MODEL_ENUMS_UNITSPOWER_SRC_UNIT_CASE
187
188 #ifdef __GNUC__
189 # pragma GCC diagnostic pop
190 #endif
191
192 }
193 }
194 }
195 }
196
197 #endif // OME_XML_MODEL_ENUMS_UNITSPOWERCONVERT_H
198
199 /*
200 * Local Variables:
201 * mode:C++
202 * End:
203 */
ome::xml::model::enums::UnitsElectricPotential
UnitsElectricPotential enumeration.
Definition: UnitsElectricPotential.h:74
ome::xml::model::primitives::detail::PowerProperties
Map a given UnitsPower enum to the corresponding language types.
Definition: UnitsPowerConvert.h:90
power.h
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::primitives::Quantity
A quantity of a defined unit.
Definition: Quantity.h:57
ome::xml::model::enums::UnitsPower
UnitsPower enumeration.
Definition: UnitsPower.h:74
ome::common::units
ome::xml::model::primitives::Quantity::getUnit
unit_type getUnit() const
Get the unit for this quantity.
Definition: Quantity.h:131