base_dimension.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /// \file
  11. /// \brief base dimensions (mass, length, time...).
  12. /// \details base dimension definition registration.
  13. #ifndef BOOST_UNITS_BASE_DIMENSION_HPP
  14. #define BOOST_UNITS_BASE_DIMENSION_HPP
  15. #include <boost/units/config.hpp>
  16. #include <boost/units/dim.hpp>
  17. #include <boost/units/static_rational.hpp>
  18. #include <boost/units/units_fwd.hpp>
  19. #include <boost/units/detail/dimension_list.hpp>
  20. #include <boost/units/detail/ordinal.hpp>
  21. #include <boost/units/detail/prevent_redefinition.hpp>
  22. namespace boost {
  23. namespace units {
  24. /// This must be in namespace boost::units so that ADL
  25. /// will work with friend functions defined inline.
  26. /// INTERNAL ONLY
  27. template<long N> struct base_dimension_ordinal { };
  28. /// INTERNAL ONLY
  29. template<class T, long N> struct base_dimension_pair { };
  30. /// INTERNAL ONLY
  31. template<class T, long N>
  32. struct check_base_dimension {
  33. enum {
  34. value =
  35. sizeof(boost_units_is_registered(units::base_dimension_ordinal<N>())) == sizeof(detail::yes) &&
  36. sizeof(boost_units_is_registered(units::base_dimension_pair<T, N>())) != sizeof(detail::yes)
  37. };
  38. };
  39. /// Defines a base dimension. To define a dimension you need to provide
  40. /// the derived class (CRTP) and a unique integer.
  41. /// @code
  42. /// struct my_dimension : boost::units::base_dimension<my_dimension, 1> {};
  43. /// @endcode
  44. /// It is designed so that you will get an error message if you try
  45. /// to use the same value in multiple definitions.
  46. template<class Derived,
  47. long N
  48. #if !defined(BOOST_UNITS_DOXYGEN) && !defined(__BORLANDC__)
  49. ,
  50. class = typename detail::ordinal_has_already_been_defined<
  51. check_base_dimension<Derived, N>::value
  52. >::type
  53. #endif
  54. >
  55. class base_dimension :
  56. public ordinal<N>
  57. {
  58. public:
  59. /// INTERNAL ONLY
  60. typedef base_dimension this_type;
  61. /// A convenience typedef. Equivalent to boost::units::derived_dimension<Derived,1>::type.
  62. #ifndef BOOST_UNITS_DOXYGEN
  63. typedef list<dim<Derived,static_rational<1> >, dimensionless_type> dimension_type;
  64. #else
  65. typedef detail::unspecified dimension_type;
  66. #endif
  67. /// Provided for mpl compatability.
  68. typedef Derived type;
  69. private:
  70. /// Check for C++0x. In C++0x, we have to have identical
  71. /// arguments but a different return type to trigger an
  72. /// error. Note that this is only needed for clang as
  73. /// check_base_dimension will trigger an error earlier
  74. /// for compilers with less strict name lookup.
  75. /// INTERNAL ONLY
  76. friend BOOST_CONSTEXPR Derived*
  77. check_double_register(const units::base_dimension_ordinal<N>&)
  78. { return(0); }
  79. /// Register this ordinal
  80. /// INTERNAL ONLY
  81. friend BOOST_CONSTEXPR detail::yes
  82. boost_units_is_registered(const units::base_dimension_ordinal<N>&)
  83. { return(detail::yes()); }
  84. /// But make sure we can identify the current instantiation!
  85. /// INTERNAL ONLY
  86. friend BOOST_CONSTEXPR detail::yes
  87. boost_units_is_registered(const units::base_dimension_pair<Derived, N>&)
  88. { return(detail::yes()); }
  89. };
  90. } // namespace units
  91. } // namespace boost
  92. #endif // BOOST_UNITS_BASE_DIMENSION_HPP