Vuo 2.4.4
Loading...
Searching...
No Matches
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
18 "title" : "Serializable Object",
19 "description" : "Base class for serializable objects.",
20 });
21#endif
23
24VuoSerializable::ConstructorMap *VuoSerializable::constructors;
25
26std::string VuoSerializable::type = "";
27
33bool 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
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
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
76std::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
96
103
110{
111 delete (VuoSerializable *)t;
112}