Vuo
2.4.4
Loading...
Searching...
No Matches
library
VuoSerializable.cc
Go to the documentation of this file.
1
10
#include "
VuoSerializable.hh
"
11
12
#include <typeinfo>
13
#include <cxxabi.h>
14
16
#ifdef VUO_COMPILER
17
VuoModuleMetadata
({
18
"title"
:
"Serializable Object"
,
19
"description"
:
"Base class for serializable objects."
,
20
});
21
#endif
23
24
VuoSerializable::ConstructorMap *VuoSerializable::constructors;
25
26
std::string
VuoSerializable::type
=
""
;
27
33
bool
VuoSerializable::registerSubclass
(std::string _type, Constructor makeFromJson)
34
{
35
static
dispatch_once_t init = 0;
36
dispatch_once(&init, ^{
37
VuoSerializable::constructors =
new
VuoSerializable::ConstructorMap;
38
});
39
(*constructors)[_type] =
makeFromJson
;
40
return
true
;
41
}
42
46
VuoSerializable
*
VuoSerializable::makeFromJson
(json_object *js)
47
{
48
json_object *o = NULL;
49
if
(!json_object_object_get_ex(js,
"type"
, &o))
50
return
NULL;
51
52
std::string typeName = json_object_get_string(o);
53
VuoSerializable::ConstructorMap::iterator i = VuoSerializable::constructors->find(typeName);
54
if
(i != VuoSerializable::constructors->end())
55
return
i->second(js);
56
57
return
NULL;
58
}
59
63
json_object *
VuoSerializable::getJson
()
64
{
65
json_object *json = json_object_new_object();
66
json_object_object_add(json,
"type"
, json_object_new_string(
getType
().c_str()));
67
return
json;
68
}
69
76
std::string
VuoSerializable::getType
()
const
77
{
78
int
status;
79
char
*unmangled = abi::__cxa_demangle(
typeid
(*this).name(), 0, 0, &status);
80
if
(status == 0)
81
{
82
std::string unmangledS(unmangled);
83
free(unmangled);
84
return
unmangledS;
85
}
86
return
""
;
87
}
88
92
VuoSerializable::VuoSerializable
()
93
{
94
VuoRegister
(
this
,
VuoSerializable::destroy
);
95
}
96
100
VuoSerializable::~VuoSerializable
()
101
{
102
}
103
109
void
VuoSerializable::destroy
(
void
*t)
110
{
111
delete
(
VuoSerializable
*)t;
112
}
Generated on Thu Nov 9 2023 16:59:29 for Vuo by
1.9.8