push_front_or_add.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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) 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. #ifndef BOOST_UNITS_DETAIL_PUSH_FRONT_OR_ADD_HPP
  11. #define BOOST_UNITS_DETAIL_PUSH_FRONT_OR_ADD_HPP
  12. #include <boost/mpl/plus.hpp>
  13. #include <boost/mpl/front.hpp>
  14. #include <boost/mpl/push_front.hpp>
  15. #include <boost/mpl/pop_front.hpp>
  16. #include <boost/type_traits/is_same.hpp>
  17. #include <boost/units/units_fwd.hpp>
  18. #include <boost/units/detail/push_front_if.hpp>
  19. namespace boost {
  20. namespace units {
  21. template<class Item, class Next>
  22. struct list;
  23. namespace detail {
  24. template<class T>
  25. struct is_empty_dim;
  26. /// add an instantiation of dim to Sequence.
  27. template<bool>
  28. struct push_front_or_add_impl;
  29. template<>
  30. struct push_front_or_add_impl<true>
  31. {
  32. template<typename Sequence, typename T>
  33. struct apply
  34. {
  35. typedef typename mpl::plus<T, typename Sequence::item>::type item;
  36. typedef typename push_front_if<!is_empty_dim<item>::value>::template apply<
  37. typename Sequence::next,
  38. item
  39. > type;
  40. };
  41. };
  42. template<>
  43. struct push_front_or_add_impl<false>
  44. {
  45. template<typename Sequence, typename T>
  46. struct apply
  47. {
  48. typedef list<T, Sequence> type;
  49. };
  50. };
  51. template<typename Sequence, typename T>
  52. struct push_front_or_add
  53. {
  54. typedef typename push_front_or_add_impl<boost::is_same<typename T::tag_type, typename Sequence::item::tag_type>::value>::template apply<
  55. Sequence,
  56. T
  57. >::type type;
  58. };
  59. template<typename T>
  60. struct push_front_or_add<dimensionless_type, T>
  61. {
  62. typedef list<T, dimensionless_type> type;
  63. };
  64. } // namespace detail
  65. } // namespace units
  66. } // namespace boost
  67. #endif