Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/units/conversion.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Boost.Units - A C++ library for zero-overhead dimensional analysis and 
0002 // unit/quantity manipulation and conversion
0003 //
0004 // Copyright (C) 2003-2008 Matthias Christian Schabel
0005 // Copyright (C) 2007-2008 Steven Watanabe
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See
0008 // accompanying file LICENSE_1_0.txt or copy at
0009 // http://www.boost.org/LICENSE_1_0.txt)
0010 
0011 #ifndef BOOST_UNITS_CONVERSION_HPP
0012 #define BOOST_UNITS_CONVERSION_HPP
0013 
0014 /// \file
0015 /// \brief Template for defining conversions between quantities.
0016 
0017 #include <boost/units/detail/conversion_impl.hpp>
0018 
0019 namespace boost {
0020 
0021 namespace units {
0022 
0023 template<class From, class To>
0024 struct conversion_helper;
0025 
0026 #ifdef BOOST_UNITS_DOXYGEN
0027 
0028 /// Template for defining conversions between
0029 /// quantities.  This template should be specialized
0030 /// for every quantity that allows conversions.
0031 /// For example, if you have a two units
0032 /// called pair and dozen you would write
0033 /// @code
0034 /// namespace boost {
0035 /// namespace units {
0036 /// template<class T0, class T1>
0037 /// struct conversion_helper<quantity<dozen, T0>, quantity<pair, T1> >
0038 /// {
0039 ///     static quantity<pair, T1> convert(const quantity<dozen, T0>& source)
0040 ///     {
0041 ///         return(quantity<pair, T1>::from_value(6 * source.value()));
0042 ///     }
0043 /// };
0044 /// }
0045 /// }
0046 /// @endcode
0047 ///
0048 /// In most cases, the predefined specializations for @c unit
0049 /// and @c absolute should be sufficient, so users should rarely
0050 /// need to use this.
0051 template<class From, class To>
0052 struct conversion_helper
0053 {
0054     static BOOST_CONSTEXPR To convert(const From&);
0055 };
0056 
0057 #endif
0058 
0059 /// Defines the conversion factor from a base unit to any unit
0060 /// or to another base unit with the correct dimensions.  Uses
0061 /// of this macro must appear at global scope.
0062 /// If the destination unit is a base unit or a unit that contains
0063 /// only one base unit which is raised to the first power (e.g. feet->meters)
0064 /// the reverse (meters->feet in this example) need not be defined explicitly.
0065 #define BOOST_UNITS_DEFINE_CONVERSION_FACTOR(Source, Destination, type_, value_)    \
0066     namespace boost {                                                       \
0067     namespace units {                                                       \
0068     template<>                                                              \
0069     struct select_base_unit_converter<                                      \
0070         unscale<Source>::type,                                              \
0071         unscale<reduce_unit<Destination::unit_type>::type>::type            \
0072     >                                                                       \
0073     {                                                                       \
0074         typedef Source source_type;                                         \
0075         typedef reduce_unit<Destination::unit_type>::type destination_type; \
0076     };                                                                      \
0077     template<>                                                              \
0078     struct base_unit_converter<Source, reduce_unit<Destination::unit_type>::type>   \
0079     {                                                                       \
0080         BOOST_STATIC_CONSTEXPR bool is_defined = true;                      \
0081         typedef type_ type;                                                 \
0082         static BOOST_CONSTEXPR type value() { return(value_); }             \
0083     };                                                                      \
0084     }                                                                       \
0085     }                                                                       \
0086     void boost_units_require_semicolon()
0087 
0088 /// Defines the conversion factor from a base unit to any other base
0089 /// unit with the same dimensions.  Params should be a Boost.Preprocessor
0090 /// Seq of template parameters, such as (class T1)(class T2)
0091 /// All uses of must appear at global scope. The reverse conversion will
0092 /// be defined automatically.  This macro is a little dangerous, because,
0093 /// unlike the non-template form, it will silently fail if either base
0094 /// unit is scaled.  This is probably not an issue if both the source
0095 /// and destination types depend on the template parameters, but be aware
0096 /// that a generic conversion to kilograms is not going to work.
0097 #define BOOST_UNITS_DEFINE_CONVERSION_FACTOR_TEMPLATE(Params, Source, Destination, type_, value_)   \
0098     namespace boost {                                                       \
0099     namespace units {                                                       \
0100     template<BOOST_PP_SEQ_ENUM(Params)>                                     \
0101     struct base_unit_converter<                                             \
0102         Source,                                                             \
0103         BOOST_UNITS_MAKE_HETEROGENEOUS_UNIT(Destination, typename Source::dimension_type)\
0104     >                                                                       \
0105     {                                                                       \
0106         BOOST_STATIC_CONSTEXPR bool is_defined = true;                      \
0107         typedef type_ type;                                                 \
0108         static BOOST_CONSTEXPR type value() { return(value_); }             \
0109     };                                                                      \
0110     }                                                                       \
0111     }                                                                       \
0112     void boost_units_require_semicolon()
0113 
0114 /// Specifies the default conversion to be applied when
0115 /// no direct conversion is available.
0116 /// Source is a base unit.  Dest is any unit with the
0117 /// same dimensions.
0118 #define BOOST_UNITS_DEFAULT_CONVERSION(Source, Dest)                \
0119     namespace boost {                                               \
0120     namespace units {                                               \
0121     template<>                                                      \
0122     struct unscaled_get_default_conversion<unscale<Source>::type>   \
0123     {                                                               \
0124         BOOST_STATIC_CONSTEXPR bool is_defined = true;              \
0125         typedef Dest::unit_type type;                               \
0126     };                                                              \
0127     }                                                               \
0128     }                                                               \
0129     void boost_units_require_semicolon()
0130 
0131 /// Specifies the default conversion to be applied when
0132 /// no direct conversion is available.
0133 /// Params is a PP Sequence of template arguments.
0134 /// Source is a base unit.  Dest is any unit with the
0135 /// same dimensions.  The source must not be a scaled
0136 /// base unit.
0137 #define BOOST_UNITS_DEFAULT_CONVERSION_TEMPLATE(Params, Source, Dest)   \
0138     namespace boost {                                                   \
0139     namespace units {                                                   \
0140     template<BOOST_PP_SEQ_ENUM(Params)>                                 \
0141     struct unscaled_get_default_conversion<Source>                      \
0142     {                                                                   \
0143         BOOST_STATIC_CONSTEXPR bool is_defined = true;                  \
0144         typedef typename Dest::unit_type type;                          \
0145     };                                                                  \
0146     }                                                                   \
0147     }                                                                   \
0148     void boost_units_require_semicolon()
0149 
0150 /// INTERNAL ONLY
0151 /// Users should not create their units in namespace boost::units.
0152 /// If we want to make this public it needs to allow better control over
0153 /// the namespaces. --SJW.
0154 /// template that defines a base_unit and conversion to another dimensionally-consistent unit
0155 #define BOOST_UNITS_DEFINE_BASE_UNIT_WITH_CONVERSIONS(namespace_, name_, name_string_, symbol_string_, factor, unit, id)\
0156 namespace boost {                                                           \
0157 namespace units {                                                           \
0158 namespace namespace_ {                                                      \
0159 struct name_ ## _base_unit                                                  \
0160   : base_unit<name_ ## _base_unit, unit::dimension_type, id> {              \
0161     static BOOST_CONSTEXPR const char* name() { return(name_string_); }     \
0162     static BOOST_CONSTEXPR const char* symbol() { return(symbol_string_); } \
0163 };                                                                          \
0164 }                                                                           \
0165 }                                                                           \
0166 }                                                                           \
0167 BOOST_UNITS_DEFINE_CONVERSION_FACTOR(namespace_::name_ ## _base_unit, unit, double, factor); \
0168 BOOST_UNITS_DEFAULT_CONVERSION(namespace_::name_ ## _base_unit, unit)
0169 
0170 /// Find the conversion factor between two units.
0171 template<class FromUnit,class ToUnit>
0172 inline
0173 BOOST_CONSTEXPR
0174 typename one_to_double_type<
0175     typename detail::conversion_factor_helper<FromUnit, ToUnit>::type
0176 >::type
0177 conversion_factor(const FromUnit&,const ToUnit&)
0178 {
0179     return(one_to_double(detail::conversion_factor_helper<FromUnit, ToUnit>::value()));
0180 }
0181 
0182 } // namespace units
0183 
0184 } // namespace boost
0185 
0186 #endif // BOOST_UNITS_CONVERSION_HPP