123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- #ifndef BOOST_LOG_ATTRIBUTES_COUNTER_HPP_INCLUDED_
- #define BOOST_LOG_ATTRIBUTES_COUNTER_HPP_INCLUDED_
- #include <boost/static_assert.hpp>
- #include <boost/type_traits/is_integral.hpp>
- #include <boost/log/detail/config.hpp>
- #include <boost/log/attributes/attribute.hpp>
- #include <boost/log/attributes/attribute_cast.hpp>
- #include <boost/log/attributes/attribute_value_impl.hpp>
- #ifndef BOOST_LOG_NO_THREADS
- #include <boost/memory_order.hpp>
- #include <boost/atomic/atomic.hpp>
- #endif
- #include <boost/log/detail/header.hpp>
- #ifdef BOOST_HAS_PRAGMA_ONCE
- #pragma once
- #endif
- namespace boost {
- BOOST_LOG_OPEN_NAMESPACE
- namespace attributes {
- template< typename T >
- class counter :
- public attribute
- {
- BOOST_STATIC_ASSERT_MSG(is_integral< T >::value, "Boost.Log: Only integral types are supported by the counter attribute");
- public:
-
- typedef T value_type;
- protected:
-
- class BOOST_SYMBOL_VISIBLE impl :
- public attribute::impl
- {
- private:
- #ifndef BOOST_LOG_NO_THREADS
- boost::atomic< value_type > m_counter;
- #else
- value_type m_counter;
- #endif
- const value_type m_step;
- public:
- impl(value_type initial, value_type step) BOOST_NOEXCEPT :
- m_counter(initial), m_step(step)
- {
- }
- attribute_value get_value()
- {
- #ifndef BOOST_LOG_NO_THREADS
- value_type value = m_counter.fetch_add(m_step, boost::memory_order_relaxed);
- #else
- value_type value = m_counter;
- m_counter += m_step;
- #endif
- return make_attribute_value(value);
- }
- };
- public:
-
- explicit counter(value_type initial = (value_type)0, value_type step = (value_type)1) :
- attribute(new impl(initial, step))
- {
- }
-
- explicit counter(cast_source const& source) :
- attribute(source.as< impl >())
- {
- }
- };
- }
- BOOST_LOG_CLOSE_NAMESPACE
- }
- #include <boost/log/detail/footer.hpp>
- #endif
|