// Boost.Units - A C++ library for zero-overhead dimensional analysis and // unit/quantity manipulation and conversion // // Copyright (C) 2003-2008 Matthias Christian Schabel // Copyright (C) 2007-2008 Steven Watanabe // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_UNITS_DETAIL_CONVERSION_IMPL_HPP #define BOOST_UNITS_DETAIL_CONVERSION_IMPL_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace units { namespace detail { template struct conversion_factor_helper; template struct call_base_unit_converter; } /// INTERNAL ONLY struct undefined_base_unit_converter_base { static const bool is_defined = false; }; /// INTERNAL ONLY struct no_default_conversion { static const bool is_defined = false; }; /// INTERNAL ONLY template struct unscaled_get_default_conversion : no_default_conversion { }; /// INTERNAL ONLY template struct unscaled_get_default_conversion_impl; /// INTERNAL ONLY template<> struct unscaled_get_default_conversion_impl { template struct apply { typedef typename unscaled_get_default_conversion::type>::type type; }; }; /// INTERNAL ONLY template<> struct unscaled_get_default_conversion_impl { template struct apply { typedef typename T::unit_type type; }; }; /// INTERNAL ONLY template struct get_default_conversion { typedef typename unscaled_get_default_conversion_impl< unscaled_get_default_conversion::type>::is_defined >::template apply::type type; }; /// INTERNAL ONLY template struct select_base_unit_converter { typedef Source source_type; typedef Destination destination_type; }; /// INTERNAL ONLY template struct base_unit_converter_base : undefined_base_unit_converter_base { }; /// INTERNAL ONLY template struct base_unit_converter_base { static const bool is_defined = true; typedef one type; static type value() { one result; return(result); } }; /// INTERNAL ONLY template struct base_unit_converter : base_unit_converter_base { }; namespace detail { template struct do_call_base_unit_converter { typedef select_base_unit_converter::type, typename unscale::type> selector; typedef typename selector::source_type source_type; typedef typename selector::destination_type destination_type; typedef base_unit_converter converter; typedef typename mpl::divides::type, typename get_scale_list::type>::type source_factor; typedef typename mpl::divides::type, typename get_scale_list::type>::type destination_factor; typedef typename mpl::divides::type factor; typedef eval_scale_list eval_factor; typedef typename multiply_typeof_helper::type type; static type value() { return(converter::value() * eval_factor::value()); } }; template struct call_base_unit_converter_base_unit_impl; template<> struct call_base_unit_converter_base_unit_impl { template struct apply : do_call_base_unit_converter { }; }; template<> struct call_base_unit_converter_base_unit_impl { template struct apply : do_call_base_unit_converter { }; }; template<> struct call_base_unit_converter_base_unit_impl { template struct apply { typedef do_call_base_unit_converter converter; typedef typename divide_typeof_helper::type type; static type value() { one numerator; return(numerator / converter::value()); } }; }; template<> struct call_base_unit_converter_base_unit_impl { template struct apply { typedef typename reduce_unit::type>::type new_source; typedef typename reduce_unit::type>::type new_dest; typedef call_base_unit_converter start; typedef detail::conversion_factor_helper< new_source, new_dest > conversion; typedef call_base_unit_converter end; typedef typename divide_typeof_helper< typename multiply_typeof_helper< typename start::type, typename conversion::type >::type, typename end::type >::type type; static type value() { return(start::value() * conversion::value() / end::value()); } }; }; template struct get_default_conversion_impl { template struct apply { typedef typename Begin::item source_pair; typedef typename source_pair::value_type exponent; typedef typename source_pair::tag_type source; typedef typename reduce_unit::type>::type new_source; typedef typename get_default_conversion_impl::template apply next_iteration; typedef typename multiply_typeof_helper::type, typename next_iteration::unit_type>::type unit_type; typedef call_base_unit_converter conversion; typedef typename multiply_typeof_helper::type type; static type value() { return(static_rational_power(conversion::value()) * next_iteration::value()); } }; }; template<> struct get_default_conversion_impl<0> { template struct apply { typedef unit > > unit_type; typedef one type; static one value() { one result; return(result); } }; }; template struct call_base_unit_converter_impl; template<> struct call_base_unit_converter_impl { template struct apply : do_call_base_unit_converter { }; }; template<> struct call_base_unit_converter_impl { template struct apply { typedef typename reduce_unit::type>::type new_source; typedef typename Dest::system_type::type system_list; typedef typename get_default_conversion_impl::template apply impl; typedef typename impl::unit_type new_dest; typedef call_base_unit_converter start; typedef conversion_factor_helper conversion; typedef typename divide_typeof_helper< typename multiply_typeof_helper< typename start::type, typename conversion::type >::type, typename impl::type >::type type; static type value() { return(start::value() * conversion::value() / impl::value()); } }; }; #define BOOST_UNITS_DETAIL_BASE_UNIT_CONVERTER_IS_DEFINED(Source, Dest)\ base_unit_converter<\ typename select_base_unit_converter::type, typename unscale::type>::source_type,\ typename select_base_unit_converter::type, typename unscale::type>::destination_type\ >::is_defined template struct call_base_unit_converter : call_base_unit_converter_impl::template apply { }; template struct call_base_unit_converter : call_base_unit_converter_base_unit_impl< BOOST_UNITS_DETAIL_BASE_UNIT_CONVERTER_IS_DEFINED(Source, typename Dest::unit_type), BOOST_UNITS_DETAIL_BASE_UNIT_CONVERTER_IS_DEFINED(Dest, typename Source::unit_type) >::template apply { }; template struct conversion_impl { template struct apply { typedef typename conversion_impl::template apply< typename Begin::next, DestinationSystem > next_iteration; typedef typename Begin::item unit_pair; typedef typename unit_pair::tag_type unit; typedef typename unit::dimension_type dimensions; typedef typename reduce_unit >::type reduced_unit; typedef detail::call_base_unit_converter converter; typedef typename multiply_typeof_helper::type type; static type value() { return(static_rational_power(converter::value()) * next_iteration::value()); } }; }; template<> struct conversion_impl<0> { template struct apply { typedef one type; static type value() { one result; return(result); } }; }; } // namespace detail /// forward to conversion_factor (intentionally allowing ADL) /// INTERNAL ONLY template struct conversion_helper, quantity > { /// INTERNAL ONLY typedef quantity destination_type; static destination_type convert(const quantity& source) { Unit1 u1; Unit2 u2; return(destination_type::from_value(static_cast(source.value() * conversion_factor(u1, u2)))); } }; namespace detail { template struct conversion_factor_helper; template struct conversion_factor_helper >, unit > > : conversion_factor_helper< typename reduce_unit > >::type, typename reduce_unit > >::type > { //typedef typename reduce_unit > >::type source_unit; //typedef typename source_unit::system_type::type unit_list; //typedef typename detail::conversion_impl::template apply< // unit_list, // homogeneous_system //> impl; //typedef typename impl::type type; //static type value() //{ // return(impl::value()); //} }; template struct conversion_factor_helper >, unit > > : conversion_factor_helper< typename reduce_unit > >::type, typename reduce_unit > >::type > { //typedef typename detail::conversion_impl::template apply< // typename L1::type, // homogeneous_system //> impl; //typedef eval_scale_list scale; //typedef typename multiply_typeof_helper::type type; //static type value() //{ // return(impl::value() * scale::value()); //} }; // There is no simple algorithm for doing this conversion // other than just defining it as the reverse of the // heterogeneous->homogeneous case template struct conversion_factor_helper >, unit > > : conversion_factor_helper< typename reduce_unit > >::type, typename reduce_unit > >::type > { //typedef typename detail::conversion_impl::template apply< // typename L2::type, // homogeneous_system //> impl; //typedef eval_scale_list scale; //typedef typename multiply_typeof_helper::type type; //static type value() //{ // one numerator; // return(numerator / (impl::value() * scale::value())); //} }; /// Requires that all possible conversions /// between base units are defined. template struct conversion_factor_helper >, unit > > { /// INTERNAL ONLY typedef typename detail::extract_base_units::template apply< typename S1::type, dimensionless_type >::type from_base_units; /// INTERNAL ONLY typedef typename detail::extract_base_units::template apply< typename S2::type, from_base_units >::type all_base_units; /// INTERNAL ONLY typedef typename detail::make_homogeneous_system::type system; typedef typename detail::conversion_impl::template apply< typename S1::type, system > conversion1; typedef typename detail::conversion_impl::template apply< typename S2::type, system > conversion2; typedef eval_scale_list::type> scale; typedef typename multiply_typeof_helper< typename conversion1::type, typename divide_typeof_helper::type >::type type; static type value() { return(conversion1::value() * (scale::value() / conversion2::value())); } }; } // namespace detail } // namespace units } // namespace boost #endif // BOOST_UNITS_CONVERSION_IMPL_HPP