Decomposer.h
1/*
2 * Copyright (C) 2008-2013 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_Decomposer_h
30#define Pt_Decomposer_h
31
32#include <Pt/Api.h>
33#include <Pt/Formatter.h>
34#include <Pt/SerializationInfo.h>
35#include <Pt/SerializationContext.h>
36
37namespace Pt {
38
121{
122 public:
124 virtual ~Decomposer()
125 {}
126
129 { _parent = parent; }
130
133 { return _parent; }
134
137 void format(Formatter& formatter)
138 { onFormat(formatter); }
139
142 void beginFormat(Formatter& formatter)
143 { onBeginFormat(formatter); }
144
148 { return onAdvanceFormat(formatter); }
149
150 protected:
154 : _parent(0)
155 {}
156
159 virtual void onFormat(Formatter& formatter)
160 {
161 onBeginFormat(formatter);
162
163 while( onAdvanceFormat(formatter) != _parent )
164 ;
165 }
166
169 virtual void onBeginFormat(Formatter& formatter) = 0;
170
173 virtual Decomposer* onAdvanceFormat(Formatter& formatter) = 0;
174
175 private:
176 Decomposer* _parent;
177};
178
183template <typename T>
185{
186 public:
190 : _type(0)
191 , _si(context)
192 , _current(0)
193 { }
194
195 // TODO: pass instance name to format()/onFormat() and
196 // beginFormat()/onBeginFormat()
197
200 void begin(const T& type, const char* name)
201 {
202 if(_type)
203 {
204 _si.clear();
206 _current = 0;
207 }
208
209 _type = &type;
210 _si.setName(name);
211
212 Pt::SerializationContext* ctx = _si.context();
213 if( ctx && ctx->isReferencing() )
214 {
215 *ctx << Pt::save() <<= type;
216 }
217 }
218
219 protected:
220 // inherit docs
221 void onFormat(Formatter& formatter)
222 {
223 _si << Pt::save() <<= *_type;
224 _si.format(formatter);
225 }
226
227 // inherit docs
228 void onBeginFormat(Formatter& formatter)
229 {
230 _si << Pt::save() <<= *_type;
231 _current = &_si;
232
233 _it = _si.beginFormat(formatter);
234 }
235
236 // inherit docs
238 {
239 if( _it == _current->end() )
240 {
241 _current->endFormat(formatter);
242
243 if( _current->sibling() )
244 {
245 _current = _current->sibling();
246 _it = _current->beginFormat(formatter);
247 }
248 else
249 {
250 _current = _current->parent();
251 if(_current)
252 _it = _current->end();
253 }
254
255 if(_current != 0 )
256 return this;
257
258 _si.clear();
259 _type = 0;
260 return parent();
261 }
262
263 SerializationInfo::Iterator it = _it->beginFormat(formatter);
264 if( it != _it->end() )
265 {
266 _current = &(*_it);
267 _it = it;
268 }
269 else
270 {
271 _it->endFormat(formatter);
272 ++_it;
273 }
274
275 return this;
276 }
277
278 private:
279 const T* _type;
281 SerializationInfo* _current;
283};
284
285} // namespace Pt
286
287#endif
void begin(const T &type, const char *name)
Begin decomposing a type.
Definition Decomposer.h:200
Decomposer * onAdvanceFormat(Formatter &formatter)
Advance formatting the type.
Definition Decomposer.h:237
void onBeginFormat(Formatter &formatter)
Begin formatting the type.
Definition Decomposer.h:228
void onFormat(Formatter &formatter)
Format the type completely.
Definition Decomposer.h:221
BasicDecomposer(SerializationContext *context=0)
Construct with context.
Definition Decomposer.h:189
virtual void onBeginFormat(Formatter &formatter)=0
Begin formatting the type.
Decomposer * advanceFormat(Formatter &formatter)
Advance formatting the type.
Definition Decomposer.h:147
void setParent(Decomposer *parent)
Sets the parent.
Definition Decomposer.h:128
Decomposer * parent() const
Returns the parent.
Definition Decomposer.h:132
virtual ~Decomposer()
Destructor.
Definition Decomposer.h:124
virtual Decomposer * onAdvanceFormat(Formatter &formatter)=0
Advance formatting the type.
virtual void onFormat(Formatter &formatter)
Format the type completely.
Definition Decomposer.h:159
Decomposer()
Default constructor.
Definition Decomposer.h:153
void format(Formatter &formatter)
Format the type completely.
Definition Decomposer.h:137
void beginFormat(Formatter &formatter)
Begin formatting the type.
Definition Decomposer.h:142
Support for serialization to different formats.
Definition Formatter.h:46
Context for the serialization of types.
Definition Api-SerializationContext.h:26
bool isReferencing() const
Returns true if references are recorded.
Definition Api-SerializationContext.h:40
Forward Iterator for child elements.
Definition SerializationInfo.h:738
Represents arbitrary types during serialization.
Definition SerializationInfo.h:59
Iterator end()
Returns an iterator to the end of child elements.
Definition SerializationInfo.h:826
Iterator beginFormat(Formatter &formatter)
Begin formatting.
Core module.
Definition Allocator.h:33