blob: d2e65c104231df061d3afe31b39e4bf286a5336e (
plain)
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
|
/*
* Atomic
* (C) 2016 Matthias Gierlings
*
* Botan is released under the Simplified BSD License (see license.txt)
**/
#ifndef BOTAN_ATOMIC_H_
#define BOTAN_ATOMIC_H_
#include <atomic>
#include <memory>
namespace Botan {
template <typename T>
/**
* Simple helper class to expand std::atomic with copy constructor and copy
* assignment operator, i.e. for use as element in a container like
* std::vector. The construction of instances of this wrapper is NOT atomic
* and needs to be properly guarded.
**/
class Atomic final
{
public:
Atomic() = default;
Atomic(const Atomic& data) : m_data(data.m_data.load()) {}
Atomic(const std::atomic<T>& data) : m_data(data.load()) {}
~Atomic() = default;
Atomic& operator=(const Atomic& a)
{
m_data.store(a.m_data.load());
return *this;
}
Atomic& operator=(const std::atomic<T>& a)
{
m_data.store(a.load());
return *this;
}
operator std::atomic<T>& () { return m_data; }
operator T() { return m_data.load(); }
private:
std::atomic<T> m_data;
};
}
#endif
|