27 using IntVariant = IntegerObj::IntVariant;
28 using FloatVariant = FloatObj::FloatVariant;
31 static bool isIntegerType(
const std::string& type)
33 static const std::unordered_set<std::string> intTypes = {
"signed char",
"int8_t",
"char",
"short",
"int16_t",
34 "int",
"int32_t",
"long",
"long long",
"int64_t",
"unsigned char",
"uint8_t",
"unsigned short",
"uint16_t",
35 "unsigned int",
"unsigned",
"uint32_t",
"unsigned long",
"unsigned long long",
"uint64_t" };
36 return intTypes.count(type) > 0;
39 static bool isFloatType(
const std::string& type)
41 return type ==
"float" || type ==
"double" || type ==
"long double";
44 static bool isStringType(
const std::string& type)
47 return type.find(
"std::string") != std::string::npos ||
48 type.find(
"std::__cxx11::basic_string") != std::string::npos ||
49 type.find(
"basic_string") != std::string::npos;
52 static bool isContainerType(
const std::string& type)
54 return type.find(
"std::vector") != std::string::npos || type.find(
"std::map") != std::string::npos ||
55 type.find(
"std::unordered_map") != std::string::npos || type.find(
"std::set") != std::string::npos ||
56 type.find(
"std::unordered_set") != std::string::npos || type.find(
"std::list") != std::string::npos ||
57 type.find(
"std::deque") != std::string::npos || type.find(
"std::multimap") != std::string::npos ||
58 type.find(
"std::array") != std::string::npos;
61 static std::unique_ptr<IntegerObj> makeIntegerObj(
const std::string& type,
void* addr)
63 if ( !addr )
return nullptr;
64 if ( type ==
"signed char" || type ==
"int8_t" || type ==
"char" )
65 return std::make_unique<IntegerObj>(*
static_cast<int8_t*
>(addr), addr);
66 if ( type ==
"short" || type ==
"int16_t" )
67 return std::make_unique<IntegerObj>(*
static_cast<int16_t*
>(addr), addr);
68 if ( type ==
"int" || type ==
"int32_t" )
69 return std::make_unique<IntegerObj>(*
static_cast<int32_t*
>(addr), addr);
70 if ( type ==
"long" || type ==
"long long" || type ==
"int64_t" )
71 return std::make_unique<IntegerObj>(*
static_cast<int64_t*
>(addr), addr);
72 if ( type ==
"unsigned char" || type ==
"uint8_t" )
73 return std::make_unique<IntegerObj>(*
static_cast<uint8_t*
>(addr), addr);
74 if ( type ==
"unsigned short" || type ==
"uint16_t" )
75 return std::make_unique<IntegerObj>(*
static_cast<uint16_t*
>(addr), addr);
76 if ( type ==
"unsigned int" || type ==
"unsigned" || type ==
"uint32_t" )
77 return std::make_unique<IntegerObj>(*
static_cast<uint32_t*
>(addr), addr);
78 if ( type ==
"unsigned long" || type ==
"unsigned long long" || type ==
"uint64_t" )
79 return std::make_unique<IntegerObj>(*
static_cast<uint64_t*
>(addr), addr);
83 static std::unique_ptr<FloatObj> makeFloatObj(
const std::string& type,
void* addr)
85 if ( !addr )
return nullptr;
86 if ( type ==
"float" )
return std::make_unique<FloatObj>(*
static_cast<float*
>(addr), addr);
87 if ( type ==
"double" )
return std::make_unique<FloatObj>(*
static_cast<double*
>(addr), addr);
88 if ( type ==
"long double" )
return std::make_unique<FloatObj>(*
static_cast<long double*
>(addr), addr);
97 if ( !objMap )
return nullptr;
99 const std::string type = objMap->
getType();
100 void* addr = objMap->
getAddr();
103 if ( type ==
"bool" && addr ) {
110 auto* o =
new BoolObj(*
static_cast<bool*
>(addr), addr);
115 if ( isIntegerType(type) ) {
122 if (
auto o = makeIntegerObj(type, addr) ) {
131 if ( isFloatType(type) ) {
138 if (
auto o = makeFloatObj(type, addr) ) {
147 if ( isStringType(type) && addr ) {
148 auto* o =
new StringObj(*
static_cast<std::string*
>(addr), addr);
170 if ( objMap->
isContainer() || isContainerType(type) ) {
172 auto* c =
new ContainerObj(name, type, vars.size(),
nullptr);
173 for (
const auto& [n, m] : vars )
174 if (
auto* child = convert(n, m, recursive) ) c->addChildObj(child);
178 if ( objMap->getCategory() == ObjectMap::ObjectCategory::Component ) {
193 if (
auto* child = convert(n, m,
false) ) node->addChildObj(child);
198 static void addChildrenFromMap(
ObjTreeCont* parent,
const ObjectMultimap& variables)
200 if ( !parent )
return;
201 for (
const auto& [name, objMap] : variables ) {
207 static void addChildrenFromMapRecursive(
ObjTreeCont* parent,
const ObjectMultimap& variables)
209 if ( !parent )
return;
210 for (
const auto& [name, objMap] : variables ) {
217 static std::unique_ptr<ObjTreeCont> convertTree(
const std::string& rootName,
ObjectMap* objMap)
219 auto root = std::make_unique<ObjTreeCont>(rootName, objMap->
getType());
221 for (
const auto& [name, childMap] : variables ) {
222 ObjTreeCont* child = convert(name, childMap,
true);
224 root->addChildObj(child);
241 if ( !compNode )
return false;
244 if ( !comp )
return false;
254 const auto& variables =
serializer.getVariables();
257 std::vector<void*> subCompAddrs;
259 collectSubComponentAddrs(compInfo, subCompAddrs);
262 for (
const auto& [name, objMap] : variables ) {
263 if ( !objMap )
continue;
266 if ( std::find(subCompAddrs.begin(), subCompAddrs.end(), objMap->
getAddr()) != subCompAddrs.end() )
269 ObjTreeCont* child = convert(name, objMap, recursive);
276 serializeSubComponents(compNode, compInfo, recursive);
290 if ( !parent )
return;
306 const std::string& rootName,
ObjectMap* objMap,
bool recursive =
true)
308 auto root = convertTree(rootName, objMap);
309 if ( !root )
return nullptr;
318 static void collectSubComponentAddrs(
ComponentInfo* compInfo, std::vector<void*>& addrs)
320 auto& subComps = compInfo->getSubComponents();
322 for (
auto it = subComps.begin(); it != subComps.end(); ++it ) {
326 addrs.push_back(
static_cast<void*
>(sub));
331 static void serializeSubComponents(ObjTreeCont* parent, ComponentInfo* compInfo,
bool recursive)
333 auto& subComps = compInfo->getSubComponents();
334 for (
auto& [compId, subInfo] : subComps ) {
335 BaseComponent* sub = subInfo.getComponent();
336 if ( !sub )
continue;
338 auto* subObj =
new ComponentObj(sub, &subInfo);
339 subObj->setName(subInfo.getName());
340 subObj->setType(subInfo.getType());
343 ComponentSerializer subSerializer(sub);
344 subSerializer.serialize();
346 if ( subSerializer.hasSerialized() ) {
347 const auto& subVars = subSerializer.getVariables();
350 std::vector<void*> nestedAddrs;
351 collectSubComponentAddrs(&subInfo, nestedAddrs);
353 for (
const auto& [name, objMap] : subVars ) {
354 if ( !objMap )
continue;
355 if ( std::find(nestedAddrs.begin(), nestedAddrs.end(), objMap->
getAddr()) != nestedAddrs.end() )
358 ObjTreeCont* child = convert(name, objMap, recursive);
359 if ( child ) subObj->addChildObj(child);
365 serializeSubComponents(subObj, &subInfo, recursive);
368 parent->addChildObj(subObj);