Chris@16
|
1 // Boost.Units - A C++ library for zero-overhead dimensional analysis and
|
Chris@16
|
2 // unit/quantity manipulation and conversion
|
Chris@16
|
3 //
|
Chris@16
|
4 // Copyright (C) 2003-2008 Matthias Christian Schabel
|
Chris@16
|
5 // Copyright (C) 2008 Steven Watanabe
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
8 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
9 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
10
|
Chris@16
|
11 ///
|
Chris@16
|
12 /// \file
|
Chris@16
|
13 /// \brief Absolute units (points rather than vectors).
|
Chris@16
|
14 /// \details Operations between absolute units, and relative units like temperature differences.
|
Chris@16
|
15 ///
|
Chris@16
|
16
|
Chris@16
|
17 #ifndef BOOST_UNITS_ABSOLUTE_HPP
|
Chris@16
|
18 #define BOOST_UNITS_ABSOLUTE_HPP
|
Chris@16
|
19
|
Chris@16
|
20 #include <iosfwd>
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/units/detail/absolute_impl.hpp>
|
Chris@16
|
23
|
Chris@16
|
24 namespace boost {
|
Chris@16
|
25
|
Chris@16
|
26 namespace units {
|
Chris@16
|
27
|
Chris@16
|
28 /// A wrapper to represent absolute units (points rather than vectors). Intended
|
Chris@16
|
29 /// originally for temperatures, this class implements operators for absolute units
|
Chris@16
|
30 /// so that addition of a relative unit to an absolute unit results in another
|
Chris@16
|
31 /// absolute unit : absolute<T> +/- T -> absolute<T> and subtraction of one absolute
|
Chris@16
|
32 /// unit from another results in a relative unit : absolute<T> - absolute<T> -> T.
|
Chris@16
|
33 template<class Y>
|
Chris@16
|
34 class absolute
|
Chris@16
|
35 {
|
Chris@16
|
36 public:
|
Chris@16
|
37 typedef absolute<Y> this_type;
|
Chris@16
|
38 typedef Y value_type;
|
Chris@16
|
39
|
Chris@16
|
40 absolute() : val_() { }
|
Chris@16
|
41 absolute(const value_type& val) : val_(val) { }
|
Chris@16
|
42 absolute(const this_type& source) : val_(source.val_) { }
|
Chris@16
|
43
|
Chris@16
|
44 this_type& operator=(const this_type& source) { val_ = source.val_; return *this; }
|
Chris@16
|
45
|
Chris@16
|
46 const value_type& value() const { return val_; }
|
Chris@16
|
47
|
Chris@16
|
48 const this_type& operator+=(const value_type& val) { val_ += val; return *this; }
|
Chris@16
|
49 const this_type& operator-=(const value_type& val) { val_ -= val; return *this; }
|
Chris@16
|
50
|
Chris@16
|
51 private:
|
Chris@16
|
52 value_type val_;
|
Chris@16
|
53 };
|
Chris@16
|
54
|
Chris@16
|
55 /// add a relative value to an absolute one
|
Chris@16
|
56 template<class Y>
|
Chris@16
|
57 absolute<Y> operator+(const absolute<Y>& aval,const Y& rval)
|
Chris@16
|
58 {
|
Chris@16
|
59 return absolute<Y>(aval.value()+rval);
|
Chris@16
|
60 }
|
Chris@16
|
61
|
Chris@16
|
62 /// add a relative value to an absolute one
|
Chris@16
|
63 template<class Y>
|
Chris@16
|
64 absolute<Y> operator+(const Y& rval,const absolute<Y>& aval)
|
Chris@16
|
65 {
|
Chris@16
|
66 return absolute<Y>(aval.value()+rval);
|
Chris@16
|
67 }
|
Chris@16
|
68
|
Chris@16
|
69 /// subtract a relative value from an absolute one
|
Chris@16
|
70 template<class Y>
|
Chris@16
|
71 absolute<Y> operator-(const absolute<Y>& aval,const Y& rval)
|
Chris@16
|
72 {
|
Chris@16
|
73 return absolute<Y>(aval.value()-rval);
|
Chris@16
|
74 }
|
Chris@16
|
75
|
Chris@16
|
76 /// subtracting two absolutes gives a difference
|
Chris@16
|
77 template<class Y>
|
Chris@16
|
78 Y operator-(const absolute<Y>& aval1,const absolute<Y>& aval2)
|
Chris@16
|
79 {
|
Chris@16
|
80 return Y(aval1.value()-aval2.value());
|
Chris@16
|
81 }
|
Chris@16
|
82
|
Chris@16
|
83 /// creates a quantity from an absolute unit and a raw value
|
Chris@16
|
84 template<class D, class S, class T>
|
Chris@16
|
85 quantity<absolute<unit<D, S> >, T> operator*(const T& t, const absolute<unit<D, S> >&)
|
Chris@16
|
86 {
|
Chris@16
|
87 return(quantity<absolute<unit<D, S> >, T>::from_value(t));
|
Chris@16
|
88 }
|
Chris@16
|
89
|
Chris@16
|
90 /// creates a quantity from an absolute unit and a raw value
|
Chris@16
|
91 template<class D, class S, class T>
|
Chris@16
|
92 quantity<absolute<unit<D, S> >, T> operator*(const absolute<unit<D, S> >&, const T& t)
|
Chris@16
|
93 {
|
Chris@16
|
94 return(quantity<absolute<unit<D, S> >, T>::from_value(t));
|
Chris@16
|
95 }
|
Chris@16
|
96
|
Chris@16
|
97 /// Print an absolute unit
|
Chris@16
|
98 template<class Char, class Traits, class Y>
|
Chris@16
|
99 std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os,const absolute<Y>& aval)
|
Chris@16
|
100 {
|
Chris@16
|
101
|
Chris@16
|
102 os << "absolute " << aval.value();
|
Chris@16
|
103
|
Chris@16
|
104 return os;
|
Chris@16
|
105 }
|
Chris@16
|
106
|
Chris@16
|
107 } // namespace units
|
Chris@16
|
108
|
Chris@16
|
109 } // namespace boost
|
Chris@16
|
110
|
Chris@16
|
111 #if BOOST_UNITS_HAS_BOOST_TYPEOF
|
Chris@16
|
112
|
Chris@16
|
113 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
|
Chris@16
|
114
|
Chris@16
|
115 BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::absolute, (class))
|
Chris@16
|
116
|
Chris@16
|
117 #endif
|
Chris@16
|
118
|
Chris@16
|
119 namespace boost {
|
Chris@16
|
120
|
Chris@16
|
121 namespace units {
|
Chris@16
|
122
|
Chris@16
|
123 /// Macro to define the offset between two absolute units.
|
Chris@16
|
124 /// Requires the value to be in the destination units e.g
|
Chris@16
|
125 /// @code
|
Chris@16
|
126 /// BOOST_UNITS_DEFINE_CONVERSION_OFFSET(celsius_base_unit, fahrenheit_base_unit, double, 32.0);
|
Chris@16
|
127 /// @endcode
|
Chris@16
|
128 /// @c BOOST_UNITS_DEFINE_CONVERSION_FACTOR is also necessary to
|
Chris@16
|
129 /// specify the conversion factor. Like @c BOOST_UNITS_DEFINE_CONVERSION_FACTOR
|
Chris@16
|
130 /// this macro defines both forward and reverse conversions so
|
Chris@16
|
131 /// defining, e.g., the conversion from celsius to fahrenheit as above will also
|
Chris@16
|
132 /// define the inverse conversion from fahrenheit to celsius.
|
Chris@16
|
133 #define BOOST_UNITS_DEFINE_CONVERSION_OFFSET(From, To, type_, value_) \
|
Chris@16
|
134 namespace boost { \
|
Chris@16
|
135 namespace units { \
|
Chris@16
|
136 template<> \
|
Chris@16
|
137 struct affine_conversion_helper< \
|
Chris@16
|
138 reduce_unit<From::unit_type>::type, \
|
Chris@16
|
139 reduce_unit<To::unit_type>::type> \
|
Chris@16
|
140 { \
|
Chris@16
|
141 static const bool is_defined = true; \
|
Chris@16
|
142 typedef type_ type; \
|
Chris@16
|
143 static type value() { return(value_); } \
|
Chris@16
|
144 }; \
|
Chris@16
|
145 } \
|
Chris@16
|
146 } \
|
Chris@16
|
147 void boost_units_require_semicolon()
|
Chris@16
|
148
|
Chris@16
|
149 } // namespace units
|
Chris@16
|
150
|
Chris@16
|
151 } // namespace boost
|
Chris@16
|
152
|
Chris@16
|
153 #endif // BOOST_UNITS_ABSOLUTE_HPP
|