/
AutoConfig.h
179 lines (147 loc) · 5.53 KB
/
AutoConfig.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved.
#pragma once
#include "AutoConfigBase.h"
#include "Autowired.h"
#include "ConfigRegistry.h"
/// <summary>
/// The type underlying the AutoConfig System.
/// Represents a unique type created by the combination of the type and a set of sigils.
/// Responsible for tracking changes to the underlying value and triggering signals,
/// making sure values are inherited correctly from enclosing contexts, and providing
/// a primitive polymorphic get/set interface (void*)
/// <summary>
template<class T, class... TKey>
class AutoConfigVar:
public AutoConfigVarBase
{
public:
static_assert(sizeof...(TKey) >= 1, "Must provide a key and optionally a set of namespaces");
template<typename t_Arg, typename ...t_Args>
AutoConfigVar(t_Arg &&arg, t_Args&&... args) :
AutoConfigVarBase(typeid(ConfigTypeExtractor<TKey...>), true),
m_value(std::forward<t_Arg>(arg), std::forward<t_Args>(args)...)
{
// Register with config registry
(void)AutoConfigVar<T, TKey...>::RegistryEntry;
if (RegistryEntry.m_hasValidator) {
if (!RegistryEntry.validatorInternal()(m_value)) {
throw autowiring_error("Cannot construct AutoConfigVar with a value that fails validation");
}
}
onChangedSignal(*this);
}
AutoConfigVar() :
AutoConfigVarBase(typeid(ConfigTypeExtractor<TKey...>)),
m_value()
{
// Register with config registry
(void)AutoConfigVar<T, TKey...>::RegistryEntry;
const auto ctxt = m_context.lock();
if (!ctxt)
return;
//This will wind up being recursive
auto parent = ctxt->GetParentContext();
if (parent != nullptr) {
auto parentVar = parent->template Inject<AutoConfigVar<T, TKey...>>();
//Only copy the value if it's initalized. Base::AutoInit will take care of
//the various listing notifications.
if (parentVar->IsConfigured()) {
m_isConfigured = true;
m_value = parentVar->m_value;
}
m_parentRegistration = *parentVar += [this](const T& val){
RunValidation(val);
SetInternal(val);
};
}
}
public:
operator const T&() const { return m_value; }
const T* operator->() const { return &m_value; }
void operator=(const T& newValue) {
RunValidation(newValue);
if (m_parentRegistration) {
auto parent_ctxt = m_context.lock()->GetParentContext();
AutowiredFast<AutoConfigVar<T, TKey...>> parentVar(parent_ctxt);
*parentVar -= m_parentRegistration;
m_parentRegistration = nullptr;
OnSetLocally();
}
SetInternal(newValue);
}
void Get(void* pValue) const override { *reinterpret_cast<T*>(pValue) = m_value; }
void Set(const void* pValue) override { *this = *reinterpret_cast<const T*>(pValue); }
void SetParsed(const std::string& value) override {
*this = RegistryEntry.template parseInternal<T>(value);
}
// Add a callback for when this config value changes
t_OnChangedSignal::registration_t* operator+=(std::function<void(const T&)>&& fx) {
return onChangedSignal += [fx](const AutoConfigVarBase& var){
fx(reinterpret_cast<const AutoConfigVar<T,TKey...>*>(&var)->m_value);
};
}
void operator-=(t_OnChangedSignal::registration_t* node) { onChangedSignal -= node; }
private:
T m_value;
void RunValidation(const T& val) {
if (RegistryEntry.m_hasValidator) {
if (!RegistryEntry.validatorInternal()(val)) {
throw autowiring_error("Validator rejected set for config value");
}
}
}
void SetInternal(const T& val) {
m_isConfigured = true;
m_value = val;
onChangedSignal(*this);
}
public:
static std::shared_ptr<AutoConfigVarBase> Inject(const std::shared_ptr<CoreContext>& ctxt, const void* value) {
if (!value)
return ctxt->Inject<AutoConfigVar<T, TKey...>>();
else
return ctxt->Inject<AutoConfigVar<T, TKey...>>(*reinterpret_cast<const T*>(value));
}
static const ConfigRegistryEntryT<T, TKey...> RegistryEntry;
};
template<class T, class... TKey>
const ConfigRegistryEntryT<T, TKey...> AutoConfigVar<T, TKey...>::RegistryEntry(&AutoConfigVar<T, TKey...>::Inject);
/// <summary>
/// Register an attribute with the AutoConfig system. For example
///
/// AutoConfig<int, struct MyNamespace, struct MyKey> m_myVal;
/// defines the key "MyNamespace.MyKey"
///
/// The Namespace field is optional, so
/// AutoConfig<int, struct MyKey> m_myVal;
/// defines the key "MyKey"
///
/// AutoConfig values can also be set from the AutoConfigListing in the same context. The string key
/// is used as the identifier for the value.
/// </summary>
template<class T, class... TKeys>
class AutoConfig : public AutoRequired<AutoConfigVar<T, TKeys...>> {
public:
typedef AutoConfigVar<T, TKeys...> t_Var;
using AutoRequired<t_Var>::operator*;
AutoConfig(const std::shared_ptr<CoreContext>& ctxt = CoreContext::CurrentContext()) :
AutoRequired<t_Var>(ctxt)
{
}
AutoConfig(T&& initialValue, const std::shared_ptr<CoreContext>& ctxt = CoreContext::CurrentContext()) :
AutoRequired<t_Var>(ctxt, std::move(initialValue))
{
if (!(*this)->IsLocal()) {
**this = std::move(initialValue);
}
}
template<typename t_Arg, typename ...t_Args>
explicit AutoConfig(t_Arg&& arg, t_Args&&... args) :
AutoRequired<t_Var>(CoreContext::CurrentContext(), std::forward<t_Arg>(arg), std::forward<t_Args>(args)...)
{
//If we wind up being a reference to an existing value, we may still want to set it...
if (!(*this)->IsLocal()) {
**this = T(std::forward<t_Arg>(arg), std::forward<t_Args>(args)...);
}
}
};