SerializationContext.h
1/*
2 * Copyright (C) 2008 by Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#ifndef Pt_SerializationContext_h
30#define Pt_SerializationContext_h
31
32#include <Pt/Api.h>
33#include <Pt/TypeInfo.h>
34#include <Pt/FixupInfo.h>
35#include <Pt/SerializationInfo.h>
36#include <Pt/SerializationSurrogate.h>
37#include <typeinfo>
38#include <string>
39
40namespace Pt {
41
42class SerializationContextImpl;
43
45
46class PT_API SerializationContext : public SerializationInfo
47{
48 public:
49 SerializationContext();
50
51 virtual ~SerializationContext();
52
53 inline void enableReferencing(bool enabled)
54 { _refsEnabled = enabled; }
55
56 inline bool isReferencing() const
57 { return _refsEnabled; }
58
59 SerializationInfo* get();
60
61 void push(SerializationInfo* si);
62
63 template <typename T>
64 void registerSurrogate( const std::string& typeName,
65 void (*compose)(const Pt::SerializationInfo& si, T& type),
66 void (*decompose)(Pt::SerializationInfo& si, const T& type) );
67
68 template <typename T>
69 void registerSurrogate( const char* typeName,
70 void (*compose)(const Pt::SerializationInfo& si, T& type),
71 void (*decompose)(Pt::SerializationInfo& si, const T& type) );
72
73 template <typename T>
74 bool compose(const SerializationInfo& si, T& type) const;
75
76 template <typename T>
77 bool decompose(SerializationInfo& si, const T& type);
78
79 template <typename T>
80 const BasicSerializationSurrogate<T>* getSurrogate() const;
81
83 template <typename T>
84 friend const BasicSerializationSurrogate<T>* getSurrogate(SerializationContext*);
85
86 const SerializationSurrogate* getSurrogate(const std::type_info& ti) const;
87
88 public:
89 void clear()
90 { onClear(); }
91
92 bool beginSave(const void* p, const char* name)
93 { return onBeginSave(p, name); }
94
95 void finishSave()
96 { onFinishSave(); }
97
98 void prepareId(const void* p)
99 { onPrepareId(p); }
100
101 const char* getId(const void* p)
102 { return onGetId(p); }
103
104 const char* makeId(const void* p)
105 { return onMakeId(p); }
106
107 public:
108 void beginLoad(void* obj, const std::type_info& ti,
109 const char* name, const char* id)
110 { onBeginLoad(obj, ti, name, id); }
111
112 void finishLoad()
113 { onFinishLoad(); }
114
115 void rebindTarget(const char* id, void* obj)
116 { onRebindTarget(id, obj); }
117
118 void rebindFixup(const char* id, void* obj, void* prev)
119 { onRebindFixup(id, obj, prev); }
120
121 void prepareFixup(void* obj, const char* id, FixupInfo::FixupHandler fh, unsigned mid)
122 { onPrepareFixup(obj, id, fh, mid); }
123
124 void fixup()
125 { onFixup(); }
126
127 protected:
128 virtual void onClear();
129
130 virtual bool onBeginSave(const void* p, const char* name);
131
132 virtual void onFinishSave();
133
134 virtual void onPrepareId(const void* p);
135
136 virtual const char* onGetId(const void* p);
137
138 virtual const char* onMakeId(const void* p);
139
140 virtual void onBeginLoad(void* obj, const std::type_info& fixupInfo,
141 const char* name, const char* id);
142
143 virtual void onFinishLoad();
144
145 virtual void onRebindTarget(const char* id, void* obj);
146
147 virtual void onRebindFixup(const char* id, void* obj, void* prev);
148
149 virtual void onPrepareFixup(void* obj, const char* id, FixupInfo::FixupHandler fh, unsigned mid);
150
151 virtual void onFixup();
152
153 private:
154 void registerSurrogate(const std::type_info& ti, SerializationSurrogate* surrogate);
155
156 SerializationContext(const SerializationContext& );
157
158 SerializationContext& operator=(const SerializationContext& );
159
160 private:
161 SerializationContextImpl* _cache;
162 bool _refsEnabled;
163};
164
166
167template <typename T>
168inline bool SerializationContext::compose(const SerializationInfo& si, T& type) const
169{
170 const BasicSerializationSurrogate<T>* surr = this->getSurrogate<T>();
171 if( ! surr )
172 return false;
173
174 surr->compose(si, type);
175 return true;
176}
177
178
179template <typename T>
181{
182 const BasicSerializationSurrogate<T>* surr = this->getSurrogate<T>();
183 if( ! surr )
184 return false;
185
186 surr->decompose(si, type);
187 si.setTypeName( surr->typeName() );
188 return true;
189}
190
191
192template <typename T>
193inline const BasicSerializationSurrogate<T>* SerializationContext::getSurrogate() const
194{
195 const SerializationSurrogate* surr = this->getSurrogate( typeid(T) );
196 if( ! surr )
197 return 0;
198
199 return static_cast<const BasicSerializationSurrogate<T>*>(surr);
200}
201
202
203template <typename T>
204inline void SerializationContext::registerSurrogate( const std::string& typeName,
205 void (*compose)(const Pt::SerializationInfo& si, T& type),
206 void (*decompose)(Pt::SerializationInfo& si, const T& type) )
207{
208 SerializationSurrogate* surr = new BasicSerializationSurrogate<T>(typeName, compose, decompose);
209 registerSurrogate(typeid(T), surr);
210}
211
212
213template <typename T>
214inline void SerializationContext::registerSurrogate( const char* typeName,
215 void (*compose)(const Pt::SerializationInfo& si, T& type),
216 void (*decompose)(Pt::SerializationInfo& si, const T& type) )
217{
218 SerializationSurrogate* surr = new BasicSerializationSurrogate<T>(typeName, compose, decompose);
219 registerSurrogate(typeid(T), surr);
220}
221
222} // namespace Pt
223
224#endif // Pt_SerializationContext_h
Context for the serialization of types.
Definition Api-SerializationContext.h:26
const BasicSerializationSurrogate< T > * getSurrogate() const
Find a surrogate for a type.
Definition SerializationContext.h:193
void registerSurrogate(const std::string &typeName, void(*compose)(const Pt::SerializationInfo &si, T &type), void(*decompose)(Pt::SerializationInfo &si, const T &type))
Register a serialization surrogate function pair.
Definition SerializationContext.h:204
bool compose(const SerializationInfo &si, T &type) const
Returns true if the type could be composed with a surrogate.
Definition SerializationContext.h:168
bool decompose(SerializationInfo &si, const T &type)
Returns true if the type could be decomposed with a surrogate.
Definition SerializationContext.h:180
Represents arbitrary types during serialization.
Definition SerializationInfo.h:59
void setTypeName(const std::string &type)
Sets the type name.
void fixup(const FixupInfo &fixup, T *&fixme)
Fixup references during serialization.
Definition FixupInfo.h:136
Core module.
Definition Allocator.h:33