counter.hpp 3.0 KB

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