Iguana
1.0.0
Implementation Guardian of Analysis Algorithms
Toggle main menu visibility
Loading...
Searching...
No Matches
ConcurrentParam.h
1
#pragma once
2
3
#include "GlobalParam.h"
4
5
namespace
iguana {
6
8
using
concurrent_key_t = std::size_t;
9
10
// ==================================================================================
11
// ConcurrentParam
12
// ==================================================================================
13
16
template
<
typename
T>
17
class
ConcurrentParam
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
54
bool
m_needs_hashing
;
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
}
iguana::ConcurrentParamFactory::Create
static std::unique_ptr< ConcurrentParam< T > > Create()
create a new ConcurrentParam-derived class instance
Definition
ConcurrentParam.h:131
iguana::ConcurrentParam::IsEmpty
bool IsEmpty() const
Definition
ConcurrentParam.h:49
iguana::ConcurrentParam::Save
virtual void Save(T const &value, concurrent_key_t const key)=0
modify a value
iguana::ConcurrentParam::m_mutex
std::mutex m_mutex
mutex for this ConcurrentParam
Definition
ConcurrentParam.h:57
iguana::ConcurrentParam::m_needs_hashing
bool m_needs_hashing
whether this ConcurrentParam needs hashing for calling Load or Save
Definition
ConcurrentParam.h:54
iguana::ConcurrentParam::HasKey
virtual bool HasKey(concurrent_key_t const key) const =0
iguana::ConcurrentParam::NeedsHashing
bool NeedsHashing() const
whether or not hashing is needed to use this parameter
Definition
ConcurrentParam.h:43
iguana::ConcurrentParam::GetSize
virtual std::size_t GetSize() const =0
iguana::ConcurrentParam::m_empty
bool m_empty
whether this ConcurrentParam has something saved
Definition
ConcurrentParam.h:60
iguana::ConcurrentParam::Load
virtual T const Load(concurrent_key_t const key) const =0
access a stored value
iguana::ConcurrentParam::ConcurrentParam
ConcurrentParam(std::string const &model)
iguana::MemoizedParam::Load
T const Load(concurrent_key_t const key) const override
access a stored value
iguana::MemoizedParam::HasKey
bool HasKey(concurrent_key_t const key) const override
iguana::MemoizedParam::GetSize
std::size_t GetSize() const override
iguana::MemoizedParam::Save
void Save(T const &value, concurrent_key_t const key) override
modify a value
iguana::SingleThreadParam::GetSize
std::size_t GetSize() const override
iguana::SingleThreadParam::Save
void Save(T const &value, concurrent_key_t const key) override
modify a value
iguana::SingleThreadParam::Load
T const Load(concurrent_key_t const key) const override
access a stored value
iguana::SingleThreadParam::HasKey
bool HasKey(concurrent_key_t const key) const override
iguana_v1.0.0
src
iguana
services
ConcurrentParam.h
Generated by
1.18.0