2
* Copyright Andrey Semashev 2007 - 2013.
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/detail/atomic_count.hpp>
26
#endif // BOOST_LOG_NO_THREADS
27
#include <boost/log/detail/header.hpp>
29
#ifdef BOOST_LOG_HAS_PRAGMA_ONCE
35
BOOST_LOG_OPEN_NAMESPACE
37
namespace attributes {
40
* \brief A class of an attribute that counts an integral value
42
* This type of attribute acts as a counter, that is, it returns a monotonously
43
* changing value each time requested. The attribute value type can be specified
44
* as a template parameter. However, the type must be an integral type of size no
45
* more than <tt>sizeof(long)</tt>.
47
template< typename T >
51
// For now only integral types up to long are supported
52
BOOST_STATIC_ASSERT_MSG(is_integral< T >::value && sizeof(T) <= sizeof(long), "Boost.Log: Only integral types up to long are supported by counter attribute");
55
//! A counter value type
59
//! Base class for factory implementation
60
class BOOST_LOG_NO_VTABLE BOOST_LOG_VISIBLE impl :
61
public attribute::impl
65
//! Generic factory implementation
67
#ifndef BOOST_LOG_NO_THREADS
68
//! Increment-by-one factory implementation
70
//! Decrement-by-one factory implementation
78
* \param initial Initial value of the counter
79
* \param step Changing step of the counter. Each value acquired from the attribute
80
* will be greater than the previous one to this amount.
82
explicit counter(value_type initial = (value_type)0, long step = 1) :
83
#ifndef BOOST_LOG_NO_THREADS
87
this->set_impl(new impl_inc(initial));
89
this->set_impl(new impl_dec(initial));
91
this->set_impl(new impl_generic(initial, step));
94
attribute(new impl_generic(initial, step))
99
* Constructor for casting support
101
explicit counter(cast_source const& source) :
102
attribute(source.as< impl >())
107
#ifndef BOOST_LOG_NO_THREADS
109
template< typename T >
110
class counter< T >::impl_generic :
115
const value_type m_Initial;
119
boost::detail::atomic_count m_Counter;
123
* Initializing constructor
125
impl_generic(value_type initial, long step) : m_Initial(initial), m_Step(step), m_Counter(-1)
129
attribute_value get_value()
131
register unsigned long next_counter = static_cast< unsigned long >(++m_Counter);
132
register value_type next = static_cast< value_type >(m_Initial + (next_counter * m_Step));
133
return make_attribute_value(next);
137
template< typename T >
138
class counter< T >::impl_inc :
143
boost::detail::atomic_count m_Counter;
147
* Initializing constructor
149
explicit impl_inc(value_type initial) : m_Counter(initial - 1)
153
attribute_value get_value()
155
return make_attribute_value(static_cast< value_type >(++m_Counter));
159
template< typename T >
160
class counter< T >::impl_dec :
165
boost::detail::atomic_count m_Counter;
169
* Initializing constructor
171
explicit impl_dec(value_type initial) : m_Counter(initial + 1)
175
attribute_value get_value()
177
return make_attribute_value(static_cast< value_type >(--m_Counter));
181
#else // BOOST_LOG_NO_THREADS
183
template< typename T >
184
class counter< T >::impl_generic :
191
value_type m_Counter;
195
* Initializing constructor
197
impl_generic(value_type initial, long step) : m_Step(step), m_Counter(initial - step)
201
attribute_value get_value()
204
return make_attribute_value(m_Counter);
208
#endif // BOOST_LOG_NO_THREADS
210
} // namespace attributes
212
BOOST_LOG_CLOSE_NAMESPACE // namespace log
216
#include <boost/log/detail/footer.hpp>
218
#endif // BOOST_LOG_ATTRIBUTES_COUNTER_HPP_INCLUDED_