4#include "GlobalParam.h"
110 std::unordered_map<concurrent_key_t, T> m_container;
119 class ConcurrentParamFactory
123 ConcurrentParamFactory() =
delete;
128 if(GlobalConcurrencyModel() ==
"none")
129 GlobalConcurrencyModel =
"memoize";
130 if(GlobalConcurrencyModel() !=
"single" && GlobalConcurrencyModel() !=
"memoize")
131 throw std::runtime_error(
"unknown GlobalConcurrencyModel '" + GlobalConcurrencyModel() +
"'; valid options are 'single' or 'memoize'");
139 template <
typename T>
140 static std::unique_ptr<ConcurrentParam<T>>
Create(std::string
const& name)
143 if(GlobalConcurrencyModel() ==
"single")
144 return std::make_unique<SingleThreadParam<T>>(name);
146 return std::make_unique<MemoizedParam<T>>(name);
156 template <
typename... Ts>
179 if(GlobalConcurrencyModel() ==
"single") {
184 std::size_t seed = 0;
185 (hash_combine(seed, args), ...);
196 return m_make_key(args...);
204 template <
typename T>
205 static void hash_combine(std::size_t& seed, T
const& value)
207 seed ^= std::hash<T>{}(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
std::size_t concurrent_key_t
concurrent key type for ConcurrentParam objects
static std::unique_ptr< ConcurrentParam< T > > Create(std::string const &name)
create a new ConcurrentParam-derived class instance
static void CheckConcurrencyModel()
check the concurrency model; if not set, it will set a default
virtual void Save(T const &value, concurrent_key_t const key)=0
modify a value
ConcurrentParam(std::string const &model, std::string const &name)
std::shared_mutex m_mutex
mutex for this ConcurrentParam
virtual bool NeedsUpdate(T const &value, concurrent_key_t const key) const =0
if either key is not found or value does not match the value at key, the caller likely needs to call ...
virtual bool HasKey(concurrent_key_t const key) const =0
std::string const m_name
a name for this parameter
virtual T const Load(concurrent_key_t const key) const =0
access a stored value
concurrent_key_t MakeKey(Ts const &... args)
make a new key
MemoizedParam(std::string const &name)
T const Load(concurrent_key_t const key) const override
access a stored value
bool NeedsUpdate(T const &value, concurrent_key_t const key) const override
if either key is not found or value does not match the value at key, the caller likely needs to call ...
bool HasKey(concurrent_key_t const key) const override
void Save(T const &value, concurrent_key_t const key) override
modify a value
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 NeedsUpdate(T const &value, concurrent_key_t const key) const override
if either key is not found or value does not match the value at key, the caller likely needs to call ...
bool HasKey(concurrent_key_t const key) const override
SingleThreadParam(std::string const &name)