quantity.hpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. // Boost.Units - A C++ library for zero-overhead dimensional analysis and
  2. // unit/quantity manipulation and conversion
  3. //
  4. // Copyright (C) 2003-2008 Matthias Christian Schabel
  5. // Copyright (C) 2007-2008 Steven Watanabe
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See
  8. // accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_UNITS_QUANTITY_HPP
  11. #define BOOST_UNITS_QUANTITY_HPP
  12. #include <algorithm>
  13. #include <boost/config.hpp>
  14. #include <boost/static_assert.hpp>
  15. #include <boost/mpl/bool.hpp>
  16. #include <boost/mpl/and.hpp>
  17. #include <boost/mpl/not.hpp>
  18. #include <boost/mpl/or.hpp>
  19. #include <boost/mpl/assert.hpp>
  20. #include <boost/utility/enable_if.hpp>
  21. #include <boost/type_traits/is_arithmetic.hpp>
  22. #include <boost/type_traits/is_convertible.hpp>
  23. #include <boost/type_traits/is_integral.hpp>
  24. #include <boost/type_traits/is_same.hpp>
  25. #include <boost/units/conversion.hpp>
  26. #include <boost/units/dimensionless_type.hpp>
  27. #include <boost/units/homogeneous_system.hpp>
  28. #include <boost/units/operators.hpp>
  29. #include <boost/units/static_rational.hpp>
  30. #include <boost/units/units_fwd.hpp>
  31. #include <boost/units/detail/dimensionless_unit.hpp>
  32. namespace boost {
  33. namespace units {
  34. namespace detail {
  35. template<class T, class Enable = void>
  36. struct is_base_unit : mpl::false_ {};
  37. template<class T>
  38. struct is_base_unit<T, typename T::boost_units_is_base_unit_type> : mpl::true_ {};
  39. template<class Source, class Destination>
  40. struct is_narrowing_conversion_impl : mpl::bool_<(sizeof(Source) > sizeof(Destination))> {};
  41. template<class Source, class Destination>
  42. struct is_non_narrowing_conversion :
  43. mpl::and_<
  44. boost::is_convertible<Source, Destination>,
  45. mpl::not_<
  46. mpl::and_<
  47. boost::is_arithmetic<Source>,
  48. boost::is_arithmetic<Destination>,
  49. mpl::or_<
  50. mpl::and_<
  51. is_integral<Destination>,
  52. mpl::not_<is_integral<Source> >
  53. >,
  54. is_narrowing_conversion_impl<Source, Destination>
  55. >
  56. >
  57. >
  58. >
  59. {};
  60. template<>
  61. struct is_non_narrowing_conversion<long double, double> : mpl::false_ {};
  62. // msvc 7.1 needs extra disambiguation
  63. template<class T, class U>
  64. struct disable_if_is_same
  65. {
  66. typedef void type;
  67. };
  68. template<class T>
  69. struct disable_if_is_same<T, T> {};
  70. }
  71. /// class declaration
  72. template<class Unit,class Y>
  73. class quantity
  74. {
  75. // base units are not the same as units.
  76. BOOST_MPL_ASSERT_NOT((detail::is_base_unit<Unit>));
  77. enum { force_instantiation_of_unit = sizeof(Unit) };
  78. typedef void (quantity::*unspecified_null_pointer_constant_type)(int*******);
  79. public:
  80. typedef quantity<Unit,Y> this_type;
  81. typedef Y value_type;
  82. typedef Unit unit_type;
  83. quantity() : val_()
  84. {
  85. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  86. }
  87. quantity(unspecified_null_pointer_constant_type) : val_()
  88. {
  89. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  90. }
  91. quantity(const this_type& source) : val_(source.val_)
  92. {
  93. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  94. }
  95. // Need to make sure that the destructor of
  96. // Unit which contains the checking is instantiated,
  97. // on sun.
  98. #ifdef __SUNPRO_CC
  99. ~quantity() {
  100. unit_type force_unit_instantiation;
  101. }
  102. #endif
  103. //~quantity() { }
  104. this_type& operator=(const this_type& source)
  105. {
  106. val_ = source.val_;
  107. return *this;
  108. }
  109. #ifndef BOOST_NO_SFINAE
  110. /// implicit conversion between value types is allowed if allowed for value types themselves
  111. template<class YY>
  112. quantity(const quantity<Unit,YY>& source,
  113. typename boost::enable_if<detail::is_non_narrowing_conversion<YY, Y> >::type* = 0) :
  114. val_(source.value())
  115. {
  116. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  117. }
  118. /// implicit conversion between value types is not allowed if not allowed for value types themselves
  119. template<class YY>
  120. explicit quantity(const quantity<Unit,YY>& source,
  121. typename boost::disable_if<detail::is_non_narrowing_conversion<YY, Y> >::type* = 0) :
  122. val_(static_cast<Y>(source.value()))
  123. {
  124. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  125. }
  126. #else
  127. /// implicit conversion between value types is allowed if allowed for value types themselves
  128. template<class YY>
  129. quantity(const quantity<Unit,YY>& source) :
  130. val_(source.value())
  131. {
  132. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  133. BOOST_STATIC_ASSERT((boost::is_convertible<YY, Y>::value == true));
  134. }
  135. #endif
  136. /// implicit assignment between value types is allowed if allowed for value types themselves
  137. template<class YY>
  138. this_type& operator=(const quantity<Unit,YY>& source)
  139. {
  140. BOOST_STATIC_ASSERT((boost::is_convertible<YY, Y>::value == true));
  141. *this = this_type(source);
  142. return *this;
  143. }
  144. #ifndef BOOST_NO_SFINAE
  145. /// explicit conversion between different unit systems is allowed if implicit conversion is disallowed
  146. template<class Unit2,class YY>
  147. explicit
  148. quantity(const quantity<Unit2,YY>& source,
  149. typename boost::disable_if<
  150. mpl::and_<
  151. //is_implicitly_convertible should be undefined when the
  152. //units are not convertible at all
  153. typename is_implicitly_convertible<Unit2,Unit>::type,
  154. detail::is_non_narrowing_conversion<YY, Y>
  155. >,
  156. typename detail::disable_if_is_same<Unit, Unit2>::type
  157. >::type* = 0)
  158. : val_(conversion_helper<quantity<Unit2,YY>,this_type>::convert(source).value())
  159. {
  160. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  161. BOOST_STATIC_ASSERT((boost::is_convertible<YY,Y>::value == true));
  162. }
  163. /// implicit conversion between different unit systems is allowed if each fundamental dimension is implicitly convertible
  164. template<class Unit2,class YY>
  165. quantity(const quantity<Unit2,YY>& source,
  166. typename boost::enable_if<
  167. mpl::and_<
  168. typename is_implicitly_convertible<Unit2,Unit>::type,
  169. detail::is_non_narrowing_conversion<YY, Y>
  170. >,
  171. typename detail::disable_if_is_same<Unit, Unit2>::type
  172. >::type* = 0)
  173. : val_(conversion_helper<quantity<Unit2,YY>,this_type>::convert(source).value())
  174. {
  175. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  176. BOOST_STATIC_ASSERT((boost::is_convertible<YY,Y>::value == true));
  177. }
  178. #else
  179. /// without SFINAE we can't distinguish between explicit and implicit conversions so
  180. /// the conversion is always explicit
  181. template<class Unit2,class YY>
  182. explicit quantity(const quantity<Unit2,YY>& source)
  183. : val_(conversion_helper<quantity<Unit2,YY>,this_type>::convert(source).value())
  184. {
  185. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  186. BOOST_STATIC_ASSERT((boost::is_convertible<YY,Y>::value == true));
  187. }
  188. #endif
  189. /// implicit assignment between different unit systems is allowed if each fundamental dimension is implicitly convertible
  190. template<class Unit2,class YY>
  191. this_type& operator=(const quantity<Unit2,YY>& source)
  192. {
  193. BOOST_STATIC_ASSERT((is_implicitly_convertible<Unit2,unit_type>::value == true));
  194. BOOST_STATIC_ASSERT((boost::is_convertible<YY,Y>::value == true));
  195. *this = this_type(source);
  196. return *this;
  197. }
  198. const value_type& value() const { return val_; } ///< constant accessor to value
  199. ///< can add a quantity of the same type if add_typeof_helper<value_type,value_type>::type is convertible to value_type
  200. template<class Unit2, class YY>
  201. this_type& operator+=(const quantity<Unit2, YY>& source)
  202. {
  203. BOOST_STATIC_ASSERT((boost::is_same<typename add_typeof_helper<Unit, Unit2>::type, Unit>::value));
  204. val_ += source.value();
  205. return *this;
  206. }
  207. ///< can subtract a quantity of the same type if subtract_typeof_helper<value_type,value_type>::type is convertible to value_type
  208. template<class Unit2, class YY>
  209. this_type& operator-=(const quantity<Unit2, YY>& source)
  210. {
  211. BOOST_STATIC_ASSERT((boost::is_same<typename subtract_typeof_helper<Unit, Unit2>::type, Unit>::value));
  212. val_ -= source.value();
  213. return *this;
  214. }
  215. template<class Unit2, class YY>
  216. this_type& operator*=(const quantity<Unit2, YY>& source)
  217. {
  218. BOOST_STATIC_ASSERT((boost::is_same<typename multiply_typeof_helper<Unit, Unit2>::type, Unit>::value));
  219. val_ *= source.value();
  220. return *this;
  221. }
  222. template<class Unit2, class YY>
  223. this_type& operator/=(const quantity<Unit2, YY>& source)
  224. {
  225. BOOST_STATIC_ASSERT((boost::is_same<typename divide_typeof_helper<Unit, Unit2>::type, Unit>::value));
  226. val_ /= source.value();
  227. return *this;
  228. }
  229. ///< can multiply a quantity by a scalar value_type if multiply_typeof_helper<value_type,value_type>::type is convertible to value_type
  230. this_type& operator*=(const value_type& source) { val_ *= source; return *this; }
  231. ///< can divide a quantity by a scalar value_type if divide_typeof_helper<value_type,value_type>::type is convertible to value_type
  232. this_type& operator/=(const value_type& source) { val_ /= source; return *this; }
  233. /// Construct quantity directly from @c value_type (potentially dangerous).
  234. static this_type from_value(const value_type& val) { return this_type(val, 0); }
  235. protected:
  236. explicit quantity(const value_type& val, int) : val_(val) { }
  237. private:
  238. value_type val_;
  239. };
  240. /// Specialization for dimensionless quantities. Implicit conversions between
  241. /// unit systems are allowed because all dimensionless quantities are equivalent.
  242. /// Implicit construction and assignment from and conversion to @c value_type is
  243. /// also allowed.
  244. template<class System,class Y>
  245. class quantity<BOOST_UNITS_DIMENSIONLESS_UNIT(System),Y>
  246. {
  247. public:
  248. typedef quantity<unit<dimensionless_type,System>,Y> this_type;
  249. typedef Y value_type;
  250. typedef System system_type;
  251. typedef dimensionless_type dimension_type;
  252. typedef unit<dimension_type,system_type> unit_type;
  253. quantity() : val_()
  254. {
  255. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  256. }
  257. /// construction from raw @c value_type is allowed
  258. quantity(value_type val) : val_(val)
  259. {
  260. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  261. }
  262. quantity(const this_type& source) : val_(source.val_)
  263. {
  264. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  265. }
  266. //~quantity() { }
  267. this_type& operator=(const this_type& source)
  268. {
  269. val_ = source.val_;
  270. return *this;
  271. }
  272. #ifndef BOOST_NO_SFINAE
  273. /// implicit conversion between value types is allowed if allowed for value types themselves
  274. template<class YY>
  275. quantity(const quantity<unit<dimension_type,system_type>,YY>& source,
  276. typename boost::enable_if<detail::is_non_narrowing_conversion<YY, Y> >::type* = 0) :
  277. val_(source.value())
  278. {
  279. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  280. }
  281. /// implicit conversion between value types is not allowed if not allowed for value types themselves
  282. template<class YY>
  283. explicit quantity(const quantity<unit<dimension_type,system_type>,YY>& source,
  284. typename boost::disable_if<detail::is_non_narrowing_conversion<YY, Y> >::type* = 0) :
  285. val_(static_cast<Y>(source.value()))
  286. {
  287. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  288. }
  289. #else
  290. /// implicit conversion between value types is allowed if allowed for value types themselves
  291. template<class YY>
  292. quantity(const quantity<unit<dimension_type,system_type>,YY>& source) :
  293. val_(source.value())
  294. {
  295. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  296. BOOST_STATIC_ASSERT((boost::is_convertible<YY, Y>::value == true));
  297. }
  298. #endif
  299. /// implicit assignment between value types is allowed if allowed for value types themselves
  300. template<class YY>
  301. this_type& operator=(const quantity<unit<dimension_type,system_type>,YY>& source)
  302. {
  303. BOOST_STATIC_ASSERT((boost::is_convertible<YY,Y>::value == true));
  304. *this = this_type(source);
  305. return *this;
  306. }
  307. #if 1
  308. /// implicit conversion between different unit systems is allowed
  309. template<class System2, class Y2>
  310. quantity(const quantity<unit<dimensionless_type, System2>,Y2>& source,
  311. #ifdef __SUNPRO_CC
  312. typename boost::enable_if<
  313. boost::mpl::and_<
  314. detail::is_non_narrowing_conversion<Y2, Y>,
  315. detail::is_dimensionless_system<System2>
  316. >
  317. >::type* = 0
  318. #else
  319. typename boost::enable_if<detail::is_non_narrowing_conversion<Y2, Y> >::type* = 0,
  320. typename detail::disable_if_is_same<System, System2>::type* = 0,
  321. typename boost::enable_if<detail::is_dimensionless_system<System2> >::type* = 0
  322. #endif
  323. ) :
  324. val_(source.value())
  325. {
  326. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  327. }
  328. /// implicit conversion between different unit systems is allowed
  329. template<class System2, class Y2>
  330. explicit quantity(const quantity<unit<dimensionless_type, System2>,Y2>& source,
  331. #ifdef __SUNPRO_CC
  332. typename boost::enable_if<
  333. boost::mpl::and_<
  334. boost::mpl::not_<detail::is_non_narrowing_conversion<Y2, Y> >,
  335. detail::is_dimensionless_system<System2>
  336. >
  337. >::type* = 0
  338. #else
  339. typename boost::disable_if<detail::is_non_narrowing_conversion<Y2, Y> >::type* = 0,
  340. typename detail::disable_if_is_same<System, System2>::type* = 0,
  341. typename boost::enable_if<detail::is_dimensionless_system<System2> >::type* = 0
  342. #endif
  343. ) :
  344. val_(static_cast<Y>(source.value()))
  345. {
  346. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  347. }
  348. #else
  349. /// implicit conversion between different unit systems is allowed
  350. template<class System2, class Y2>
  351. quantity(const quantity<unit<dimensionless_type,homogeneous_system<System2> >,Y2>& source) :
  352. val_(source.value())
  353. {
  354. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  355. BOOST_STATIC_ASSERT((boost::is_convertible<Y2, Y>::value == true));
  356. }
  357. #endif
  358. /// conversion between different unit systems is explicit when
  359. /// the units are not equivalent.
  360. template<class System2, class Y2>
  361. explicit quantity(const quantity<unit<dimensionless_type, System2>,Y2>& source,
  362. typename boost::disable_if<detail::is_dimensionless_system<System2> >::type* = 0) :
  363. val_(conversion_helper<quantity<unit<dimensionless_type, System2>,Y2>, this_type>::convert(source).value())
  364. {
  365. BOOST_UNITS_CHECK_LAYOUT_COMPATIBILITY(this_type, Y);
  366. }
  367. #ifndef __SUNPRO_CC
  368. /// implicit assignment between different unit systems is allowed
  369. template<class System2>
  370. this_type& operator=(const quantity<BOOST_UNITS_DIMENSIONLESS_UNIT(System2),Y>& source)
  371. {
  372. *this = this_type(source);
  373. return *this;
  374. }
  375. #endif
  376. /// implicit conversion to @c value_type is allowed
  377. operator value_type() const { return val_; }
  378. const value_type& value() const { return val_; } ///< constant accessor to value
  379. ///< can add a quantity of the same type if add_typeof_helper<value_type,value_type>::type is convertible to value_type
  380. this_type& operator+=(const this_type& source) { val_ += source.val_; return *this; }
  381. ///< can subtract a quantity of the same type if subtract_typeof_helper<value_type,value_type>::type is convertible to value_type
  382. this_type& operator-=(const this_type& source) { val_ -= source.val_; return *this; }
  383. ///< can multiply a quantity by a scalar value_type if multiply_typeof_helper<value_type,value_type>::type is convertible to value_type
  384. this_type& operator*=(const value_type& val) { val_ *= val; return *this; }
  385. ///< can divide a quantity by a scalar value_type if divide_typeof_helper<value_type,value_type>::type is convertible to value_type
  386. this_type& operator/=(const value_type& val) { val_ /= val; return *this; }
  387. /// Construct quantity directly from @c value_type.
  388. static this_type from_value(const value_type& val) { return this_type(val); }
  389. private:
  390. value_type val_;
  391. };
  392. #ifdef BOOST_MSVC
  393. // HACK: For some obscure reason msvc 8.0 needs these specializations
  394. template<class System, class T>
  395. class quantity<unit<int, System>, T> {};
  396. template<class T>
  397. class quantity<int, T> {};
  398. #endif
  399. } // namespace units
  400. } // namespace boost
  401. #if BOOST_UNITS_HAS_BOOST_TYPEOF
  402. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  403. BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::quantity, 2)
  404. #endif
  405. namespace boost {
  406. namespace units {
  407. namespace detail {
  408. /// helper class for quantity_cast
  409. template<class X,class Y> struct quantity_cast_helper;
  410. /// specialization for casting to the value type
  411. template<class Y,class X,class Unit>
  412. struct quantity_cast_helper<Y,quantity<Unit,X> >
  413. {
  414. typedef Y type;
  415. type operator()(quantity<Unit,X>& source) { return const_cast<X&>(source.value()); }
  416. };
  417. /// specialization for casting to the value type
  418. template<class Y,class X,class Unit>
  419. struct quantity_cast_helper<Y,const quantity<Unit,X> >
  420. {
  421. typedef Y type;
  422. type operator()(const quantity<Unit,X>& source) { return source.value(); }
  423. };
  424. } // namespace detail
  425. /// quantity_cast provides mutating access to underlying quantity value_type
  426. template<class X,class Y>
  427. inline
  428. X
  429. quantity_cast(Y& source)
  430. {
  431. detail::quantity_cast_helper<X,Y> qch;
  432. return qch(source);
  433. }
  434. template<class X,class Y>
  435. inline
  436. X
  437. quantity_cast(const Y& source)
  438. {
  439. detail::quantity_cast_helper<X,const Y> qch;
  440. return qch(source);
  441. }
  442. /// swap quantities
  443. template<class Unit,class Y>
  444. inline void swap(quantity<Unit,Y>& lhs, quantity<Unit,Y>& rhs)
  445. {
  446. using std::swap;
  447. swap(quantity_cast<Y&>(lhs),quantity_cast<Y&>(rhs));
  448. }
  449. /// specialize unary plus typeof helper
  450. /// INTERNAL ONLY
  451. template<class Unit,class Y>
  452. struct unary_plus_typeof_helper< quantity<Unit,Y> >
  453. {
  454. typedef typename unary_plus_typeof_helper<Y>::type value_type;
  455. typedef typename unary_plus_typeof_helper<Unit>::type unit_type;
  456. typedef quantity<unit_type,value_type> type;
  457. };
  458. /// specialize unary minus typeof helper
  459. /// INTERNAL ONLY
  460. template<class Unit,class Y>
  461. struct unary_minus_typeof_helper< quantity<Unit,Y> >
  462. {
  463. typedef typename unary_minus_typeof_helper<Y>::type value_type;
  464. typedef typename unary_minus_typeof_helper<Unit>::type unit_type;
  465. typedef quantity<unit_type,value_type> type;
  466. };
  467. /// specialize add typeof helper
  468. /// INTERNAL ONLY
  469. template<class Unit1,
  470. class Unit2,
  471. class X,
  472. class Y>
  473. struct add_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >
  474. {
  475. typedef typename add_typeof_helper<X,Y>::type value_type;
  476. typedef typename add_typeof_helper<Unit1,Unit2>::type unit_type;
  477. typedef quantity<unit_type,value_type> type;
  478. };
  479. /// for sun CC we need to invoke SFINAE at
  480. /// the top level, otherwise it will silently
  481. /// return int.
  482. template<class Dim1, class System1,
  483. class Dim2, class System2,
  484. class X,
  485. class Y>
  486. struct add_typeof_helper< quantity<unit<Dim1, System1>,X>,quantity<unit<Dim2, System2>,Y> >
  487. {
  488. };
  489. template<class Dim,
  490. class System,
  491. class X,
  492. class Y>
  493. struct add_typeof_helper< quantity<unit<Dim, System>,X>,quantity<unit<Dim, System>,Y> >
  494. {
  495. typedef typename add_typeof_helper<X,Y>::type value_type;
  496. typedef unit<Dim, System> unit_type;
  497. typedef quantity<unit_type,value_type> type;
  498. };
  499. /// specialize subtract typeof helper
  500. /// INTERNAL ONLY
  501. template<class Unit1,
  502. class Unit2,
  503. class X,
  504. class Y>
  505. struct subtract_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >
  506. {
  507. typedef typename subtract_typeof_helper<X,Y>::type value_type;
  508. typedef typename subtract_typeof_helper<Unit1,Unit2>::type unit_type;
  509. typedef quantity<unit_type,value_type> type;
  510. };
  511. // Force adding different units to fail on sun.
  512. template<class Dim1, class System1,
  513. class Dim2, class System2,
  514. class X,
  515. class Y>
  516. struct subtract_typeof_helper< quantity<unit<Dim1, System1>,X>,quantity<unit<Dim2, System2>,Y> >
  517. {
  518. };
  519. template<class Dim,
  520. class System,
  521. class X,
  522. class Y>
  523. struct subtract_typeof_helper< quantity<unit<Dim, System>,X>,quantity<unit<Dim, System>,Y> >
  524. {
  525. typedef typename subtract_typeof_helper<X,Y>::type value_type;
  526. typedef unit<Dim, System> unit_type;
  527. typedef quantity<unit_type,value_type> type;
  528. };
  529. /// scalar times unit typeof helper
  530. /// INTERNAL ONLY
  531. template<class System,
  532. class Dim,
  533. class X>
  534. struct multiply_typeof_helper< X,unit<Dim,System> >
  535. {
  536. typedef X value_type;
  537. typedef unit<Dim,System> unit_type;
  538. typedef quantity<unit_type,value_type> type;
  539. };
  540. /// unit times scalar typeof helper
  541. /// INTERNAL ONLY
  542. template<class System,
  543. class Dim,
  544. class X>
  545. struct multiply_typeof_helper< unit<Dim,System>,X >
  546. {
  547. typedef X value_type;
  548. typedef unit<Dim,System> unit_type;
  549. typedef quantity<unit_type,value_type> type;
  550. };
  551. /// scalar times quantity typeof helper
  552. /// INTERNAL ONLY
  553. template<class Unit,
  554. class X,
  555. class Y>
  556. struct multiply_typeof_helper< X,quantity<Unit,Y> >
  557. {
  558. typedef typename multiply_typeof_helper<X,Y>::type value_type;
  559. typedef Unit unit_type;
  560. typedef quantity<unit_type,value_type> type;
  561. };
  562. /// disambiguate
  563. /// INTERNAL ONLY
  564. template<class Unit,
  565. class Y>
  566. struct multiply_typeof_helper< one,quantity<Unit,Y> >
  567. {
  568. typedef quantity<Unit,Y> type;
  569. };
  570. /// quantity times scalar typeof helper
  571. /// INTERNAL ONLY
  572. template<class Unit,
  573. class X,
  574. class Y>
  575. struct multiply_typeof_helper< quantity<Unit,X>,Y >
  576. {
  577. typedef typename multiply_typeof_helper<X,Y>::type value_type;
  578. typedef Unit unit_type;
  579. typedef quantity<unit_type,value_type> type;
  580. };
  581. /// disambiguate
  582. /// INTERNAL ONLY
  583. template<class Unit,
  584. class X>
  585. struct multiply_typeof_helper< quantity<Unit,X>,one >
  586. {
  587. typedef quantity<Unit,X> type;
  588. };
  589. /// unit times quantity typeof helper
  590. /// INTERNAL ONLY
  591. template<class Unit,
  592. class System,
  593. class Dim,
  594. class X>
  595. struct multiply_typeof_helper< unit<Dim,System>,quantity<Unit,X> >
  596. {
  597. typedef X value_type;
  598. typedef typename multiply_typeof_helper< unit<Dim,System>,Unit >::type unit_type;
  599. typedef quantity<unit_type,value_type> type;
  600. };
  601. /// quantity times unit typeof helper
  602. /// INTERNAL ONLY
  603. template<class Unit,
  604. class System,
  605. class Dim,
  606. class X>
  607. struct multiply_typeof_helper< quantity<Unit,X>,unit<Dim,System> >
  608. {
  609. typedef X value_type;
  610. typedef typename multiply_typeof_helper< Unit,unit<Dim,System> >::type unit_type;
  611. typedef quantity<unit_type,value_type> type;
  612. };
  613. /// quantity times quantity typeof helper
  614. /// INTERNAL ONLY
  615. template<class Unit1,
  616. class Unit2,
  617. class X,
  618. class Y>
  619. struct multiply_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >
  620. {
  621. typedef typename multiply_typeof_helper<X,Y>::type value_type;
  622. typedef typename multiply_typeof_helper<Unit1,Unit2>::type unit_type;
  623. typedef quantity<unit_type,value_type> type;
  624. };
  625. /// scalar divided by unit typeof helper
  626. /// INTERNAL ONLY
  627. template<class System,
  628. class Dim,
  629. class X>
  630. struct divide_typeof_helper< X,unit<Dim,System> >
  631. {
  632. typedef X value_type;
  633. typedef typename power_typeof_helper< unit<Dim,System>,static_rational<-1> >::type unit_type;
  634. typedef quantity<unit_type,value_type> type;
  635. };
  636. /// unit divided by scalar typeof helper
  637. /// INTERNAL ONLY
  638. template<class System,
  639. class Dim,
  640. class X>
  641. struct divide_typeof_helper< unit<Dim,System>,X >
  642. {
  643. typedef typename divide_typeof_helper<X,X>::type value_type;
  644. typedef unit<Dim,System> unit_type;
  645. typedef quantity<unit_type,value_type> type;
  646. };
  647. /// scalar divided by quantity typeof helper
  648. /// INTERNAL ONLY
  649. template<class Unit,
  650. class X,
  651. class Y>
  652. struct divide_typeof_helper< X,quantity<Unit,Y> >
  653. {
  654. typedef typename divide_typeof_helper<X,Y>::type value_type;
  655. typedef typename power_typeof_helper< Unit,static_rational<-1> >::type unit_type;
  656. typedef quantity<unit_type,value_type> type;
  657. };
  658. /// disambiguate
  659. /// INTERNAL ONLY
  660. template<class Unit,
  661. class Y>
  662. struct divide_typeof_helper< one,quantity<Unit,Y> >
  663. {
  664. typedef quantity<Unit,Y> type;
  665. };
  666. /// quantity divided by scalar typeof helper
  667. /// INTERNAL ONLY
  668. template<class Unit,
  669. class X,
  670. class Y>
  671. struct divide_typeof_helper< quantity<Unit,X>,Y >
  672. {
  673. typedef typename divide_typeof_helper<X,Y>::type value_type;
  674. typedef Unit unit_type;
  675. typedef quantity<unit_type,value_type> type;
  676. };
  677. /// disambiguate
  678. /// INTERNAL ONLY
  679. template<class Unit,
  680. class X>
  681. struct divide_typeof_helper< quantity<Unit,X>,one >
  682. {
  683. typedef quantity<Unit,X> type;
  684. };
  685. /// unit divided by quantity typeof helper
  686. /// INTERNAL ONLY
  687. template<class Unit,
  688. class System,
  689. class Dim,
  690. class X>
  691. struct divide_typeof_helper< unit<Dim,System>,quantity<Unit,X> >
  692. {
  693. typedef typename divide_typeof_helper<X,X>::type value_type;
  694. typedef typename divide_typeof_helper< unit<Dim,System>,Unit >::type unit_type;
  695. typedef quantity<unit_type,value_type> type;
  696. };
  697. /// quantity divided by unit typeof helper
  698. /// INTERNAL ONLY
  699. template<class Unit,
  700. class System,
  701. class Dim,
  702. class X>
  703. struct divide_typeof_helper< quantity<Unit,X>,unit<Dim,System> >
  704. {
  705. typedef X value_type;
  706. typedef typename divide_typeof_helper< Unit,unit<Dim,System> >::type unit_type;
  707. typedef quantity<unit_type,value_type> type;
  708. };
  709. /// quantity divided by quantity typeof helper
  710. /// INTERNAL ONLY
  711. template<class Unit1,
  712. class Unit2,
  713. class X,
  714. class Y>
  715. struct divide_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >
  716. {
  717. typedef typename divide_typeof_helper<X,Y>::type value_type;
  718. typedef typename divide_typeof_helper<Unit1,Unit2>::type unit_type;
  719. typedef quantity<unit_type,value_type> type;
  720. };
  721. /// specialize power typeof helper
  722. /// INTERNAL ONLY
  723. template<class Unit,long N,long D,class Y>
  724. struct power_typeof_helper< quantity<Unit,Y>,static_rational<N,D> >
  725. {
  726. typedef typename power_typeof_helper<Y,static_rational<N,D> >::type value_type;
  727. typedef typename power_typeof_helper<Unit,static_rational<N,D> >::type unit_type;
  728. typedef quantity<unit_type,value_type> type;
  729. static type value(const quantity<Unit,Y>& x)
  730. {
  731. return type::from_value(power_typeof_helper<Y,static_rational<N,D> >::value(x.value()));
  732. }
  733. };
  734. /// specialize root typeof helper
  735. /// INTERNAL ONLY
  736. template<class Unit,long N,long D,class Y>
  737. struct root_typeof_helper< quantity<Unit,Y>,static_rational<N,D> >
  738. {
  739. typedef typename root_typeof_helper<Y,static_rational<N,D> >::type value_type;
  740. typedef typename root_typeof_helper<Unit,static_rational<N,D> >::type unit_type;
  741. typedef quantity<unit_type,value_type> type;
  742. static type value(const quantity<Unit,Y>& x)
  743. {
  744. return type::from_value(root_typeof_helper<Y,static_rational<N,D> >::value(x.value()));
  745. }
  746. };
  747. /// runtime unit times scalar
  748. /// INTERNAL ONLY
  749. template<class System,
  750. class Dim,
  751. class Y>
  752. inline
  753. typename multiply_typeof_helper< unit<Dim,System>,Y >::type
  754. operator*(const unit<Dim,System>&,const Y& rhs)
  755. {
  756. typedef typename multiply_typeof_helper< unit<Dim,System>,Y >::type type;
  757. return type::from_value(rhs);
  758. }
  759. /// runtime unit divided by scalar
  760. template<class System,
  761. class Dim,
  762. class Y>
  763. inline
  764. typename divide_typeof_helper< unit<Dim,System>,Y >::type
  765. operator/(const unit<Dim,System>&,const Y& rhs)
  766. {
  767. typedef typename divide_typeof_helper<unit<Dim,System>,Y>::type type;
  768. return type::from_value(Y(1)/rhs);
  769. }
  770. /// runtime scalar times unit
  771. template<class System,
  772. class Dim,
  773. class Y>
  774. inline
  775. typename multiply_typeof_helper< Y,unit<Dim,System> >::type
  776. operator*(const Y& lhs,const unit<Dim,System>&)
  777. {
  778. typedef typename multiply_typeof_helper< Y,unit<Dim,System> >::type type;
  779. return type::from_value(lhs);
  780. }
  781. /// runtime scalar divided by unit
  782. template<class System,
  783. class Dim,
  784. class Y>
  785. inline
  786. typename divide_typeof_helper< Y,unit<Dim,System> >::type
  787. operator/(const Y& lhs,const unit<Dim,System>&)
  788. {
  789. typedef typename divide_typeof_helper< Y,unit<Dim,System> >::type type;
  790. return type::from_value(lhs);
  791. }
  792. ///// runtime quantity times scalar
  793. //template<class Unit,
  794. // class X,
  795. // class Y>
  796. //inline
  797. //typename multiply_typeof_helper< quantity<Unit,X>,Y >::type
  798. //operator*(const quantity<Unit,X>& lhs,const Y& rhs)
  799. //{
  800. // typedef typename multiply_typeof_helper< quantity<Unit,X>,Y >::type type;
  801. //
  802. // return type::from_value(lhs.value()*rhs);
  803. //}
  804. //
  805. ///// runtime scalar times quantity
  806. //template<class Unit,
  807. // class X,
  808. // class Y>
  809. //inline
  810. //typename multiply_typeof_helper< X,quantity<Unit,Y> >::type
  811. //operator*(const X& lhs,const quantity<Unit,Y>& rhs)
  812. //{
  813. // typedef typename multiply_typeof_helper< X,quantity<Unit,Y> >::type type;
  814. //
  815. // return type::from_value(lhs*rhs.value());
  816. //}
  817. /// runtime quantity times scalar
  818. template<class Unit,
  819. class X>
  820. inline
  821. typename multiply_typeof_helper< quantity<Unit,X>,X >::type
  822. operator*(const quantity<Unit,X>& lhs,const X& rhs)
  823. {
  824. typedef typename multiply_typeof_helper< quantity<Unit,X>,X >::type type;
  825. return type::from_value(lhs.value()*rhs);
  826. }
  827. /// runtime scalar times quantity
  828. template<class Unit,
  829. class X>
  830. inline
  831. typename multiply_typeof_helper< X,quantity<Unit,X> >::type
  832. operator*(const X& lhs,const quantity<Unit,X>& rhs)
  833. {
  834. typedef typename multiply_typeof_helper< X,quantity<Unit,X> >::type type;
  835. return type::from_value(lhs*rhs.value());
  836. }
  837. ///// runtime quantity divided by scalar
  838. //template<class Unit,
  839. // class X,
  840. // class Y>
  841. //inline
  842. //typename divide_typeof_helper< quantity<Unit,X>,Y >::type
  843. //operator/(const quantity<Unit,X>& lhs,const Y& rhs)
  844. //{
  845. // typedef typename divide_typeof_helper< quantity<Unit,X>,Y >::type type;
  846. //
  847. // return type::from_value(lhs.value()/rhs);
  848. //}
  849. //
  850. ///// runtime scalar divided by quantity
  851. //template<class Unit,
  852. // class X,
  853. // class Y>
  854. //inline
  855. //typename divide_typeof_helper< X,quantity<Unit,Y> >::type
  856. //operator/(const X& lhs,const quantity<Unit,Y>& rhs)
  857. //{
  858. // typedef typename divide_typeof_helper< X,quantity<Unit,Y> >::type type;
  859. //
  860. // return type::from_value(lhs/rhs.value());
  861. //}
  862. /// runtime quantity divided by scalar
  863. template<class Unit,
  864. class X>
  865. inline
  866. typename divide_typeof_helper< quantity<Unit,X>,X >::type
  867. operator/(const quantity<Unit,X>& lhs,const X& rhs)
  868. {
  869. typedef typename divide_typeof_helper< quantity<Unit,X>,X >::type type;
  870. return type::from_value(lhs.value()/rhs);
  871. }
  872. /// runtime scalar divided by quantity
  873. template<class Unit,
  874. class X>
  875. inline
  876. typename divide_typeof_helper< X,quantity<Unit,X> >::type
  877. operator/(const X& lhs,const quantity<Unit,X>& rhs)
  878. {
  879. typedef typename divide_typeof_helper< X,quantity<Unit,X> >::type type;
  880. return type::from_value(lhs/rhs.value());
  881. }
  882. /// runtime unit times quantity
  883. template<class System1,
  884. class Dim1,
  885. class Unit2,
  886. class Y>
  887. inline
  888. typename multiply_typeof_helper< unit<Dim1,System1>,quantity<Unit2,Y> >::type
  889. operator*(const unit<Dim1,System1>&,const quantity<Unit2,Y>& rhs)
  890. {
  891. typedef typename multiply_typeof_helper< unit<Dim1,System1>,quantity<Unit2,Y> >::type type;
  892. return type::from_value(rhs.value());
  893. }
  894. /// runtime unit divided by quantity
  895. template<class System1,
  896. class Dim1,
  897. class Unit2,
  898. class Y>
  899. inline
  900. typename divide_typeof_helper< unit<Dim1,System1>,quantity<Unit2,Y> >::type
  901. operator/(const unit<Dim1,System1>&,const quantity<Unit2,Y>& rhs)
  902. {
  903. typedef typename divide_typeof_helper< unit<Dim1,System1>,quantity<Unit2,Y> >::type type;
  904. return type::from_value(Y(1)/rhs.value());
  905. }
  906. /// runtime quantity times unit
  907. template<class Unit1,
  908. class System2,
  909. class Dim2,
  910. class Y>
  911. inline
  912. typename multiply_typeof_helper< quantity<Unit1,Y>,unit<Dim2,System2> >::type
  913. operator*(const quantity<Unit1,Y>& lhs,const unit<Dim2,System2>&)
  914. {
  915. typedef typename multiply_typeof_helper< quantity<Unit1,Y>,unit<Dim2,System2> >::type type;
  916. return type::from_value(lhs.value());
  917. }
  918. /// runtime quantity divided by unit
  919. template<class Unit1,
  920. class System2,
  921. class Dim2,
  922. class Y>
  923. inline
  924. typename divide_typeof_helper< quantity<Unit1,Y>,unit<Dim2,System2> >::type
  925. operator/(const quantity<Unit1,Y>& lhs,const unit<Dim2,System2>&)
  926. {
  927. typedef typename divide_typeof_helper< quantity<Unit1,Y>,unit<Dim2,System2> >::type type;
  928. return type::from_value(lhs.value());
  929. }
  930. /// runtime unary plus quantity
  931. template<class Unit,class Y>
  932. typename unary_plus_typeof_helper< quantity<Unit,Y> >::type
  933. operator+(const quantity<Unit,Y>& val)
  934. {
  935. typedef typename unary_plus_typeof_helper< quantity<Unit,Y> >::type type;
  936. return type::from_value(+val.value());
  937. }
  938. /// runtime unary minus quantity
  939. template<class Unit,class Y>
  940. typename unary_minus_typeof_helper< quantity<Unit,Y> >::type
  941. operator-(const quantity<Unit,Y>& val)
  942. {
  943. typedef typename unary_minus_typeof_helper< quantity<Unit,Y> >::type type;
  944. return type::from_value(-val.value());
  945. }
  946. /// runtime quantity plus quantity
  947. template<class Unit1,
  948. class Unit2,
  949. class X,
  950. class Y>
  951. inline
  952. typename add_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type
  953. operator+(const quantity<Unit1,X>& lhs,
  954. const quantity<Unit2,Y>& rhs)
  955. {
  956. typedef typename add_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type type;
  957. return type::from_value(lhs.value()+rhs.value());
  958. }
  959. /// runtime quantity minus quantity
  960. template<class Unit1,
  961. class Unit2,
  962. class X,
  963. class Y>
  964. inline
  965. typename subtract_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type
  966. operator-(const quantity<Unit1,X>& lhs,
  967. const quantity<Unit2,Y>& rhs)
  968. {
  969. typedef typename subtract_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type type;
  970. return type::from_value(lhs.value()-rhs.value());
  971. }
  972. /// runtime quantity times quantity
  973. template<class Unit1,
  974. class Unit2,
  975. class X,
  976. class Y>
  977. inline
  978. typename multiply_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type
  979. operator*(const quantity<Unit1,X>& lhs,
  980. const quantity<Unit2,Y>& rhs)
  981. {
  982. typedef typename multiply_typeof_helper< quantity<Unit1,X>,
  983. quantity<Unit2,Y> >::type type;
  984. return type::from_value(lhs.value()*rhs.value());
  985. }
  986. /// runtime quantity divided by quantity
  987. template<class Unit1,
  988. class Unit2,
  989. class X,
  990. class Y>
  991. inline
  992. typename divide_typeof_helper< quantity<Unit1,X>,quantity<Unit2,Y> >::type
  993. operator/(const quantity<Unit1,X>& lhs,
  994. const quantity<Unit2,Y>& rhs)
  995. {
  996. typedef typename divide_typeof_helper< quantity<Unit1,X>,
  997. quantity<Unit2,Y> >::type type;
  998. return type::from_value(lhs.value()/rhs.value());
  999. }
  1000. /// runtime operator==
  1001. template<class Unit,
  1002. class X,
  1003. class Y>
  1004. inline
  1005. bool
  1006. operator==(const quantity<Unit,X>& val1,
  1007. const quantity<Unit,Y>& val2)
  1008. {
  1009. return val1.value() == val2.value();
  1010. }
  1011. /// runtime operator!=
  1012. template<class Unit,
  1013. class X,
  1014. class Y>
  1015. inline
  1016. bool
  1017. operator!=(const quantity<Unit,X>& val1,
  1018. const quantity<Unit,Y>& val2)
  1019. {
  1020. return val1.value() != val2.value();
  1021. }
  1022. /// runtime operator<
  1023. template<class Unit,
  1024. class X,
  1025. class Y>
  1026. inline
  1027. bool
  1028. operator<(const quantity<Unit,X>& val1,
  1029. const quantity<Unit,Y>& val2)
  1030. {
  1031. return val1.value() < val2.value();
  1032. }
  1033. /// runtime operator<=
  1034. template<class Unit,
  1035. class X,
  1036. class Y>
  1037. inline
  1038. bool
  1039. operator<=(const quantity<Unit,X>& val1,
  1040. const quantity<Unit,Y>& val2)
  1041. {
  1042. return val1.value() <= val2.value();
  1043. }
  1044. /// runtime operator>
  1045. template<class Unit,
  1046. class X,
  1047. class Y>
  1048. inline
  1049. bool
  1050. operator>(const quantity<Unit,X>& val1,
  1051. const quantity<Unit,Y>& val2)
  1052. {
  1053. return val1.value() > val2.value();
  1054. }
  1055. /// runtime operator>=
  1056. template<class Unit,
  1057. class X,
  1058. class Y>
  1059. inline
  1060. bool
  1061. operator>=(const quantity<Unit,X>& val1,
  1062. const quantity<Unit,Y>& val2)
  1063. {
  1064. return val1.value() >= val2.value();
  1065. }
  1066. } // namespace units
  1067. } // namespace boost
  1068. #endif // BOOST_UNITS_QUANTITY_HPP