lintel 0.2.0
Loading...
Searching...
No Matches
PropertyValueStorage.h
Go to the documentation of this file.
1#ifndef LINTEL_PROPERTYVALUESTORAGE_H
2#define LINTEL_PROPERTYVALUESTORAGE_H
3
4#include <atomic>
5#include <shared_mutex>
6#include <type_traits>
7
8namespace lintel {
14template<class T, class Enable = void> struct IsLockFreeAtomic : std::false_type
15{
16};
17
18template<class T>
19struct IsLockFreeAtomic<T, std::enable_if_t<std::is_trivially_copyable_v<T>>>
20 : std::bool_constant<std::atomic<T>::is_always_lock_free>
21{
22};
23
32template<class T, class Enable = void> class PropertyValueStorage
33{
34private:
35 mutable std::shared_mutex m_mutex;
36 T m_value;
37
38public:
39 PropertyValueStorage() : m_value() {}
40
41 explicit PropertyValueStorage(const T &value) : m_value(value) {}
42
44 T load() const
45 {
46 std::shared_lock<std::shared_mutex> lock(m_mutex);
47 return m_value;
48 }
49
51 void store(const T &value)
52 {
53 std::lock_guard<std::shared_mutex> lock(m_mutex);
54 m_value = value;
55 }
56
58 [[nodiscard]] static constexpr bool isLockFree() { return false; }
59};
60
62template<class T> class PropertyValueStorage<T, std::enable_if_t<IsLockFreeAtomic<T>::value>>
63{
64private:
65 std::atomic<T> m_value;
66
67public:
68 PropertyValueStorage() : m_value() {}
69
70 explicit PropertyValueStorage(const T &value) : m_value(value) {}
71
73 T load() const { return m_value.load(std::memory_order_relaxed); }
74
76 void store(const T &value) { m_value.store(value, std::memory_order_relaxed); }
77
79 [[nodiscard]] static constexpr bool isLockFree() { return std::atomic<T>::is_always_lock_free; }
80};
81
82}// namespace lintel
83#endif// LINTEL_PROPERTYVALUESTORAGE_H
static constexpr bool isLockFree()
Definition PropertyValueStorage.h:79
void store(const T &value)
Definition PropertyValueStorage.h:76
PropertyValueStorage(const T &value)
Definition PropertyValueStorage.h:70
Definition PropertyValueStorage.h:33
PropertyValueStorage(const T &value)
Definition PropertyValueStorage.h:41
static constexpr bool isLockFree()
Definition PropertyValueStorage.h:58
T load() const
Definition PropertyValueStorage.h:44
void store(const T &value)
Definition PropertyValueStorage.h:51
PropertyValueStorage()
Definition PropertyValueStorage.h:39
Definition BaseFeature.h:6
constexpr std::string_view type_name()
Definition TypeName.h:9
Definition PropertyValueStorage.h:15