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) 2007-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_DIMENSION_HPP
12 #define BOOST_UNITS_DIMENSION_HPP
14 #include <boost/static_assert.hpp>
16 #include <boost/type_traits/is_same.hpp>
18 #include <boost/mpl/arithmetic.hpp>
20 #include <boost/units/static_rational.hpp>
21 #include <boost/units/detail/dimension_list.hpp>
22 #include <boost/units/detail/dimension_impl.hpp>
25 /// \brief Core metaprogramming utilities for compile-time dimensional analysis.
31 /// Reduce dimension list to cardinal form. This algorithm collapses duplicate
32 /// base dimension tags and sorts the resulting list by the tag ordinal value.
33 /// Dimension lists that resolve to the same dimension are guaranteed to be
34 /// represented by an identical type.
36 /// The argument should be an MPL forward sequence containing instances
37 /// of the @c dim template.
39 /// The result is also an MPL forward sequence. It also supports the
40 /// following metafunctions to allow use as a dimension.
42 /// - @c mpl::plus is defined only on two equal dimensions and returns the argument unchanged.
43 /// - @c mpl::minus is defined only for two equal dimensions and returns the argument unchanged.
44 /// - @c mpl::negate will return its argument unchanged.
45 /// - @c mpl::times is defined for any dimensions and adds corresponding exponents.
46 /// - @c mpl::divides is defined for any dimensions and subtracts the exponents of the
47 /// right had argument from the corresponding exponents of the left had argument.
48 /// Missing base dimension tags are assumed to have an exponent of zero.
49 /// - @c static_power takes a dimension and a static_rational and multiplies all
50 /// the exponents of the dimension by the static_rational.
51 /// - @c static_root takes a dimension and a static_rational and divides all
52 /// the exponents of the dimension by the static_rational.
53 template<typename Seq>
54 struct make_dimension_list
56 typedef typename detail::sort_dims<Seq>::type type;
59 /// Raise a dimension list to a scalar power.
60 template<typename DL,typename Ex>
63 typedef typename detail::static_power_impl<DL::size::value>::template apply<
69 /// Take a scalar root of a dimension list.
70 template<typename DL,typename Rt>
73 typedef typename detail::static_root_impl<DL::size::value>::template apply<
81 #ifndef BOOST_UNITS_DOXYGEN
86 struct plus_impl<boost::units::detail::dimension_list_tag,boost::units::detail::dimension_list_tag>
88 template<class T0, class T1>
91 BOOST_STATIC_ASSERT((boost::is_same<T0,T1>::value == true));
97 struct minus_impl<boost::units::detail::dimension_list_tag,boost::units::detail::dimension_list_tag>
99 template<class T0, class T1>
102 BOOST_STATIC_ASSERT((boost::is_same<T0,T1>::value == true));
108 struct times_impl<boost::units::detail::dimension_list_tag,boost::units::detail::dimension_list_tag>
110 template<class T0, class T1>
113 typedef typename boost::units::detail::merge_dimensions<T0,T1>::type type;
118 struct divides_impl<boost::units::detail::dimension_list_tag,boost::units::detail::dimension_list_tag>
120 template<class T0, class T1>
123 typedef typename boost::units::detail::merge_dimensions<
125 typename boost::units::detail::static_inverse_impl<
135 struct negate_impl<boost::units::detail::dimension_list_tag>
150 #endif // BOOST_UNITS_DIMENSION_HPP