// 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_HETEROGENEOUS_SYSTEM_HPP #define BOOST_UNITS_HETEROGENEOUS_SYSTEM_HPP /// \file /// \brief A heterogeneous system is a sorted list of base unit/exponent pairs. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace units { namespace detail { // A normal system is a sorted list of base units. // A heterogeneous system is a sorted list of base unit/exponent pairs. // As long as we don't need to convert heterogeneous systems // directly everything is cool. template struct is_zero : mpl::false_ {}; template<> struct is_zero > : mpl::true_ {}; } // namespace detail /// INTERNAL ONLY template struct heterogeneous_system_impl { typedef L type; typedef Dimensions dimensions; typedef Scale scale; }; /// INTERNAL ONLY typedef dimensionless_type no_scale; /// A system that can represent any possible combination /// of units at the expense of not preserving information /// about how it was created. Do not create specializations /// of this template directly. Instead use @c reduce_unit and /// @c base_unit<...>::unit_type. template struct heterogeneous_system : T {}; /// INTERNAL ONLY struct heterogeneous_system_dim_tag {}; /// INTERNAL ONLY template struct heterogeneous_system_dim { typedef heterogeneous_system_dim_tag tag; typedef heterogeneous_system_dim type; typedef Unit tag_type; typedef Exponent value_type; }; /// INTERNAL ONLY #define BOOST_UNITS_MAKE_HETEROGENEOUS_UNIT(BaseUnit, Dimensions) \ boost::units::unit< \ Dimensions, \ boost::units::heterogeneous_system< \ boost::units::heterogeneous_system_impl< \ boost::units::list< \ boost::units::heterogeneous_system_dim< \ BaseUnit, \ boost::units::static_rational<1> \ >, \ boost::units::dimensionless_type \ >, \ Dimensions, \ boost::units::no_scale \ > \ > \ > } // namespace units } // namespace boost #if BOOST_UNITS_HAS_BOOST_TYPEOF #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP() BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::heterogeneous_system_impl, (class)(class)(class)) BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::heterogeneous_system, (class)) BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::heterogeneous_system_dim, (class)(class)) #endif namespace boost { namespace mpl { /// INTERNAL ONLY template<> struct less_impl { template struct apply : mpl::less {}; }; } namespace units { namespace detail { template struct is_empty_dim > : detail::is_zero {}; } // namespace detail } // namespace units namespace mpl { /// INTERNAL ONLY template<> struct plus_impl { template struct apply { typedef boost::units::heterogeneous_system_dim< typename T0::tag_type, typename mpl::plus::type > type; }; }; /// INTERNAL ONLY template<> struct times_impl { template struct apply { typedef boost::units::heterogeneous_system_dim< typename T0::tag_type, typename mpl::times::type > type; }; }; /// INTERNAL ONLY template<> struct divides_impl { template struct apply { typedef boost::units::heterogeneous_system_dim< typename T0::tag_type, typename mpl::divides::type > type; }; }; /// INTERNAL ONLY template<> struct negate_impl { template struct apply { typedef boost::units::heterogeneous_system_dim::type> type; }; }; } // namespace mpl namespace units { namespace detail { template struct make_heterogeneous_system_impl { template struct apply { typedef typename push_front_if::value)>::template apply< typename make_heterogeneous_system_impl::template apply< typename UnitsBegin::next, typename ExponentsBegin::next >::type, heterogeneous_system_dim >::type type; }; }; template<> struct make_heterogeneous_system_impl<0> { template struct apply { typedef dimensionless_type type; }; }; template struct make_heterogeneous_system { typedef typename calculate_base_unit_exponents::type exponents; BOOST_MPL_ASSERT_MSG((!boost::is_same::value), the_specified_dimension_is_not_representible_in_the_given_system, (types)); typedef typename make_heterogeneous_system_impl::template apply< typename System::type, exponents >::type unit_list; typedef heterogeneous_system > type; }; template struct make_heterogeneous_system > { typedef heterogeneous_system type; }; template struct multiply_systems { typedef heterogeneous_system< heterogeneous_system_impl< typename mpl::times::type, typename mpl::times::type, typename mpl::times::type > > type; }; template struct divide_systems { typedef heterogeneous_system< heterogeneous_system_impl< typename mpl::divides::type, typename mpl::divides::type, typename mpl::divides::type > > type; }; } // namespace detail /// INTERNAL ONLY template struct static_power, static_rational > { typedef heterogeneous_system< heterogeneous_system_impl< typename static_power >::type, typename static_power >::type, typename static_power >::type > > type; }; /// INTERNAL ONLY template struct static_root, static_rational > { typedef heterogeneous_system< heterogeneous_system_impl< typename static_root >::type, typename static_root >::type, typename static_root >::type > > type; }; namespace detail { template struct unscale_heterogeneous_system_impl { template struct apply { typedef typename push_front_or_add< typename unscale_heterogeneous_system_impl::template apply< typename Begin::next >::type, typename unscale::type >::type type; }; }; template<> struct unscale_heterogeneous_system_impl<0> { template struct apply { typedef dimensionless_type type; }; }; } // namespace detail /// Unscale all the base units. e.g /// km s -> m s /// cm km -> m^2 /// INTERNAL ONLY template struct unscale > { typedef heterogeneous_system< heterogeneous_system_impl< typename detail::unscale_heterogeneous_system_impl< T::type::size::value >::template apply< typename T::type >::type, typename T::dimensions, no_scale > > type; }; /// INTERNAL ONLY template struct unscale > { typedef heterogeneous_system_dim::type, Exponent> type; }; namespace detail { template struct get_scale_list_of_heterogeneous_system_impl { template struct apply { typedef typename mpl::times< typename get_scale_list_of_heterogeneous_system_impl::template apply< typename Begin::next >::type, typename get_scale_list::type >::type type; }; }; template<> struct get_scale_list_of_heterogeneous_system_impl<0> { template struct apply { typedef dimensionless_type type; }; }; } // namespace detail /// INTERNAL ONLY template struct get_scale_list > { typedef typename mpl::times< typename detail::get_scale_list_of_heterogeneous_system_impl< T::type::size::value >::template apply::type, typename T::scale >::type type; }; /// INTERNAL ONLY template struct get_scale_list > { typedef typename static_power::type, Exponent>::type type; }; namespace detail { template struct check_system : mpl::false_ {}; template struct check_system >, Dimension> : mpl::true_ {}; } // namespace detail } // namespace units } // namespace boost #endif