File indexing completed on 2025-01-18 09:39:18
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef BOOST_LOG_ATTRIBUTES_COUNTER_HPP_INCLUDED_
0016 #define BOOST_LOG_ATTRIBUTES_COUNTER_HPP_INCLUDED_
0017
0018 #include <boost/type_traits/is_integral.hpp>
0019 #include <boost/log/detail/config.hpp>
0020 #include <boost/log/attributes/attribute.hpp>
0021 #include <boost/log/attributes/attribute_cast.hpp>
0022 #include <boost/log/attributes/attribute_value_impl.hpp>
0023 #ifndef BOOST_LOG_NO_THREADS
0024 #include <boost/memory_order.hpp>
0025 #include <boost/atomic/atomic.hpp>
0026 #endif
0027 #include <boost/log/detail/header.hpp>
0028
0029 #ifdef BOOST_HAS_PRAGMA_ONCE
0030 #pragma once
0031 #endif
0032
0033 namespace boost {
0034
0035 BOOST_LOG_OPEN_NAMESPACE
0036
0037 namespace attributes {
0038
0039
0040
0041
0042
0043
0044
0045
0046 template< typename T >
0047 class counter :
0048 public attribute
0049 {
0050 static_assert(is_integral< T >::value, "Boost.Log: Only integral types are supported by the counter attribute");
0051
0052 public:
0053
0054 typedef T value_type;
0055
0056 protected:
0057
0058 class BOOST_SYMBOL_VISIBLE impl :
0059 public attribute::impl
0060 {
0061 private:
0062 #ifndef BOOST_LOG_NO_THREADS
0063 boost::atomic< value_type > m_counter;
0064 #else
0065 value_type m_counter;
0066 #endif
0067 const value_type m_step;
0068
0069 public:
0070 impl(value_type initial, value_type step) BOOST_NOEXCEPT :
0071 m_counter(initial), m_step(step)
0072 {
0073 }
0074
0075 attribute_value get_value()
0076 {
0077 #ifndef BOOST_LOG_NO_THREADS
0078 value_type value = m_counter.fetch_add(m_step, boost::memory_order_relaxed);
0079 #else
0080 value_type value = m_counter;
0081 m_counter += m_step;
0082 #endif
0083 return make_attribute_value(value);
0084 }
0085 };
0086
0087 public:
0088
0089
0090
0091
0092
0093
0094
0095 explicit counter(value_type initial = (value_type)0, value_type step = (value_type)1) :
0096 attribute(new impl(initial, step))
0097 {
0098 }
0099
0100
0101
0102
0103 explicit counter(cast_source const& source) :
0104 attribute(source.as< impl >())
0105 {
0106 }
0107 };
0108
0109 }
0110
0111 BOOST_LOG_CLOSE_NAMESPACE
0112
0113 }
0114
0115 #include <boost/log/detail/footer.hpp>
0116
0117 #endif