-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounter.cpp
executable file
·86 lines (71 loc) · 2.01 KB
/
Counter.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Counter.cpp
*
* Created on: 01 apr 2021
* Author: daniele
*/
#include "Counter.h"
namespace dferone::counters {
#ifdef DFERONE_THREAD_SAFE
constinit std::mutex Counter::mutex = std::mutex();
#endif
std::map<std::string, double> Counter::static_counters_ = std::map<std::string, double>();
Counter::Counter(std::string name) : name_(name) {
#ifdef DFERONE_THREAD_SAFE
std::lock_guard<std::mutex> lock(mutex);
#endif
if (!Counter::static_counters_.contains(name)) {
Counter::static_counters_[name] = 0.0;
}
}
Counter &Counter::operator =(double val) {
#ifdef DFERONE_THREAD_SAFE
std::lock_guard<std::mutex> lock(mutex);
#endif
static_counters_[name_] = val;
return *this;
}
Counter::operator double() const {
#ifdef DFERONE_THREAD_SAFE
std::lock_guard<std::mutex> lock(mutex);
#endif
return static_counters_[name_];
}
double Counter::operator++(int) {
#ifdef DFERONE_THREAD_SAFE
std::lock_guard<std::mutex> lock(mutex);
#endif
double val = static_counters_[name_];
++static_counters_[name_];
return val;
}
double Counter::operator++() {
#ifdef DFERONE_THREAD_SAFE
std::lock_guard<std::mutex> lock(mutex);
#endif
return ++static_counters_[name_];
}
double Counter::operator+=(double val) {
#ifdef DFERONE_THREAD_SAFE
std::lock_guard<std::mutex> lock(mutex);
#endif
static_counters_[name_] += val;
return static_counters_[name_];
}
double Counter::operator-=(double val) {
#ifdef DFERONE_THREAD_SAFE
std::lock_guard<std::mutex> lock(mutex);
#endif
static_counters_[name_] -= val;
return static_counters_[name_];
}
bool Counter::operator==(const Counter &other) const {
#ifdef DFERONE_THREAD_SAFE
std::lock_guard<std::mutex> lock(mutex);
#endif
if (name_ == other.name_) {
return true;
}
return static_counters_[name_] == static_counters_[other.name_];
}
}