absolute.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. ///
  11. /// \file
  12. /// \brief Absolute units (points rather than vectors).
  13. /// \details Operations between absolute units, and relative units like temperature differences.
  14. ///
  15. #ifndef BOOST_UNITS_ABSOLUTE_HPP
  16. #define BOOST_UNITS_ABSOLUTE_HPP
  17. #include <iosfwd>
  18. #include <boost/units/detail/absolute_impl.hpp>
  19. namespace boost {
  20. namespace units {
  21. /// A wrapper to represent absolute units (points rather than vectors). Intended
  22. /// originally for temperatures, this class implements operators for absolute units
  23. /// so that addition of a relative unit to an absolute unit results in another
  24. /// absolute unit : absolute<T> +/- T -> absolute<T> and subtraction of one absolute
  25. /// unit from another results in a relative unit : absolute<T> - absolute<T> -> T.
  26. template<class Y>
  27. class absolute
  28. {
  29. public:
  30. typedef absolute<Y> this_type;
  31. typedef Y value_type;
  32. BOOST_CONSTEXPR absolute() : val_() { }
  33. BOOST_CONSTEXPR absolute(const value_type& val) : val_(val) { }
  34. BOOST_CONSTEXPR absolute(const this_type& source) : val_(source.val_) { }
  35. BOOST_CXX14_CONSTEXPR this_type& operator=(const this_type& source) { val_ = source.val_; return *this; }
  36. BOOST_CONSTEXPR const value_type& value() const { return val_; }
  37. BOOST_CXX14_CONSTEXPR const this_type& operator+=(const value_type& val) { val_ += val; return *this; }
  38. BOOST_CXX14_CONSTEXPR const this_type& operator-=(const value_type& val) { val_ -= val; return *this; }
  39. private:
  40. value_type val_;
  41. };
  42. /// add a relative value to an absolute one
  43. template<class Y>
  44. BOOST_CONSTEXPR absolute<Y> operator+(const absolute<Y>& aval,const Y& rval)
  45. {
  46. return absolute<Y>(aval.value()+rval);
  47. }
  48. /// add a relative value to an absolute one
  49. template<class Y>
  50. BOOST_CONSTEXPR absolute<Y> operator+(const Y& rval,const absolute<Y>& aval)
  51. {
  52. return absolute<Y>(aval.value()+rval);
  53. }
  54. /// subtract a relative value from an absolute one
  55. template<class Y>
  56. BOOST_CONSTEXPR absolute<Y> operator-(const absolute<Y>& aval,const Y& rval)
  57. {
  58. return absolute<Y>(aval.value()-rval);
  59. }
  60. /// subtracting two absolutes gives a difference
  61. template<class Y>
  62. BOOST_CONSTEXPR Y operator-(const absolute<Y>& aval1,const absolute<Y>& aval2)
  63. {
  64. return Y(aval1.value()-aval2.value());
  65. }
  66. /// creates a quantity from an absolute unit and a raw value
  67. template<class D, class S, class T>
  68. BOOST_CONSTEXPR quantity<absolute<unit<D, S> >, T> operator*(const T& t, const absolute<unit<D, S> >&)
  69. {
  70. return(quantity<absolute<unit<D, S> >, T>::from_value(t));
  71. }
  72. /// creates a quantity from an absolute unit and a raw value
  73. template<class D, class S, class T>
  74. BOOST_CONSTEXPR quantity<absolute<unit<D, S> >, T> operator*(const absolute<unit<D, S> >&, const T& t)
  75. {
  76. return(quantity<absolute<unit<D, S> >, T>::from_value(t));
  77. }
  78. /// Print an absolute unit
  79. template<class Char, class Traits, class Y>
  80. std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os,const absolute<Y>& aval)
  81. {
  82. os << "absolute " << aval.value();
  83. return os;
  84. }
  85. } // namespace units
  86. } // namespace boost
  87. #if BOOST_UNITS_HAS_BOOST_TYPEOF
  88. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  89. BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::absolute, (class))
  90. #endif
  91. namespace boost {
  92. namespace units {
  93. /// Macro to define the offset between two absolute units.
  94. /// Requires the value to be in the destination units e.g
  95. /// @code
  96. /// BOOST_UNITS_DEFINE_CONVERSION_OFFSET(celsius_base_unit, fahrenheit_base_unit, double, 32.0);
  97. /// @endcode
  98. /// @c BOOST_UNITS_DEFINE_CONVERSION_FACTOR is also necessary to
  99. /// specify the conversion factor. Like @c BOOST_UNITS_DEFINE_CONVERSION_FACTOR
  100. /// this macro defines both forward and reverse conversions so
  101. /// defining, e.g., the conversion from celsius to fahrenheit as above will also
  102. /// define the inverse conversion from fahrenheit to celsius.
  103. #define BOOST_UNITS_DEFINE_CONVERSION_OFFSET(From, To, type_, value_) \
  104. namespace boost { \
  105. namespace units { \
  106. template<> \
  107. struct affine_conversion_helper< \
  108. reduce_unit<From::unit_type>::type, \
  109. reduce_unit<To::unit_type>::type> \
  110. { \
  111. BOOST_STATIC_CONSTEXPR bool is_defined = true; \
  112. typedef type_ type; \
  113. static BOOST_CONSTEXPR type value() { return(value_); } \
  114. }; \
  115. } \
  116. } \
  117. void boost_units_require_semicolon()
  118. } // namespace units
  119. } // namespace boost
  120. #endif // BOOST_UNITS_ABSOLUTE_HPP