fix doc example typo
[boost.git] / boost / units / absolute.hpp
blob987ffaa7294bc9a466c14b77291db9aaf0159d6f
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)
11 #ifndef BOOST_UNITS_ABSOLUTE_HPP
12 #define BOOST_UNITS_ABSOLUTE_HPP
14 // necessary because the expression os << "absolute " is not dependent.
15 #include <ostream>
17 #include <boost/units/detail/absolute_impl.hpp>
19 namespace boost {
21 namespace units {
23 /// A wrapper to represent absolute units (points rather than vectors). Intended
24 /// originally for temperatures, this class implements operators for absolute units
25 /// so that addition of a relative unit to an absolute unit results in another
26 /// absolute unit : absolute<T> +/- T -> absolute<T> and subtraction of one absolute
27 /// unit from another results in a relative unit : absolute<T> - absolute<T> -> T
28 template<class Y>
29 class absolute
31 public:
32 typedef absolute<Y> this_type;
33 typedef Y value_type;
35 absolute() : val_() { }
36 absolute(const value_type& val) : val_(val) { }
37 absolute(const this_type& source) : val_(source.val_) { }
39 this_type& operator=(const this_type& source) { val_ = source.val_; return *this; }
41 const value_type& value() const { return val_; }
43 const this_type& operator+=(const value_type& val) { val_ += val; return *this; }
44 const this_type& operator-=(const value_type& val) { val_ -= val; return *this; }
46 private:
47 value_type val_;
50 /// add a relative value to an absolute one
51 template<class Y>
52 absolute<Y> operator+(const absolute<Y>& aval,const Y& rval)
54 return absolute<Y>(aval.value()+rval);
57 /// add a relative value to an absolute one
58 template<class Y>
59 absolute<Y> operator+(const Y& rval,const absolute<Y>& aval)
61 return absolute<Y>(aval.value()+rval);
64 /// subtract a relative value from an absolute one
65 template<class Y>
66 absolute<Y> operator-(const absolute<Y>& aval,const Y& rval)
68 return absolute<Y>(aval.value()-rval);
71 /// subtracting two absolutes gives a difference
72 template<class Y>
73 Y operator-(const absolute<Y>& aval1,const absolute<Y>& aval2)
75 return Y(aval1.value()-aval2.value());
78 /// creates a quantity from an absolute unit and a raw value
79 template<class D, class S, class T>
80 quantity<absolute<unit<D, S> >, T> operator*(const T& t, const absolute<unit<D, S> >&)
82 return(quantity<absolute<unit<D, S> >, T>::from_value(t));
85 /// creates a quantity from an absolute unit and a raw value
86 template<class D, class S, class T>
87 quantity<absolute<unit<D, S> >, T> operator*(const absolute<unit<D, S> >&, const T& t)
89 return(quantity<absolute<unit<D, S> >, T>::from_value(t));
92 /// Print an absolute unit
93 template<class Y>
94 std::ostream& operator<<(std::ostream& os,const absolute<Y>& aval)
97 os << "absolute " << aval.value();
99 return os;
102 } // namespace units
104 } // namespace boost
106 #if BOOST_UNITS_HAS_BOOST_TYPEOF
108 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
110 BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::absolute, (class))
112 #endif
114 namespace boost {
116 namespace units {
118 /// Macro to define the offset between two absolute units.
119 /// Requires the value to be in the destination units e.g
120 /// @code
121 /// BOOST_UNITS_DEFINE_CONVERSION_OFFSET(celsius_base_unit, fahrenheit_base_unit, double, 32.0);
122 /// @endcode
123 /// @c BOOST_UNITS_DEFINE_CONVERSION_FACTOR is also necessary to
124 /// specify the conversion factor. Like @c BOOST_UNITS_DEFINE_CONVERSION_FACTOR
125 /// this macro defines both forward and reverse conversions so
126 /// defining, e.g., the conversion from celsius to fahrenheit as above will also
127 /// define the inverse conversion from fahrenheit to celsius.
128 #define BOOST_UNITS_DEFINE_CONVERSION_OFFSET(From, To, type_, value_) \
129 namespace boost { \
130 namespace units { \
131 template<> \
132 struct affine_conversion_helper< \
133 reduce_unit<From::unit_type>::type, \
134 reduce_unit<To::unit_type>::type> \
136 static const bool is_defined = true; \
137 typedef type_ type; \
138 static type value() { return(value_); } \
139 }; \
142 void boost_units_require_semicolon()
144 } // namespace units
146 } // namespace boost
148 #endif // BOOST_UNITS_ABSOLUTE_HPP