Botan 3.9.0
Crypto and TLS for C&
atomic.h
Go to the documentation of this file.
1/*
2 * Atomic
3 * (C) 2016 Matthias Gierlings
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 **/
7
8#ifndef BOTAN_ATOMIC_H_
9#define BOTAN_ATOMIC_H_
10
11#include <botan/types.h>
12#include <atomic>
13#include <memory>
14
15namespace Botan {
16
17template <typename T>
18/**
19 * Simple helper class to expand std::atomic with copy constructor and copy
20 * assignment operator, i.e. for use as element in a container like
21 * std::vector. The construction of instances of this wrapper is NOT atomic
22 * and needs to be properly guarded.
23 **/
24class Atomic final {
25 public:
26 Atomic() = default;
27
28 Atomic(const Atomic& data) : m_data(data.m_data.load()) {}
29
30 explicit Atomic(const std::atomic<T>& data) : m_data(data.load()) {}
31
32 ~Atomic() = default;
33
34 Atomic(Atomic&& other) = default;
35 Atomic& operator=(Atomic&& other) = default;
36
37 Atomic& operator=(const Atomic& other) {
38 if(this != &other) {
39 m_data.store(other.m_data.load());
40 }
41 return *this;
42 }
43
44 Atomic& operator=(const std::atomic<T>& a) {
45 m_data.store(a.load());
46 return *this;
47 }
48
49 explicit operator std::atomic<T>&() { return m_data; }
50
51 // NOLINTNEXTLINE(*-explicit-conversions) FIXME
52 operator T() { return m_data.load(); }
53
54 private:
55 std::atomic<T> m_data;
56};
57
58} // namespace Botan
59
60#endif
Atomic(Atomic &&other)=default
Atomic()=default
~Atomic()=default
Atomic(const std::atomic< T > &data)
Definition atomic.h:30
Atomic & operator=(Atomic &&other)=default
Atomic & operator=(const std::atomic< T > &a)
Definition atomic.h:44
Atomic & operator=(const Atomic &other)
Definition atomic.h:37
Atomic(const Atomic &data)
Definition atomic.h:28