absolute_impl.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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) 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_ABSOLUTE_IMPL_HPP
  11. #define BOOST_UNITS_ABSOLUTE_IMPL_HPP
  12. #include <iosfwd>
  13. #include <boost/units/config.hpp>
  14. #include <boost/units/conversion.hpp>
  15. #include <boost/units/heterogeneous_system.hpp>
  16. #include <boost/units/units_fwd.hpp>
  17. namespace boost {
  18. namespace units {
  19. /// INTERNAL ONLY
  20. template<class D, class S>
  21. struct reduce_unit<absolute<unit<D, S> > >
  22. {
  23. typedef absolute<typename reduce_unit<unit<D, S> >::type> type;
  24. };
  25. namespace detail {
  26. struct undefined_affine_conversion_base {
  27. BOOST_STATIC_CONSTEXPR bool is_defined = false;
  28. };
  29. } // namespace detail
  30. /// INTERNAL ONLY
  31. template<class From, class To>
  32. struct affine_conversion_helper : detail::undefined_affine_conversion_base { };
  33. namespace detail {
  34. template<bool IsDefined, bool ReverseIsDefined>
  35. struct affine_conversion_impl;
  36. template<bool ReverseIsDefined>
  37. struct affine_conversion_impl<true, ReverseIsDefined>
  38. {
  39. template<class Unit1, class Unit2, class T0, class T1>
  40. struct apply {
  41. static BOOST_CONSTEXPR T1 value(const T0& t0)
  42. {
  43. return(
  44. t0 *
  45. conversion_factor(Unit1(), Unit2()) +
  46. affine_conversion_helper<typename reduce_unit<Unit1>::type, typename reduce_unit<Unit2>::type>::value());
  47. }
  48. };
  49. };
  50. template<>
  51. struct affine_conversion_impl<false, true>
  52. {
  53. template<class Unit1, class Unit2, class T0, class T1>
  54. struct apply
  55. {
  56. static BOOST_CONSTEXPR T1 value(const T0& t0)
  57. {
  58. return(
  59. (t0 - affine_conversion_helper<typename reduce_unit<Unit2>::type, typename reduce_unit<Unit1>::type>::value()) *
  60. conversion_factor(Unit1(), Unit2()));
  61. }
  62. };
  63. };
  64. } // namespace detail
  65. /// INTERNAL ONLY
  66. template<class Unit1, class T1, class Unit2, class T2>
  67. struct conversion_helper<quantity<absolute<Unit1>, T1>, quantity<absolute<Unit2>, T2> >
  68. {
  69. typedef quantity<absolute<Unit1>, T1> from_quantity_type;
  70. typedef quantity<absolute<Unit2>, T2> to_quantity_type;
  71. static BOOST_CONSTEXPR to_quantity_type convert(const from_quantity_type& source)
  72. {
  73. return(
  74. to_quantity_type::from_value(
  75. detail::affine_conversion_impl<
  76. affine_conversion_helper<typename reduce_unit<Unit1>::type, typename reduce_unit<Unit2>::type>::is_defined,
  77. affine_conversion_helper<typename reduce_unit<Unit2>::type, typename reduce_unit<Unit1>::type>::is_defined
  78. >::template apply<Unit1, Unit2, T1, T2>::value(source.value())
  79. )
  80. );
  81. }
  82. };
  83. } // namespace units
  84. } // namespace boost
  85. #endif // BOOST_UNITS_ABSOLUTE_IMPL_HPP