ome-xml 5.2.3

UnitsTemperatureConvert.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_UNITSTEMPERATURECONVERT_H
40 #define OME_XML_MODEL_ENUMS_UNITSTEMPERATURECONVERT_H
41
42 #include <boost/preprocessor.hpp>
43
44 #include <ome/common/units/temperature.h>
45
46 #include <ome/xml/model/enums/UnitsTemperature.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::UnitsTemperature;
62
63 #define OME_XML_MODEL_ENUMS_UNITSTEMPERATURE_PROPERTY_LIST \
64 ((CELSIUS)(celsius_absolute_quantity)) \
65 ((FAHRENHEIT)(fahrenheit_absolute_quantity)) \
66 ((KELVIN)(kelvin_absolute_quantity)) \
67 ((RANKINE)(rankine_absolute_quantity))
68
72 template<int>
73 struct TemperatureProperties;
74
75 #define OME_XML_MODEL_ENUMS_UNITSTEMPERATURE_UNIT_CASE(maR, maProperty, maType) \
76 template<> \
77 struct TemperatureProperties<UnitsTemperature::BOOST_PP_SEQ_ELEM(0, maType)> \
78 { \
79 typedef BOOST_PP_SEQ_ELEM(1, maType) quantity_type; \
80 };
81
82 BOOST_PP_SEQ_FOR_EACH(OME_XML_MODEL_ENUMS_UNITSTEMPERATURE_UNIT_CASE, %%, OME_XML_MODEL_ENUMS_UNITSTEMPERATURE_PROPERTY_LIST)
83
84 #undef OME_XML_MODEL_ENUMS_UNITSTEMPERATURE_UNIT_CASE
85 #undef OME_XML_MODEL_ENUMS_UNITSTEMPERATURE_PROPERTY_LIST
86
87 // Convert two units
88 template<typename Q, int Src, int Dest>
89 Q
90 temperature_convert_src_dest(typename Q::value_type v,
91 typename Q::unit_type dest)
92 {
93 typename TemperatureProperties<Dest>::quantity_type d(TemperatureProperties<Src>::quantity_type::from_value(v));
94 return Q(quantity_cast<typename Q::value_type>(d), dest);
95 }
96
97 // No switch default to avoid -Wunreachable-code errors.
98 // However, this then makes -Wswitch-default complain. Disable
99 // temporarily.
100 #ifdef __GNUC__
101 # pragma GCC diagnostic push
102 # pragma GCC diagnostic ignored "-Wswitch-default"
103 #endif
104
105 #define OME_XML_MODEL_ENUMS_UNITSTEMPERATURE_DEST_UNIT_CASE(maR, maProperty, maType) \
106 case UnitsTemperature::maType: \
107 { \
108 maProperty = temperature_convert_src_dest<Q, Src, UnitsTemperature::maType>(value, dest); \
109 } \
110 break;
111
112 template<typename Q, int Src>
113 Q
114 temperature_convert_dest(typename Q::value_type value,
115 typename Q::unit_type dest)
116 {
117 Q q;
118
119 switch(dest)
120 {
121 BOOST_PP_SEQ_FOR_EACH(OME_XML_MODEL_ENUMS_UNITSTEMPERATURE_DEST_UNIT_CASE, q, OME_XML_MODEL_ENUMS_UNITSTEMPERATURE_VALUES);
122 }
123
124 return q;
125 }
126
127 #undef OME_XML_MODEL_ENUMS_UNITSTEMPERATURE_DEST_UNIT_CASE
128
129 #ifdef __GNUC__
130 # pragma GCC diagnostic pop
131 #endif
132
133 }
134
135 // No switch default to avoid -Wunreachable-code errors.
136 // However, this then makes -Wswitch-default complain. Disable
137 // temporarily.
138 #ifdef __GNUC__
139 # pragma GCC diagnostic push
140 # pragma GCC diagnostic ignored "-Wswitch-default"
141 #endif
142
143 #define OME_XML_MODEL_ENUMS_UNITSTEMPERATURE_SRC_UNIT_CASE(maR, maProperty, maType) \
144 case ome::xml::model::enums::UnitsTemperature::maType: \
145 maProperty = detail::temperature_convert_dest<Quantity<ome::xml::model::enums::UnitsTemperature>, \
146 ome::xml::model::enums::UnitsTemperature::maType>(quantity.getValue(), unit); \
147 break;
148
150 template<typename Value>
151 struct QuantityConverter<ome::xml::model::enums::UnitsTemperature, Value>
152 {
154 inline
155 Quantity<ome::xml::model::enums::UnitsTemperature, Value>
156 operator() (const Quantity<ome::xml::model::enums::UnitsTemperature, Value>& quantity,
157 typename Quantity<ome::xml::model::enums::UnitsTemperature, Value>::unit_type unit) const
158 {
159 Quantity<ome::xml::model::enums::UnitsTemperature, Value> q;
160
161 switch(quantity.getUnit())
162 {
163 BOOST_PP_SEQ_FOR_EACH(OME_XML_MODEL_ENUMS_UNITSTEMPERATURE_SRC_UNIT_CASE, q, OME_XML_MODEL_ENUMS_UNITSTEMPERATURE_VALUES);
164 }
165
166 return q;
167 }
168 };
169
170 #undef OME_XML_MODEL_ENUMS_UNITSTEMPERATURE_SRC_UNIT_CASE
171
172 #ifdef __GNUC__
173 # pragma GCC diagnostic pop
174 #endif
175
176 }
177 }
178 }
179 }
180
181 #endif // OME_XML_MODEL_ENUMS_UNITSTEMPERATURECONVERT_H
182
183 /*
184 * Local Variables:
185 * mode:C++
186 * End:
187 */
ome::xml::model::primitives::detail::TemperatureProperties
Map a given UnitsTemperature enum to the corresponding language types.
Definition: UnitsTemperatureConvert.h:73
temperature.h
ome::xml::model::enums::UnitsElectricPotential
UnitsElectricPotential enumeration.
Definition: UnitsElectricPotential.h:74
ome::xml::model::enums::UnitsTemperature
UnitsTemperature enumeration.
Definition: UnitsTemperature.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::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