-
Notifications
You must be signed in to change notification settings - Fork 8
/
CounterSingleton.hpp
50 lines (39 loc) · 1.24 KB
/
CounterSingleton.hpp
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
#ifndef CPP_REFACTORING_FOR_TESTABILITY_COUNTERSINGLETON_HPP
#define CPP_REFACTORING_FOR_TESTABILITY_COUNTERSINGLETON_HPP
#include <Counter.hpp>
#include <atomic>
namespace before
{
struct CounterSingleton
{
CounterSingleton(const CounterSingleton&) = delete;
CounterSingleton(CounterSingleton&&) = delete;
CounterSingleton& operator=(const CounterSingleton&) = delete;
CounterSingleton& operator=(CounterSingleton&&) = delete;
void increment();
void decrement();
int get() const;
static CounterSingleton& getInstance();
private:
CounterSingleton() = default;
std::atomic<int> mCounter{0};
};
} // namespace before
namespace after
{
struct CounterSingleton : public Counter
{
CounterSingleton(const CounterSingleton&) = delete;
CounterSingleton(CounterSingleton&&) = delete;
CounterSingleton& operator=(const CounterSingleton&) = delete;
CounterSingleton& operator=(CounterSingleton&&) = delete;
void increment() override;
void decrement() override;
int get() const override;
static CounterSingleton& getInstance();
private:
CounterSingleton() = default;
std::atomic<int> mCounter{0};
};
} // namespace after
#endif // CPP_REFACTORING_FOR_TESTABILITY_COUNTERSINGLETON_HPP