12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280 |
- // 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_QUANTITY_HPP
- #define BOOST_UNITS_QUANTITY_HPP
- #include <algorithm>
- #include <boost/config.hpp>
- #include <boost/static_assert.hpp>
- #include <boost/mpl/bool.hpp>
- #include <boost/mpl/and.hpp>
- #include <boost/mpl/not.hpp>
- #include <boost/mpl/or.hpp>
- #include <boost/mpl/assert.hpp>
- #include <boost/utility/enable_if.hpp>
- #include <boost/type_traits/is_arithmetic.hpp>
- #include <boost/type_traits/is_convertible.hpp>
- #include <boost/type_traits/is_integral.hpp>
- #include <boost/type_traits/is_same.hpp>
- #include <boost/units/conversion.hpp>
- #include <boost/units/dimensionless_type.hpp>
- #include <boost/units/homogeneous_system.hpp>
- #include <boost/units/operators.hpp>
- #include <boost/units/static_rational.hpp>
- #include <boost/units/units_fwd.hpp>
- #include <boost/units/detail/dimensionless_unit.hpp>
- namespace boost {
- namespace units {
- namespace detail {
- template<class T, class Enable = void>
- struct is_base_unit : mpl::false_ {};
- template<class T>
- struct is_base_unit<T, typename T::boost_units_is_base_unit_type> : mpl::true_ {};
- template<class Source, class Destination>
- struct is_narrowing_conversion_impl : mpl::bool_<(sizeof(Source) > sizeof(Destination))> {};
- template<class Source, class Destination>
- struct is_non_narrowing_conversion :
- mpl::and_<
- boost::is_convertible<Source, Destination>,
- mpl::not_<
- mpl::and_<
- boost::is_arithmetic<Source>,
- boost::is_arithmetic<Destination>,
- mpl::or_<
- mpl::and_<
- is_integral<Destination>,
- mpl::not_<is_integral<Source> >
- >,
- is_narrowing_conversion_impl<Source, Destination>
- >
- >
- >
- >
- {};
- template<>
- struct is_non_narrowing_conversion<long double, double> : mpl::false_ {};
- // msvc 7.1 needs extra disambiguation
- template<class T, class U>
- struct disable_if_is_same
- {
- typedef void type;
- };
- template<class T>
- struct disable_if_is_same<T, T> {};
- }
-
- /// class declaration
- template<class Unit,class Y>
- class quantity
- {
- // base units are not the same as units.
- BOOST_MPL_ASSERT_NOT((detail::is_base_unit<Unit>));
- enum { force_instantiation_of_unit = sizeof(Unit) };
- typedef void (quantity::*unspecified_null_pointer_constant_type)(int*******);
- public:
- typedef quantity<Unit,Y> this_type;
-
- typedef Y value_type;
- typedef Unit unit_type;
-
- BOOST_CONSTEXPR quantity() : val_()
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- }
- BOOST_CONSTEXPR quantity(unspecified_null_pointer_constant_type) : val_()
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- }
-
- BOOST_CONSTEXPR quantity(const this_type& source) : val_(source.val_)
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- }
-
- // Need to make sure that the destructor of
- // Unit which contains the checking is instantiated,
- // on sun.
- #ifdef __SUNPRO_CC
- ~quantity() {
- unit_type force_unit_instantiation;
- }
- #endif
-
- //~quantity() { }
-
- BOOST_CXX14_CONSTEXPR this_type& operator=(const this_type& source)
- {
- val_ = source.val_;
-
- return *this;
- }
- #ifndef BOOST_NO_SFINAE
- /// implicit conversion between value types is allowed if allowed for value types themselves
- template<class YY>
- BOOST_CONSTEXPR quantity(const quantity<Unit,YY>& source,
- typename boost::enable_if<detail::is_non_narrowing_conversion<YY, Y> >::type* = 0) :
- val_(source.value())
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- }
- /// implicit conversion between value types is not allowed if not allowed for value types themselves
- template<class YY>
- explicit BOOST_CONSTEXPR quantity(const quantity<Unit,YY>& source,
- typename boost::disable_if<detail::is_non_narrowing_conversion<YY, Y> >::type* = 0) :
- val_(static_cast<Y>(source.value()))
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- }
- #else
- /// implicit conversion between value types is allowed if allowed for value types themselves
- template<class YY>
- BOOST_CONSTEXPR quantity(const quantity<Unit,YY>& source) :
- val_(source.value())
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- BOOST_STATIC_ASSERT((boost::is_convertible<YY, Y>::value == true));
- }
- #endif
-
- /// implicit assignment between value types is allowed if allowed for value types themselves
- template<class YY>
- BOOST_CXX14_CONSTEXPR this_type& operator=(const quantity<Unit,YY>& source)
- {
- BOOST_STATIC_ASSERT((boost::is_convertible<YY, Y>::value == true));
- *this = this_type(source);
-
- return *this;
- }
- #ifndef BOOST_NO_SFINAE
- /// explicit conversion between different unit systems is allowed if implicit conversion is disallowed
- template<class Unit2,class YY>
- explicit
- BOOST_CONSTEXPR quantity(const quantity<Unit2,YY>& source,
- typename boost::disable_if<
- mpl::and_<
- //is_implicitly_convertible should be undefined when the
- //units are not convertible at all
- typename is_implicitly_convertible<Unit2,Unit>::type,
- detail::is_non_narrowing_conversion<YY, Y>
- >,
- typename detail::disable_if_is_same<Unit, Unit2>::type
- >::type* = 0)
- : val_(conversion_helper<quantity<Unit2,YY>,this_type>::convert(source).value())
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- BOOST_STATIC_ASSERT((boost::is_convertible<YY,Y>::value == true));
- }
- /// implicit conversion between different unit systems is allowed if each fundamental dimension is implicitly convertible
- template<class Unit2,class YY>
- BOOST_CONSTEXPR quantity(const quantity<Unit2,YY>& source,
- typename boost::enable_if<
- mpl::and_<
- typename is_implicitly_convertible<Unit2,Unit>::type,
- detail::is_non_narrowing_conversion<YY, Y>
- >,
- typename detail::disable_if_is_same<Unit, Unit2>::type
- >::type* = 0)
- : val_(conversion_helper<quantity<Unit2,YY>,this_type>::convert(source).value())
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- BOOST_STATIC_ASSERT((boost::is_convertible<YY,Y>::value == true));
- }
- #else
- /// without SFINAE we can't distinguish between explicit and implicit conversions so
- /// the conversion is always explicit
- template<class Unit2,class YY>
- explicit BOOST_CONSTEXPR quantity(const quantity<Unit2,YY>& source)
- : val_(conversion_helper<quantity<Unit2,YY>,this_type>::convert(source).value())
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- BOOST_STATIC_ASSERT((boost::is_convertible<YY,Y>::value == true));
- }
- #endif
-
- /// implicit assignment between different unit systems is allowed if each fundamental dimension is implicitly convertible
- template<class Unit2,class YY>
- BOOST_CXX14_CONSTEXPR this_type& operator=(const quantity<Unit2,YY>& source)
- {
-
- BOOST_STATIC_ASSERT((is_implicitly_convertible<Unit2,unit_type>::value == true));
- BOOST_STATIC_ASSERT((boost::is_convertible<YY,Y>::value == true));
-
- *this = this_type(source);
-
- return *this;
- }
- BOOST_CONSTEXPR const value_type& value() const { return val_; } ///< constant accessor to value
-
- ///< can add a quantity of the same type if add_typeof_helper<value_type,value_type>::type is convertible to value_type
- template<class Unit2, class YY>
- BOOST_CXX14_CONSTEXPR this_type& operator+=(const quantity<Unit2, YY>& source)
- {
- BOOST_STATIC_ASSERT((boost::is_same<typename add_typeof_helper<Unit, Unit2>::type, Unit>::value));
- val_ += source.value();
- return *this;
- }
- ///< can subtract a quantity of the same type if subtract_typeof_helper<value_type,value_type>::type is convertible to value_type
- template<class Unit2, class YY>
- BOOST_CXX14_CONSTEXPR this_type& operator-=(const quantity<Unit2, YY>& source)
- {
- BOOST_STATIC_ASSERT((boost::is_same<typename subtract_typeof_helper<Unit, Unit2>::type, Unit>::value));
- val_ -= source.value();
- return *this;
- }
- template<class Unit2, class YY>
- BOOST_CXX14_CONSTEXPR this_type& operator*=(const quantity<Unit2, YY>& source)
- {
- BOOST_STATIC_ASSERT((boost::is_same<typename multiply_typeof_helper<Unit, Unit2>::type, Unit>::value));
- val_ *= source.value();
- return *this;
- }
-
- template<class Unit2, class YY>
- BOOST_CXX14_CONSTEXPR this_type& operator/=(const quantity<Unit2, YY>& source)
- {
- BOOST_STATIC_ASSERT((boost::is_same<typename divide_typeof_helper<Unit, Unit2>::type, Unit>::value));
- val_ /= source.value();
- return *this;
- }
- ///< can multiply a quantity by a scalar value_type if multiply_typeof_helper<value_type,value_type>::type is convertible to value_type
- BOOST_CXX14_CONSTEXPR this_type& operator*=(const value_type& source) { val_ *= source; return *this; }
- ///< can divide a quantity by a scalar value_type if divide_typeof_helper<value_type,value_type>::type is convertible to value_type
- BOOST_CXX14_CONSTEXPR this_type& operator/=(const value_type& source) { val_ /= source; return *this; }
-
- /// Construct quantity directly from @c value_type (potentially dangerous).
- static BOOST_CONSTEXPR this_type from_value(const value_type& val) { return this_type(val, 0); }
- protected:
- explicit BOOST_CONSTEXPR quantity(const value_type& val, int) : val_(val) { }
-
- private:
- value_type val_;
- };
- /// Specialization for dimensionless quantities. Implicit conversions between
- /// unit systems are allowed because all dimensionless quantities are equivalent.
- /// Implicit construction and assignment from and conversion to @c value_type is
- /// also allowed.
- template<class System,class Y>
- class quantity<BOOST_UNITS_DIMENSIONLESS_UNIT(System),Y>
- {
- public:
- typedef quantity<unit<dimensionless_type,System>,Y> this_type;
-
- typedef Y value_type;
- typedef System system_type;
- typedef dimensionless_type dimension_type;
- typedef unit<dimension_type,system_type> unit_type;
-
- BOOST_CONSTEXPR quantity() : val_()
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- }
-
- /// construction from raw @c value_type is allowed
- BOOST_CONSTEXPR quantity(value_type val) : val_(val)
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- }
-
- BOOST_CONSTEXPR quantity(const this_type& source) : val_(source.val_)
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- }
-
- //~quantity() { }
-
- BOOST_CXX14_CONSTEXPR this_type& operator=(const this_type& source)
- {
- val_ = source.val_;
-
- return *this;
- }
-
- #ifndef BOOST_NO_SFINAE
- /// implicit conversion between value types is allowed if allowed for value types themselves
- template<class YY>
- BOOST_CONSTEXPR quantity(const quantity<unit<dimension_type,system_type>,YY>& source,
- typename boost::enable_if<detail::is_non_narrowing_conversion<YY, Y> >::type* = 0) :
- val_(source.value())
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- }
- /// implicit conversion between value types is not allowed if not allowed for value types themselves
- template<class YY>
- explicit BOOST_CONSTEXPR quantity(const quantity<unit<dimension_type,system_type>,YY>& source,
- typename boost::disable_if<detail::is_non_narrowing_conversion<YY, Y> >::type* = 0) :
- val_(static_cast<Y>(source.value()))
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- }
- #else
- /// implicit conversion between value types is allowed if allowed for value types themselves
- template<class YY>
- BOOST_CONSTEXPR quantity(const quantity<unit<dimension_type,system_type>,YY>& source) :
- val_(source.value())
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- BOOST_STATIC_ASSERT((boost::is_convertible<YY, Y>::value == true));
- }
- #endif
-
- /// implicit assignment between value types is allowed if allowed for value types themselves
- template<class YY>
- BOOST_CXX14_CONSTEXPR this_type& operator=(const quantity<unit<dimension_type,system_type>,YY>& source)
- {
- BOOST_STATIC_ASSERT((boost::is_convertible<YY,Y>::value == true));
- *this = this_type(source);
-
- return *this;
- }
- #if 1
- /// implicit conversion between different unit systems is allowed
- template<class System2, class Y2>
- BOOST_CONSTEXPR quantity(const quantity<unit<dimensionless_type, System2>,Y2>& source,
- #ifdef __SUNPRO_CC
- typename boost::enable_if<
- boost::mpl::and_<
- detail::is_non_narrowing_conversion<Y2, Y>,
- detail::is_dimensionless_system<System2>
- >
- >::type* = 0
- #else
- typename boost::enable_if<detail::is_non_narrowing_conversion<Y2, Y> >::type* = 0,
- typename detail::disable_if_is_same<System, System2>::type* = 0,
- typename boost::enable_if<detail::is_dimensionless_system<System2> >::type* = 0
- #endif
- ) :
- val_(source.value())
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- }
- /// implicit conversion between different unit systems is allowed
- template<class System2, class Y2>
- explicit BOOST_CONSTEXPR quantity(const quantity<unit<dimensionless_type, System2>,Y2>& source,
- #ifdef __SUNPRO_CC
- typename boost::enable_if<
- boost::mpl::and_<
- boost::mpl::not_<detail::is_non_narrowing_conversion<Y2, Y> >,
- detail::is_dimensionless_system<System2>
- >
- >::type* = 0
- #else
- typename boost::disable_if<detail::is_non_narrowing_conversion<Y2, Y> >::type* = 0,
- typename detail::disable_if_is_same<System, System2>::type* = 0,
- typename boost::enable_if<detail::is_dimensionless_system<System2> >::type* = 0
- #endif
- ) :
- val_(static_cast<Y>(source.value()))
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- }
- #else
- /// implicit conversion between different unit systems is allowed
- template<class System2, class Y2>
- BOOST_CONSTEXPR quantity(const quantity<unit<dimensionless_type,homogeneous_system<System2> >,Y2>& source) :
- val_(source.value())
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- BOOST_STATIC_ASSERT((boost::is_convertible<Y2, Y>::value == true));
- }
- #endif
- /// conversion between different unit systems is explicit when
- /// the units are not equivalent.
- template<class System2, class Y2>
- explicit BOOST_CONSTEXPR quantity(const quantity<unit<dimensionless_type, System2>,Y2>& source,
- typename boost::disable_if<detail::is_dimensionless_system<System2> >::type* = 0) :
- val_(conversion_helper<quantity<unit<dimensionless_type, System2>,Y2>, this_type>::convert(source).value())
- {
- BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
- }
-
- #ifndef __SUNPRO_CC
- /// implicit assignment between different unit systems is allowed
- template<class System2>
- BOOST_CXX14_CONSTEXPR this_type& operator=(const quantity<BOOST_UNITS_DIMENSIONLESS_UNIT(System2),Y>& source)
- {
- *this = this_type(source);
-
- return *this;
- }
-
- #endif
-
- /// implicit conversion to @c value_type is allowed
- BOOST_CONSTEXPR operator value_type() const { return val_; }
-
- BOOST_CONSTEXPR const value_type& value() const { return val_; } ///< constant accessor to value
-
- ///< can add a quantity of the same type if add_typeof_helper<value_type,value_type>::type is convertible to value_type
- BOOST_CXX14_CONSTEXPR this_type& operator+=(const this_type& source) { val_ += source.val_; return *this; }
-
- ///< can subtract a quantity of the same type if subtract_typeof_helper<value_type,value_type>::type is convertible to value_type
- BOOST_CXX14_CONSTEXPR this_type& operator-=(const this_type& source) { val_ -= source.val_; return *this; }
-
- ///< can multiply a quantity by a scalar value_type if multiply_typeof_helper<value_type,value_type>::type is convertible to value_type
- BOOST_CXX14_CONSTEXPR this_type& operator*=(const value_type& val) { val_ *= val; return *this; }
- ///< can divide a quantity by a scalar value_type if divide_typeof_helper<value_type,value_type>::type is convertible to value_type
- BOOST_CXX14_CONSTEXPR this_type& operator/=(const value_type& val) { val_ /= val; return *this; }
- /// Construct quantity directly from @c value_type.
- static BOOST_CONSTEXPR this_type from_value(const value_type& val) { return this_type(val); }
- private:
- value_type val_;
- };
- #ifdef BOOST_MSVC
- // HACK: For some obscure reason msvc 8.0 needs these specializations
- template<class System, class T>
- class quantity<unit<int, System>, T> {};
- template<class T>
- class quantity<int, T> {};
- #endif
- } // namespace units
- } // namespace boost
- #if BOOST_UNITS_HAS_BOOST_TYPEOF
- #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
- BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::quantity, 2)
- #endif
- namespace boost {
- namespace units {
- namespace detail {
- /// helper class for quantity_cast
- template<class X,class Y> struct quantity_cast_helper;
- /// specialization for casting to the value type
- template<class Y,class X,class Unit>
- struct quantity_cast_helper<Y,quantity<Unit,X> >
- {
- typedef Y type;
-
- BOOST_CONSTEXPR type operator()(quantity<Unit,X>& source) const { return const_cast<X&>(source.value()); }
- };
- /// specialization for casting to the value type
- template<class Y,class X,class Unit>
- struct quantity_cast_helper<Y,const quantity<Unit,X> >
- {
- typedef Y type;
-
- BOOST_CONSTEXPR type operator()(const quantity<Unit,X>& source) const { return source.value(); }
- };
- } // namespace detail
- /// quantity_cast provides mutating access to underlying quantity value_type
- template<class X,class Y>
- inline
- BOOST_CONSTEXPR
- X
- quantity_cast(Y& source)
- {
- return detail::quantity_cast_helper<X,Y>()(source);
- }
- template<class X,class Y>
- inline
- BOOST_CONSTEXPR
- X
- quantity_cast(const Y& source)
- {
- return detail::quantity_cast_helper<X,const Y>()(source);
- }
- /// swap quantities
- template<class Unit,class Y>
- inline void swap(quantity<Unit,Y>& lhs, quantity<Unit,Y>& rhs)
- {
- using std::swap;
- swap(quantity_cast<Y&>(lhs),quantity_cast<Y&>(rhs));
- }
- /// specialize unary plus typeof helper
- /// INTERNAL ONLY
- template<class Unit,class Y>
- struct unary_plus_typeof_helper< quantity<Unit,Y> >
- {
- typedef typename unary_plus_typeof_helper<Y>::type value_type;
- typedef typename unary_plus_typeof_helper<Unit>::type unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// specialize unary minus typeof helper
- /// INTERNAL ONLY
- template<class Unit,class Y>
- struct unary_minus_typeof_helper< quantity<Unit,Y> >
- {
- typedef typename unary_minus_typeof_helper<Y>::type value_type;
- typedef typename unary_minus_typeof_helper<Unit>::type unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// specialize add typeof helper
- /// INTERNAL ONLY
- template<class Unit1,
- class Unit2,
- class X,
- class Y>
- struct add_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >
- {
- typedef typename add_typeof_helper<X,Y>::type value_type;
- typedef typename add_typeof_helper<Unit1,Unit2>::type unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// for sun CC we need to invoke SFINAE at
- /// the top level, otherwise it will silently
- /// return int.
- template<class Dim1, class System1,
- class Dim2, class System2,
- class X,
- class Y>
- struct add_typeof_helper< quantity<unit<Dim1, System1>,X>,quantity<unit<Dim2, System2>,Y> >
- {
- };
- template<class Dim,
- class System,
- class X,
- class Y>
- struct add_typeof_helper< quantity<unit<Dim, System>,X>,quantity<unit<Dim, System>,Y> >
- {
- typedef typename add_typeof_helper<X,Y>::type value_type;
- typedef unit<Dim, System> unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// specialize subtract typeof helper
- /// INTERNAL ONLY
- template<class Unit1,
- class Unit2,
- class X,
- class Y>
- struct subtract_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >
- {
- typedef typename subtract_typeof_helper<X,Y>::type value_type;
- typedef typename subtract_typeof_helper<Unit1,Unit2>::type unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- // Force adding different units to fail on sun.
- template<class Dim1, class System1,
- class Dim2, class System2,
- class X,
- class Y>
- struct subtract_typeof_helper< quantity<unit<Dim1, System1>,X>,quantity<unit<Dim2, System2>,Y> >
- {
- };
- template<class Dim,
- class System,
- class X,
- class Y>
- struct subtract_typeof_helper< quantity<unit<Dim, System>,X>,quantity<unit<Dim, System>,Y> >
- {
- typedef typename subtract_typeof_helper<X,Y>::type value_type;
- typedef unit<Dim, System> unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// scalar times unit typeof helper
- /// INTERNAL ONLY
- template<class System,
- class Dim,
- class X>
- struct multiply_typeof_helper< X,unit<Dim,System> >
- {
- typedef X value_type;
- typedef unit<Dim,System> unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// unit times scalar typeof helper
- /// INTERNAL ONLY
- template<class System,
- class Dim,
- class X>
- struct multiply_typeof_helper< unit<Dim,System>,X >
- {
- typedef X value_type;
- typedef unit<Dim,System> unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// scalar times quantity typeof helper
- /// INTERNAL ONLY
- template<class Unit,
- class X,
- class Y>
- struct multiply_typeof_helper< X,quantity<Unit,Y> >
- {
- typedef typename multiply_typeof_helper<X,Y>::type value_type;
- typedef Unit unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// disambiguate
- /// INTERNAL ONLY
- template<class Unit,
- class Y>
- struct multiply_typeof_helper< one,quantity<Unit,Y> >
- {
- typedef quantity<Unit,Y> type;
- };
- /// quantity times scalar typeof helper
- /// INTERNAL ONLY
- template<class Unit,
- class X,
- class Y>
- struct multiply_typeof_helper< quantity<Unit,X>,Y >
- {
- typedef typename multiply_typeof_helper<X,Y>::type value_type;
- typedef Unit unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// disambiguate
- /// INTERNAL ONLY
- template<class Unit,
- class X>
- struct multiply_typeof_helper< quantity<Unit,X>,one >
- {
- typedef quantity<Unit,X> type;
- };
- /// unit times quantity typeof helper
- /// INTERNAL ONLY
- template<class Unit,
- class System,
- class Dim,
- class X>
- struct multiply_typeof_helper< unit<Dim,System>,quantity<Unit,X> >
- {
- typedef X value_type;
- typedef typename multiply_typeof_helper< unit<Dim,System>,Unit >::type unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// quantity times unit typeof helper
- /// INTERNAL ONLY
- template<class Unit,
- class System,
- class Dim,
- class X>
- struct multiply_typeof_helper< quantity<Unit,X>,unit<Dim,System> >
- {
- typedef X value_type;
- typedef typename multiply_typeof_helper< Unit,unit<Dim,System> >::type unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// quantity times quantity typeof helper
- /// INTERNAL ONLY
- template<class Unit1,
- class Unit2,
- class X,
- class Y>
- struct multiply_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >
- {
- typedef typename multiply_typeof_helper<X,Y>::type value_type;
- typedef typename multiply_typeof_helper<Unit1,Unit2>::type unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// scalar divided by unit typeof helper
- /// INTERNAL ONLY
- template<class System,
- class Dim,
- class X>
- struct divide_typeof_helper< X,unit<Dim,System> >
- {
- typedef X value_type;
- typedef typename power_typeof_helper< unit<Dim,System>,static_rational<-1> >::type unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// unit divided by scalar typeof helper
- /// INTERNAL ONLY
- template<class System,
- class Dim,
- class X>
- struct divide_typeof_helper< unit<Dim,System>,X >
- {
- typedef typename divide_typeof_helper<X,X>::type value_type;
- typedef unit<Dim,System> unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// scalar divided by quantity typeof helper
- /// INTERNAL ONLY
- template<class Unit,
- class X,
- class Y>
- struct divide_typeof_helper< X,quantity<Unit,Y> >
- {
- typedef typename divide_typeof_helper<X,Y>::type value_type;
- typedef typename power_typeof_helper< Unit,static_rational<-1> >::type unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// disambiguate
- /// INTERNAL ONLY
- template<class Unit,
- class Y>
- struct divide_typeof_helper< one,quantity<Unit,Y> >
- {
- typedef quantity<Unit,Y> type;
- };
- /// quantity divided by scalar typeof helper
- /// INTERNAL ONLY
- template<class Unit,
- class X,
- class Y>
- struct divide_typeof_helper< quantity<Unit,X>,Y >
- {
- typedef typename divide_typeof_helper<X,Y>::type value_type;
- typedef Unit unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// disambiguate
- /// INTERNAL ONLY
- template<class Unit,
- class X>
- struct divide_typeof_helper< quantity<Unit,X>,one >
- {
- typedef quantity<Unit,X> type;
- };
- /// unit divided by quantity typeof helper
- /// INTERNAL ONLY
- template<class Unit,
- class System,
- class Dim,
- class X>
- struct divide_typeof_helper< unit<Dim,System>,quantity<Unit,X> >
- {
- typedef typename divide_typeof_helper<X,X>::type value_type;
- typedef typename divide_typeof_helper< unit<Dim,System>,Unit >::type unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// quantity divided by unit typeof helper
- /// INTERNAL ONLY
- template<class Unit,
- class System,
- class Dim,
- class X>
- struct divide_typeof_helper< quantity<Unit,X>,unit<Dim,System> >
- {
- typedef X value_type;
- typedef typename divide_typeof_helper< Unit,unit<Dim,System> >::type unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// quantity divided by quantity typeof helper
- /// INTERNAL ONLY
- template<class Unit1,
- class Unit2,
- class X,
- class Y>
- struct divide_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >
- {
- typedef typename divide_typeof_helper<X,Y>::type value_type;
- typedef typename divide_typeof_helper<Unit1,Unit2>::type unit_type;
- typedef quantity<unit_type,value_type> type;
- };
- /// specialize power typeof helper
- /// INTERNAL ONLY
- template<class Unit,long N,long D,class Y>
- struct power_typeof_helper< quantity<Unit,Y>,static_rational<N,D> >
- {
- typedef typename power_typeof_helper<Y,static_rational<N,D> >::type value_type;
- typedef typename power_typeof_helper<Unit,static_rational<N,D> >::type unit_type;
- typedef quantity<unit_type,value_type> type;
-
- static BOOST_CONSTEXPR type value(const quantity<Unit,Y>& x)
- {
- return type::from_value(power_typeof_helper<Y,static_rational<N,D> >::value(x.value()));
- }
- };
- /// specialize root typeof helper
- /// INTERNAL ONLY
- template<class Unit,long N,long D,class Y>
- struct root_typeof_helper< quantity<Unit,Y>,static_rational<N,D> >
- {
- typedef typename root_typeof_helper<Y,static_rational<N,D> >::type value_type;
- typedef typename root_typeof_helper<Unit,static_rational<N,D> >::type unit_type;
- typedef quantity<unit_type,value_type> type;
-
- static BOOST_CONSTEXPR type value(const quantity<Unit,Y>& x)
- {
- return type::from_value(root_typeof_helper<Y,static_rational<N,D> >::value(x.value()));
- }
- };
- /// runtime unit times scalar
- /// INTERNAL ONLY
- template<class System,
- class Dim,
- class Y>
- inline
- BOOST_CONSTEXPR
- typename multiply_typeof_helper< unit<Dim,System>,Y >::type
- operator*(const unit<Dim,System>&,const Y& rhs)
- {
- typedef typename multiply_typeof_helper< unit<Dim,System>,Y >::type type;
-
- return type::from_value(rhs);
- }
- /// runtime unit divided by scalar
- template<class System,
- class Dim,
- class Y>
- inline
- BOOST_CONSTEXPR
- typename divide_typeof_helper< unit<Dim,System>,Y >::type
- operator/(const unit<Dim,System>&,const Y& rhs)
- {
- typedef typename divide_typeof_helper<unit<Dim,System>,Y>::type type;
-
- return type::from_value(Y(1)/rhs);
- }
- /// runtime scalar times unit
- template<class System,
- class Dim,
- class Y>
- inline
- BOOST_CONSTEXPR
- typename multiply_typeof_helper< Y,unit<Dim,System> >::type
- operator*(const Y& lhs,const unit<Dim,System>&)
- {
- typedef typename multiply_typeof_helper< Y,unit<Dim,System> >::type type;
-
- return type::from_value(lhs);
- }
- /// runtime scalar divided by unit
- template<class System,
- class Dim,
- class Y>
- inline
- BOOST_CONSTEXPR
- typename divide_typeof_helper< Y,unit<Dim,System> >::type
- operator/(const Y& lhs,const unit<Dim,System>&)
- {
- typedef typename divide_typeof_helper< Y,unit<Dim,System> >::type type;
-
- return type::from_value(lhs);
- }
- ///// runtime quantity times scalar
- //template<class Unit,
- // class X,
- // class Y>
- //inline
- //BOOST_CONSTEXPR
- //typename multiply_typeof_helper< quantity<Unit,X>,Y >::type
- //operator*(const quantity<Unit,X>& lhs,const Y& rhs)
- //{
- // typedef typename multiply_typeof_helper< quantity<Unit,X>,Y >::type type;
- //
- // return type::from_value(lhs.value()*rhs);
- //}
- //
- ///// runtime scalar times quantity
- //template<class Unit,
- // class X,
- // class Y>
- //inline
- //BOOST_CONSTEXPR
- //typename multiply_typeof_helper< X,quantity<Unit,Y> >::type
- //operator*(const X& lhs,const quantity<Unit,Y>& rhs)
- //{
- // typedef typename multiply_typeof_helper< X,quantity<Unit,Y> >::type type;
- //
- // return type::from_value(lhs*rhs.value());
- //}
- /// runtime quantity times scalar
- template<class Unit,
- class X>
- inline
- BOOST_CONSTEXPR
- typename multiply_typeof_helper< quantity<Unit,X>,X >::type
- operator*(const quantity<Unit,X>& lhs,const X& rhs)
- {
- typedef typename multiply_typeof_helper< quantity<Unit,X>,X >::type type;
-
- return type::from_value(lhs.value()*rhs);
- }
- /// runtime scalar times quantity
- template<class Unit,
- class X>
- inline
- BOOST_CONSTEXPR
- typename multiply_typeof_helper< X,quantity<Unit,X> >::type
- operator*(const X& lhs,const quantity<Unit,X>& rhs)
- {
- typedef typename multiply_typeof_helper< X,quantity<Unit,X> >::type type;
-
- return type::from_value(lhs*rhs.value());
- }
- ///// runtime quantity divided by scalar
- //template<class Unit,
- // class X,
- // class Y>
- //inline
- //BOOST_CONSTEXPR
- //typename divide_typeof_helper< quantity<Unit,X>,Y >::type
- //operator/(const quantity<Unit,X>& lhs,const Y& rhs)
- //{
- // typedef typename divide_typeof_helper< quantity<Unit,X>,Y >::type type;
- //
- // return type::from_value(lhs.value()/rhs);
- //}
- //
- ///// runtime scalar divided by quantity
- //template<class Unit,
- // class X,
- // class Y>
- //inline
- //BOOST_CONSTEXPR
- //typename divide_typeof_helper< X,quantity<Unit,Y> >::type
- //operator/(const X& lhs,const quantity<Unit,Y>& rhs)
- //{
- // typedef typename divide_typeof_helper< X,quantity<Unit,Y> >::type type;
- //
- // return type::from_value(lhs/rhs.value());
- //}
- /// runtime quantity divided by scalar
- template<class Unit,
- class X>
- inline
- BOOST_CONSTEXPR
- typename divide_typeof_helper< quantity<Unit,X>,X >::type
- operator/(const quantity<Unit,X>& lhs,const X& rhs)
- {
- typedef typename divide_typeof_helper< quantity<Unit,X>,X >::type type;
-
- return type::from_value(lhs.value()/rhs);
- }
- /// runtime scalar divided by quantity
- template<class Unit,
- class X>
- inline
- BOOST_CONSTEXPR
- typename divide_typeof_helper< X,quantity<Unit,X> >::type
- operator/(const X& lhs,const quantity<Unit,X>& rhs)
- {
- typedef typename divide_typeof_helper< X,quantity<Unit,X> >::type type;
-
- return type::from_value(lhs/rhs.value());
- }
- /// runtime unit times quantity
- template<class System1,
- class Dim1,
- class Unit2,
- class Y>
- inline
- BOOST_CONSTEXPR
- typename multiply_typeof_helper< unit<Dim1,System1>,quantity<Unit2,Y> >::type
- operator*(const unit<Dim1,System1>&,const quantity<Unit2,Y>& rhs)
- {
- typedef typename multiply_typeof_helper< unit<Dim1,System1>,quantity<Unit2,Y> >::type type;
-
- return type::from_value(rhs.value());
- }
- /// runtime unit divided by quantity
- template<class System1,
- class Dim1,
- class Unit2,
- class Y>
- inline
- BOOST_CONSTEXPR
- typename divide_typeof_helper< unit<Dim1,System1>,quantity<Unit2,Y> >::type
- operator/(const unit<Dim1,System1>&,const quantity<Unit2,Y>& rhs)
- {
- typedef typename divide_typeof_helper< unit<Dim1,System1>,quantity<Unit2,Y> >::type type;
-
- return type::from_value(Y(1)/rhs.value());
- }
- /// runtime quantity times unit
- template<class Unit1,
- class System2,
- class Dim2,
- class Y>
- inline
- BOOST_CONSTEXPR
- typename multiply_typeof_helper< quantity<Unit1,Y>,unit<Dim2,System2> >::type
- operator*(const quantity<Unit1,Y>& lhs,const unit<Dim2,System2>&)
- {
- typedef typename multiply_typeof_helper< quantity<Unit1,Y>,unit<Dim2,System2> >::type type;
-
- return type::from_value(lhs.value());
- }
- /// runtime quantity divided by unit
- template<class Unit1,
- class System2,
- class Dim2,
- class Y>
- inline
- BOOST_CONSTEXPR
- typename divide_typeof_helper< quantity<Unit1,Y>,unit<Dim2,System2> >::type
- operator/(const quantity<Unit1,Y>& lhs,const unit<Dim2,System2>&)
- {
- typedef typename divide_typeof_helper< quantity<Unit1,Y>,unit<Dim2,System2> >::type type;
-
- return type::from_value(lhs.value());
- }
- /// runtime unary plus quantity
- template<class Unit,class Y>
- BOOST_CONSTEXPR
- typename unary_plus_typeof_helper< quantity<Unit,Y> >::type
- operator+(const quantity<Unit,Y>& val)
- {
- typedef typename unary_plus_typeof_helper< quantity<Unit,Y> >::type type;
-
- return type::from_value(+val.value());
- }
- /// runtime unary minus quantity
- template<class Unit,class Y>
- BOOST_CONSTEXPR
- typename unary_minus_typeof_helper< quantity<Unit,Y> >::type
- operator-(const quantity<Unit,Y>& val)
- {
- typedef typename unary_minus_typeof_helper< quantity<Unit,Y> >::type type;
-
- return type::from_value(-val.value());
- }
- /// runtime quantity plus quantity
- template<class Unit1,
- class Unit2,
- class X,
- class Y>
- inline
- BOOST_CONSTEXPR
- typename add_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type
- operator+(const quantity<Unit1,X>& lhs,
- const quantity<Unit2,Y>& rhs)
- {
- typedef typename add_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type type;
-
- return type::from_value(lhs.value()+rhs.value());
- }
- /// runtime quantity minus quantity
- template<class Unit1,
- class Unit2,
- class X,
- class Y>
- inline
- BOOST_CONSTEXPR
- typename subtract_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type
- operator-(const quantity<Unit1,X>& lhs,
- const quantity<Unit2,Y>& rhs)
- {
- typedef typename subtract_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type type;
-
- return type::from_value(lhs.value()-rhs.value());
- }
- /// runtime quantity times quantity
- template<class Unit1,
- class Unit2,
- class X,
- class Y>
- inline
- BOOST_CONSTEXPR
- typename multiply_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type
- operator*(const quantity<Unit1,X>& lhs,
- const quantity<Unit2,Y>& rhs)
- {
- typedef typename multiply_typeof_helper< quantity<Unit1,X>,
- quantity<Unit2,Y> >::type type;
-
- return type::from_value(lhs.value()*rhs.value());
- }
- /// runtime quantity divided by quantity
- template<class Unit1,
- class Unit2,
- class X,
- class Y>
- inline
- BOOST_CONSTEXPR
- typename divide_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type
- operator/(const quantity<Unit1,X>& lhs,
- const quantity<Unit2,Y>& rhs)
- {
- typedef typename divide_typeof_helper< quantity<Unit1,X>,
- quantity<Unit2,Y> >::type type;
-
- return type::from_value(lhs.value()/rhs.value());
- }
- /// runtime operator==
- template<class Unit,
- class X,
- class Y>
- inline
- BOOST_CONSTEXPR
- bool
- operator==(const quantity<Unit,X>& val1,
- const quantity<Unit,Y>& val2)
- {
- return val1.value() == val2.value();
- }
- /// runtime operator!=
- template<class Unit,
- class X,
- class Y>
- inline
- BOOST_CONSTEXPR
- bool
- operator!=(const quantity<Unit,X>& val1,
- const quantity<Unit,Y>& val2)
- {
- return val1.value() != val2.value();
- }
- /// runtime operator<
- template<class Unit,
- class X,
- class Y>
- inline
- BOOST_CONSTEXPR
- bool
- operator<(const quantity<Unit,X>& val1,
- const quantity<Unit,Y>& val2)
- {
- return val1.value() < val2.value();
- }
- /// runtime operator<=
- template<class Unit,
- class X,
- class Y>
- inline
- BOOST_CONSTEXPR
- bool
- operator<=(const quantity<Unit,X>& val1,
- const quantity<Unit,Y>& val2)
- {
- return val1.value() <= val2.value();
- }
- /// runtime operator>
- template<class Unit,
- class X,
- class Y>
- inline
- BOOST_CONSTEXPR
- bool
- operator>(const quantity<Unit,X>& val1,
- const quantity<Unit,Y>& val2)
- {
- return val1.value() > val2.value();
- }
- /// runtime operator>=
- template<class Unit,
- class X,
- class Y>
- inline
- BOOST_CONSTEXPR
- bool
- operator>=(const quantity<Unit,X>& val1,
- const quantity<Unit,Y>& val2)
- {
- return val1.value() >= val2.value();
- }
- } // namespace units
- } // namespace boost
- #endif // BOOST_UNITS_QUANTITY_HPP
|