Iguana 1.0.0
Implementation Guardian of Analysis Algorithms
Loading...
Searching...
No Matches
ConcurrentParam.h
1#pragma once
2
3#include "GlobalParam.h"
4
5namespace iguana {
6
8 using concurrent_key_t = std::size_t;
9
10 // ==================================================================================
11 // ConcurrentParam
12 // ==================================================================================
13
16 template <typename T>
18 {
19
20 public:
21
24 ConcurrentParam(std::string const& model);
25 virtual ~ConcurrentParam() = default;
26
30 virtual T const Load(concurrent_key_t const key) const = 0;
31
35 virtual void Save(T const& value, concurrent_key_t const key) = 0;
36
39 virtual bool HasKey(concurrent_key_t const key) const = 0;
40
43 bool NeedsHashing() const { return m_needs_hashing; }
44
46 virtual std::size_t GetSize() const = 0;
47
49 bool IsEmpty() const { return m_empty; }
50
51 protected:
52
55
57 std::mutex m_mutex;
58
60 bool m_empty{true};
61 };
62
63 // ==================================================================================
64 // SingleThreadParam
65 // ==================================================================================
66
69 template <typename T>
70 class SingleThreadParam : public ConcurrentParam<T>
71 {
72
73 public:
74 SingleThreadParam();
75 ~SingleThreadParam() override = default;
76 T const Load(concurrent_key_t const key) const override;
77 void Save(T const& value, concurrent_key_t const key) override;
78 bool HasKey(concurrent_key_t const key) const override;
79 std::size_t GetSize() const override;
80
81 private:
82
85 T m_value;
86 };
87
88 // ==================================================================================
89 // MemoizedParam
90 // ==================================================================================
91
94 template <typename T>
95 class MemoizedParam : public ConcurrentParam<T>
96 {
97
99 using container_t = std::unordered_map<concurrent_key_t, T>;
100
101 public:
102 MemoizedParam();
103 ~MemoizedParam() override = default;
104 T const Load(concurrent_key_t const key) const override;
105 void Save(T const& value, concurrent_key_t const key) override;
106 bool HasKey(concurrent_key_t const key) const override;
107 std::size_t GetSize() const override;
108
109 private:
110
112 container_t m_container;
113 };
114
115 // ==================================================================================
116 // ConcurrentParamFactory
117 // ==================================================================================
118
120 class ConcurrentParamFactory
121 {
122
123 public:
124 ConcurrentParamFactory() = delete;
125
130 template <typename T>
131 static std::unique_ptr<ConcurrentParam<T>> Create()
132 {
133
134 if(GlobalConcurrencyModel() == "none")
135 GlobalConcurrencyModel = "memoize"; // the safest default, but not the fastest for single-threaded users
136
137 if(GlobalConcurrencyModel() == "single")
138 return std::make_unique<SingleThreadParam<T>>();
139 else if(GlobalConcurrencyModel() == "memoize")
140 return std::make_unique<MemoizedParam<T>>();
141
142 throw std::runtime_error("unknown GlobalConcurrencyModel '" + GlobalConcurrencyModel() + "'; valid options are 'single' or 'memoize'");
143 }
144 };
145
146}
static std::unique_ptr< ConcurrentParam< T > > Create()
create a new ConcurrentParam-derived class instance
virtual void Save(T const &value, concurrent_key_t const key)=0
modify a value
std::mutex m_mutex
mutex for this ConcurrentParam
bool m_needs_hashing
whether this ConcurrentParam needs hashing for calling Load or Save
virtual bool HasKey(concurrent_key_t const key) const =0
bool NeedsHashing() const
whether or not hashing is needed to use this parameter
virtual std::size_t GetSize() const =0
bool m_empty
whether this ConcurrentParam has something saved
virtual T const Load(concurrent_key_t const key) const =0
access a stored value
ConcurrentParam(std::string const &model)
T const Load(concurrent_key_t const key) const override
access a stored value
bool HasKey(concurrent_key_t const key) const override
std::size_t GetSize() const override
void Save(T const &value, concurrent_key_t const key) override
modify a value
std::size_t GetSize() const override
void Save(T const &value, concurrent_key_t const key) override
modify a value
T const Load(concurrent_key_t const key) const override
access a stored value
bool HasKey(concurrent_key_t const key) const override