Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* @file counter.hpp
* @copyright Copyright (c) 2022 University of Turku, MIT License
* @author Nicolas Pope
*/
#pragma once
#include <atomic>
namespace ftl {
class Counter {
public:
inline explicit Counter(std::atomic_int *c): counter_(c) {
++(*c);
}
Counter() = delete;
inline Counter(const Counter &c): counter_(c.counter_) {
if (counter_) {
++(*counter_);
}
}
inline Counter(Counter &&c): counter_(c.counter_) {
c.counter_ = nullptr;
}
inline ~Counter() {
if (counter_) {
--(*counter_);
}
}
private:
std::atomic_int *counter_;
};
} // namespace ftl