absolute.hpp 5.0 KB

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