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 #ifndef BOOST_UNITS_DETAIL_HETEROGENEOUS_CONVERSION_HPP
|
Chris@16
|
12 #define BOOST_UNITS_DETAIL_HETEROGENEOUS_CONVERSION_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/mpl/minus.hpp>
|
Chris@16
|
15 #include <boost/mpl/times.hpp>
|
Chris@16
|
16
|
Chris@16
|
17 #include <boost/units/static_rational.hpp>
|
Chris@16
|
18 #include <boost/units/homogeneous_system.hpp>
|
Chris@16
|
19 #include <boost/units/detail/linear_algebra.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 namespace boost {
|
Chris@16
|
22
|
Chris@16
|
23 namespace units {
|
Chris@16
|
24
|
Chris@16
|
25 namespace detail {
|
Chris@16
|
26
|
Chris@16
|
27 struct solve_end {
|
Chris@16
|
28 template<class Begin, class Y>
|
Chris@16
|
29 struct apply {
|
Chris@16
|
30 typedef dimensionless_type type;
|
Chris@16
|
31 };
|
Chris@16
|
32 };
|
Chris@16
|
33
|
Chris@16
|
34 struct no_solution {};
|
Chris@16
|
35
|
Chris@16
|
36 template<class X1, class X2, class Next>
|
Chris@16
|
37 struct solve_normal {
|
Chris@16
|
38 template<class Begin, class Y>
|
Chris@16
|
39 struct apply {
|
Chris@16
|
40 typedef typename Begin::next next;
|
Chris@16
|
41 typedef list<
|
Chris@16
|
42 typename mpl::minus<
|
Chris@16
|
43 typename mpl::times<X1, Y>::type,
|
Chris@16
|
44 typename mpl::times<X2, typename Begin::item>::type
|
Chris@16
|
45 >::type,
|
Chris@16
|
46 typename Next::template apply<next, Y>::type
|
Chris@16
|
47 > type;
|
Chris@16
|
48 };
|
Chris@16
|
49 };
|
Chris@16
|
50
|
Chris@16
|
51 template<class Next>
|
Chris@16
|
52 struct solve_leading_zeroes {
|
Chris@16
|
53 template<class Begin>
|
Chris@16
|
54 struct apply {
|
Chris@16
|
55 typedef list<
|
Chris@16
|
56 typename Begin::item,
|
Chris@16
|
57 typename Next::template apply<typename Begin::next>::type
|
Chris@16
|
58 > type;
|
Chris@16
|
59 };
|
Chris@16
|
60 typedef solve_leading_zeroes type;
|
Chris@16
|
61 };
|
Chris@16
|
62
|
Chris@16
|
63 template<>
|
Chris@16
|
64 struct solve_leading_zeroes<no_solution> {
|
Chris@16
|
65 typedef no_solution type;
|
Chris@16
|
66 };
|
Chris@16
|
67
|
Chris@16
|
68 template<class Next>
|
Chris@16
|
69 struct solve_first_non_zero {
|
Chris@16
|
70 template<class Begin>
|
Chris@16
|
71 struct apply {
|
Chris@16
|
72 typedef typename Next::template apply<
|
Chris@16
|
73 typename Begin::next,
|
Chris@16
|
74 typename Begin::item
|
Chris@16
|
75 >::type type;
|
Chris@16
|
76 };
|
Chris@16
|
77 };
|
Chris@16
|
78
|
Chris@16
|
79 template<class Next>
|
Chris@16
|
80 struct solve_internal_zero {
|
Chris@16
|
81 template<class Begin, class Y>
|
Chris@16
|
82 struct apply {
|
Chris@16
|
83 typedef list<
|
Chris@16
|
84 typename Begin::item,
|
Chris@16
|
85 typename Next::template apply<typename Begin::next, Y>::type
|
Chris@16
|
86 > type;
|
Chris@16
|
87 };
|
Chris@16
|
88 };
|
Chris@16
|
89
|
Chris@16
|
90 template<class T>
|
Chris@16
|
91 struct make_solve_list_internal_zero {
|
Chris@16
|
92 template<class Next, class X>
|
Chris@16
|
93 struct apply {
|
Chris@16
|
94 typedef solve_normal<T, X, Next> type;
|
Chris@16
|
95 };
|
Chris@16
|
96 };
|
Chris@16
|
97
|
Chris@16
|
98 template<>
|
Chris@16
|
99 struct make_solve_list_internal_zero<static_rational<0> > {
|
Chris@16
|
100 template<class Next, class X>
|
Chris@16
|
101 struct apply {
|
Chris@16
|
102 typedef solve_internal_zero<Next> type;
|
Chris@16
|
103 };
|
Chris@16
|
104 };
|
Chris@16
|
105
|
Chris@16
|
106 template<int N>
|
Chris@16
|
107 struct make_solve_list_normal {
|
Chris@16
|
108 template<class Begin, class X>
|
Chris@16
|
109 struct apply {
|
Chris@16
|
110 typedef typename make_solve_list_internal_zero<
|
Chris@16
|
111 typename Begin::item
|
Chris@16
|
112 >::template apply<
|
Chris@16
|
113 typename make_solve_list_normal<N-1>::template apply<typename Begin::next, X>::type,
|
Chris@16
|
114 X
|
Chris@16
|
115 >::type type;
|
Chris@16
|
116 };
|
Chris@16
|
117 };
|
Chris@16
|
118
|
Chris@16
|
119 template<>
|
Chris@16
|
120 struct make_solve_list_normal<0> {
|
Chris@16
|
121 template<class Begin, class X>
|
Chris@16
|
122 struct apply {
|
Chris@16
|
123 typedef solve_end type;
|
Chris@16
|
124 };
|
Chris@16
|
125 };
|
Chris@16
|
126
|
Chris@16
|
127 template<int N>
|
Chris@16
|
128 struct make_solve_list_leading_zeroes;
|
Chris@16
|
129
|
Chris@16
|
130 template<class T>
|
Chris@16
|
131 struct make_solve_list_first_non_zero {
|
Chris@16
|
132 template<class Begin, int N>
|
Chris@16
|
133 struct apply {
|
Chris@16
|
134 typedef solve_first_non_zero<
|
Chris@16
|
135 typename make_solve_list_normal<N-1>::template apply<
|
Chris@16
|
136 typename Begin::next,
|
Chris@16
|
137 typename Begin::item
|
Chris@16
|
138 >::type
|
Chris@16
|
139 > type;
|
Chris@16
|
140 };
|
Chris@16
|
141 };
|
Chris@16
|
142
|
Chris@16
|
143 template<>
|
Chris@16
|
144 struct make_solve_list_first_non_zero<static_rational<0> > {
|
Chris@16
|
145 template<class Begin, int N>
|
Chris@16
|
146 struct apply {
|
Chris@16
|
147 typedef typename solve_leading_zeroes<
|
Chris@16
|
148 typename make_solve_list_leading_zeroes<N-1>::template apply<
|
Chris@16
|
149 typename Begin::next
|
Chris@16
|
150 >::type
|
Chris@16
|
151 >::type type;
|
Chris@16
|
152 };
|
Chris@16
|
153 };
|
Chris@16
|
154
|
Chris@16
|
155 template<int N>
|
Chris@16
|
156 struct make_solve_list_leading_zeroes {
|
Chris@16
|
157 template<class Begin>
|
Chris@16
|
158 struct apply {
|
Chris@16
|
159 typedef typename make_solve_list_first_non_zero<typename Begin::item>::template apply<Begin, N>::type type;
|
Chris@16
|
160 };
|
Chris@16
|
161 };
|
Chris@16
|
162
|
Chris@16
|
163 template<>
|
Chris@16
|
164 struct make_solve_list_leading_zeroes<0> {
|
Chris@16
|
165 template<class Begin>
|
Chris@16
|
166 struct apply {
|
Chris@16
|
167 typedef no_solution type;
|
Chris@16
|
168 };
|
Chris@16
|
169 };
|
Chris@16
|
170
|
Chris@16
|
171 template<int N>
|
Chris@16
|
172 struct try_add_unit_impl {
|
Chris@16
|
173 template<class Begin, class L>
|
Chris@16
|
174 struct apply {
|
Chris@16
|
175 typedef typename try_add_unit_impl<N-1>::template apply<typename Begin::next, L>::type next;
|
Chris@16
|
176 typedef typename Begin::item::template apply<next>::type type;
|
Chris@16
|
177 BOOST_STATIC_ASSERT((next::size::value - 1 == type::size::value));
|
Chris@16
|
178 };
|
Chris@16
|
179 };
|
Chris@16
|
180
|
Chris@16
|
181 template<>
|
Chris@16
|
182 struct try_add_unit_impl<0> {
|
Chris@16
|
183 template<class Begin, class L>
|
Chris@16
|
184 struct apply {
|
Chris@16
|
185 typedef L type;
|
Chris@16
|
186 };
|
Chris@16
|
187 };
|
Chris@16
|
188
|
Chris@16
|
189 template<int N>
|
Chris@16
|
190 struct make_homogeneous_system_impl;
|
Chris@16
|
191
|
Chris@16
|
192 template<class T, bool is_done>
|
Chris@16
|
193 struct make_homogeneous_system_func;
|
Chris@16
|
194
|
Chris@16
|
195 template<class T>
|
Chris@16
|
196 struct make_homogeneous_system_func<T, false> {
|
Chris@16
|
197 template<class Begin, class Current, class Units, class Dimensions, int N>
|
Chris@16
|
198 struct apply {
|
Chris@16
|
199 typedef typename make_homogeneous_system_impl<N-1>::template apply<
|
Chris@16
|
200 typename Begin::next,
|
Chris@16
|
201 list<T, Current>,
|
Chris@16
|
202 list<typename Begin::item, Units>,
|
Chris@16
|
203 Dimensions
|
Chris@16
|
204 >::type type;
|
Chris@16
|
205 };
|
Chris@16
|
206 };
|
Chris@16
|
207
|
Chris@16
|
208 template<class T>
|
Chris@16
|
209 struct make_homogeneous_system_func<T, true> {
|
Chris@16
|
210 template<class Begin, class Current, class Units, class Dimensions, int N>
|
Chris@16
|
211 struct apply {
|
Chris@16
|
212 typedef list<typename Begin::item, Units> type;
|
Chris@16
|
213 };
|
Chris@16
|
214 };
|
Chris@16
|
215
|
Chris@16
|
216 template<>
|
Chris@16
|
217 struct make_homogeneous_system_func<no_solution, false> {
|
Chris@16
|
218 template<class Begin, class Current, class Units, class Dimensions, int N>
|
Chris@16
|
219 struct apply {
|
Chris@16
|
220 typedef typename make_homogeneous_system_impl<N-1>::template apply<
|
Chris@16
|
221 typename Begin::next,
|
Chris@16
|
222 Current,
|
Chris@16
|
223 Units,
|
Chris@16
|
224 Dimensions
|
Chris@16
|
225 >::type type;
|
Chris@16
|
226 };
|
Chris@16
|
227 };
|
Chris@16
|
228
|
Chris@16
|
229 template<>
|
Chris@16
|
230 struct make_homogeneous_system_func<no_solution, true> {
|
Chris@16
|
231 template<class Begin, class Current, class Units, class Dimensions, int N>
|
Chris@16
|
232 struct apply {
|
Chris@16
|
233 typedef typename make_homogeneous_system_impl<N-1>::template apply<
|
Chris@16
|
234 typename Begin::next,
|
Chris@16
|
235 Current,
|
Chris@16
|
236 Units,
|
Chris@16
|
237 Dimensions
|
Chris@16
|
238 >::type type;
|
Chris@16
|
239 };
|
Chris@16
|
240 };
|
Chris@16
|
241
|
Chris@16
|
242 template<int N>
|
Chris@16
|
243 struct make_homogeneous_system_impl {
|
Chris@16
|
244 template<class Begin, class Current, class Units, class Dimensions>
|
Chris@16
|
245 struct apply {
|
Chris@16
|
246 typedef typename expand_dimensions<Dimensions::size::value>::template apply<
|
Chris@16
|
247 Dimensions,
|
Chris@16
|
248 typename Begin::item::dimension_type
|
Chris@16
|
249 >::type dimensions;
|
Chris@16
|
250 typedef typename try_add_unit_impl<Current::size::value>::template apply<Current, dimensions>::type new_element;
|
Chris@16
|
251 typedef typename make_solve_list_leading_zeroes<new_element::size::value>::template apply<new_element>::type new_func;
|
Chris@16
|
252 typedef typename make_homogeneous_system_func<
|
Chris@16
|
253 new_func,
|
Chris@16
|
254 ((Current::size::value)+1) == (Dimensions::size::value)
|
Chris@16
|
255 >::template apply<Begin, Current, Units, Dimensions, N>::type type;
|
Chris@16
|
256 };
|
Chris@16
|
257 };
|
Chris@16
|
258
|
Chris@16
|
259 template<>
|
Chris@16
|
260 struct make_homogeneous_system_impl<0> {
|
Chris@16
|
261 template<class Begin, class Current, class Units, class Dimensions>
|
Chris@16
|
262 struct apply {
|
Chris@16
|
263 typedef Units type;
|
Chris@16
|
264 };
|
Chris@16
|
265 };
|
Chris@16
|
266
|
Chris@16
|
267 template<class Units>
|
Chris@16
|
268 struct make_homogeneous_system {
|
Chris@16
|
269 typedef typename find_base_dimensions<Units>::type base_dimensions;
|
Chris@16
|
270 typedef homogeneous_system<
|
Chris@16
|
271 typename insertion_sort<
|
Chris@16
|
272 typename make_homogeneous_system_impl<
|
Chris@16
|
273 Units::size::value
|
Chris@16
|
274 >::template apply<
|
Chris@16
|
275 Units,
|
Chris@16
|
276 dimensionless_type,
|
Chris@16
|
277 dimensionless_type,
|
Chris@16
|
278 base_dimensions
|
Chris@16
|
279 >::type
|
Chris@16
|
280 >::type
|
Chris@16
|
281 > type;
|
Chris@16
|
282 };
|
Chris@16
|
283
|
Chris@16
|
284 template<int N>
|
Chris@16
|
285 struct extract_base_units {
|
Chris@16
|
286 template<class Begin, class T>
|
Chris@16
|
287 struct apply {
|
Chris@16
|
288 typedef list<
|
Chris@16
|
289 typename Begin::item::tag_type,
|
Chris@16
|
290 typename extract_base_units<N-1>::template apply<typename Begin::next, T>::type
|
Chris@16
|
291 > type;
|
Chris@16
|
292 };
|
Chris@16
|
293 };
|
Chris@16
|
294
|
Chris@16
|
295 template<>
|
Chris@16
|
296 struct extract_base_units<0> {
|
Chris@16
|
297 template<class Begin, class T>
|
Chris@16
|
298 struct apply {
|
Chris@16
|
299 typedef T type;
|
Chris@16
|
300 };
|
Chris@16
|
301 };
|
Chris@16
|
302
|
Chris@16
|
303 }
|
Chris@16
|
304
|
Chris@16
|
305 }
|
Chris@16
|
306
|
Chris@16
|
307 }
|
Chris@16
|
308
|
Chris@16
|
309 #endif
|