cs-midi/Submodules/Encoder/AtomicPosition.hpp

34 lines
974 B
C++

#pragma once
#include <Settings/NamespaceSettings.hpp>
#include <atomic>
BEGIN_CS_NAMESPACE
template <class T>
struct AtomicPosition {
using type = T;
std::atomic<type> value {};
AtomicPosition(T t) : value {t} {}
AtomicPosition(const AtomicPosition &o) : AtomicPosition {o.get()} {}
AtomicPosition(AtomicPosition &&o) : AtomicPosition {o.get()} {}
AtomicPosition &operator=(const AtomicPosition &o) {
this->set(o.get());
return *this;
}
AtomicPosition &operator=(AtomicPosition &&o) {
this->set(o.get());
return *this;
}
constexpr static std::memory_order mo_rlx = std::memory_order_relaxed;
void add(type other) { value.fetch_add(other, mo_rlx); }
type get() const { return value.load(mo_rlx); }
void set(type other) { value.store(other, mo_rlx); }
type xchg(type other) { return value.exchange(other, mo_rlx); }
void add_isr(type other) { add(other); }
};
END_CS_NAMESPACE