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)
9 * \author Andrey Semashev
12 * The header contains implementation of the counter attribute.
15 #ifndef BOOST_LOG_ATTRIBUTES_COUNTER_HPP_INCLUDED_
16 #define BOOST_LOG_ATTRIBUTES_COUNTER_HPP_INCLUDED_
18 #include <boost/static_assert.hpp>
19 #include <boost/type_traits/is_integral.hpp>
20 #include <boost/log/detail/config.hpp>
21 #include <boost/log/attributes/attribute.hpp>
22 #include <boost/log/attributes/attribute_cast.hpp>
23 #include <boost/log/attributes/attribute_value_impl.hpp>
24 #ifndef BOOST_LOG_NO_THREADS
25 #include <boost/memory_order.hpp>
26 #include <boost/atomic/atomic.hpp>
27 #endif // BOOST_LOG_NO_THREADS
28 #include <boost/log/detail/header.hpp>
30 #ifdef BOOST_HAS_PRAGMA_ONCE
36 BOOST_LOG_OPEN_NAMESPACE
38 namespace attributes {
41 * \brief A class of an attribute that counts an integral value
43 * This attribute acts as a counter - it returns a monotonously
44 * changing value each time requested. The attribute value type can be specified
45 * as a template parameter. The type must be an integral type.
47 template< typename T >
51 BOOST_STATIC_ASSERT_MSG(is_integral< T >::value, "Boost.Log: Only integral types are supported by the counter attribute");
54 //! A counter value type
58 //! Factory implementation
59 class BOOST_SYMBOL_VISIBLE impl :
60 public attribute::impl
63 #ifndef BOOST_LOG_NO_THREADS
64 boost::atomic< value_type > m_counter;
68 const value_type m_step;
71 impl(value_type initial, value_type step) BOOST_NOEXCEPT :
72 m_counter(initial), m_step(step)
76 attribute_value get_value()
78 #ifndef BOOST_LOG_NO_THREADS
79 value_type value = m_counter.fetch_add(m_step, boost::memory_order_relaxed);
81 value_type value = m_counter;
84 return make_attribute_value(value);
92 * \param initial Initial value of the counter
93 * \param step Changing step of the counter. Each value acquired from the attribute
94 * will be greater than the previous one by this amount.
96 explicit counter(value_type initial = (value_type)0, value_type step = (value_type)1) :
97 attribute(new impl(initial, step))
102 * Constructor for casting support
104 explicit counter(cast_source const& source) :
105 attribute(source.as< impl >())
110 } // namespace attributes
112 BOOST_LOG_CLOSE_NAMESPACE // namespace log
116 #include <boost/log/detail/footer.hpp>
118 #endif // BOOST_LOG_ATTRIBUTES_COUNTER_HPP_INCLUDED_