1//
2// AtomicCounter.h
3//
4// Library: Foundation
5// Package: Core
6// Module: AtomicCounter
7//
8// Definition of the AtomicCounter class.
9//
10// Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// SPDX-License-Identifier: BSL-1.0
14//
15
16
17#ifndef Foundation_AtomicCounter_INCLUDED
18#define Foundation_AtomicCounter_INCLUDED
19
20
21#include "Poco/Foundation.h"
22#include <atomic>
23
24
25namespace Poco {
26
27
28class Foundation_API AtomicCounter
29 /// This class implements a simple counter, which
30 /// provides atomic operations that are safe to
31 /// use in a multithreaded environment.
32 ///
33 /// Typical usage of AtomicCounter is for implementing
34 /// reference counting and similar functionality.
35{
36public:
37 typedef int ValueType; /// The underlying integer type.
38
39 AtomicCounter();
40 /// Creates a new AtomicCounter and initializes it to zero.
41
42 explicit AtomicCounter(ValueType initialValue);
43 /// Creates a new AtomicCounter and initializes it with
44 /// the given value.
45
46 AtomicCounter(const AtomicCounter& counter);
47 /// Creates the counter by copying another one.
48
49 ~AtomicCounter();
50 /// Destroys the AtomicCounter.
51
52 AtomicCounter& operator = (const AtomicCounter& counter);
53 /// Assigns the value of another AtomicCounter.
54
55 AtomicCounter& operator = (ValueType value);
56 /// Assigns a value to the counter.
57
58 operator ValueType () const;
59 /// Converts the AtomicCounter to ValueType.
60
61 ValueType value() const;
62 /// Returns the value of the counter.
63
64 ValueType operator ++ (); // prefix
65 /// Increments the counter and returns the result.
66
67 ValueType operator ++ (int); // postfix
68 /// Increments the counter and returns the previous value.
69
70 ValueType operator -- (); // prefix
71 /// Decrements the counter and returns the result.
72
73 ValueType operator -- (int); // postfix
74 /// Decrements the counter and returns the previous value.
75
76 bool operator ! () const;
77 /// Returns true if the counter is zero, false otherwise.
78
79private:
80 std::atomic<int> _counter;
81};
82
83
84//
85// inlines
86//
87
88inline AtomicCounter::operator AtomicCounter::ValueType () const
89{
90 return _counter.load();
91}
92
93
94inline AtomicCounter::ValueType AtomicCounter::value() const
95{
96 return _counter.load();
97}
98
99
100inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix
101{
102 return ++_counter;
103}
104
105
106inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix
107{
108 return _counter++;
109}
110
111
112inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix
113{
114 return --_counter;
115}
116
117
118inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix
119{
120 return _counter--;
121}
122
123
124inline bool AtomicCounter::operator ! () const
125{
126 return _counter.load() == 0;
127}
128
129
130} // namespace Poco
131
132
133#endif // Foundation_AtomicCounter_INCLUDED
134