Composer.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 #ifndef Pt_Composer_h
29 #define Pt_Composer_h
30 
31 #include <Pt/Api.h>
32 #include <Pt/SerializationInfo.h>
33 #include <Pt/SerializationContext.h>
34 #include <cstddef>
35 
36 namespace Pt {
37 
42 class Composer
43 {
44  public:
47  virtual ~Composer()
48  {}
49 
53  { _parent = parent; }
54 
57  Composer* parent() const
58  { return _parent; }
59 
64  void setTypeName(const std::string& type)
65  { onSetTypeName( type.c_str(), type.size() ); }
66 
71  void setTypeName(const char* type, std::size_t len)
72  { onSetTypeName(type, len); }
73 
78  void setId(const std::string& id)
79  { onSetId( id.c_str(), id.size() ); }
80 
85  void setId(const char* id, std::size_t len)
86  { onSetId(id, len); }
87 
90  void setString(const Pt::String& value)
91  { onSetString( value.c_str(), value.size() ); }
92 
95  void setString(const Pt::Char* value, std::size_t len)
96  { onSetString(value, len); }
97 
100  void setBinary(const char* data, std::size_t length)
101  { onSetBinary(data, length); }
102 
105  void setChar(const Pt::Char& ch)
106  { onSetChar(ch); }
107 
110  void setBool(bool value)
111  { onSetBool(value); }
112 
118  void setInt(Pt::int64_t value)
119  { onSetInt(value); }
120 
126  void setUInt(Pt::int64_t value)
127  { onSetUInt(value); }
128 
131  void setFloat(long double value)
132  { onSetFloat(value); }
133 
136  void setReference(const std::string& id)
137  { onSetReference(id.c_str(), id.size()); }
138 
141  void setReference(const char* id, std::size_t len)
142  { onSetReference(id, len); }
143 
146  Composer* beginMember(const std::string& name)
147  { return onBeginMember( name.c_str(), name.size() ); }
148 
151  Composer* beginMember(const char* name, std::size_t len)
152  { return onBeginMember(name, len); }
153 
157  { return onBeginElement(); }
158 
167  { return onBeginDictElement(); }
168 
172  { return onBeginDictKey(); }
173 
177  { return onBeginDictValue(); }
178 
182  { return onFinish(); }
183 
184  protected:
188  : _parent(0)
189  {}
190 
192  virtual void onSetTypeName(const char*, std::size_t)
193  {}
194 
196  virtual void onSetId(const char* id, std::size_t len) = 0;
197 
199  virtual void onSetString(const Pt::Char*, std::size_t)
200  { throw SerializationError("unexpected string value"); }
201 
203  virtual void onSetBinary(const char*, std::size_t)
204  { throw SerializationError("unexpected binary value"); }
205 
207  virtual void onSetChar(const Pt::Char&)
208  { throw SerializationError("unexpected char value"); }
209 
211  virtual void onSetBool(bool)
212  { throw SerializationError("unexpected bool value"); }
213 
215  virtual void onSetInt(Pt::int64_t)
216  { throw SerializationError("unexpected integer value"); }
217 
219  virtual void onSetUInt(Pt::uint64_t)
220  { throw SerializationError("unexpected unsigned value"); }
221 
223  virtual void onSetFloat(long double)
224  { throw SerializationError("unexpected float value"); }
225 
227  virtual void onSetReference(const char*, std::size_t)
228  { throw SerializationError("unexpected reference"); }
229 
231  virtual Composer* onBeginMember(const char*, std::size_t)
232  { throw SerializationError("unexpected struct"); }
233 
237  { throw SerializationError("unexpected sequence"); }
238 
242  { throw SerializationError("unexpected dict"); }
243 
247  { throw SerializationError("unexpected dict"); }
248 
252  { throw SerializationError("unexpected dict"); }
253 
256  virtual Composer* onFinish()
257  { return _parent; }
258 
259  private:
260  Composer* _parent;
261 };
262 
267 template <typename T>
268 class BasicComposer : public Composer
269 {
270  public:
273  : _type(0)
274  , _si(context)
275  , _current(&_si)
276  { }
277 
279  void begin(T& type)
280  {
281  if(_type)
282  {
283  _si.clear();
284  }
285 
286  _type = &type;
287  _current = &_si;
288  }
289 
290  protected:
291  // inherit docs
292  void onSetId(const char* id, std::size_t len)
293  {
294  _current->setId(id, len);
295  }
296 
297  // inherit docs
298  void onSetTypeName(const char* type, std::size_t len)
299  {
300  _current->setTypeName(type, len);
301  }
302 
303  void onSetString(const Pt::Char* value, std::size_t len)
304  {
305  _current->setString(value, len);
306  }
307 
308  // inherit docs
309  void onSetBinary(const char* data, std::size_t length)
310  {
311  _current->setBinary(data, length);
312  }
313 
314  // inherit docs
315  void onSetChar(const Pt::Char& ch)
316  {
317  _current->setChar(ch);
318  }
319 
320  // inherit docs
321  void onSetBool(bool value)
322  {
323  _current->setBool(value);
324  }
325 
326  // inherit docs
327  void onSetInt(Pt::int64_t value)
328  {
329  _current->setInt64(value);
330  }
331 
332  // inherit docs
334  {
335  _current->setUInt64(value);
336  }
337 
338  // inherit docs
339  void onSetFloat(long double value)
340  {
341  _current->setLongDouble(value);
342  }
343 
344  // inherit docs
345  void onSetReference(const char* id, std::size_t len)
346  {
347  _current->setReference(id, len);
348  }
349 
350  // inherit docs
351  Composer* onBeginMember(const char* name, std::size_t len)
352  {
353  SerializationInfo& child = _current->addMember(name, len);
354  _current = &child;
355  return this;
356  }
357 
358  // inherit docs
360  {
361  SerializationInfo& child = _current->addElement();
362  _current = &child;
363  return this;
364  }
365 
366  // inherit docs
368  {
369  SerializationInfo& child = _current->addDictElement();
370  _current = &child;
371  return this;
372  }
373 
374  // inherit docs
376  {
377  SerializationInfo& child = _current->addDictKey();
378  _current = &child;
379  return this;
380  }
381 
382  // inherit docs
384  {
385  SerializationInfo& child = _current->addDictValue();
386  _current = &child;
387  return this;
388  }
389 
390  // inherit docs
392  {
393  if( ! _current->parent() )
394  {
395  *_current >> Pt::load() >>= *_type;
396  _si.clear();
397  _type = 0;
398  return parent();
399  }
400 
401  _current = _current->parent();
402  return this;
403  }
404 
405  private:
406  T* _type;
408  Pt::SerializationInfo* _current;
409 };
410 
411 } // namespace Pt
412 
413 #endif
void setReference(const std::string &id)
Composes a reference.
Definition: Composer.h:136
void setChar(const Pt::Char &ch)
Composes a char value.
Definition: Composer.h:105
Composer * onBeginDictElement()
Begins composition of a dict key.
Definition: Composer.h:367
void setReference(const void *ref)
Set to reference for which to create an ID.
void setFloat(long double value)
Composes a float value.
Definition: Composer.h:131
virtual void onSetInt(Pt::int64_t)
Compose a integer value.
Definition: Composer.h:215
Composer * beginDictValue()
Begins composition of a dict value.
Definition: Composer.h:176
void onSetReference(const char *id, std::size_t len)
Compose a reference.
Definition: Composer.h:345
SerializationInfo * parent()
Returns the parent node.
Definition: SerializationInfo.h:206
size_type size() const
Returns the length of the string.
Definition: String.h:239
virtual void onSetBinary(const char *, std::size_t)
Compose a binary value.
Definition: Composer.h:203
virtual Composer * onBeginMember(const char *, std::size_t)
Begin composition os a struct member.
Definition: Composer.h:231
Composer * finish()
Finishes composition of a struct or sequence member.
Definition: Composer.h:181
virtual void onSetId(const char *id, std::size_t len)=0
Set reference ID.
Composes types during serialization.
Definition: Composer.h:42
virtual Composer * onBeginDictValue()
Begins composition of a dict value.
Definition: Composer.h:251
SerializationInfo & addElement()
Add a sequence element.
Composer * beginDictKey()
Begins composition of a dict key.
Definition: Composer.h:171
virtual void onSetChar(const Pt::Char &)
Compose a character value.
Definition: Composer.h:207
void setId(const std::string &id)
Sets the reference id of the type to compose.
Definition: Composer.h:78
void setTypeName(const char *type, std::size_t len)
Sets the type name of the type to compose.
Definition: Composer.h:71
Composer()
Constructor.
Definition: Composer.h:187
void setInt(Pt::int64_t value)
Composes a signed integer type.
Definition: Composer.h:118
void setInt64(Pt::int64_t l)
Set to 64-bit integer value.
void onSetBinary(const char *data, std::size_t length)
Compose a binary value.
Definition: Composer.h:309
SerializationInfo & addMember(const std::string &name)
Add a struct member.
Definition: SerializationInfo.h:449
virtual void onSetBool(bool)
Compose a bool value.
Definition: Composer.h:211
SerializationInfo & addDictValue()
Add a dict value.
Composer * beginMember(const std::string &name)
Begins composition of a struct member.
Definition: Composer.h:146
void onSetBool(bool value)
Compose a bool value.
Definition: Composer.h:321
virtual Composer * onFinish()
Finishes composition of a struct or sequence member.
Definition: Composer.h:256
void setBool(bool value)
Composes a boolean value.
Definition: Composer.h:110
void onSetUInt(Pt::uint64_t value)
Compose a unsigned integer value.
Definition: Composer.h:333
Error during serialization of a type.
Definition: SerializationError.h:45
Composer * onBeginDictValue()
Begins composition of a dict value.
Definition: Composer.h:383
void setUInt(Pt::int64_t value)
Composes an unsigned integer type.
Definition: Composer.h:126
virtual void onSetReference(const char *, std::size_t)
Compose a reference.
Definition: Composer.h:227
virtual Composer * onBeginDictKey()
Begins composition of a dict key.
Definition: Composer.h:246
Composer * onBeginElement()
Begins composition of a sequence member.
Definition: Composer.h:359
void setBinary(const char *data, std::size_t length)
Set to binary value.
Composer * beginElement()
Begins composition of a sequence member.
Definition: Composer.h:156
const Pt::Char * c_str() const
Returns a null terminated C string.
Definition: String.h:264
void setLongDouble(long double d)
Set to long double value.
void setUInt64(Pt::uint64_t n)
Set to 64-bit unsigned integer value.
Unicode character type.
Definition: String.h:66
virtual Composer * onBeginElement()
Begins composition of a sequence member.
Definition: Composer.h:236
int_type int64_t
Signed 64-bit integer type.
Definition: Types.h:48
void onSetString(const Pt::Char *value, std::size_t len)
Compose a string value.
Definition: Composer.h:303
void setBool(bool b)
Set to bool value.
void clear()
Clears all content.
virtual ~Composer()
Destructor.
Definition: Composer.h:47
virtual void onSetString(const Pt::Char *, std::size_t)
Compose a string value.
Definition: Composer.h:199
void setString(const Pt::String &value)
Composes a string value.
Definition: Composer.h:90
Composer * beginMember(const char *name, std::size_t len)
Begins composition of a struct member.
Definition: Composer.h:151
void setChar(char c)
Set to character value.
virtual void onSetTypeName(const char *, std::size_t)
Set type name.
Definition: Composer.h:192
void onSetInt(Pt::int64_t value)
Compose a integer value.
Definition: Composer.h:327
Manages the composition of types during serialization.
Definition: Composer.h:268
Composer * parent() const
Returns the parent composer.
Definition: Composer.h:57
SerializationInfo & addDictElement()
Add a dict element.
Composer * onFinish()
Finishes composition of a struct or sequence member.
Definition: Composer.h:391
Composer * onBeginDictKey()
Begins composition of a dict key.
Definition: Composer.h:375
void setString(const Pt::Char *value, std::size_t len)
Composes a string value.
Definition: Composer.h:95
void onSetFloat(long double value)
Compose a floating point value.
Definition: Composer.h:339
virtual void onSetUInt(Pt::uint64_t)
Compose a unsigned integer value.
Definition: Composer.h:219
uint_type uint64_t
Unsigned 64-bit integer type.
Definition: Types.h:54
Unicode capable basic_string.
Definition: String.h:42
void setString(const char *s)
Set to string value.
void setReference(const char *id, std::size_t len)
Composes a reference.
Definition: Composer.h:141
Composer * beginDictElement()
Begins composition of a dict key.
Definition: Composer.h:166
void setId(const std::string &id)
Sets the reference ID.
void setId(const char *id, std::size_t len)
Sets the reference id of the type to compose.
Definition: Composer.h:85
Composer * onBeginMember(const char *name, std::size_t len)
Begin composition os a struct member.
Definition: Composer.h:351
Context for the serialization of types.
Definition: SerializationContext.h:20
void onSetId(const char *id, std::size_t len)
Set reference ID.
Definition: Composer.h:292
Represents arbitrary types during serialization.
Definition: SerializationInfo.h:58
void setTypeName(const std::string &type)
Sets the type name of the type to compose.
Definition: Composer.h:64
SerializationInfo & addDictKey()
Add a dict key.
virtual void onSetFloat(long double)
Compose a floating point value.
Definition: Composer.h:223
BasicComposer(SerializationContext *context=0)
Construct with context.
Definition: Composer.h:272
void onSetTypeName(const char *type, std::size_t len)
Set type name.
Definition: Composer.h:298
virtual Composer * onBeginDictElement()
Begins composition of a dict key.
Definition: Composer.h:241
void begin(T &type)
Begin composing a type.
Definition: Composer.h:279
void setParent(Composer *parent)
Sets the parent composer.
Definition: Composer.h:52
void onSetChar(const Pt::Char &ch)
Compose a character value.
Definition: Composer.h:315
void setBinary(const char *data, std::size_t length)
Composes a binary value.
Definition: Composer.h:100
void setTypeName(const std::string &type)
Sets the type name.