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) 2007-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 #ifndef BOOST_UNITS_DIM_HPP
|
Chris@16
|
12 #define BOOST_UNITS_DIM_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/static_assert.hpp>
|
Chris@16
|
15
|
Chris@16
|
16 #include <boost/type_traits/is_same.hpp>
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/mpl/arithmetic.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #include <boost/units/config.hpp>
|
Chris@16
|
21 #include <boost/units/static_rational.hpp>
|
Chris@16
|
22 #include <boost/units/detail/dim_impl.hpp>
|
Chris@16
|
23
|
Chris@16
|
24 /// \file dim.hpp
|
Chris@16
|
25 /// \brief Handling of fundamental dimension/exponent pairs.
|
Chris@16
|
26
|
Chris@16
|
27 namespace boost {
|
Chris@16
|
28
|
Chris@16
|
29 namespace units {
|
Chris@16
|
30
|
Chris@16
|
31 namespace detail {
|
Chris@16
|
32
|
Chris@16
|
33 struct dim_tag { };
|
Chris@16
|
34
|
Chris@16
|
35 }
|
Chris@16
|
36
|
Chris@16
|
37 /// \brief Dimension tag/exponent pair for a single fundamental dimension.
|
Chris@16
|
38 ///
|
Chris@16
|
39 /// \details
|
Chris@16
|
40 /// The dim class represents a single dimension tag/dimension exponent pair.
|
Chris@16
|
41 /// That is, @c dim<tag_type,value_type> is a pair where @c tag_type represents the
|
Chris@16
|
42 /// fundamental dimension being represented and @c value_type represents the
|
Chris@16
|
43 /// exponent of that fundamental dimension as a @c static_rational. @c tag_type must
|
Chris@16
|
44 /// be a derived from a specialization of @c base_dimension.
|
Chris@16
|
45 /// Specialization of the following Boost.MPL metafunctions are provided
|
Chris@16
|
46 ///
|
Chris@16
|
47 /// - @c mpl::plus for two @c dims
|
Chris@16
|
48 /// - @c mpl::minus for two @c dims
|
Chris@16
|
49 /// - @c mpl::negate for a @c dim
|
Chris@16
|
50 ///
|
Chris@16
|
51 /// These metafunctions all operate on the exponent, and require
|
Chris@16
|
52 /// that the @c dim operands have the same base dimension tag.
|
Chris@16
|
53 /// In addition, multiplication and division by @c static_rational
|
Chris@16
|
54 /// is supported.
|
Chris@16
|
55 ///
|
Chris@16
|
56 /// - @c mpl::times for a @c static_rational and a @c dim in either order
|
Chris@16
|
57 /// - @c mpl::divides for a @c static_rational and a @c dim in either order
|
Chris@16
|
58 ///
|
Chris@16
|
59 /// These metafunctions likewise operate on the exponent only.
|
Chris@16
|
60 template<typename T,typename V>
|
Chris@16
|
61 struct dim
|
Chris@16
|
62 {
|
Chris@16
|
63 typedef dim type;
|
Chris@16
|
64 typedef detail::dim_tag tag;
|
Chris@16
|
65 typedef T tag_type;
|
Chris@16
|
66 typedef V value_type;
|
Chris@16
|
67 };
|
Chris@16
|
68
|
Chris@16
|
69 } // namespace units
|
Chris@16
|
70
|
Chris@16
|
71 } // namespace boost
|
Chris@16
|
72
|
Chris@16
|
73 #if BOOST_UNITS_HAS_BOOST_TYPEOF
|
Chris@16
|
74
|
Chris@16
|
75 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
|
Chris@16
|
76
|
Chris@16
|
77 BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::dim, 2)
|
Chris@16
|
78
|
Chris@16
|
79 #endif
|
Chris@16
|
80
|
Chris@16
|
81 #ifndef BOOST_UNITS_DOXYGEN
|
Chris@16
|
82
|
Chris@16
|
83 namespace boost {
|
Chris@16
|
84
|
Chris@16
|
85 namespace mpl {
|
Chris@16
|
86
|
Chris@16
|
87 // define MPL operators acting on dim<T,V>
|
Chris@16
|
88
|
Chris@16
|
89 template<>
|
Chris@16
|
90 struct plus_impl<boost::units::detail::dim_tag,boost::units::detail::dim_tag>
|
Chris@16
|
91 {
|
Chris@16
|
92 template<class T0, class T1>
|
Chris@16
|
93 struct apply
|
Chris@16
|
94 {
|
Chris@16
|
95 BOOST_STATIC_ASSERT((boost::is_same<typename T0::tag_type,typename T1::tag_type>::value == true));
|
Chris@16
|
96 typedef boost::units::dim<typename T0::tag_type, typename mpl::plus<typename T0::value_type, typename T1::value_type>::type> type;
|
Chris@16
|
97 };
|
Chris@16
|
98 };
|
Chris@16
|
99
|
Chris@16
|
100 template<>
|
Chris@16
|
101 struct minus_impl<boost::units::detail::dim_tag,boost::units::detail::dim_tag>
|
Chris@16
|
102 {
|
Chris@16
|
103 template<class T0, class T1>
|
Chris@16
|
104 struct apply
|
Chris@16
|
105 {
|
Chris@16
|
106 BOOST_STATIC_ASSERT((boost::is_same<typename T0::tag_type,typename T1::tag_type>::value == true));
|
Chris@16
|
107 typedef boost::units::dim<typename T0::tag_type, typename mpl::minus<typename T0::value_type, typename T1::value_type>::type> type;
|
Chris@16
|
108 };
|
Chris@16
|
109 };
|
Chris@16
|
110
|
Chris@16
|
111 template<>
|
Chris@16
|
112 struct times_impl<boost::units::detail::dim_tag,boost::units::detail::static_rational_tag>
|
Chris@16
|
113 {
|
Chris@16
|
114 template<class T0, class T1>
|
Chris@16
|
115 struct apply
|
Chris@16
|
116 {
|
Chris@16
|
117 typedef boost::units::dim<typename T0::tag_type, typename mpl::times<typename T0::value_type, T1>::type> type;
|
Chris@16
|
118 };
|
Chris@16
|
119 };
|
Chris@16
|
120
|
Chris@16
|
121 template<>
|
Chris@16
|
122 struct times_impl<boost::units::detail::static_rational_tag,boost::units::detail::dim_tag>
|
Chris@16
|
123 {
|
Chris@16
|
124 template<class T0, class T1>
|
Chris@16
|
125 struct apply
|
Chris@16
|
126 {
|
Chris@16
|
127 typedef boost::units::dim<typename T1::tag_type, typename mpl::times<T0, typename T1::value_type>::type> type;
|
Chris@16
|
128 };
|
Chris@16
|
129 };
|
Chris@16
|
130
|
Chris@16
|
131 template<>
|
Chris@16
|
132 struct divides_impl<boost::units::detail::dim_tag,boost::units::detail::static_rational_tag>
|
Chris@16
|
133 {
|
Chris@16
|
134 template<class T0, class T1>
|
Chris@16
|
135 struct apply
|
Chris@16
|
136 {
|
Chris@16
|
137 typedef boost::units::dim<typename T0::tag_type, typename mpl::divides<typename T0::value_type, T1>::type> type;
|
Chris@16
|
138 };
|
Chris@16
|
139 };
|
Chris@16
|
140
|
Chris@16
|
141 template<>
|
Chris@16
|
142 struct divides_impl<boost::units::detail::static_rational_tag,boost::units::detail::dim_tag>
|
Chris@16
|
143 {
|
Chris@16
|
144 template<class T0, class T1>
|
Chris@16
|
145 struct apply
|
Chris@16
|
146 {
|
Chris@16
|
147 typedef boost::units::dim<typename T1::tag_type, typename mpl::divides<T0, typename T1::value_type>::type> type;
|
Chris@16
|
148 };
|
Chris@16
|
149 };
|
Chris@16
|
150
|
Chris@16
|
151 template<>
|
Chris@16
|
152 struct negate_impl<boost::units::detail::dim_tag>
|
Chris@16
|
153 {
|
Chris@16
|
154 template<class T0>
|
Chris@16
|
155 struct apply
|
Chris@16
|
156 {
|
Chris@16
|
157 typedef boost::units::dim<typename T0::tag_type,typename mpl::negate<typename T0::value_type>::type> type;
|
Chris@16
|
158 };
|
Chris@16
|
159 };
|
Chris@16
|
160
|
Chris@16
|
161 } // namespace mpl
|
Chris@16
|
162
|
Chris@16
|
163 } // namespace boost
|
Chris@16
|
164
|
Chris@16
|
165 #endif
|
Chris@16
|
166
|
Chris@16
|
167 #endif // BOOST_UNITS_DIM_HPP
|