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 
415 template <>
417 {
418  public:
421  : _si(0)
422  , _current(_si)
423  { }
424 
427  {
428  if(_si)
429  {
430  _si->clear();
431  }
432 
433  _si = &si;
434  _current = _si;
435  }
436 
437  protected:
438  // inherit docs
439  void onSetId(const char* id, std::size_t len)
440  {
441  _current->setId(id, len);
442  }
443 
444  // inherit docs
445  void onSetTypeName(const char* type, std::size_t len)
446  {
447  _current->setTypeName(type, len);
448  }
449 
450  void onSetString(const Pt::Char* value, std::size_t len)
451  {
452  _current->setString(value, len);
453  }
454 
455  // inherit docs
456  void onSetBinary(const char* data, std::size_t length)
457  {
458  _current->setBinary(data, length);
459  }
460 
461  // inherit docs
462  void onSetChar(const Pt::Char& ch)
463  {
464  _current->setChar(ch);
465  }
466 
467  // inherit docs
468  void onSetBool(bool value)
469  {
470  _current->setBool(value);
471  }
472 
473  // inherit docs
474  void onSetInt(Pt::int64_t value)
475  {
476  _current->setInt64(value);
477  }
478 
479  // inherit docs
481  {
482  _current->setUInt64(value);
483  }
484 
485  // inherit docs
486  void onSetFloat(long double value)
487  {
488  _current->setLongDouble(value);
489  }
490 
491  // inherit docs
492  void onSetReference(const char* id, std::size_t len)
493  {
494  _current->setReference(id, len);
495  }
496 
497  // inherit docs
498  Composer* onBeginMember(const char* name, std::size_t len)
499  {
500  SerializationInfo& child = _current->addMember(name, len);
501  _current = &child;
502  return this;
503  }
504 
505  // inherit docs
507  {
508  SerializationInfo& child = _current->addElement();
509  _current = &child;
510  return this;
511  }
512 
513  // inherit docs
515  {
516  SerializationInfo& child = _current->addDictElement();
517  _current = &child;
518  return this;
519  }
520 
521  // inherit docs
523  {
524  SerializationInfo& child = _current->addDictKey();
525  _current = &child;
526  return this;
527  }
528 
529  // inherit docs
531  {
532  SerializationInfo& child = _current->addDictValue();
533  _current = &child;
534  return this;
535  }
536 
537  // inherit docs
539  {
540  if( ! _current->parent() )
541  {
542  return parent();
543  }
544 
545  _current = _current->parent();
546  return this;
547  }
548 
549  private:
551  Pt::SerializationInfo* _current;
552 };
553 
554 } // namespace Pt
555 
556 #endif
Core module.
Definition: Allocator.h:33
void setId(const char *id, std::size_t len)
Sets the reference id of the type to compose.
Definition: Composer.h:85
void onSetInt(Pt::int64_t value)
Compose a integer value.
Definition: Composer.h:474
Composer * beginDictElement()
Begins composition of a dict key.
Definition: Composer.h:166
void onSetId(const char *id, std::size_t len)
Set reference ID.
Definition: Composer.h:292
virtual void onSetReference(const char *, std::size_t)
Compose a reference.
Definition: Composer.h:227
void setString(const Pt::Char *value, std::size_t len)
Composes a string value.
Definition: Composer.h:95
Error during serialization of a type.
Definition: SerializationError.h:46
virtual void onSetUInt(Pt::uint64_t)
Compose a unsigned integer value.
Definition: Composer.h:219
size_type size() const
Returns the length of the string.
Definition: Api-String.h:240
Composes serializable types during serialization.
Definition: Composer.h:269
Composer * onBeginDictKey()
Begins composition of a dict key.
Definition: Composer.h:375
void setTypeName(const std::string &type)
Sets the type name.
virtual Composer * onBeginDictElement()
Begins composition of a dict key.
Definition: Composer.h:241
Composer * onFinish()
Finishes composition of a struct or sequence member.
Definition: Composer.h:391
void setParent(Composer *parent)
Sets the parent composer.
Definition: Composer.h:52
Composer()
Constructor.
Definition: Composer.h:187
void setReference(const char *id, std::size_t len)
Composes a reference.
Definition: Composer.h:141
SerializationInfo & addDictValue()
Add a dict value.
void setUInt(Pt::int64_t value)
Composes an unsigned integer type.
Definition: Composer.h:126
Composer * onBeginDictElement()
Begins composition of a dict key.
Definition: Composer.h:367
void onSetBool(bool value)
Compose a bool value.
Definition: Composer.h:468
void onSetUInt(Pt::uint64_t value)
Compose a unsigned integer value.
Definition: Composer.h:480
BasicComposer()
Construct with context.
Definition: Composer.h:420
void onSetString(const Pt::Char *value, std::size_t len)
Compose a string value.
Definition: Composer.h:303
const Pt::Char * c_str() const
Returns a null terminated C string.
Definition: Api-String.h:265
void begin(Pt::SerializationInfo &si)
Begin composing a type.
Definition: Composer.h:426
void setReference(const void *ref)
Set to reference for which to create an ID.
Composer * onBeginMember(const char *name, std::size_t len)
Begin composition os a struct member.
Definition: Composer.h:351
Composer * beginMember(const std::string &name)
Begins composition of a struct member.
Definition: Composer.h:146
virtual void onSetBinary(const char *, std::size_t)
Compose a binary value.
Definition: Composer.h:203
virtual void onSetInt(Pt::int64_t)
Compose a integer value.
Definition: Composer.h:215
void setInt64(Pt::int64_t l)
Set to 64-bit integer value.
SerializationInfo & addMember(const std::string &name)
Add a struct member.
Definition: SerializationInfo.h:449
Composer * parent() const
Returns the parent composer.
Definition: Composer.h:57
Composer * beginMember(const char *name, std::size_t len)
Begins composition of a struct member.
Definition: Composer.h:151
SerializationInfo * parent()
Returns the parent node.
Definition: SerializationInfo.h:206
virtual void onSetBool(bool)
Compose a bool value.
Definition: Composer.h:211
void onSetChar(const Pt::Char &ch)
Compose a character value.
Definition: Composer.h:315
void setLongDouble(long double d)
Set to long double value.
Composer * beginDictValue()
Begins composition of a dict value.
Definition: Composer.h:176
void setReference(const std::string &id)
Composes a reference.
Definition: Composer.h:136
void onSetInt(Pt::int64_t value)
Compose a integer value.
Definition: Composer.h:327
void setChar(const Pt::Char &ch)
Composes a char value.
Definition: Composer.h:105
virtual void onSetFloat(long double)
Compose a floating point value.
Definition: Composer.h:223
Composer * finish()
Finishes composition of a struct or sequence member.
Definition: Composer.h:181
SerializationInfo & addElement()
Add a sequence element.
virtual ~Composer()
Destructor.
Definition: Composer.h:47
void setBool(bool b)
Set to bool value.
Context for the serialization of types.
Definition: Api-SerializationContext.h:22
uint_type uint64_t
Unsigned 64-bit integer type.
Definition: Api-Types.h:62
void setFloat(long double value)
Composes a float value.
Definition: Composer.h:131
int_type int64_t
Signed 64-bit integer type.
Definition: Api-Types.h:55
void clear()
Clears all content.
void onSetFloat(long double value)
Compose a floating point value.
Definition: Composer.h:486
void begin(T &type)
Begin composing a type.
Definition: Composer.h:279
void onSetTypeName(const char *type, std::size_t len)
Set type name.
Definition: Composer.h:445
Unicode character type.
Definition: String.h:67
virtual Composer * onBeginElement()
Begins composition of a sequence member.
Definition: Composer.h:236
void setBinary(const char *data, std::size_t length)
Set to binary value.
void setChar(char c)
Set to character value.
SerializationInfo & addDictElement()
Add a dict element.
Composer * onBeginMember(const char *name, std::size_t len)
Begin composition os a struct member.
Definition: Composer.h:498
virtual void onSetId(const char *id, std::size_t len)=0
Set reference ID.
virtual void onSetString(const Pt::Char *, std::size_t)
Compose a string value.
Definition: Composer.h:199
void onSetBinary(const char *data, std::size_t length)
Compose a binary value.
Definition: Composer.h:456
void setUInt64(Pt::uint64_t n)
Set to 64-bit unsigned integer value.
Composer * beginElement()
Begins composition of a sequence member.
Definition: Composer.h:156
void setString(const char *s)
Set to string value.
Composer * beginDictKey()
Begins composition of a dict key.
Definition: Composer.h:171
void setInt(Pt::int64_t value)
Composes a signed integer type.
Definition: Composer.h:118
virtual Composer * onBeginDictKey()
Begins composition of a dict key.
Definition: Composer.h:246
virtual void onSetChar(const Pt::Char &)
Compose a character value.
Definition: Composer.h:207
virtual Composer * onBeginDictValue()
Begins composition of a dict value.
Definition: Composer.h:251
Composer * onBeginDictElement()
Begins composition of a dict key.
Definition: Composer.h:514
void onSetBool(bool value)
Compose a bool value.
Definition: Composer.h:321
virtual Composer * onBeginMember(const char *, std::size_t)
Begin composition os a struct member.
Definition: Composer.h:231
void onSetChar(const Pt::Char &ch)
Compose a character value.
Definition: Composer.h:462
void onSetUInt(Pt::uint64_t value)
Compose a unsigned integer value.
Definition: Composer.h:333
void setTypeName(const std::string &type)
Sets the type name of the type to compose.
Definition: Composer.h:64
Composer * onBeginDictValue()
Begins composition of a dict value.
Definition: Composer.h:383
void setId(const std::string &id)
Sets the reference ID.
BasicComposer(SerializationContext *context=0)
Construct with context.
Definition: Composer.h:272
Composes types during serialization.
Definition: Composer.h:43
virtual Composer * onFinish()
Finishes composition of a struct or sequence member.
Definition: Composer.h:256
void onSetId(const char *id, std::size_t len)
Set reference ID.
Definition: Composer.h:439
void onSetReference(const char *id, std::size_t len)
Compose a reference.
Definition: Composer.h:345
Unicode capable basic_string.
Definition: Api-String.h:44
Composer * onFinish()
Finishes composition of a struct or sequence member.
Definition: Composer.h:538
void setBinary(const char *data, std::size_t length)
Composes a binary value.
Definition: Composer.h:100
void setId(const std::string &id)
Sets the reference id of the type to compose.
Definition: Composer.h:78
void setString(const Pt::String &value)
Composes a string value.
Definition: Composer.h:90
virtual void onSetTypeName(const char *, std::size_t)
Set type name.
Definition: Composer.h:192
Composer * onBeginElement()
Begins composition of a sequence member.
Definition: Composer.h:359
void setBool(bool value)
Composes a boolean value.
Definition: Composer.h:110
Represents arbitrary types during serialization.
Definition: SerializationInfo.h:59
void onSetReference(const char *id, std::size_t len)
Compose a reference.
Definition: Composer.h:492
void onSetString(const Pt::Char *value, std::size_t len)
Compose a string value.
Definition: Composer.h:450
void onSetTypeName(const char *type, std::size_t len)
Set type name.
Definition: Composer.h:298
Composer * onBeginDictValue()
Begins composition of a dict value.
Definition: Composer.h:530
SerializationInfo & addDictKey()
Add a dict key.
void setTypeName(const char *type, std::size_t len)
Sets the type name of the type to compose.
Definition: Composer.h:71
void onSetFloat(long double value)
Compose a floating point value.
Definition: Composer.h:339
Composer * onBeginElement()
Begins composition of a sequence member.
Definition: Composer.h:506
Composer * onBeginDictKey()
Begins composition of a dict key.
Definition: Composer.h:522
void onSetBinary(const char *data, std::size_t length)
Compose a binary value.
Definition: Composer.h:309