Vuo 2.4.2
Loading...
Searching...
No Matches
VuoSerializable.cc
Go to the documentation of this file.
1
10#include "VuoSerializable.hh"
11#include "type.h"
12
13#include <typeinfo>
14#include <cxxabi.h>
15
17#ifdef VUO_COMPILER
19 "title" : "Serializable Object",
20 "description" : "Base class for serializable objects.",
21 });
22#endif
24
25VuoSerializable::ConstructorMap *VuoSerializable::constructors;
26
27std::string VuoSerializable::type = "";
28
34bool VuoSerializable::registerSubclass(std::string _type, Constructor makeFromJson)
35{
36 static dispatch_once_t init = 0;
37 dispatch_once(&init, ^{
38 VuoSerializable::constructors = new VuoSerializable::ConstructorMap;
39 });
40 (*constructors)[_type] = makeFromJson;
41 return true;
42}
43
48{
49 json_object *o = NULL;
50 if (!json_object_object_get_ex(js, "type", &o))
51 return NULL;
52
53 std::string typeName = json_object_get_string(o);
54 VuoSerializable::ConstructorMap::iterator i = VuoSerializable::constructors->find(typeName);
55 if (i != VuoSerializable::constructors->end())
56 return i->second(js);
57
58 return NULL;
59}
60
65{
66 json_object *json = json_object_new_object();
67 json_object_object_add(json, "type", json_object_new_string(getType().c_str()));
68 return json;
69}
70
77std::string VuoSerializable::getType() const
78{
79 int status;
80 char *unmangled = abi::__cxa_demangle(typeid(*this).name(), 0, 0, &status);
81 if (status == 0)
82 {
83 std::string unmangledS(unmangled);
84 free(unmangled);
85 return unmangledS;
86 }
87 return "";
88}
89
94{
96}
97
102{
103}
104
111{
112 delete (VuoSerializable *)t;
113}