conversion_impl.hpp

来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 532 行 · 第 1/2 页

HPP
532
字号
// 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 <boost/mpl/bool.hpp>#include <boost/mpl/and.hpp>#include <boost/mpl/divides.hpp>#include <boost/preprocessor/seq/enum.hpp>#include <boost/type_traits/is_same.hpp>#include <boost/units/heterogeneous_system.hpp>#include <boost/units/homogeneous_system.hpp>#include <boost/units/reduce_unit.hpp>#include <boost/units/static_rational.hpp>#include <boost/units/units_fwd.hpp>#include <boost/units/detail/dimension_list.hpp>#include <boost/units/detail/heterogeneous_conversion.hpp>#include <boost/units/detail/one.hpp>#include <boost/units/detail/static_rational_power.hpp>#include <boost/units/detail/unscale.hpp>#include <boost/units/units_fwd.hpp>namespace boost {namespace units {namespace detail {template<class Source, class Dest>struct conversion_factor_helper;template<class Source, class Dest>struct call_base_unit_converter;}/// INTERNAL ONLYstruct undefined_base_unit_converter_base {    static const bool is_defined = false;};/// INTERNAL ONLYstruct no_default_conversion {    static const bool is_defined = false;};/// INTERNAL ONLYtemplate<class BaseUnit>struct unscaled_get_default_conversion : no_default_conversion { };/// INTERNAL ONLYtemplate<bool is_defined>struct unscaled_get_default_conversion_impl;/// INTERNAL ONLYtemplate<>struct unscaled_get_default_conversion_impl<true>{    template<class T>    struct apply    {        typedef typename unscaled_get_default_conversion<typename unscale<T>::type>::type type;    };};/// INTERNAL ONLYtemplate<>struct unscaled_get_default_conversion_impl<false>{    template<class T>    struct apply    {        typedef typename T::unit_type type;    };};/// INTERNAL ONLYtemplate<class BaseUnit>struct get_default_conversion{    typedef typename unscaled_get_default_conversion_impl<        unscaled_get_default_conversion<typename unscale<BaseUnit>::type>::is_defined    >::template apply<BaseUnit>::type type;};/// INTERNAL ONLYtemplate<class Source, class Destination>struct select_base_unit_converter{    typedef Source source_type;    typedef Destination destination_type;};/// INTERNAL ONLYtemplate<class Source, class Dest>struct base_unit_converter_base : undefined_base_unit_converter_base {};/// INTERNAL ONLYtemplate<class Source>struct base_unit_converter_base<Source, BOOST_UNITS_MAKE_HETEROGENEOUS_UNIT(Source, typename Source::dimension_type)>{    static const bool is_defined = true;    typedef one type;    static type value() {        return(one());    }};/// INTERNAL ONLYtemplate<class Source, class Dest>struct base_unit_converter : base_unit_converter_base<Source, Dest> { };namespace detail {template<class Source, class Dest>struct do_call_base_unit_converter {    typedef select_base_unit_converter<typename unscale<Source>::type, typename unscale<Dest>::type> selector;    typedef typename selector::source_type source_type;    typedef typename selector::destination_type destination_type;    typedef base_unit_converter<source_type, destination_type> converter;    typedef typename mpl::divides<typename get_scale_list<Source>::type, typename get_scale_list<source_type>::type>::type source_factor;    typedef typename mpl::divides<typename get_scale_list<Dest>::type, typename get_scale_list<destination_type>::type>::type destination_factor;    typedef typename mpl::divides<source_factor, destination_factor>::type factor;    typedef eval_scale_list<factor> eval_factor;    typedef typename multiply_typeof_helper<typename converter::type, typename eval_factor::type>::type type;    static type value()    {        return(converter::value() * eval_factor::value());    }};template<bool forward_is_defined, bool reverse_is_defined>struct call_base_unit_converter_base_unit_impl;template<>struct call_base_unit_converter_base_unit_impl<true, true>{    template<class Source, class Dest>    struct apply        : do_call_base_unit_converter<Source, typename Dest::unit_type>     {    };};template<>struct call_base_unit_converter_base_unit_impl<true, false>{    template<class Source, class Dest>    struct apply        : do_call_base_unit_converter<Source, typename Dest::unit_type>     {    };};template<>struct call_base_unit_converter_base_unit_impl<false, true>{    template<class Source, class Dest>    struct apply    {        typedef do_call_base_unit_converter<Dest, typename Source::unit_type> converter;        typedef typename divide_typeof_helper<one, typename converter::type>::type type;        static type value() {            return(one() / converter::value());        }    };};template<>struct call_base_unit_converter_base_unit_impl<false, false>{    template<class Source, class Dest>    struct apply    {        typedef typename reduce_unit<typename get_default_conversion<Source>::type>::type new_source;        typedef typename reduce_unit<typename get_default_conversion<Dest>::type>::type new_dest;        typedef call_base_unit_converter<Source, new_source> start;        typedef detail::conversion_factor_helper<            new_source,            new_dest        > conversion;        typedef call_base_unit_converter<Dest, new_dest> 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<int N>struct get_default_conversion_impl{    template<class Begin>    struct apply    {        typedef typename Begin::item source_pair;        typedef typename source_pair::value_type exponent;        typedef typename source_pair::tag_type source;        typedef typename get_default_conversion<source>::type new_source;        typedef typename get_default_conversion_impl<N-1>::template apply<typename Begin::next> next_iteration;        typedef typename multiply_typeof_helper<typename power_typeof_helper<new_source, exponent>::type, typename next_iteration::unit_type>::type unit_type;        typedef call_base_unit_converter<source, new_source> conversion;        typedef typename multiply_typeof_helper<typename conversion::type, typename next_iteration::type>::type type;        static type value() {            return(static_rational_power<exponent>(conversion::value()) * next_iteration::value());        }    };};template<>struct get_default_conversion_impl<0>{    template<class Begin>    struct apply    {        typedef unit<dimensionless_type, heterogeneous_system<heterogeneous_system_impl<dimensionless_type, dimensionless_type, no_scale> > > unit_type;        typedef one type;        static type value() {            return(type());        }    };};template<bool is_defined>struct call_base_unit_converter_impl;template<>struct call_base_unit_converter_impl<true>{    template<class Source, class Dest>    struct apply        : do_call_base_unit_converter<Source, Dest>     {    };};template<>struct call_base_unit_converter_impl<false>{    template<class Source, class Dest>    struct apply {        typedef typename reduce_unit<typename get_default_conversion<Source>::type>::type new_source;        typedef typename Dest::system_type::type system_list;        typedef typename get_default_conversion_impl<system_list::size::value>::template apply<system_list> impl;        typedef typename impl::unit_type new_dest;        typedef call_base_unit_converter<Source, new_source> start;        typedef conversion_factor_helper<new_source, new_dest> conversion;        typedef typename divide_typeof_helper<            typename multiply_typeof_helper<

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?