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 
40 namespace Pt {
41 
42 class SerializationContextImpl;
43 
45 class PT_API SerializationContext : public SerializationInfo
46 {
47  public:
48  SerializationContext();
49 
50  virtual ~SerializationContext();
51 
52  inline void enableReferencing(bool enabled)
53  { _refsEnabled = enabled; }
54 
55  inline bool isReferencing() const
56  { return _refsEnabled; }
57 
58  SerializationInfo* get();
59 
60  void push(SerializationInfo* si);
61 
62  template <typename T>
63  void registerSurrogate( const std::string& typeName,
64  void (*compose)(const Pt::SerializationInfo& si, T& type),
65  void (*decompose)(Pt::SerializationInfo& si, const T& type) );
66 
67  template <typename T>
68  void registerSurrogate( const char* typeName,
69  void (*compose)(const Pt::SerializationInfo& si, T& type),
70  void (*decompose)(Pt::SerializationInfo& si, const T& type) );
71 
72  template <typename T>
73  bool compose(const SerializationInfo& si, T& type) const;
74 
75  template <typename T>
76  bool decompose(SerializationInfo& si, const T& type);
77 
78  template <typename T>
79  const BasicSerializationSurrogate<T>* getSurrogate() const;
80 
82  template <typename T>
83  friend const BasicSerializationSurrogate<T>* getSurrogate(SerializationContext*);
84 
85  const SerializationSurrogate* getSurrogate(const std::type_info& ti) const;
86 
87  public:
88  void clear()
89  { onClear(); }
90 
91  bool beginSave(const void* p, const char* name)
92  { return onBeginSave(p, name); }
93 
94  void finishSave()
95  { onFinishSave(); }
96 
97  void prepareId(const void* p)
98  { onPrepareId(p); }
99 
100  const char* getId(const void* p)
101  { return onGetId(p); }
103  const char* makeId(const void* p)
104  { return onMakeId(p); }
105 
106  public:
107  void beginLoad(void* obj, const std::type_info& ti,
108  const char* name, const char* id)
109  { onBeginLoad(obj, ti, name, id); }
110 
111  void finishLoad()
112  { onFinishLoad(); }
114  void rebindTarget(const char* id, void* obj)
115  { onRebindTarget(id, obj); }
116 
117  void rebindFixup(const char* id, void* obj, void* prev)
118  { onRebindFixup(id, obj, prev); }
119 
120  void prepareFixup(void* obj, const char* id, FixupInfo::FixupHandler fh, unsigned mid)
121  { onPrepareFixup(obj, id, fh, mid); }
122 
123  void fixup()
124  { onFixup(); }
125 
126  protected:
127  virtual void onClear();
129  virtual bool onBeginSave(const void* p, const char* name);
130 
131  virtual void onFinishSave();
132 
133  virtual void onPrepareId(const void* p);
134 
135  virtual const char* onGetId(const void* p);
136 
137  virtual const char* onMakeId(const void* p);
138 
139  virtual void onBeginLoad(void* obj, const std::type_info& fixupInfo,
140  const char* name, const char* id);
141 
142  virtual void onFinishLoad();
143 
144  virtual void onRebindTarget(const char* id, void* obj);
145 
146  virtual void onRebindFixup(const char* id, void* obj, void* prev);
147 
148  virtual void onPrepareFixup(void* obj, const char* id, FixupInfo::FixupHandler fh, unsigned mid);
149 
150  virtual void onFixup();
151 
152  private:
153  void registerSurrogate(const std::type_info& ti, SerializationSurrogate* surrogate);
154 
155  SerializationContext(const SerializationContext& );
156 
157  SerializationContext& operator=(const SerializationContext& );
158 
159  private:
160  SerializationContextImpl* _cache;
161  bool _refsEnabled;
162 };
164 
165 template <typename T>
166 inline bool SerializationContext::compose(const SerializationInfo& si, T& type) const
167 {
168  const BasicSerializationSurrogate<T>* surr = this->getSurrogate<T>();
169  if( ! surr )
170  return false;
171 
172  surr->compose(si, type);
173  return true;
174 }
175 
176 
177 template <typename T>
178 inline bool SerializationContext::decompose(SerializationInfo& si, const T& type)
179 {
180  const BasicSerializationSurrogate<T>* surr = this->getSurrogate<T>();
181  if( ! surr )
182  return false;
183 
184  surr->decompose(si, type);
185  si.setTypeName( surr->typeName() );
186  return true;
187 }
188 
189 
190 template <typename T>
191 inline const BasicSerializationSurrogate<T>* SerializationContext::getSurrogate() const
192 {
193  const SerializationSurrogate* surr = this->getSurrogate( typeid(T) );
194  if( ! surr )
195  return 0;
196 
197  return static_cast<const BasicSerializationSurrogate<T>*>(surr);
198 }
199 
200 
201 template <typename T>
202 inline void SerializationContext::registerSurrogate( const std::string& typeName,
203  void (*compose)(const Pt::SerializationInfo& si, T& type),
204  void (*decompose)(Pt::SerializationInfo& si, const T& type) )
205 {
206  SerializationSurrogate* surr = new BasicSerializationSurrogate<T>(typeName, compose, decompose);
207  registerSurrogate(typeid(T), surr);
208 }
209 
210 
211 template <typename T>
212 inline void SerializationContext::registerSurrogate( const char* typeName,
213  void (*compose)(const Pt::SerializationInfo& si, T& type),
214  void (*decompose)(Pt::SerializationInfo& si, const T& type) )
215 {
216  SerializationSurrogate* surr = new BasicSerializationSurrogate<T>(typeName, compose, decompose);
217  registerSurrogate(typeid(T), surr);
218 }
219 
220 } // namespace Pt
221 
222 #endif // Pt_SerializationContext_h
const BasicSerializationSurrogate< T > * getSurrogate() const
Find a surrogate for a type.
Definition: SerializationContext.h:191
bool compose(const SerializationInfo &si, T &type) const
Returns true if the type could be composed with a surrogate.
Definition: SerializationContext.h:166
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:202
Represents arbitrary types during serialization.
Definition: SerializationInfo.h:58
bool decompose(SerializationInfo &si, const T &type)
Returns true if the type could be decomposed with a surrogate.
Definition: SerializationContext.h:178
void setTypeName(const std::string &type)
Sets the type name.