1 #include <unordered_map> 4 namespace annotatable {
9 Anything::Anything(
void *data, std::function<
void(
void *data)> destructor, std::type_index type) :
11 destructor(destructor),
18 Anything::Anything() :
20 destructor([](void*){}),
21 type(std::type_index(
typeid(
nullptr)))
27 Anything::~Anything() {
36 Anything::Anything(Anything &&src) :
38 destructor(
std::move(src.destructor)),
39 type(
std::move(src.type))
47 Anything& Anything::operator=(Anything &&src) {
52 destructor = std::move(src.destructor);
53 type = std::move(src.type);
61 std::type_index Anything::get_type_index()
const {
70 void SerDesRegistry::serialize(std::shared_ptr<Anything> obj, cbor::MapWriter &map)
const {
72 auto it = serializers.find(obj->get_type_index());
73 if (it != serializers.end()) {
83 std::shared_ptr<Anything> SerDesRegistry::deserialize(
const std::string &key,
const cbor::Reader &value)
const {
84 auto it = deserializers.find(key);
85 if (it != deserializers.end()) {
86 return it->second(value.as_map());
96 SerDesRegistry serdes_registry = SerDesRegistry();
102 Annotatable::~Annotatable() {
110 void Annotatable::copy_annotations(
const Annotatable &src) {
111 for (
const auto &src_it : src.annotations) {
112 TREE_MAP_SET(annotations, src_it.first, src_it.second);
124 void Annotatable::serialize_annotations(cbor::MapWriter &map)
const {
125 for (
auto it : annotations) {
126 serdes_registry.serialize(it.second, map);
139 void Annotatable::deserialize_annotations(
const cbor::MapReader &map) {
140 for (
auto it : map) {
143 if (!it.first.empty() && (it.first[0] ==
'{') && (it.first[it.first.size() - 1] ==
'}')) {
144 std::shared_ptr<Anything> value{};
145 value = serdes_registry.deserialize(it.first, it.second);
147 TREE_MAP_SET(annotations, value->get_type_index(), value);