counter.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 counter.hpp
  9. * \author Andrey Semashev
  10. * \date 01.05.2007
  11. *
  12. * The header contains implementation of the counter attribute.
  13. */
  14. #ifndef BOOST_LOG_ATTRIBUTES_COUNTER_HPP_INCLUDED_
  15. #define BOOST_LOG_ATTRIBUTES_COUNTER_HPP_INCLUDED_
  16. #include <boost/static_assert.hpp>
  17. #include <boost/type_traits/is_integral.hpp>
  18. #include <boost/log/detail/config.hpp>
  19. #include <boost/log/attributes/attribute.hpp>
  20. #include <boost/log/attributes/attribute_cast.hpp>
  21. #include <boost/log/attributes/attribute_value_impl.hpp>
  22. #ifndef BOOST_LOG_NO_THREADS
  23. #include <boost/memory_order.hpp>
  24. #include <boost/atomic/atomic.hpp>
  25. #endif // BOOST_LOG_NO_THREADS
  26. #include <boost/log/detail/header.hpp>
  27. #ifdef BOOST_HAS_PRAGMA_ONCE
  28. #pragma once
  29. #endif
  30. namespace boost {
  31. BOOST_LOG_OPEN_NAMESPACE
  32. namespace attributes {
  33. /*!
  34. * \brief A class of an attribute that counts an integral value
  35. *
  36. * This attribute acts as a counter - it returns a monotonously
  37. * changing value each time requested. The attribute value type can be specified
  38. * as a template parameter. The type must be an integral type.
  39. */
  40. template< typename T >
  41. class counter :
  42. public attribute
  43. {
  44. BOOST_STATIC_ASSERT_MSG(is_integral< T >::value, "Boost.Log: Only integral types are supported by the counter attribute");
  45. public:
  46. //! A counter value type
  47. typedef T value_type;
  48. protected:
  49. //! Factory implementation
  50. class BOOST_SYMBOL_VISIBLE impl :
  51. public attribute::impl
  52. {
  53. private:
  54. #ifndef BOOST_LOG_NO_THREADS
  55. boost::atomic< value_type > m_counter;
  56. #else
  57. value_type m_counter;
  58. #endif
  59. const value_type m_step;
  60. public:
  61. impl(value_type initial, value_type step) BOOST_NOEXCEPT :
  62. m_counter(initial), m_step(step)
  63. {
  64. }
  65. attribute_value get_value()
  66. {
  67. #ifndef BOOST_LOG_NO_THREADS
  68. value_type value = m_counter.fetch_add(m_step, boost::memory_order_relaxed);
  69. #else
  70. value_type value = m_counter;
  71. m_counter += m_step;
  72. #endif
  73. return make_attribute_value(value);
  74. }
  75. };
  76. public:
  77. /*!
  78. * Constructor
  79. *
  80. * \param initial Initial value of the counter
  81. * \param step Changing step of the counter. Each value acquired from the attribute
  82. * will be greater than the previous one by this amount.
  83. */
  84. explicit counter(value_type initial = (value_type)0, value_type step = (value_type)1) :
  85. attribute(new impl(initial, step))
  86. {
  87. }
  88. /*!
  89. * Constructor for casting support
  90. */
  91. explicit counter(cast_source const& source) :
  92. attribute(source.as< impl >())
  93. {
  94. }
  95. };
  96. } // namespace attributes
  97. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  98. } // namespace boost
  99. #include <boost/log/detail/footer.hpp>
  100. #endif // BOOST_LOG_ATTRIBUTES_COUNTER_HPP_INCLUDED_