Chris@16: // Boost.Units - A C++ library for zero-overhead dimensional analysis and Chris@16: // unit/quantity manipulation and conversion Chris@16: // Chris@16: // Copyright (C) 2003-2008 Matthias Christian Schabel Chris@16: // Copyright (C) 2007-2008 Steven Watanabe Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_UNITS_CONVERSION_HPP Chris@16: #define BOOST_UNITS_CONVERSION_HPP Chris@16: Chris@16: /// \file Chris@16: /// \brief Template for defining conversions between quantities. Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: namespace units { Chris@16: Chris@16: template Chris@16: struct conversion_helper; Chris@16: Chris@16: #ifdef BOOST_UNITS_DOXYGEN Chris@16: Chris@16: /// Template for defining conversions between Chris@16: /// quantities. This template should be specialized Chris@16: /// for every quantity that allows conversions. Chris@16: /// For example, if you have a two units Chris@16: /// called pair and dozen you would write Chris@16: /// @code Chris@16: /// namespace boost { Chris@16: /// namespace units { Chris@16: /// template Chris@16: /// struct conversion_helper, quantity > Chris@16: /// { Chris@16: /// static quantity convert(const quantity& source) Chris@16: /// { Chris@16: /// return(quantity::from_value(6 * source.value())); Chris@16: /// } Chris@16: /// }; Chris@16: /// } Chris@16: /// } Chris@16: /// @endcode Chris@16: /// Chris@16: /// In most cases, the predefined specializations for @c unit Chris@16: /// and @c absolute should be sufficient, so users should rarely Chris@16: /// need to use this. Chris@16: template Chris@16: struct conversion_helper Chris@16: { Chris@16: static To convert(const From&); Chris@16: }; Chris@16: Chris@16: #endif Chris@16: Chris@16: /// Defines the conversion factor from a base unit to any unit Chris@16: /// or to another base unit with the correct dimensions. Uses Chris@16: /// of this macro must appear at global scope. Chris@16: /// If the destination unit is a base unit or a unit that contains Chris@16: /// only one base unit which is raised to the first power (e.g. feet->meters) Chris@16: /// the reverse (meters->feet in this example) need not be defined explicitly. Chris@16: #define BOOST_UNITS_DEFINE_CONVERSION_FACTOR(Source, Destination, type_, value_) \ Chris@16: namespace boost { \ Chris@16: namespace units { \ Chris@16: template<> \ Chris@16: struct select_base_unit_converter< \ Chris@16: unscale::type, \ Chris@16: unscale::type>::type \ Chris@16: > \ Chris@16: { \ Chris@16: typedef Source source_type; \ Chris@16: typedef reduce_unit::type destination_type; \ Chris@16: }; \ Chris@16: template<> \ Chris@16: struct base_unit_converter::type> \ Chris@16: { \ Chris@16: static const bool is_defined = true; \ Chris@16: typedef type_ type; \ Chris@16: static type value() { return(value_); } \ Chris@16: }; \ Chris@16: } \ Chris@16: } \ Chris@16: void boost_units_require_semicolon() Chris@16: Chris@16: /// Defines the conversion factor from a base unit to any other base Chris@16: /// unit with the same dimensions. Params should be a Boost.Preprocessor Chris@16: /// Seq of template parameters, such as (class T1)(class T2) Chris@16: /// All uses of must appear at global scope. The reverse conversion will Chris@16: /// be defined automatically. This macro is a little dangerous, because, Chris@16: /// unlike the non-template form, it will silently fail if either base Chris@16: /// unit is scaled. This is probably not an issue if both the source Chris@16: /// and destination types depend on the template parameters, but be aware Chris@16: /// that a generic conversion to kilograms is not going to work. Chris@16: #define BOOST_UNITS_DEFINE_CONVERSION_FACTOR_TEMPLATE(Params, Source, Destination, type_, value_) \ Chris@16: namespace boost { \ Chris@16: namespace units { \ Chris@16: template \ Chris@16: struct base_unit_converter< \ Chris@16: Source, \ Chris@16: BOOST_UNITS_MAKE_HETEROGENEOUS_UNIT(Destination, typename Source::dimension_type)\ Chris@16: > \ Chris@16: { \ Chris@16: static const bool is_defined = true; \ Chris@16: typedef type_ type; \ Chris@16: static type value() { return(value_); } \ Chris@16: }; \ Chris@16: } \ Chris@16: } \ Chris@16: void boost_units_require_semicolon() Chris@16: Chris@16: /// Specifies the default conversion to be applied when Chris@16: /// no direct conversion is available. Chris@16: /// Source is a base unit. Dest is any unit with the Chris@16: /// same dimensions. Chris@16: #define BOOST_UNITS_DEFAULT_CONVERSION(Source, Dest) \ Chris@16: namespace boost { \ Chris@16: namespace units { \ Chris@16: template<> \ Chris@16: struct unscaled_get_default_conversion::type> \ Chris@16: { \ Chris@16: static const bool is_defined = true; \ Chris@16: typedef Dest::unit_type type; \ Chris@16: }; \ Chris@16: } \ Chris@16: } \ Chris@16: void boost_units_require_semicolon() Chris@16: Chris@16: /// Specifies the default conversion to be applied when Chris@16: /// no direct conversion is available. Chris@16: /// Params is a PP Sequence of template arguments. Chris@16: /// Source is a base unit. Dest is any unit with the Chris@16: /// same dimensions. The source must not be a scaled Chris@16: /// base unit. Chris@16: #define BOOST_UNITS_DEFAULT_CONVERSION_TEMPLATE(Params, Source, Dest) \ Chris@16: namespace boost { \ Chris@16: namespace units { \ Chris@16: template \ Chris@16: struct unscaled_get_default_conversion \ Chris@16: { \ Chris@16: static const bool is_defined = true; \ Chris@16: typedef typename Dest::unit_type type; \ Chris@16: }; \ Chris@16: } \ Chris@16: } \ Chris@16: void boost_units_require_semicolon() Chris@16: Chris@16: /// INTERNAL ONLY Chris@16: /// Users should not create their units in namespace boost::units. Chris@16: /// If we want to make this public it needs to allow better control over Chris@16: /// the namespaces. --SJW. Chris@16: /// template that defines a base_unit and conversion to another dimensionally-consistent unit Chris@16: #define BOOST_UNITS_DEFINE_BASE_UNIT_WITH_CONVERSIONS(namespace_, name_, name_string_, symbol_string_, factor, unit, id)\ Chris@16: namespace boost { \ Chris@16: namespace units { \ Chris@16: namespace namespace_ { \ Chris@16: struct name_ ## _base_unit \ Chris@16: : base_unit { \ Chris@16: static const char* name() { return(name_string_); } \ Chris@16: static const char* symbol() { return(symbol_string_); }; \ Chris@16: }; \ Chris@16: } \ Chris@16: } \ Chris@16: } \ Chris@16: BOOST_UNITS_DEFINE_CONVERSION_FACTOR(namespace_::name_ ## _base_unit, unit, double, factor); \ Chris@16: BOOST_UNITS_DEFAULT_CONVERSION(namespace_::name_ ## _base_unit, unit) Chris@16: Chris@16: /// Find the conversion factor between two units. Chris@16: template Chris@16: inline Chris@16: typename one_to_double_type< Chris@16: typename detail::conversion_factor_helper::type Chris@16: >::type Chris@16: conversion_factor(const FromUnit&,const ToUnit&) Chris@16: { Chris@16: return(one_to_double(detail::conversion_factor_helper::value())); Chris@16: } Chris@16: Chris@16: } // namespace units Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_UNITS_CONVERSION_HPP