Repository navigation
Expand file tree
/
Copy pathGenericAttributeSerialization.h
More file actions
139 lines (125 loc) · 5.22 KB
/
Copy pathGenericAttributeSerialization.h
File metadata and controls
139 lines (125 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
This file is part of the Util library.
Copyright (C) 2007-2012 Benjamin Eikel <benjamin@eikel.org>
Copyright (C) 2007-2012 Claudius Jähn <claudius@uni-paderborn.de>
Copyright (C) 2007-2012 Ralf Petring <ralf@petring.net>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef UTIL_GENERICATTRIBUTESERIALIZATION_H
#define UTIL_GENERICATTRIBUTESERIALIZATION_H
#include "Factory/WrapperFactory.h"
#include <functional>
#include <string>
#include <typeindex>
#include <typeinfo>
#include <utility>
namespace Util {
class GenericAttribute;
class GenericAttributeMap;
/**
* @brief Serialization for GenericAttribute
*
* Class for serialization and unserialization of GenericAttribute objects.
* @author Benjamin Eikel
* @date 2012-03-12
* @ingroup generic_attr
*/
class GenericAttributeSerialization {
public:
typedef std::pair<std::string, std::string> serializer_type_t;
typedef std::pair<const GenericAttribute *,
const GenericAttributeMap *> serializer_parameter_t;
typedef GenericAttribute * unserializer_type_t;
typedef std::pair<std::string,
const GenericAttributeMap *> unserializer_parameter_t;
private:
typedef WrapperFactory<serializer_parameter_t,
serializer_type_t,
std::type_index> serializer_factory_t;
UTILAPI static serializer_factory_t & getSerializerFactory();
typedef WrapperFactory<unserializer_parameter_t,
unserializer_type_t,
std::string> unserializer_factory_t;
UTILAPI static unserializer_factory_t & getUnserializerFactory();
public:
/**
* Initialize the serializers for the standard GenericAttributes.
* This function has to be called at least once before using the class.
* It is called from init.
*
* @retval true if the initialization was successful
* @retval false if an error occurred
*/
UTILAPI static bool init();
/**
* Convert the given generic attribute to a string.
* The string is a JSON representation of the generic attribute.
* It is constructed as follows:
* @code
* {
* "type" : "[string identifying the type]",
* "value" : "[string representation of the content]"
* }
* @endcode
*
* @param attribute Generic attribute that is to be converted
* @return String representation
*/
UTILAPI static std::string serialize(const GenericAttribute * attribute);
/**
* Convert the given generic attribute to a string.
* Use the given context to look up further information that is needed.
*
* @param attribute Generic attribute that is to be converted
* @param context A map containing specific information that is needed
* during unserialization
* @return String representation
*/
UTILAPI static std::string serialize(const GenericAttribute * attribute,
const GenericAttributeMap * context);
/**
* Convert the given string representation to a generic attribute.
*
* @param representation String representation that was created by
* serialize()
* @return Newly constructed, generic attribute
* @note The caller has to free the memory for the generic attribute.
*/
UTILAPI static GenericAttribute * unserialize(const std::string & representation);
/**
* Convert the given string representation to a generic attribute.
* Use the given context to look up further information that is needed.
*
* @param representation String representation that was created by
* serialize()
* @param context A map containing specific information that is needed
* during unserialization
* @return Newly constructed, generic attribute
* @note The caller has to free the memory for the generic attribute.
*/
UTILAPI static GenericAttribute * unserialize(const std::string & representation,
const GenericAttributeMap * context);
/**
* Register a serializer and unserializer for a generic attribute type.
*
* @tparam AttributeType Type of the generic attribute to be registered
* @param typeName String representation of the generic attribute type that has to be unique for generic attribute serialization
* @param serializer Serializer function converting a generic attribute into a @p typeName and a string representation of the content
* @param unserialize Unserializer function reconstructing a generic attribute from a string representation
* @return @c true if both functions have been registered successfully, @c false otherwise
* @note Any previous functions for the given type are unregistered
*/
template<typename AttributeType>
static bool registerSerializer(const std::string & typeName,
const std::function<serializer_type_t (const serializer_parameter_t &)> & serializer,
const std::function<unserializer_type_t (const unserializer_parameter_t &)> & unserializer) {
getSerializerFactory().unregisterType(typeid(AttributeType));
getUnserializerFactory().unregisterType(typeName);
return getSerializerFactory().registerType(typeid(AttributeType), serializer) &&
getUnserializerFactory().registerType(typeName, unserializer);
}
};
}
#endif /* UTIL_GENERICATTRIBUTESERIALIZATION_H */