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