| 1 | // Boost.Units - A C++ library for zero-overhead dimensional analysis and |
| 2 | // unit/quantity manipulation and conversion |
| 3 | // |
| 4 | // Copyright (C) 2003-2008 Matthias Christian Schabel |
| 5 | // Copyright (C) 2007-2008 Steven Watanabe |
| 6 | // |
| 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) |
| 10 | |
| 11 | #ifndef BOOST_UNITS_HETEROGENEOUS_SYSTEM_HPP |
| 12 | #define BOOST_UNITS_HETEROGENEOUS_SYSTEM_HPP |
| 13 | |
| 14 | /// \file |
| 15 | /// \brief A heterogeneous system is a sorted list of base unit/exponent pairs. |
| 16 | |
| 17 | #include <boost/mpl/bool.hpp> |
| 18 | #include <boost/mpl/plus.hpp> |
| 19 | #include <boost/mpl/times.hpp> |
| 20 | #include <boost/mpl/divides.hpp> |
| 21 | #include <boost/mpl/negate.hpp> |
| 22 | #include <boost/mpl/less.hpp> |
| 23 | #include <boost/mpl/size.hpp> |
| 24 | #include <boost/mpl/begin.hpp> |
| 25 | #include <boost/mpl/next.hpp> |
| 26 | #include <boost/mpl/deref.hpp> |
| 27 | #include <boost/mpl/front.hpp> |
| 28 | #include <boost/mpl/push_front.hpp> |
| 29 | #include <boost/mpl/pop_front.hpp> |
| 30 | #include <boost/mpl/assert.hpp> |
| 31 | #include <boost/type_traits/is_same.hpp> |
| 32 | |
| 33 | #include <boost/units/config.hpp> |
| 34 | #include <boost/units/static_rational.hpp> |
| 35 | #include <boost/units/dimension.hpp> |
| 36 | #include <boost/units/units_fwd.hpp> |
| 37 | #include <boost/units/detail/push_front_if.hpp> |
| 38 | #include <boost/units/detail/push_front_or_add.hpp> |
| 39 | #include <boost/units/detail/linear_algebra.hpp> |
| 40 | #include <boost/units/detail/unscale.hpp> |
| 41 | |
| 42 | namespace boost { |
| 43 | |
| 44 | namespace units { |
| 45 | |
| 46 | namespace detail { |
| 47 | |
| 48 | // A normal system is a sorted list of base units. |
| 49 | // A heterogeneous system is a sorted list of base unit/exponent pairs. |
| 50 | // As long as we don't need to convert heterogeneous systems |
| 51 | // directly everything is cool. |
| 52 | |
| 53 | template<class T> |
| 54 | struct is_zero : mpl::false_ {}; |
| 55 | |
| 56 | template<> |
| 57 | struct is_zero<static_rational<0> > : mpl::true_ {}; |
| 58 | |
| 59 | } // namespace detail |
| 60 | |
| 61 | /// INTERNAL ONLY |
| 62 | template<class L, class Dimensions, class Scale> |
| 63 | struct heterogeneous_system_impl |
| 64 | { |
| 65 | typedef L type; |
| 66 | typedef Dimensions dimensions; |
| 67 | typedef Scale scale; |
| 68 | }; |
| 69 | |
| 70 | /// INTERNAL ONLY |
| 71 | typedef dimensionless_type no_scale; |
| 72 | |
| 73 | /// A system that can represent any possible combination |
| 74 | /// of units at the expense of not preserving information |
| 75 | /// about how it was created. Do not create specializations |
| 76 | /// of this template directly. Instead use @c reduce_unit and |
| 77 | /// @c base_unit<...>::unit_type. |
| 78 | template<class T> |
| 79 | struct heterogeneous_system : T {}; |
| 80 | |
| 81 | /// INTERNAL ONLY |
| 82 | struct heterogeneous_system_dim_tag {}; |
| 83 | |
| 84 | /// INTERNAL ONLY |
| 85 | template<class Unit, class Exponent> |
| 86 | struct heterogeneous_system_dim |
| 87 | { |
| 88 | typedef heterogeneous_system_dim_tag tag; |
| 89 | typedef heterogeneous_system_dim type; |
| 90 | typedef Unit tag_type; |
| 91 | typedef Exponent value_type; |
| 92 | }; |
| 93 | |
| 94 | /// INTERNAL ONLY |
| 95 | #define BOOST_UNITS_MAKE_HETEROGENEOUS_UNIT(BaseUnit, Dimensions) \ |
| 96 | boost::units::unit< \ |
| 97 | Dimensions, \ |
| 98 | boost::units::heterogeneous_system< \ |
| 99 | boost::units::heterogeneous_system_impl< \ |
| 100 | boost::units::list< \ |
| 101 | boost::units::heterogeneous_system_dim< \ |
| 102 | BaseUnit, \ |
| 103 | boost::units::static_rational<1> \ |
| 104 | >, \ |
| 105 | boost::units::dimensionless_type \ |
| 106 | >, \ |
| 107 | Dimensions, \ |
| 108 | boost::units::no_scale \ |
| 109 | > \ |
| 110 | > \ |
| 111 | > |
| 112 | |
| 113 | } // namespace units |
| 114 | |
| 115 | } // namespace boost |
| 116 | |
| 117 | |
| 118 | #if BOOST_UNITS_HAS_BOOST_TYPEOF |
| 119 | |
| 120 | #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP() |
| 121 | |
| 122 | BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::heterogeneous_system_impl, (class)(class)(class)) |
| 123 | BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::heterogeneous_system, (class)) |
| 124 | BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::heterogeneous_system_dim, (class)(class)) |
| 125 | |
| 126 | #endif |
| 127 | |
| 128 | namespace boost { |
| 129 | |
| 130 | namespace mpl { |
| 131 | |
| 132 | /// INTERNAL ONLY |
| 133 | template<> |
| 134 | struct less_impl<boost::units::heterogeneous_system_dim_tag, boost::units::heterogeneous_system_dim_tag> |
| 135 | { |
| 136 | template<class T0, class T1> |
| 137 | struct apply : mpl::less<typename T0::tag_type, typename T1::tag_type> {}; |
| 138 | }; |
| 139 | |
| 140 | } |
| 141 | |
| 142 | namespace units { |
| 143 | |
| 144 | namespace detail { |
| 145 | |
| 146 | template<class Unit1, class Exponent1> |
| 147 | struct is_empty_dim<heterogeneous_system_dim<Unit1,Exponent1> > : detail::is_zero<Exponent1> {}; |
| 148 | |
| 149 | } // namespace detail |
| 150 | |
| 151 | } // namespace units |
| 152 | |
| 153 | namespace mpl { |
| 154 | |
| 155 | /// INTERNAL ONLY |
| 156 | template<> |
| 157 | struct plus_impl<boost::units::heterogeneous_system_dim_tag, boost::units::heterogeneous_system_dim_tag> |
| 158 | { |
| 159 | template<class T0, class T1> |
| 160 | struct apply |
| 161 | { |
| 162 | typedef boost::units::heterogeneous_system_dim< |
| 163 | typename T0::tag_type, |
| 164 | typename mpl::plus<typename T0::value_type,typename T1::value_type>::type |
| 165 | > type; |
| 166 | }; |
| 167 | }; |
| 168 | |
| 169 | /// INTERNAL ONLY |
| 170 | template<> |
| 171 | struct times_impl<boost::units::heterogeneous_system_dim_tag, boost::units::detail::static_rational_tag> |
| 172 | { |
| 173 | template<class T0, class T1> |
| 174 | struct apply |
| 175 | { |
| 176 | typedef boost::units::heterogeneous_system_dim< |
| 177 | typename T0::tag_type, |
| 178 | typename mpl::times<typename T0::value_type,T1>::type |
| 179 | > type; |
| 180 | }; |
| 181 | }; |
| 182 | |
| 183 | /// INTERNAL ONLY |
| 184 | template<> |
| 185 | struct divides_impl<boost::units::heterogeneous_system_dim_tag, boost::units::detail::static_rational_tag> |
| 186 | { |
| 187 | template<class T0, class T1> |
| 188 | struct apply |
| 189 | { |
| 190 | typedef boost::units::heterogeneous_system_dim< |
| 191 | typename T0::tag_type, |
| 192 | typename mpl::divides<typename T0::value_type,T1>::type |
| 193 | > type; |
| 194 | }; |
| 195 | }; |
| 196 | |
| 197 | /// INTERNAL ONLY |
| 198 | template<> |
| 199 | struct negate_impl<boost::units::heterogeneous_system_dim_tag> |
| 200 | { |
| 201 | template<class T> |
| 202 | struct apply |
| 203 | { |
| 204 | typedef boost::units::heterogeneous_system_dim<typename T::tag_type, typename mpl::negate<typename T::value_type>::type> type; |
| 205 | }; |
| 206 | }; |
| 207 | |
| 208 | } // namespace mpl |
| 209 | |
| 210 | namespace units { |
| 211 | |
| 212 | namespace detail { |
| 213 | |
| 214 | template<int N> |
| 215 | struct make_heterogeneous_system_impl |
| 216 | { |
| 217 | template<class UnitsBegin, class ExponentsBegin> |
| 218 | struct apply |
| 219 | { |
| 220 | typedef typename push_front_if<!(is_zero<typename ExponentsBegin::item>::value)>::template apply< |
| 221 | typename make_heterogeneous_system_impl<N-1>::template apply< |
| 222 | typename UnitsBegin::next, |
| 223 | typename ExponentsBegin::next |
| 224 | >::type, |
| 225 | heterogeneous_system_dim<typename UnitsBegin::item, typename ExponentsBegin::item> |
| 226 | >::type type; |
| 227 | }; |
| 228 | }; |
| 229 | |
| 230 | template<> |
| 231 | struct make_heterogeneous_system_impl<0> |
| 232 | { |
| 233 | template<class UnitsBegin, class ExponentsBegin> |
| 234 | struct apply |
| 235 | { |
| 236 | typedef dimensionless_type type; |
| 237 | }; |
| 238 | }; |
| 239 | |
| 240 | template<class Dimensions, class System> |
| 241 | struct make_heterogeneous_system |
| 242 | { |
| 243 | typedef typename calculate_base_unit_exponents<typename System::type, Dimensions>::type exponents; |
| 244 | BOOST_MPL_ASSERT_MSG((!boost::is_same<exponents, inconsistent>::value), the_specified_dimension_is_not_representible_in_the_given_system, (types<Dimensions, System>)); |
| 245 | typedef typename make_heterogeneous_system_impl<System::type::size::value>::template apply< |
| 246 | typename System::type, |
| 247 | exponents |
| 248 | >::type unit_list; |
| 249 | typedef heterogeneous_system<heterogeneous_system_impl<unit_list, Dimensions, no_scale> > type; |
| 250 | }; |
| 251 | |
| 252 | template<class Dimensions, class T> |
| 253 | struct make_heterogeneous_system<Dimensions, heterogeneous_system<T> > |
| 254 | { |
| 255 | typedef heterogeneous_system<T> type; |
| 256 | }; |
| 257 | |
| 258 | template<class T0, class T1> |
| 259 | struct multiply_systems |
| 260 | { |
| 261 | typedef heterogeneous_system< |
| 262 | heterogeneous_system_impl< |
| 263 | typename mpl::times<typename T0::type, typename T1::type>::type, |
| 264 | typename mpl::times<typename T0::dimensions, typename T1::dimensions>::type, |
| 265 | typename mpl::times<typename T0::scale, typename T1::scale>::type |
| 266 | > |
| 267 | > type; |
| 268 | }; |
| 269 | |
| 270 | template<class T0, class T1> |
| 271 | struct divide_systems |
| 272 | { |
| 273 | typedef heterogeneous_system< |
| 274 | heterogeneous_system_impl< |
| 275 | typename mpl::divides<typename T0::type, typename T1::type>::type, |
| 276 | typename mpl::divides<typename T0::dimensions, typename T1::dimensions>::type, |
| 277 | typename mpl::divides<typename T0::scale, typename T1::scale>::type |
| 278 | > |
| 279 | > type; |
| 280 | }; |
| 281 | |
| 282 | } // namespace detail |
| 283 | |
| 284 | /// INTERNAL ONLY |
| 285 | template<class S, long N, long D> |
| 286 | struct static_power<heterogeneous_system<S>, static_rational<N,D> > |
| 287 | { |
| 288 | typedef heterogeneous_system< |
| 289 | heterogeneous_system_impl< |
| 290 | typename static_power<typename S::type, static_rational<N,D> >::type, |
| 291 | typename static_power<typename S::dimensions, static_rational<N,D> >::type, |
| 292 | typename static_power<typename S::scale, static_rational<N,D> >::type |
| 293 | > |
| 294 | > type; |
| 295 | }; |
| 296 | |
| 297 | /// INTERNAL ONLY |
| 298 | template<class S, long N, long D> |
| 299 | struct static_root<heterogeneous_system<S>, static_rational<N,D> > |
| 300 | { |
| 301 | typedef heterogeneous_system< |
| 302 | heterogeneous_system_impl< |
| 303 | typename static_root<typename S::type, static_rational<N,D> >::type, |
| 304 | typename static_root<typename S::dimensions, static_rational<N,D> >::type, |
| 305 | typename static_root<typename S::scale, static_rational<N,D> >::type |
| 306 | > |
| 307 | > type; |
| 308 | }; |
| 309 | |
| 310 | namespace detail { |
| 311 | |
| 312 | template<int N> |
| 313 | struct unscale_heterogeneous_system_impl |
| 314 | { |
| 315 | template<class Begin> |
| 316 | struct apply |
| 317 | { |
| 318 | typedef typename push_front_or_add< |
| 319 | typename unscale_heterogeneous_system_impl<N-1>::template apply< |
| 320 | typename Begin::next |
| 321 | >::type, |
| 322 | typename unscale<typename Begin::item>::type |
| 323 | >::type type; |
| 324 | }; |
| 325 | }; |
| 326 | |
| 327 | template<> |
| 328 | struct unscale_heterogeneous_system_impl<0> |
| 329 | { |
| 330 | template<class Begin> |
| 331 | struct apply |
| 332 | { |
| 333 | typedef dimensionless_type type; |
| 334 | }; |
| 335 | }; |
| 336 | |
| 337 | } // namespace detail |
| 338 | |
| 339 | /// Unscale all the base units. e.g |
| 340 | /// km s -> m s |
| 341 | /// cm km -> m^2 |
| 342 | /// INTERNAL ONLY |
| 343 | template<class T> |
| 344 | struct unscale<heterogeneous_system<T> > |
| 345 | { |
| 346 | typedef heterogeneous_system< |
| 347 | heterogeneous_system_impl< |
| 348 | typename detail::unscale_heterogeneous_system_impl< |
| 349 | T::type::size::value |
| 350 | >::template apply< |
| 351 | typename T::type |
| 352 | >::type, |
| 353 | typename T::dimensions, |
| 354 | no_scale |
| 355 | > |
| 356 | > type; |
| 357 | }; |
| 358 | |
| 359 | /// INTERNAL ONLY |
| 360 | template<class Unit, class Exponent> |
| 361 | struct unscale<heterogeneous_system_dim<Unit, Exponent> > |
| 362 | { |
| 363 | typedef heterogeneous_system_dim<typename unscale<Unit>::type, Exponent> type; |
| 364 | }; |
| 365 | |
| 366 | namespace detail { |
| 367 | |
| 368 | template<int N> |
| 369 | struct get_scale_list_of_heterogeneous_system_impl |
| 370 | { |
| 371 | template<class Begin> |
| 372 | struct apply |
| 373 | { |
| 374 | typedef typename mpl::times< |
| 375 | typename get_scale_list_of_heterogeneous_system_impl<N-1>::template apply< |
| 376 | typename Begin::next |
| 377 | >::type, |
| 378 | typename get_scale_list<typename Begin::item>::type |
| 379 | >::type type; |
| 380 | }; |
| 381 | }; |
| 382 | |
| 383 | template<> |
| 384 | struct get_scale_list_of_heterogeneous_system_impl<0> |
| 385 | { |
| 386 | template<class Begin> |
| 387 | struct apply |
| 388 | { |
| 389 | typedef dimensionless_type type; |
| 390 | }; |
| 391 | }; |
| 392 | |
| 393 | } // namespace detail |
| 394 | |
| 395 | /// INTERNAL ONLY |
| 396 | template<class T> |
| 397 | struct get_scale_list<heterogeneous_system<T> > |
| 398 | { |
| 399 | typedef typename mpl::times< |
| 400 | typename detail::get_scale_list_of_heterogeneous_system_impl< |
| 401 | T::type::size::value |
| 402 | >::template apply<typename T::type>::type, |
| 403 | typename T::scale |
| 404 | >::type type; |
| 405 | }; |
| 406 | |
| 407 | /// INTERNAL ONLY |
| 408 | template<class Unit, class Exponent> |
| 409 | struct get_scale_list<heterogeneous_system_dim<Unit, Exponent> > |
| 410 | { |
| 411 | typedef typename static_power<typename get_scale_list<Unit>::type, Exponent>::type type; |
| 412 | }; |
| 413 | |
| 414 | namespace detail { |
| 415 | |
| 416 | template<class System, class Dimension> |
| 417 | struct check_system : mpl::false_ {}; |
| 418 | |
| 419 | template<class System, class Dimension, class Scale> |
| 420 | struct check_system<heterogeneous_system<heterogeneous_system_impl<System, Dimension, Scale> >, Dimension> : mpl::true_ {}; |
| 421 | |
| 422 | } // namespace detail |
| 423 | |
| 424 | } // namespace units |
| 425 | |
| 426 | } // namespace boost |
| 427 | |
| 428 | #endif |
| 429 | |