conversion_impl.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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_DETAIL_CONVERSION_IMPL_HPP
  11. #define BOOST_UNITS_DETAIL_CONVERSION_IMPL_HPP
  12. #include <boost/mpl/bool.hpp>
  13. #include <boost/mpl/and.hpp>
  14. #include <boost/mpl/divides.hpp>
  15. #include <boost/preprocessor/seq/enum.hpp>
  16. #include <boost/type_traits/is_same.hpp>
  17. #include <boost/units/heterogeneous_system.hpp>
  18. #include <boost/units/homogeneous_system.hpp>
  19. #include <boost/units/reduce_unit.hpp>
  20. #include <boost/units/static_rational.hpp>
  21. #include <boost/units/units_fwd.hpp>
  22. #include <boost/units/detail/dimension_list.hpp>
  23. #include <boost/units/detail/heterogeneous_conversion.hpp>
  24. #include <boost/units/detail/one.hpp>
  25. #include <boost/units/detail/static_rational_power.hpp>
  26. #include <boost/units/detail/unscale.hpp>
  27. #include <boost/units/units_fwd.hpp>
  28. namespace boost {
  29. namespace units {
  30. namespace detail {
  31. template<class Source, class Dest>
  32. struct conversion_factor_helper;
  33. template<class Source, class Dest>
  34. struct call_base_unit_converter;
  35. }
  36. /// INTERNAL ONLY
  37. struct undefined_base_unit_converter_base {
  38. static const bool is_defined = false;
  39. };
  40. /// INTERNAL ONLY
  41. struct no_default_conversion {
  42. static const bool is_defined = false;
  43. };
  44. /// INTERNAL ONLY
  45. template<class BaseUnit>
  46. struct unscaled_get_default_conversion : no_default_conversion { };
  47. /// INTERNAL ONLY
  48. template<bool is_defined>
  49. struct unscaled_get_default_conversion_impl;
  50. /// INTERNAL ONLY
  51. template<>
  52. struct unscaled_get_default_conversion_impl<true>
  53. {
  54. template<class T>
  55. struct apply
  56. {
  57. typedef typename unscaled_get_default_conversion<typename unscale<T>::type>::type type;
  58. };
  59. };
  60. /// INTERNAL ONLY
  61. template<>
  62. struct unscaled_get_default_conversion_impl<false>
  63. {
  64. template<class T>
  65. struct apply
  66. {
  67. typedef typename T::unit_type type;
  68. };
  69. };
  70. /// INTERNAL ONLY
  71. template<class BaseUnit>
  72. struct get_default_conversion
  73. {
  74. typedef typename unscaled_get_default_conversion_impl<
  75. unscaled_get_default_conversion<typename unscale<BaseUnit>::type>::is_defined
  76. >::template apply<BaseUnit>::type type;
  77. };
  78. /// INTERNAL ONLY
  79. template<class Source, class Destination>
  80. struct select_base_unit_converter
  81. {
  82. typedef Source source_type;
  83. typedef Destination destination_type;
  84. };
  85. /// INTERNAL ONLY
  86. template<class Source, class Dest>
  87. struct base_unit_converter_base : undefined_base_unit_converter_base {
  88. };
  89. /// INTERNAL ONLY
  90. template<class Source>
  91. struct base_unit_converter_base<Source, BOOST_UNITS_MAKE_HETEROGENEOUS_UNIT(Source, typename Source::dimension_type)>
  92. {
  93. static const bool is_defined = true;
  94. typedef one type;
  95. static type value() {
  96. one result;
  97. return(result);
  98. }
  99. };
  100. /// INTERNAL ONLY
  101. template<class Source, class Dest>
  102. struct base_unit_converter : base_unit_converter_base<Source, Dest> { };
  103. namespace detail {
  104. template<class Source, class Dest>
  105. struct do_call_base_unit_converter {
  106. typedef select_base_unit_converter<typename unscale<Source>::type, typename unscale<Dest>::type> selector;
  107. typedef typename selector::source_type source_type;
  108. typedef typename selector::destination_type destination_type;
  109. typedef base_unit_converter<source_type, destination_type> converter;
  110. typedef typename mpl::divides<typename get_scale_list<Source>::type, typename get_scale_list<source_type>::type>::type source_factor;
  111. typedef typename mpl::divides<typename get_scale_list<Dest>::type, typename get_scale_list<destination_type>::type>::type destination_factor;
  112. typedef typename mpl::divides<source_factor, destination_factor>::type factor;
  113. typedef eval_scale_list<factor> eval_factor;
  114. typedef typename multiply_typeof_helper<typename converter::type, typename eval_factor::type>::type type;
  115. static type value()
  116. {
  117. return(converter::value() * eval_factor::value());
  118. }
  119. };
  120. template<bool forward_is_defined, bool reverse_is_defined>
  121. struct call_base_unit_converter_base_unit_impl;
  122. template<>
  123. struct call_base_unit_converter_base_unit_impl<true, true>
  124. {
  125. template<class Source, class Dest>
  126. struct apply
  127. : do_call_base_unit_converter<Source, typename Dest::unit_type>
  128. {
  129. };
  130. };
  131. template<>
  132. struct call_base_unit_converter_base_unit_impl<true, false>
  133. {
  134. template<class Source, class Dest>
  135. struct apply
  136. : do_call_base_unit_converter<Source, typename Dest::unit_type>
  137. {
  138. };
  139. };
  140. template<>
  141. struct call_base_unit_converter_base_unit_impl<false, true>
  142. {
  143. template<class Source, class Dest>
  144. struct apply
  145. {
  146. typedef do_call_base_unit_converter<Dest, typename Source::unit_type> converter;
  147. typedef typename divide_typeof_helper<one, typename converter::type>::type type;
  148. static type value() {
  149. one numerator;
  150. return(numerator / converter::value());
  151. }
  152. };
  153. };
  154. template<>
  155. struct call_base_unit_converter_base_unit_impl<false, false>
  156. {
  157. template<class Source, class Dest>
  158. struct apply
  159. {
  160. typedef typename reduce_unit<typename get_default_conversion<Source>::type>::type new_source;
  161. typedef typename reduce_unit<typename get_default_conversion<Dest>::type>::type new_dest;
  162. typedef call_base_unit_converter<Source, new_source> start;
  163. typedef detail::conversion_factor_helper<
  164. new_source,
  165. new_dest
  166. > conversion;
  167. typedef call_base_unit_converter<Dest, new_dest> end;
  168. typedef typename divide_typeof_helper<
  169. typename multiply_typeof_helper<
  170. typename start::type,
  171. typename conversion::type
  172. >::type,
  173. typename end::type
  174. >::type type;
  175. static type value() {
  176. return(start::value() * conversion::value() / end::value());
  177. }
  178. };
  179. };
  180. template<int N>
  181. struct get_default_conversion_impl
  182. {
  183. template<class Begin>
  184. struct apply
  185. {
  186. typedef typename Begin::item source_pair;
  187. typedef typename source_pair::value_type exponent;
  188. typedef typename source_pair::tag_type source;
  189. typedef typename reduce_unit<typename get_default_conversion<source>::type>::type new_source;
  190. typedef typename get_default_conversion_impl<N-1>::template apply<typename Begin::next> next_iteration;
  191. typedef typename multiply_typeof_helper<typename power_typeof_helper<new_source, exponent>::type, typename next_iteration::unit_type>::type unit_type;
  192. typedef call_base_unit_converter<source, new_source> conversion;
  193. typedef typename multiply_typeof_helper<typename conversion::type, typename next_iteration::type>::type type;
  194. static type value() {
  195. return(static_rational_power<exponent>(conversion::value()) * next_iteration::value());
  196. }
  197. };
  198. };
  199. template<>
  200. struct get_default_conversion_impl<0>
  201. {
  202. template<class Begin>
  203. struct apply
  204. {
  205. typedef unit<dimensionless_type, heterogeneous_system<heterogeneous_system_impl<dimensionless_type, dimensionless_type, no_scale> > > unit_type;
  206. typedef one type;
  207. static one value() {
  208. one result;
  209. return(result);
  210. }
  211. };
  212. };
  213. template<bool is_defined>
  214. struct call_base_unit_converter_impl;
  215. template<>
  216. struct call_base_unit_converter_impl<true>
  217. {
  218. template<class Source, class Dest>
  219. struct apply
  220. : do_call_base_unit_converter<Source, Dest>
  221. {
  222. };
  223. };
  224. template<>
  225. struct call_base_unit_converter_impl<false>
  226. {
  227. template<class Source, class Dest>
  228. struct apply {
  229. typedef typename reduce_unit<typename get_default_conversion<Source>::type>::type new_source;
  230. typedef typename Dest::system_type::type system_list;
  231. typedef typename get_default_conversion_impl<system_list::size::value>::template apply<system_list> impl;
  232. typedef typename impl::unit_type new_dest;
  233. typedef call_base_unit_converter<Source, new_source> start;
  234. typedef conversion_factor_helper<new_source, new_dest> conversion;
  235. typedef typename divide_typeof_helper<
  236. typename multiply_typeof_helper<
  237. typename start::type,
  238. typename conversion::type
  239. >::type,
  240. typename impl::type
  241. >::type type;
  242. static type value() {
  243. return(start::value() * conversion::value() / impl::value());
  244. }
  245. };
  246. };
  247. #define BOOST_UNITS_DETAIL_BASE_UNIT_CONVERTER_IS_DEFINED(Source, Dest)\
  248. base_unit_converter<\
  249. typename select_base_unit_converter<typename unscale<Source>::type, typename unscale<Dest>::type>::source_type,\
  250. typename select_base_unit_converter<typename unscale<Source>::type, typename unscale<Dest>::type>::destination_type\
  251. >::is_defined
  252. template<class Source, class Dest>
  253. struct call_base_unit_converter : call_base_unit_converter_impl<BOOST_UNITS_DETAIL_BASE_UNIT_CONVERTER_IS_DEFINED(Source, Dest)>::template apply<Source, Dest>
  254. {
  255. };
  256. template<class Source, class Dest>
  257. struct call_base_unit_converter<Source, BOOST_UNITS_MAKE_HETEROGENEOUS_UNIT(Dest, typename Source::dimension_type)> :
  258. call_base_unit_converter_base_unit_impl<
  259. BOOST_UNITS_DETAIL_BASE_UNIT_CONVERTER_IS_DEFINED(Source, typename Dest::unit_type),
  260. BOOST_UNITS_DETAIL_BASE_UNIT_CONVERTER_IS_DEFINED(Dest, typename Source::unit_type)
  261. >::template apply<Source, Dest>
  262. {
  263. };
  264. template<int N>
  265. struct conversion_impl
  266. {
  267. template<class Begin, class DestinationSystem>
  268. struct apply
  269. {
  270. typedef typename conversion_impl<N-1>::template apply<
  271. typename Begin::next,
  272. DestinationSystem
  273. > next_iteration;
  274. typedef typename Begin::item unit_pair;
  275. typedef typename unit_pair::tag_type unit;
  276. typedef typename unit::dimension_type dimensions;
  277. typedef typename reduce_unit<units::unit<dimensions, DestinationSystem> >::type reduced_unit;
  278. typedef detail::call_base_unit_converter<unit, reduced_unit> converter;
  279. typedef typename multiply_typeof_helper<typename converter::type, typename next_iteration::type>::type type;
  280. static type value() { return(static_rational_power<typename unit_pair::value_type>(converter::value()) * next_iteration::value()); }
  281. };
  282. };
  283. template<>
  284. struct conversion_impl<0>
  285. {
  286. template<class Begin, class DestinationSystem>
  287. struct apply
  288. {
  289. typedef one type;
  290. static type value() { one result; return(result); }
  291. };
  292. };
  293. } // namespace detail
  294. /// forward to conversion_factor (intentionally allowing ADL)
  295. /// INTERNAL ONLY
  296. template<class Unit1, class T1, class Unit2, class T2>
  297. struct conversion_helper<quantity<Unit1, T1>, quantity<Unit2, T2> >
  298. {
  299. /// INTERNAL ONLY
  300. typedef quantity<Unit2, T2> destination_type;
  301. static destination_type convert(const quantity<Unit1, T1>& source)
  302. {
  303. Unit1 u1;
  304. Unit2 u2;
  305. return(destination_type::from_value(static_cast<T2>(source.value() * conversion_factor(u1, u2))));
  306. }
  307. };
  308. namespace detail {
  309. template<class Source, class Dest>
  310. struct conversion_factor_helper;
  311. template<class D, class L1, class L2>
  312. struct conversion_factor_helper<unit<D, homogeneous_system<L1> >, unit<D, homogeneous_system<L2> > >
  313. : conversion_factor_helper<
  314. typename reduce_unit<unit<D, homogeneous_system<L1> > >::type,
  315. typename reduce_unit<unit<D, homogeneous_system<L2> > >::type
  316. >
  317. {
  318. //typedef typename reduce_unit<unit<D, homogeneous_system<L1> > >::type source_unit;
  319. //typedef typename source_unit::system_type::type unit_list;
  320. //typedef typename detail::conversion_impl<unit_list::size::value>::template apply<
  321. // unit_list,
  322. // homogeneous_system<L2>
  323. //> impl;
  324. //typedef typename impl::type type;
  325. //static type value()
  326. //{
  327. // return(impl::value());
  328. //}
  329. };
  330. template<class D, class L1, class L2>
  331. struct conversion_factor_helper<unit<D, heterogeneous_system<L1> >, unit<D, homogeneous_system<L2> > >
  332. : conversion_factor_helper<
  333. typename reduce_unit<unit<D, heterogeneous_system<L1> > >::type,
  334. typename reduce_unit<unit<D, homogeneous_system<L2> > >::type
  335. >
  336. {
  337. //typedef typename detail::conversion_impl<L1::type::size::value>::template apply<
  338. // typename L1::type,
  339. // homogeneous_system<L2>
  340. //> impl;
  341. //typedef eval_scale_list<typename L1::scale> scale;
  342. //typedef typename multiply_typeof_helper<typename impl::type, typename scale::type>::type type;
  343. //static type value()
  344. //{
  345. // return(impl::value() * scale::value());
  346. //}
  347. };
  348. // There is no simple algorithm for doing this conversion
  349. // other than just defining it as the reverse of the
  350. // heterogeneous->homogeneous case
  351. template<class D, class L1, class L2>
  352. struct conversion_factor_helper<unit<D, homogeneous_system<L1> >, unit<D, heterogeneous_system<L2> > >
  353. : conversion_factor_helper<
  354. typename reduce_unit<unit<D, homogeneous_system<L1> > >::type,
  355. typename reduce_unit<unit<D, heterogeneous_system<L2> > >::type
  356. >
  357. {
  358. //typedef typename detail::conversion_impl<L2::type::size::value>::template apply<
  359. // typename L2::type,
  360. // homogeneous_system<L1>
  361. //> impl;
  362. //typedef eval_scale_list<typename L2::scale> scale;
  363. //typedef typename multiply_typeof_helper<typename impl::type, typename scale::type>::type type;
  364. //static type value()
  365. //{
  366. // one numerator;
  367. // return(numerator / (impl::value() * scale::value()));
  368. //}
  369. };
  370. /// Requires that all possible conversions
  371. /// between base units are defined.
  372. template<class D, class S1, class S2>
  373. struct conversion_factor_helper<unit<D, heterogeneous_system<S1> >, unit<D, heterogeneous_system<S2> > >
  374. {
  375. /// INTERNAL ONLY
  376. typedef typename detail::extract_base_units<S1::type::size::value>::template apply<
  377. typename S1::type,
  378. dimensionless_type
  379. >::type from_base_units;
  380. /// INTERNAL ONLY
  381. typedef typename detail::extract_base_units<S2::type::size::value>::template apply<
  382. typename S2::type,
  383. from_base_units
  384. >::type all_base_units;
  385. /// INTERNAL ONLY
  386. typedef typename detail::make_homogeneous_system<all_base_units>::type system;
  387. typedef typename detail::conversion_impl<S1::type::size::value>::template apply<
  388. typename S1::type,
  389. system
  390. > conversion1;
  391. typedef typename detail::conversion_impl<S2::type::size::value>::template apply<
  392. typename S2::type,
  393. system
  394. > conversion2;
  395. typedef eval_scale_list<typename mpl::divides<typename S1::scale, typename S2::scale>::type> scale;
  396. typedef typename multiply_typeof_helper<
  397. typename conversion1::type,
  398. typename divide_typeof_helper<typename scale::type, typename conversion2::type>::type
  399. >::type type;
  400. static type value()
  401. {
  402. return(conversion1::value() * (scale::value() / conversion2::value()));
  403. }
  404. };
  405. } // namespace detail
  406. } // namespace units
  407. } // namespace boost
  408. #endif // BOOST_UNITS_CONVERSION_IMPL_HPP