base_unit.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 unit (meter, kg, sec...).
  12. /// \details base unit definition registration.
  13. #ifndef BOOST_UNITS_BASE_UNIT_HPP
  14. #define BOOST_UNITS_BASE_UNIT_HPP
  15. #include <boost/units/config.hpp>
  16. #include <boost/units/heterogeneous_system.hpp>
  17. #include <boost/units/static_rational.hpp>
  18. #include <boost/units/units_fwd.hpp>
  19. #include <boost/units/unit.hpp>
  20. #include <boost/units/detail/dimension_list.hpp>
  21. #include <boost/units/detail/ordinal.hpp>
  22. #include <boost/units/detail/prevent_redefinition.hpp>
  23. namespace boost {
  24. namespace units {
  25. /// This must be in namespace boost::units so that ADL
  26. /// will work with friend functions defined inline.
  27. /// Base dimensions and base units are independent.
  28. /// INTERNAL ONLY
  29. template<long N> struct base_unit_ordinal { };
  30. /// INTERNAL ONLY
  31. template<class T, long N> struct base_unit_pair { };
  32. /// INTERNAL ONLY
  33. template<class T, long N>
  34. struct check_base_unit {
  35. enum {
  36. value =
  37. sizeof(boost_units_unit_is_registered(units::base_unit_ordinal<N>())) == sizeof(detail::yes) &&
  38. sizeof(boost_units_unit_is_registered(units::base_unit_pair<T, N>())) != sizeof(detail::yes)
  39. };
  40. };
  41. /// Defines a base unit. To define a unit you need to provide
  42. /// the derived class (CRTP), a dimension list and a unique integer.
  43. /// @code
  44. /// struct my_unit : boost::units::base_unit<my_unit, length_dimension, 1> {};
  45. /// @endcode
  46. /// It is designed so that you will get an error message if you try
  47. /// to use the same value in multiple definitions.
  48. template<class Derived,
  49. class Dim,
  50. long N
  51. #if !defined(BOOST_UNITS_DOXYGEN) && !defined(__BORLANDC__)
  52. ,
  53. class = typename detail::ordinal_has_already_been_defined<
  54. check_base_unit<Derived, N>::value
  55. >::type
  56. #endif
  57. >
  58. class base_unit :
  59. public ordinal<N>
  60. {
  61. public:
  62. /// INTERNAL ONLY
  63. typedef void boost_units_is_base_unit_type;
  64. /// INTERNAL ONLY
  65. typedef base_unit this_type;
  66. /// The dimensions of this base unit.
  67. typedef Dim dimension_type;
  68. /// Provided for mpl compatability.
  69. typedef Derived type;
  70. /// The unit corresponding to this base unit.
  71. #ifndef BOOST_UNITS_DOXYGEN
  72. typedef unit<
  73. Dim,
  74. heterogeneous_system<
  75. heterogeneous_system_impl<
  76. list<
  77. heterogeneous_system_dim<Derived,static_rational<1> >,
  78. dimensionless_type
  79. >,
  80. Dim,
  81. no_scale
  82. >
  83. >
  84. > unit_type;
  85. #else
  86. typedef detail::unspecified unit_type;
  87. #endif
  88. private:
  89. /// Check for C++0x. In C++0x, we have to have identical
  90. /// arguments but a different return type to trigger an
  91. /// error. Note that this is only needed for clang as
  92. /// check_base_unit will trigger an error earlier
  93. /// for compilers with less strict name lookup.
  94. /// INTERNAL ONLY
  95. friend BOOST_CONSTEXPR Derived*
  96. check_double_register(const units::base_unit_ordinal<N>&)
  97. { return(0); }
  98. /// Register this ordinal
  99. /// INTERNAL ONLY
  100. friend BOOST_CONSTEXPR detail::yes
  101. boost_units_unit_is_registered(const units::base_unit_ordinal<N>&)
  102. { return(detail::yes()); }
  103. /// But make sure we can identify the current instantiation!
  104. /// INTERNAL ONLY
  105. friend BOOST_CONSTEXPR detail::yes
  106. boost_units_unit_is_registered(const units::base_unit_pair<Derived, N>&)
  107. { return(detail::yes()); }
  108. };
  109. } // namespace units
  110. } // namespace boost
  111. #endif // BOOST_UNITS_BASE_UNIT_HPP