-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconditional_trigger.h
61 lines (49 loc) · 1.44 KB
/
conditional_trigger.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef conditional_trigger_h
#define conditional_trigger_h
#include "variant/variant.h"
#include <unordered_map>
#include <functional>
namespace async
{
class conditional_trigger
{
public:
conditional_trigger() = default;
conditional_trigger(
const std::function<void(conditional_trigger&)>& fn,
const std::function<bool()>& cond = nullptr
)
: fn_(fn), cond_(cond)
{ }
void operator()(bool trigger = false);
void set_property(const std::string& key, const estd::variant& value);
estd::variant& get_property(const std::string& key);
const estd::variant& get_property(const std::string& key) const;
void invalidate() { is_valid_ = false; }
private:
std::function<void(conditional_trigger&)> fn_;
std::function<bool()> cond_;
std::unordered_map<std::string, estd::variant> local_storage_;
bool is_valid_ = true;
};
inline void conditional_trigger::operator()(bool trigger)
{
if (is_valid_ && (trigger || (cond_ && cond_())))
{
fn_(*this);
}
}
inline void conditional_trigger::set_property(const std::string& key, const estd::variant& value)
{
local_storage_[key] = value;
}
inline estd::variant& conditional_trigger::get_property(const std::string& key)
{
return local_storage_[key];
}
inline const estd::variant& conditional_trigger::get_property(const std::string& key) const
{
return local_storage_.at(key);
}
} // namespace async
#endif