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_DIMENSION_HPP
|
Chris@16
|
12 #define BOOST_UNITS_DIMENSION_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/static_rational.hpp>
|
Chris@16
|
21 #include <boost/units/detail/dimension_list.hpp>
|
Chris@16
|
22 #include <boost/units/detail/dimension_impl.hpp>
|
Chris@16
|
23
|
Chris@16
|
24 /// \file
|
Chris@16
|
25 /// \brief Core metaprogramming utilities for compile-time dimensional analysis.
|
Chris@16
|
26
|
Chris@16
|
27 namespace boost {
|
Chris@16
|
28
|
Chris@16
|
29 namespace units {
|
Chris@16
|
30
|
Chris@16
|
31 /// Reduce dimension list to cardinal form. This algorithm collapses duplicate
|
Chris@16
|
32 /// base dimension tags and sorts the resulting list by the tag ordinal value.
|
Chris@16
|
33 /// Dimension lists that resolve to the same dimension are guaranteed to be
|
Chris@16
|
34 /// represented by an identical type.
|
Chris@16
|
35 ///
|
Chris@16
|
36 /// The argument should be an MPL forward sequence containing instances
|
Chris@16
|
37 /// of the @c dim template.
|
Chris@16
|
38 ///
|
Chris@16
|
39 /// The result is also an MPL forward sequence. It also supports the
|
Chris@16
|
40 /// following metafunctions to allow use as a dimension.
|
Chris@16
|
41 ///
|
Chris@16
|
42 /// - @c mpl::plus is defined only on two equal dimensions and returns the argument unchanged.
|
Chris@16
|
43 /// - @c mpl::minus is defined only for two equal dimensions and returns the argument unchanged.
|
Chris@16
|
44 /// - @c mpl::negate will return its argument unchanged.
|
Chris@16
|
45 /// - @c mpl::times is defined for any dimensions and adds corresponding exponents.
|
Chris@16
|
46 /// - @c mpl::divides is defined for any dimensions and subtracts the exponents of the
|
Chris@16
|
47 /// right had argument from the corresponding exponents of the left had argument.
|
Chris@16
|
48 /// Missing base dimension tags are assumed to have an exponent of zero.
|
Chris@16
|
49 /// - @c static_power takes a dimension and a static_rational and multiplies all
|
Chris@16
|
50 /// the exponents of the dimension by the static_rational.
|
Chris@16
|
51 /// - @c static_root takes a dimension and a static_rational and divides all
|
Chris@16
|
52 /// the exponents of the dimension by the static_rational.
|
Chris@16
|
53 template<typename Seq>
|
Chris@16
|
54 struct make_dimension_list
|
Chris@16
|
55 {
|
Chris@16
|
56 typedef typename detail::sort_dims<Seq>::type type;
|
Chris@16
|
57 };
|
Chris@16
|
58
|
Chris@16
|
59 /// Raise a dimension list to a scalar power.
|
Chris@16
|
60 template<typename DL,typename Ex>
|
Chris@16
|
61 struct static_power
|
Chris@16
|
62 {
|
Chris@16
|
63 typedef typename detail::static_power_impl<DL::size::value>::template apply<
|
Chris@16
|
64 DL,
|
Chris@16
|
65 Ex
|
Chris@16
|
66 >::type type;
|
Chris@16
|
67 };
|
Chris@16
|
68
|
Chris@16
|
69 /// Take a scalar root of a dimension list.
|
Chris@16
|
70 template<typename DL,typename Rt>
|
Chris@16
|
71 struct static_root
|
Chris@16
|
72 {
|
Chris@16
|
73 typedef typename detail::static_root_impl<DL::size::value>::template apply<
|
Chris@16
|
74 DL,
|
Chris@16
|
75 Rt
|
Chris@16
|
76 >::type type;
|
Chris@16
|
77 };
|
Chris@16
|
78
|
Chris@16
|
79 } // namespace units
|
Chris@16
|
80
|
Chris@16
|
81 #ifndef BOOST_UNITS_DOXYGEN
|
Chris@16
|
82
|
Chris@16
|
83 namespace mpl {
|
Chris@16
|
84
|
Chris@16
|
85 template<>
|
Chris@16
|
86 struct plus_impl<boost::units::detail::dimension_list_tag,boost::units::detail::dimension_list_tag>
|
Chris@16
|
87 {
|
Chris@16
|
88 template<class T0, class T1>
|
Chris@16
|
89 struct apply
|
Chris@16
|
90 {
|
Chris@16
|
91 BOOST_STATIC_ASSERT((boost::is_same<T0,T1>::value == true));
|
Chris@16
|
92 typedef T0 type;
|
Chris@16
|
93 };
|
Chris@16
|
94 };
|
Chris@16
|
95
|
Chris@16
|
96 template<>
|
Chris@16
|
97 struct minus_impl<boost::units::detail::dimension_list_tag,boost::units::detail::dimension_list_tag>
|
Chris@16
|
98 {
|
Chris@16
|
99 template<class T0, class T1>
|
Chris@16
|
100 struct apply
|
Chris@16
|
101 {
|
Chris@16
|
102 BOOST_STATIC_ASSERT((boost::is_same<T0,T1>::value == true));
|
Chris@16
|
103 typedef T0 type;
|
Chris@16
|
104 };
|
Chris@16
|
105 };
|
Chris@16
|
106
|
Chris@16
|
107 template<>
|
Chris@16
|
108 struct times_impl<boost::units::detail::dimension_list_tag,boost::units::detail::dimension_list_tag>
|
Chris@16
|
109 {
|
Chris@16
|
110 template<class T0, class T1>
|
Chris@16
|
111 struct apply
|
Chris@16
|
112 {
|
Chris@16
|
113 typedef typename boost::units::detail::merge_dimensions<T0,T1>::type type;
|
Chris@16
|
114 };
|
Chris@16
|
115 };
|
Chris@16
|
116
|
Chris@16
|
117 template<>
|
Chris@16
|
118 struct divides_impl<boost::units::detail::dimension_list_tag,boost::units::detail::dimension_list_tag>
|
Chris@16
|
119 {
|
Chris@16
|
120 template<class T0, class T1>
|
Chris@16
|
121 struct apply
|
Chris@16
|
122 {
|
Chris@16
|
123 typedef typename boost::units::detail::merge_dimensions<
|
Chris@16
|
124 T0,
|
Chris@16
|
125 typename boost::units::detail::static_inverse_impl<
|
Chris@16
|
126 T1::size::value
|
Chris@16
|
127 >::template apply<
|
Chris@16
|
128 T1
|
Chris@16
|
129 >::type
|
Chris@16
|
130 >::type type;
|
Chris@16
|
131 };
|
Chris@16
|
132 };
|
Chris@16
|
133
|
Chris@16
|
134 template<>
|
Chris@16
|
135 struct negate_impl<boost::units::detail::dimension_list_tag>
|
Chris@16
|
136 {
|
Chris@16
|
137 template<class T0>
|
Chris@16
|
138 struct apply
|
Chris@16
|
139 {
|
Chris@16
|
140 typedef T0 type;
|
Chris@16
|
141 };
|
Chris@16
|
142 };
|
Chris@16
|
143
|
Chris@16
|
144 } // namespace mpl
|
Chris@16
|
145
|
Chris@16
|
146 #endif
|
Chris@16
|
147
|
Chris@16
|
148 } // namespace boost
|
Chris@16
|
149
|
Chris@16
|
150 #endif // BOOST_UNITS_DIMENSION_HPP
|