Chris@16
|
1 // Boost.Units - A C++ library for zero-overhead dimensional analysis and
|
Chris@16
|
2 // unit/quantity manipulation and conversion
|
Chris@16
|
3 //
|
Chris@16
|
4 // Copyright (C) 2003-2008 Matthias Christian Schabel
|
Chris@16
|
5 // Copyright (C) 2007-2008 Steven Watanabe
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
8 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
9 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_UNITS_CONVERSION_HPP
|
Chris@16
|
12 #define BOOST_UNITS_CONVERSION_HPP
|
Chris@16
|
13
|
Chris@16
|
14 /// \file
|
Chris@16
|
15 /// \brief Template for defining conversions between quantities.
|
Chris@16
|
16
|
Chris@16
|
17 #include <boost/units/detail/conversion_impl.hpp>
|
Chris@16
|
18
|
Chris@16
|
19 namespace boost {
|
Chris@16
|
20
|
Chris@16
|
21 namespace units {
|
Chris@16
|
22
|
Chris@16
|
23 template<class From, class To>
|
Chris@16
|
24 struct conversion_helper;
|
Chris@16
|
25
|
Chris@16
|
26 #ifdef BOOST_UNITS_DOXYGEN
|
Chris@16
|
27
|
Chris@16
|
28 /// Template for defining conversions between
|
Chris@16
|
29 /// quantities. This template should be specialized
|
Chris@16
|
30 /// for every quantity that allows conversions.
|
Chris@16
|
31 /// For example, if you have a two units
|
Chris@16
|
32 /// called pair and dozen you would write
|
Chris@16
|
33 /// @code
|
Chris@16
|
34 /// namespace boost {
|
Chris@16
|
35 /// namespace units {
|
Chris@16
|
36 /// template<class T0, class T1>
|
Chris@16
|
37 /// struct conversion_helper<quantity<dozen, T0>, quantity<pair, T1> >
|
Chris@16
|
38 /// {
|
Chris@16
|
39 /// static quantity<pair, T1> convert(const quantity<dozen, T0>& source)
|
Chris@16
|
40 /// {
|
Chris@16
|
41 /// return(quantity<pair, T1>::from_value(6 * source.value()));
|
Chris@16
|
42 /// }
|
Chris@16
|
43 /// };
|
Chris@16
|
44 /// }
|
Chris@16
|
45 /// }
|
Chris@16
|
46 /// @endcode
|
Chris@16
|
47 ///
|
Chris@16
|
48 /// In most cases, the predefined specializations for @c unit
|
Chris@16
|
49 /// and @c absolute should be sufficient, so users should rarely
|
Chris@16
|
50 /// need to use this.
|
Chris@16
|
51 template<class From, class To>
|
Chris@16
|
52 struct conversion_helper
|
Chris@16
|
53 {
|
Chris@16
|
54 static To convert(const From&);
|
Chris@16
|
55 };
|
Chris@16
|
56
|
Chris@16
|
57 #endif
|
Chris@16
|
58
|
Chris@16
|
59 /// Defines the conversion factor from a base unit to any unit
|
Chris@16
|
60 /// or to another base unit with the correct dimensions. Uses
|
Chris@16
|
61 /// of this macro must appear at global scope.
|
Chris@16
|
62 /// If the destination unit is a base unit or a unit that contains
|
Chris@16
|
63 /// only one base unit which is raised to the first power (e.g. feet->meters)
|
Chris@16
|
64 /// the reverse (meters->feet in this example) need not be defined explicitly.
|
Chris@16
|
65 #define BOOST_UNITS_DEFINE_CONVERSION_FACTOR(Source, Destination, type_, value_) \
|
Chris@16
|
66 namespace boost { \
|
Chris@16
|
67 namespace units { \
|
Chris@16
|
68 template<> \
|
Chris@16
|
69 struct select_base_unit_converter< \
|
Chris@16
|
70 unscale<Source>::type, \
|
Chris@16
|
71 unscale<reduce_unit<Destination::unit_type>::type>::type \
|
Chris@16
|
72 > \
|
Chris@16
|
73 { \
|
Chris@16
|
74 typedef Source source_type; \
|
Chris@16
|
75 typedef reduce_unit<Destination::unit_type>::type destination_type; \
|
Chris@16
|
76 }; \
|
Chris@16
|
77 template<> \
|
Chris@16
|
78 struct base_unit_converter<Source, reduce_unit<Destination::unit_type>::type> \
|
Chris@16
|
79 { \
|
Chris@16
|
80 static const bool is_defined = true; \
|
Chris@16
|
81 typedef type_ type; \
|
Chris@16
|
82 static type value() { return(value_); } \
|
Chris@16
|
83 }; \
|
Chris@16
|
84 } \
|
Chris@16
|
85 } \
|
Chris@16
|
86 void boost_units_require_semicolon()
|
Chris@16
|
87
|
Chris@16
|
88 /// Defines the conversion factor from a base unit to any other base
|
Chris@16
|
89 /// unit with the same dimensions. Params should be a Boost.Preprocessor
|
Chris@16
|
90 /// Seq of template parameters, such as (class T1)(class T2)
|
Chris@16
|
91 /// All uses of must appear at global scope. The reverse conversion will
|
Chris@16
|
92 /// be defined automatically. This macro is a little dangerous, because,
|
Chris@16
|
93 /// unlike the non-template form, it will silently fail if either base
|
Chris@16
|
94 /// unit is scaled. This is probably not an issue if both the source
|
Chris@16
|
95 /// and destination types depend on the template parameters, but be aware
|
Chris@16
|
96 /// that a generic conversion to kilograms is not going to work.
|
Chris@16
|
97 #define BOOST_UNITS_DEFINE_CONVERSION_FACTOR_TEMPLATE(Params, Source, Destination, type_, value_) \
|
Chris@16
|
98 namespace boost { \
|
Chris@16
|
99 namespace units { \
|
Chris@16
|
100 template<BOOST_PP_SEQ_ENUM(Params)> \
|
Chris@16
|
101 struct base_unit_converter< \
|
Chris@16
|
102 Source, \
|
Chris@16
|
103 BOOST_UNITS_MAKE_HETEROGENEOUS_UNIT(Destination, typename Source::dimension_type)\
|
Chris@16
|
104 > \
|
Chris@16
|
105 { \
|
Chris@16
|
106 static const bool is_defined = true; \
|
Chris@16
|
107 typedef type_ type; \
|
Chris@16
|
108 static type value() { return(value_); } \
|
Chris@16
|
109 }; \
|
Chris@16
|
110 } \
|
Chris@16
|
111 } \
|
Chris@16
|
112 void boost_units_require_semicolon()
|
Chris@16
|
113
|
Chris@16
|
114 /// Specifies the default conversion to be applied when
|
Chris@16
|
115 /// no direct conversion is available.
|
Chris@16
|
116 /// Source is a base unit. Dest is any unit with the
|
Chris@16
|
117 /// same dimensions.
|
Chris@16
|
118 #define BOOST_UNITS_DEFAULT_CONVERSION(Source, Dest) \
|
Chris@16
|
119 namespace boost { \
|
Chris@16
|
120 namespace units { \
|
Chris@16
|
121 template<> \
|
Chris@16
|
122 struct unscaled_get_default_conversion<unscale<Source>::type> \
|
Chris@16
|
123 { \
|
Chris@16
|
124 static const bool is_defined = true; \
|
Chris@16
|
125 typedef Dest::unit_type type; \
|
Chris@16
|
126 }; \
|
Chris@16
|
127 } \
|
Chris@16
|
128 } \
|
Chris@16
|
129 void boost_units_require_semicolon()
|
Chris@16
|
130
|
Chris@16
|
131 /// Specifies the default conversion to be applied when
|
Chris@16
|
132 /// no direct conversion is available.
|
Chris@16
|
133 /// Params is a PP Sequence of template arguments.
|
Chris@16
|
134 /// Source is a base unit. Dest is any unit with the
|
Chris@16
|
135 /// same dimensions. The source must not be a scaled
|
Chris@16
|
136 /// base unit.
|
Chris@16
|
137 #define BOOST_UNITS_DEFAULT_CONVERSION_TEMPLATE(Params, Source, Dest) \
|
Chris@16
|
138 namespace boost { \
|
Chris@16
|
139 namespace units { \
|
Chris@16
|
140 template<BOOST_PP_SEQ_ENUM(Params)> \
|
Chris@16
|
141 struct unscaled_get_default_conversion<Source> \
|
Chris@16
|
142 { \
|
Chris@16
|
143 static const bool is_defined = true; \
|
Chris@16
|
144 typedef typename Dest::unit_type type; \
|
Chris@16
|
145 }; \
|
Chris@16
|
146 } \
|
Chris@16
|
147 } \
|
Chris@16
|
148 void boost_units_require_semicolon()
|
Chris@16
|
149
|
Chris@16
|
150 /// INTERNAL ONLY
|
Chris@16
|
151 /// Users should not create their units in namespace boost::units.
|
Chris@16
|
152 /// If we want to make this public it needs to allow better control over
|
Chris@16
|
153 /// the namespaces. --SJW.
|
Chris@16
|
154 /// template that defines a base_unit and conversion to another dimensionally-consistent unit
|
Chris@16
|
155 #define BOOST_UNITS_DEFINE_BASE_UNIT_WITH_CONVERSIONS(namespace_, name_, name_string_, symbol_string_, factor, unit, id)\
|
Chris@16
|
156 namespace boost { \
|
Chris@16
|
157 namespace units { \
|
Chris@16
|
158 namespace namespace_ { \
|
Chris@16
|
159 struct name_ ## _base_unit \
|
Chris@16
|
160 : base_unit<name_ ## _base_unit, unit::dimension_type, id> { \
|
Chris@16
|
161 static const char* name() { return(name_string_); } \
|
Chris@16
|
162 static const char* symbol() { return(symbol_string_); }; \
|
Chris@16
|
163 }; \
|
Chris@16
|
164 } \
|
Chris@16
|
165 } \
|
Chris@16
|
166 } \
|
Chris@16
|
167 BOOST_UNITS_DEFINE_CONVERSION_FACTOR(namespace_::name_ ## _base_unit, unit, double, factor); \
|
Chris@16
|
168 BOOST_UNITS_DEFAULT_CONVERSION(namespace_::name_ ## _base_unit, unit)
|
Chris@16
|
169
|
Chris@16
|
170 /// Find the conversion factor between two units.
|
Chris@16
|
171 template<class FromUnit,class ToUnit>
|
Chris@16
|
172 inline
|
Chris@16
|
173 typename one_to_double_type<
|
Chris@16
|
174 typename detail::conversion_factor_helper<FromUnit, ToUnit>::type
|
Chris@16
|
175 >::type
|
Chris@16
|
176 conversion_factor(const FromUnit&,const ToUnit&)
|
Chris@16
|
177 {
|
Chris@16
|
178 return(one_to_double(detail::conversion_factor_helper<FromUnit, ToUnit>::value()));
|
Chris@16
|
179 }
|
Chris@16
|
180
|
Chris@16
|
181 } // namespace units
|
Chris@16
|
182
|
Chris@16
|
183 } // namespace boost
|
Chris@16
|
184
|
Chris@16
|
185 #endif // BOOST_UNITS_CONVERSION_HPP
|