1 // Boost.Units - A C++ library for zero-overhead dimensional analysis and
2 // unit/quantity manipulation and conversion
4 // Copyright (C) 2003-2008 Matthias Christian Schabel
5 // Copyright (C) 2008 Steven Watanabe
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_DETAIL_HETEROGENEOUS_CONVERSION_HPP
12 #define BOOST_UNITS_DETAIL_HETEROGENEOUS_CONVERSION_HPP
14 #include <boost/mpl/minus.hpp>
15 #include <boost/mpl/times.hpp>
17 #include <boost/units/static_rational.hpp>
18 #include <boost/units/homogeneous_system.hpp>
19 #include <boost/units/detail/linear_algebra.hpp>
28 template<class Begin, class Y>
30 typedef dimensionless_type type;
34 struct no_solution {};
36 template<class X1, class X2, class Next>
38 template<class Begin, class Y>
40 typedef typename Begin::next next;
43 typename mpl::times<X1, Y>::type,
44 typename mpl::times<X2, typename Begin::item>::type
46 typename Next::template apply<next, Y>::type
52 struct solve_leading_zeroes {
57 typename Next::template apply<typename Begin::next>::type
60 typedef solve_leading_zeroes type;
64 struct solve_leading_zeroes<no_solution> {
65 typedef no_solution type;
69 struct solve_first_non_zero {
72 typedef typename Next::template apply<
80 struct solve_internal_zero {
81 template<class Begin, class Y>
85 typename Next::template apply<typename Begin::next, Y>::type
91 struct make_solve_list_internal_zero {
92 template<class Next, class X>
94 typedef solve_normal<T, X, Next> type;
99 struct make_solve_list_internal_zero<static_rational<0> > {
100 template<class Next, class X>
102 typedef solve_internal_zero<Next> type;
107 struct make_solve_list_normal {
108 template<class Begin, class X>
110 typedef typename make_solve_list_internal_zero<
113 typename make_solve_list_normal<N-1>::template apply<typename Begin::next, X>::type,
120 struct make_solve_list_normal<0> {
121 template<class Begin, class X>
123 typedef solve_end type;
128 struct make_solve_list_leading_zeroes;
131 struct make_solve_list_first_non_zero {
132 template<class Begin, int N>
134 typedef solve_first_non_zero<
135 typename make_solve_list_normal<N-1>::template apply<
136 typename Begin::next,
144 struct make_solve_list_first_non_zero<static_rational<0> > {
145 template<class Begin, int N>
147 typedef typename solve_leading_zeroes<
148 typename make_solve_list_leading_zeroes<N-1>::template apply<
156 struct make_solve_list_leading_zeroes {
157 template<class Begin>
159 typedef typename make_solve_list_first_non_zero<typename Begin::item>::template apply<Begin, N>::type type;
164 struct make_solve_list_leading_zeroes<0> {
165 template<class Begin>
167 typedef no_solution type;
172 struct try_add_unit_impl {
173 template<class Begin, class L>
175 typedef typename try_add_unit_impl<N-1>::template apply<typename Begin::next, L>::type next;
176 typedef typename Begin::item::template apply<next>::type type;
177 BOOST_STATIC_ASSERT((next::size::value - 1 == type::size::value));
182 struct try_add_unit_impl<0> {
183 template<class Begin, class L>
190 struct make_homogeneous_system_impl;
192 template<class T, bool is_done>
193 struct make_homogeneous_system_func;
196 struct make_homogeneous_system_func<T, false> {
197 template<class Begin, class Current, class Units, class Dimensions, int N>
199 typedef typename make_homogeneous_system_impl<N-1>::template apply<
200 typename Begin::next,
202 list<typename Begin::item, Units>,
209 struct make_homogeneous_system_func<T, true> {
210 template<class Begin, class Current, class Units, class Dimensions, int N>
212 typedef list<typename Begin::item, Units> type;
217 struct make_homogeneous_system_func<no_solution, false> {
218 template<class Begin, class Current, class Units, class Dimensions, int N>
220 typedef typename make_homogeneous_system_impl<N-1>::template apply<
221 typename Begin::next,
230 struct make_homogeneous_system_func<no_solution, true> {
231 template<class Begin, class Current, class Units, class Dimensions, int N>
233 typedef typename make_homogeneous_system_impl<N-1>::template apply<
234 typename Begin::next,
243 struct make_homogeneous_system_impl {
244 template<class Begin, class Current, class Units, class Dimensions>
246 typedef typename expand_dimensions<Dimensions::size::value>::template apply<
248 typename Begin::item::dimension_type
250 typedef typename try_add_unit_impl<Current::size::value>::template apply<Current, dimensions>::type new_element;
251 typedef typename make_solve_list_leading_zeroes<new_element::size::value>::template apply<new_element>::type new_func;
252 typedef typename make_homogeneous_system_func<
254 ((Current::size::value)+1) == (Dimensions::size::value)
255 >::template apply<Begin, Current, Units, Dimensions, N>::type type;
260 struct make_homogeneous_system_impl<0> {
261 template<class Begin, class Current, class Units, class Dimensions>
267 template<class Units>
268 struct make_homogeneous_system {
269 typedef typename find_base_dimensions<Units>::type base_dimensions;
270 typedef homogeneous_system<
271 typename insertion_sort<
272 typename make_homogeneous_system_impl<
285 struct extract_base_units {
286 template<class Begin, class T>
289 typename Begin::item::tag_type,
290 typename extract_base_units<N-1>::template apply<typename Begin::next, T>::type
296 struct extract_base_units<0> {
297 template<class Begin, class T>