SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Atomic.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 #ifndef swcdb_core_Atomic_h
7 #define swcdb_core_Atomic_h
8 
9 
10 #include <atomic>
11 
12 
13 namespace SWC { namespace Core {
14 
15 
16 
17 template<typename T, std::memory_order OrderT=std::memory_order_relaxed>
18 struct AtomicBase : protected std::atomic<T> {
19 
20  constexpr SWC_CAN_INLINE
21  explicit AtomicBase() noexcept { }
22 
23  template<typename ValueT>
24  constexpr SWC_CAN_INLINE
25  explicit AtomicBase(ValueT initial) noexcept : std::atomic<T>(initial) { }
26 
27  AtomicBase(const AtomicBase<T>&) = delete;
28 
29  AtomicBase(const AtomicBase<T>&&) = delete;
30 
31  template<typename ValueT> AtomicBase<T>& operator=(ValueT v) = delete;
32 
33  ~AtomicBase() noexcept { }
34 
35 
36  constexpr SWC_CAN_INLINE
37  void store(T v) noexcept {
38  std::atomic<T>::store(v, OrderT);
39  }
40 
41  constexpr SWC_CAN_INLINE
42  T load() const noexcept {
43  return std::atomic<T>::load(OrderT);
44  }
45 
46  constexpr SWC_CAN_INLINE
47  T exchange(T value) noexcept {
48  return std::atomic<T>::exchange(value, OrderT);
49  }
50 
51  constexpr SWC_CAN_INLINE
52  bool compare_exchange_weak(T& at, T value) noexcept {
53  return std::atomic<T>::compare_exchange_weak(at, value, OrderT);
54  }
55 
56  constexpr SWC_CAN_INLINE
57  operator T() const noexcept {
58  return load();
59  }
60 
61 };
62 
64 
65 
66 
67 
68 template<typename T, std::memory_order OrderT=std::memory_order_relaxed>
69 struct Atomic : public AtomicBase<T> {
70 
71  constexpr SWC_CAN_INLINE
72  explicit Atomic() noexcept { }
73 
74  template<typename ValueT>
75  constexpr SWC_CAN_INLINE
76  explicit Atomic(ValueT initial) noexcept : AtomicBase<T>(initial) { }
77 
78  Atomic(const Atomic<T>&) = delete;
79 
80  Atomic(const Atomic<T>&&) = delete;
81 
82  template<typename ValueT> Atomic<T>& operator=(ValueT v) = delete;
83 
84  ~Atomic() noexcept {}
85 
86 
87  constexpr SWC_CAN_INLINE
88  T fetch_sub(T v) noexcept {
89  return AtomicBase<T>::fetch_sub(v, OrderT);
90  }
91 
92  constexpr SWC_CAN_INLINE
93  T fetch_add(T v) noexcept {
94  return AtomicBase<T>::fetch_add(v, OrderT);
95  }
96 
97  constexpr SWC_CAN_INLINE
98  T fetch_xor(T v) noexcept {
99  return AtomicBase<T>::fetch_xor(v, OrderT);
100  }
101 
102  constexpr SWC_CAN_INLINE
103  T fetch_and(T v) noexcept {
104  return AtomicBase<T>::fetch_and(v, OrderT);
105  }
106 
107  constexpr SWC_CAN_INLINE
108  T fetch_or(T v) noexcept {
109  return AtomicBase<T>::fetch_or(v, OrderT);
110  }
111 
112 
113 
114  constexpr SWC_CAN_INLINE
115  T sub_rslt(T v) noexcept {
116  return fetch_sub(v) - v;
117  }
118 
119  constexpr SWC_CAN_INLINE
120  T add_rslt(T v) noexcept {
121  return fetch_add(v) + v;
122  }
123 
124  constexpr SWC_CAN_INLINE
125  T xor_rslt(T v) noexcept {
126  return fetch_xor(v) ^ v;
127  }
128 
129  constexpr SWC_CAN_INLINE
130  T and_rslt(T v) noexcept {
131  return fetch_and(v) & v;
132  }
133 
134  constexpr SWC_CAN_INLINE
135  T or_rslt(T v) noexcept {
136  return fetch_or(v) | v;
137  }
138 
139 #pragma GCC diagnostic push
140 #pragma GCC diagnostic ignored "-Weffc++"
141 
142  constexpr SWC_CAN_INLINE
143  T operator++(int) noexcept {
144  return fetch_add(1);
145  }
146 
147  constexpr SWC_CAN_INLINE
148  T operator--(int) noexcept {
149  return fetch_sub(1);
150  }
151 
152 #pragma GCC diagnostic pop
153 
154 
155  /* Is/Should post OP value ever used ?
156 
157  constexpr SWC_CAN_INLINE
158  T operator++() noexcept {
159  return add_rslt(1);
160  }
161  constexpr SWC_CAN_INLINE
162  T operator--() noexcept {
163  return sub_rslt(1);
164  }
165 
166  constexpr SWC_CAN_INLINE
167  T operator+=(T v) noexcept {
168  return add_rslt(v);
169  }
170 
171  constexpr SWC_CAN_INLINE
172  T operator-=(T v) noexcept {
173  return sub_rslt(v);
174  }
175 
176  constexpr SWC_CAN_INLINE
177  T operator^=(T v) noexcept {
178  return xor_rslt(v);
179  }
180 
181  constexpr SWC_CAN_INLINE
182  T operator&=(T v) noexcept {
183  return and_rslt(v);
184  }
185 
186  constexpr SWC_CAN_INLINE
187  T operator|=(T v) noexcept {
188  return or_rslt(v);
189  }
190  */
191 
192 };
193 
194 
195 
196 template<typename T, std::memory_order OrderT=std::memory_order_relaxed>
197 extern SWC_CAN_INLINE
198 std::ostream& operator<<(std::ostream& out, const AtomicBase<T, OrderT>& v) {
199  return out << v.load();
200 }
201 
202 }} // namespace SWC::Core
203 
204 
205 #endif // swcdb_core_Atomic_h
SWC::Core::AtomicBase::compare_exchange_weak
constexpr SWC_CAN_INLINE bool compare_exchange_weak(T &at, T value) noexcept
Definition: Atomic.h:52
SWC::Core::Atomic::operator++
constexpr SWC_CAN_INLINE T operator++(int) noexcept
Definition: Atomic.h:143
SWC::Core::AtomicBase::AtomicBase
AtomicBase(const AtomicBase< T > &)=delete
SWC::Core::AtomicBase
Definition: Atomic.h:18
SWC::Core::Atomic
Definition: Atomic.h:69
SWC::Core::AtomicBase::AtomicBase
constexpr SWC_CAN_INLINE AtomicBase() noexcept
Definition: Atomic.h:21
SWC::Common::Files::Schema::load
void load(int &err, const std::string &filepath, DB::Schema::Ptr &schema)
Definition: Schema.h:87
SWC::Core::AtomicBase::AtomicBase
constexpr SWC_CAN_INLINE AtomicBase(ValueT initial) noexcept
Definition: Atomic.h:25
SWC::Core::AtomicBase::~AtomicBase
~AtomicBase() noexcept
Definition: Atomic.h:33
SWC::Core::Atomic::and_rslt
constexpr SWC_CAN_INLINE T and_rslt(T v) noexcept
Definition: Atomic.h:130
SWC::Core::Atomic::sub_rslt
constexpr SWC_CAN_INLINE T sub_rslt(T v) noexcept
Definition: Atomic.h:115
SWC::Core::Atomic::add_rslt
constexpr SWC_CAN_INLINE T add_rslt(T v) noexcept
Definition: Atomic.h:120
SWC::Core::AtomicBase::AtomicBase
AtomicBase(const AtomicBase< T > &&)=delete
SWC::Core::AtomicBase::store
constexpr SWC_CAN_INLINE void store(T v) noexcept
Definition: Atomic.h:37
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::Core::Atomic::Atomic
Atomic(const Atomic< T > &)=delete
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Core::Atomic::xor_rslt
constexpr SWC_CAN_INLINE T xor_rslt(T v) noexcept
Definition: Atomic.h:125
SWC::Core::Atomic::operator--
constexpr SWC_CAN_INLINE T operator--(int) noexcept
Definition: Atomic.h:148
SWC::Core::AtomicBase::operator=
AtomicBase< T > & operator=(ValueT v)=delete
SWC::Core::Atomic::Atomic
constexpr SWC_CAN_INLINE Atomic() noexcept
Definition: Atomic.h:72
SWC::Core::AtomicBase::exchange
constexpr SWC_CAN_INLINE T exchange(T value) noexcept
Definition: Atomic.h:47
SWC::Core::operator<<
SWC_CAN_INLINE std::ostream & operator<<(std::ostream &out, const AtomicBase< T, OrderT > &v)
Definition: Atomic.h:198
SWC::Core::Atomic::or_rslt
constexpr SWC_CAN_INLINE T or_rslt(T v) noexcept
Definition: Atomic.h:135
SWC::Core::Atomic::Atomic
Atomic(const Atomic< T > &&)=delete
SWC::Core::Atomic::fetch_and
constexpr SWC_CAN_INLINE T fetch_and(T v) noexcept
Definition: Atomic.h:103
SWC::Config::T
const uint64_t T
Definition: Property.h:27
SWC::Core::AtomicBase::load
constexpr SWC_CAN_INLINE T load() const noexcept
Definition: Atomic.h:42
SWC::Core::Atomic::operator=
Atomic< T > & operator=(ValueT v)=delete
SWC::Core::Atomic::Atomic
constexpr SWC_CAN_INLINE Atomic(ValueT initial) noexcept
Definition: Atomic.h:76
SWC::Core::Atomic::~Atomic
~Atomic() noexcept
Definition: Atomic.h:84
SWC::Core::Atomic::fetch_sub
constexpr SWC_CAN_INLINE T fetch_sub(T v) noexcept
Definition: Atomic.h:88
SWC::Core::Atomic::fetch_add
constexpr SWC_CAN_INLINE T fetch_add(T v) noexcept
Definition: Atomic.h:93
SWC::Core::Atomic::fetch_or
constexpr SWC_CAN_INLINE T fetch_or(T v) noexcept
Definition: Atomic.h:108
SWC::Core::Atomic::fetch_xor
constexpr SWC_CAN_INLINE T fetch_xor(T v) noexcept
Definition: Atomic.h:98
SWC::Core::AtomicBool
AtomicBase< bool > AtomicBool
Definition: Atomic.h:63