tagged_integer.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file tagged_integer.hpp
  9. * \author Andrey Semashev
  10. * \date 11.01.2008
  11. *
  12. * \brief This header is the Boost.Log library implementation, see the library documentation
  13. * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
  14. */
  15. #ifndef BOOST_LOG_TAGGED_INTEGER_HPP_INCLUDED_
  16. #define BOOST_LOG_TAGGED_INTEGER_HPP_INCLUDED_
  17. #include <boost/log/detail/config.hpp>
  18. #include <boost/log/detail/header.hpp>
  19. #ifdef BOOST_HAS_PRAGMA_ONCE
  20. #pragma once
  21. #endif
  22. namespace boost {
  23. BOOST_LOG_OPEN_NAMESPACE
  24. namespace aux {
  25. //! A tagged integer wrapper for type safety
  26. template< typename IntT, typename TagT >
  27. struct tagged_integer
  28. {
  29. //! Contained value type
  30. typedef IntT integer_type;
  31. //! Tag
  32. typedef TagT tag;
  33. //! Contained value
  34. integer_type value;
  35. //! Conversion operator
  36. BOOST_CONSTEXPR operator integer_type() const BOOST_NOEXCEPT { return value; }
  37. // Increment
  38. tagged_integer& operator++ () BOOST_NOEXCEPT { ++value; return *this; }
  39. tagged_integer operator++ (int) BOOST_NOEXCEPT { tagged_integer temp = *this; ++value; return temp; }
  40. // Decrement
  41. tagged_integer& operator-- () BOOST_NOEXCEPT { --value; return *this; }
  42. tagged_integer operator-- (int) BOOST_NOEXCEPT { tagged_integer temp = *this; --value; return temp; }
  43. #define BOOST_LOG_TAGGED_INTEGER_OP(op)\
  44. tagged_integer& operator op (tagged_integer const& that) BOOST_NOEXCEPT { value op that.value; return *this; }
  45. BOOST_LOG_TAGGED_INTEGER_OP(|=)
  46. BOOST_LOG_TAGGED_INTEGER_OP(&=)
  47. BOOST_LOG_TAGGED_INTEGER_OP(^=)
  48. BOOST_LOG_TAGGED_INTEGER_OP(+=)
  49. BOOST_LOG_TAGGED_INTEGER_OP(-=)
  50. BOOST_LOG_TAGGED_INTEGER_OP(*=)
  51. BOOST_LOG_TAGGED_INTEGER_OP(/=)
  52. BOOST_LOG_TAGGED_INTEGER_OP(%=)
  53. #undef BOOST_LOG_TAGGED_INTEGER_OP
  54. //! Inversion operator
  55. tagged_integer& operator~ () BOOST_NOEXCEPT { ~value; return *this; }
  56. // Shift operators
  57. template< typename T >
  58. tagged_integer& operator<<= (T const& that) BOOST_NOEXCEPT { value <<= that; return *this; }
  59. template< typename T >
  60. tagged_integer& operator>>= (T const& that) BOOST_NOEXCEPT { value >>= that; return *this; }
  61. private:
  62. // Protection against improper usage
  63. template< typename T1, typename T2 >
  64. tagged_integer& operator<<= (tagged_integer< T1, T2 > const&);
  65. template< typename T1, typename T2 >
  66. tagged_integer& operator>>= (tagged_integer< T1, T2 > const&);
  67. };
  68. // Relational operators
  69. #define BOOST_LOG_TAGGED_INTEGER_OP(op)\
  70. template< typename IntT, typename TagT >\
  71. inline bool operator op (\
  72. tagged_integer< IntT, TagT > const& left, tagged_integer< IntT, TagT > const& right) BOOST_NOEXCEPT\
  73. {\
  74. return (left.value op right.value);\
  75. }
  76. BOOST_LOG_TAGGED_INTEGER_OP(==)
  77. BOOST_LOG_TAGGED_INTEGER_OP(!=)
  78. BOOST_LOG_TAGGED_INTEGER_OP(<)
  79. BOOST_LOG_TAGGED_INTEGER_OP(>)
  80. BOOST_LOG_TAGGED_INTEGER_OP(<=)
  81. BOOST_LOG_TAGGED_INTEGER_OP(>=)
  82. #undef BOOST_LOG_TAGGED_INTEGER_OP
  83. #define BOOST_LOG_TAGGED_INTEGER_OP(op)\
  84. template< typename IntT, typename TagT >\
  85. inline tagged_integer< IntT, TagT > operator op (\
  86. tagged_integer< IntT, TagT > const& left, tagged_integer< IntT, TagT > const& right) BOOST_NOEXCEPT\
  87. {\
  88. tagged_integer< IntT, TagT > temp = left;\
  89. temp op##= right;\
  90. return temp;\
  91. }
  92. BOOST_LOG_TAGGED_INTEGER_OP(|)
  93. BOOST_LOG_TAGGED_INTEGER_OP(&)
  94. BOOST_LOG_TAGGED_INTEGER_OP(^)
  95. BOOST_LOG_TAGGED_INTEGER_OP(+)
  96. BOOST_LOG_TAGGED_INTEGER_OP(-)
  97. BOOST_LOG_TAGGED_INTEGER_OP(*)
  98. BOOST_LOG_TAGGED_INTEGER_OP(/)
  99. BOOST_LOG_TAGGED_INTEGER_OP(%)
  100. #undef BOOST_LOG_TAGGED_INTEGER_OP
  101. #define BOOST_LOG_TAGGED_INTEGER_OP(op)\
  102. template< typename IntT, typename TagT, typename T >\
  103. inline tagged_integer< IntT, TagT > operator op (\
  104. tagged_integer< IntT, TagT > const& left, T const& right) BOOST_NOEXCEPT\
  105. {\
  106. tagged_integer< IntT, TagT > temp = left;\
  107. temp op##= right;\
  108. return temp;\
  109. }
  110. BOOST_LOG_TAGGED_INTEGER_OP(<<)
  111. BOOST_LOG_TAGGED_INTEGER_OP(>>)
  112. #undef BOOST_LOG_TAGGED_INTEGER_OP
  113. } // namespace aux
  114. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  115. } // namespace boost
  116. #include <boost/log/detail/footer.hpp>
  117. #endif // BOOST_LOG_TAGGED_INTEGER_HPP_INCLUDED_