-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlagsObject.cpp
319 lines (290 loc) · 14.1 KB
/
FlagsObject.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//===- FlagsObject.cpp - metadata type implement define -----------------------------*- C++ -*-===//
//
// This file implements CFlagsCfgObject class code.
//
//===------------------------------------------------------------------------------------------===//
#include "stdafx.h"
#include "FlagsObject.h"
#include "utils.h"
#include "HipsCfgObject.h"
#include "ParsingImpl.h"
namespace cchips {
void CFlagsCfgObject::InitializeAdditionalType(const CRapidJsonWrapper& document)
{
if (auto anyvalue(document.GetMember(FL_ADDITIONAL));
anyvalue.has_value() && anyvalue.type() == typeid(ConstRapidObject))
{
std::for_each(std::any_cast<ConstRapidObject>(anyvalue).begin(), std::any_cast<ConstRapidObject>(anyvalue).end(), [&](auto& additional_object) {
if (additional_object.value.IsNull())
{
error_log("CFlagsConfigObject::Initialize the additional {} is null!", additional_object.name.GetString());
}
else {
if (std::string_view alias_name = additional_object.name.GetString(); alias_name.length() && additional_object.value.IsString())
{
std::string_view iden_name = additional_object.value.GetString();
std::shared_ptr<CObObject> ob_object = CLexerTy::GetInstance().GetIdentifier(iden_name);
if (ob_object)
AddNewType(IDENPAIR{ alias_name, std::move(ob_object) });
else
AddDelayedType(DELTPAIR(alias_name, iden_name));
}
}
});
}
return;
}
void CFlagsCfgObject::InitializeStructType(const CRapidJsonWrapper& document)
{
if (auto anyvalue(document.GetMember(FL_STRUCTURE));
anyvalue.has_value() && anyvalue.type() == typeid(ConstRapidObject))
{
std::for_each(std::any_cast<ConstRapidObject>(anyvalue).begin(), std::any_cast<ConstRapidObject>(anyvalue).end(), [&](auto& struct_object) {
if (struct_object.value.IsNull())
{
error_log("CFlagsConfigObject::Initialize the struct {} is null!", struct_object.name.GetString());
}
else {
bool bdelayed = false;
bool bsuccess = true;
std::string_view struct_name = struct_object.name.GetString();
std::shared_ptr<CStructObject> ob_object = std::make_shared<CStructObject>(struct_name);
for (auto& data_object : struct_object.value.GetObject())
{
if (std::string_view element_name = data_object.name.GetString(); element_name.length() && !data_object.value.IsNull())
{
if (data_object.value.IsString())
{
std::string_view iden_name = data_object.value.GetString();
std::shared_ptr<CObObject> iden_object = CLexerTy::GetInstance().GetIdentifier(iden_name);
if (!iden_object) bdelayed = true;
(*ob_object).AddElement(element_name, CStructObject::ElementTypeDefine(std::string(iden_name), { CStructObject::InvalidOffset, std::move(iden_object) }));
}
else
{
error_log("CFlagsConfigObject::Initialize the element {}! not a string!", std::string(element_name));
bsuccess = false; break;
}
}
else
{
error_log("CFlagsConfigObject::Initialize the element {}! is null!", data_object.name.GetString());
bsuccess = false; break;
}
}
if (bsuccess) {
if (bdelayed)
AddDelayedStruct(DELSPAIR(struct_name, std::move(ob_object)));
else
AddNewType(DELSPAIR(struct_name, std::move(ob_object)));
}
}
});
}
return;
}
void CFlagsCfgObject::InitializeFlagType(const CRapidJsonWrapper& document)
{
if (auto anyvalue(document.GetMember(FL_FLAGS));
anyvalue.has_value() && anyvalue.type() == typeid(ConstRapidObject))
{
std::for_each(std::any_cast<ConstRapidObject>(anyvalue).begin(), std::any_cast<ConstRapidObject>(anyvalue).end(), [&](auto& flag_object) {
std::string_view flag_name = flag_object.name.GetString();
std::shared_ptr<CFlagObject> ob_object = std::make_shared<CFlagObject>(flag_name);
if (!ob_object) return;
std::string name_str(flag_name);
auto anydata(document.GetMember(std::vector<std::string> {FL_FLAGS, name_str, FLS_DATA}));
auto anyop(document.GetMember(std::vector<std::string> {FL_FLAGS, name_str, FLS_OP}));
auto anystrflg(document.GetMember(std::vector<std::string> {FL_FLAGS, name_str, FLS_VALUE}));
if ([&]() ->bool{
if (!anydata.has_value() || !anyop.has_value() || !anystrflg.has_value())
{
error_log("CFlagsConfigObject::Initialize the flags {} is a invalid flags!", std::string(flag_name));
return false;
}
if( anydata.type() != typeid(std::string_view) ||
anyop.type() != typeid(std::string_view) ||
anystrflg.type() != typeid(ConstRapidObject) )
{
error_log("CFlagsConfigObject::Initialize the flags {} data format incorrect!", std::string(flag_name));
return false;
}
return true;
}())
{
if (CFlagObject::_flag_operation flag_op; (flag_op = ob_object->GetFlagOp(std::string(std::any_cast<std::string_view>(anyop)))) == CFlagObject::flag_invalid)
{
error_log("CFlagsConfigObject::Initialize the flags {} flag_op incorrect!", std::string(flag_name));
}
else {
if (std::shared_ptr<CObObject> data_object; (data_object = CLexerTy::GetInstance().GetIdentifier(std::string(std::any_cast<std::string_view>(anydata)))) == nullptr)
{
error_log("CFlagsConfigObject::Initialize the flags {} data_object incorrect!", std::string(flag_name));
}
else {
if (std::unique_ptr<std::map<std::string, unsigned int>> flag_values = std::make_unique<std::map<std::string, unsigned int>>(); flag_values)
{
for (auto& value_object : std::any_cast<ConstRapidObject>(anystrflg))
(*flag_values)[value_object.name.GetString()] = value_object.value.GetUint();
if (flag_values->size() == 0)
{
error_log("CFlagsConfigObject::Initialize the flags {} flag_values null!", std::string(flag_name));
}
else {
if (!ob_object->AddFlags(std::move(data_object), flag_op, std::move(flag_values)))
{
error_log("CFlagsConfigObject::Initialize the flags {} AddFlags failed!", std::string(flag_name));
}
else {
AddNewType(IDENPAIR{ flag_name, std::move(ob_object) });
}
}
}
}
}
}
});
}
return;
}
bool CFlagsCfgObject::Initialize(const std::string_view& json_str)
{
if (json_str.length() == 0) return false;
CRapidJsonWrapper document(json_str);
if (!document.IsValid()) {
//config data is incorrect.
error_log("CFlagsConfigObject::Initialize failed!");
return false;
}
InitializeAdditionalType(document);
InitializeStructType(document);
InitializeFlagType(document);
// processing delayed types
ProcessingDelayTypes();
m_bValid = true;
return true;
}
bool CFlagsCfgObject::AddNewType(const IDENPAIR& delay_pair) const
{
return CLexerTy::GetInstance().AddIdentifier(delay_pair);
}
bool CFlagsCfgObject::AddDelayedType(const DELTPAIR& delay_pair)
{
auto it = m_delayed_type_list.find(delay_pair.first);
if (it != m_delayed_type_list.end())
{
error_log("AddDelayedType: the type {} has exist!", delay_pair.first);
return false;
}
m_delayed_type_list[delay_pair.first] = delay_pair.second;
return true;
}
bool CFlagsCfgObject::AddDelayedStruct(const DELSPAIR& delay_pair)
{
auto it = m_delayed_struct_list.find(delay_pair.first);
if (it != m_delayed_struct_list.end())
{
error_log("AddDelayedStruct: new struct {} has exist!", it->first);
return false;
}
m_delayed_struct_list[delay_pair.first] = delay_pair.second;
return true;
}
bool CFlagsCfgObject::ProcessingDelayType(const std::string& name)
{
const auto it = m_delayed_type_list.find(CLexerTy::GetWordRoot(name));
if (it == m_delayed_type_list.end()) return false;
return ProcessingDelayType(*it);
}
bool CFlagsCfgObject::ProcessingDelayType(const DELTPAIR& delt_pair)
{
std::shared_ptr<CObObject> ob_object = CLexerTy::GetInstance().GetIdentifier(delt_pair.second);
if (ob_object)
return AddNewType(IDENPAIR{ delt_pair.first, ob_object });
else
{
if (!ProcessingDelayTypes(delt_pair.second))
{
error_log("ProcessingDelayType: delay type {} can not handle!", delt_pair.first);
return false;
}
ob_object = CLexerTy::GetInstance().GetIdentifier(delt_pair.second);
if (!ob_object)
{
error_log("ProcessingDelayType: delay type {} can not handle!", delt_pair.first);
return false;
}
return AddNewType(IDENPAIR{ delt_pair.first, ob_object });
}
return false;
}
bool CFlagsCfgObject::ProcessingDelayStruct(const std::string& name)
{
const auto it = m_delayed_struct_list.find(CLexerTy::GetWordRoot(name));
if (it == m_delayed_struct_list.end()) return false;
return ProcessingDelayStruct(*it);
}
bool CFlagsCfgObject::ProcessingDelayStruct(const DELSPAIR& dels_pair)
{
std::map<int, std::pair<std::string, CStructObject::ElementTypeDefine>> cache_map;
for (auto delay_elem : (*(dels_pair.second)))
{
cache_map[delay_elem.second.second.first] = std::pair<std::string, CStructObject::ElementTypeDefine>(std::move(delay_elem.first), std::move(delay_elem.second));
}
std::string_view struct_name = dels_pair.second->GetName();
std::shared_ptr<CStructObject> ob_object = std::make_shared<CStructObject>(struct_name);
for (const auto& struc_elem : cache_map)
{
std::shared_ptr<CObObject> iden_object = nullptr;
if (struc_elem.second.second.second.second)
iden_object = struc_elem.second.second.second.second;
else
{
iden_object = CLexerTy::GetInstance().GetIdentifier(struc_elem.second.second.first);
if (!iden_object)
{
if (!ProcessingDelayTypes(struc_elem.second.second.first))
{
error_log("ProcessingDelayStruct: delay struct({}) element({}) can not handle!", dels_pair.first, struc_elem.second.first);
return false;
}
iden_object = CLexerTy::GetInstance().GetIdentifier(struc_elem.second.second.first);
if (!iden_object)
{
error_log("ProcessingDelayStruct: delay struct({}) element({}) can not handle!", dels_pair.first, struc_elem.second.first);
return false;
}
}
}
(*ob_object).AddElement(struc_elem.second.first, CStructObject::ElementTypeDefine(std::string(struc_elem.second.second.first), { CStructObject::InvalidOffset, std::move(iden_object) }));
}
return AddNewType(IDENPAIR{ dels_pair.first, ob_object });
}
bool CFlagsCfgObject::ProcessingDelayTypes(const std::string& name)
{
if (ProcessingDelayType(name))
return true;
return ProcessingDelayStruct(name);
}
void CFlagsCfgObject::ProcessingDelayType()
{
std::for_each(m_delayed_type_list.begin(), m_delayed_type_list.end(), [&](const auto& pair) {
if (ProcessingDelayType(pair))
return;
ProcessingDelayStruct(pair.first);
});
m_delayed_type_list.clear();
}
void CFlagsCfgObject::ProcessingDelayStruct()
{
std::for_each(m_delayed_struct_list.begin(), m_delayed_struct_list.end(), [&](auto& pair) {
ProcessingDelayStruct(pair);
});
m_delayed_struct_list.clear();
}
void CFlagsCfgObject::ProcessingDelayTypes()
{
ProcessingDelayType();
ProcessingDelayStruct();
}
} // namespace cchips