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
36namespace Pt {
37
101{
102 public:
105 virtual ~Composer()
106 {}
107
111 { _parent = parent; }
112
116 { return _parent; }
117
122 void setTypeName(const std::string& type)
123 { onSetTypeName( type.c_str(), type.size() ); }
124
129 void setTypeName(const char* type, std::size_t len)
130 { onSetTypeName(type, len); }
131
136 void setId(const std::string& id)
137 { onSetId( id.c_str(), id.size() ); }
138
143 void setId(const char* id, std::size_t len)
144 { onSetId(id, len); }
145
148 void setString(const Pt::String& value)
149 { onSetString( value.c_str(), value.size() ); }
150
153 void setString(const Pt::Char* value, std::size_t len)
154 { onSetString(value, len); }
155
158 void setBinary(const char* data, std::size_t length)
159 { onSetBinary(data, length); }
160
163 void setChar(const Pt::Char& ch)
164 { onSetChar(ch); }
165
168 void setBool(bool value)
169 { onSetBool(value); }
170
176 void setInt(Pt::int64_t value)
177 { onSetInt(value); }
178
185 { onSetUInt(value); }
186
189 void setFloat(long double value)
190 { onSetFloat(value); }
191
194 void setReference(const std::string& id)
195 { onSetReference(id.c_str(), id.size()); }
196
199 void setReference(const char* id, std::size_t len)
200 { onSetReference(id, len); }
201
204 Composer* beginMember(const std::string& name)
205 { return onBeginMember( name.c_str(), name.size() ); }
206
209 Composer* beginMember(const char* name, std::size_t len)
210 { return onBeginMember(name, len); }
211
216
226
231
236
240 { return onFinish(); }
241
242 protected:
246 : _parent(0)
247 {}
248
250 virtual void onSetTypeName(const char*, std::size_t)
251 {}
252
254 virtual void onSetId(const char* id, std::size_t len) = 0;
255
257 virtual void onSetString(const Pt::Char*, std::size_t)
258 { throw SerializationError("unexpected string value"); }
259
261 virtual void onSetBinary(const char*, std::size_t)
262 { throw SerializationError("unexpected binary value"); }
263
265 virtual void onSetChar(const Pt::Char&)
266 { throw SerializationError("unexpected char value"); }
267
269 virtual void onSetBool(bool)
270 { throw SerializationError("unexpected bool value"); }
271
273 virtual void onSetInt(Pt::int64_t)
274 { throw SerializationError("unexpected integer value"); }
275
278 { throw SerializationError("unexpected unsigned value"); }
279
281 virtual void onSetFloat(long double)
282 { throw SerializationError("unexpected float value"); }
283
285 virtual void onSetReference(const char*, std::size_t)
286 { throw SerializationError("unexpected reference"); }
287
289 virtual Composer* onBeginMember(const char*, std::size_t)
290 { throw SerializationError("unexpected struct"); }
291
295 { throw SerializationError("unexpected sequence"); }
296
300 { throw SerializationError("unexpected dict"); }
301
305 { throw SerializationError("unexpected dict"); }
306
310 { throw SerializationError("unexpected dict"); }
311
315 { return _parent; }
316
317 private:
318 Composer* _parent;
319};
320
325template <typename T>
327{
328 public:
331 : _type(0)
332 , _si(context)
333 , _current(&_si)
334 { }
335
337 void begin(T& type)
338 {
339 if(_type)
340 {
341 _si.clear();
342 }
343
344 _type = &type;
345 _current = &_si;
346 }
347
348 protected:
349 // inherit docs
350 void onSetId(const char* id, std::size_t len)
351 {
352 _current->setId(id, len);
353 }
354
355 // inherit docs
356 void onSetTypeName(const char* type, std::size_t len)
357 {
358 _current->setTypeName(type, len);
359 }
360
361 void onSetString(const Pt::Char* value, std::size_t len)
362 {
363 _current->setString(value, len);
364 }
365
366 // inherit docs
367 void onSetBinary(const char* data, std::size_t length)
368 {
369 _current->setBinary(data, length);
370 }
371
372 // inherit docs
373 void onSetChar(const Pt::Char& ch)
374 {
375 _current->setChar(ch);
376 }
377
378 // inherit docs
379 void onSetBool(bool value)
380 {
381 _current->setBool(value);
382 }
383
384 // inherit docs
386 {
387 _current->setInt64(value);
388 }
389
390 // inherit docs
392 {
393 _current->setUInt64(value);
394 }
395
396 // inherit docs
397 void onSetFloat(long double value)
398 {
399 _current->setLongDouble(value);
400 }
401
402 // inherit docs
403 void onSetReference(const char* id, std::size_t len)
404 {
405 _current->setReference(id, len);
406 }
407
408 // inherit docs
409 Composer* onBeginMember(const char* name, std::size_t len)
410 {
411 SerializationInfo& child = _current->addMember(name, len);
412 _current = &child;
413 return this;
414 }
415
416 // inherit docs
418 {
419 SerializationInfo& child = _current->addElement();
420 _current = &child;
421 return this;
422 }
423
424 // inherit docs
426 {
427 SerializationInfo& child = _current->addDictElement();
428 _current = &child;
429 return this;
430 }
431
432 // inherit docs
434 {
435 SerializationInfo& child = _current->addDictKey();
436 _current = &child;
437 return this;
438 }
439
440 // inherit docs
442 {
443 SerializationInfo& child = _current->addDictValue();
444 _current = &child;
445 return this;
446 }
447
448 // inherit docs
450 {
451 if( ! _current->parent() )
452 {
453 *_current >> Pt::load() >>= *_type;
454 _si.clear();
455 _type = 0;
456 return parent();
457 }
458
459 _current = _current->parent();
460 return this;
461 }
462
463 private:
464 T* _type;
466 Pt::SerializationInfo* _current;
467};
468
473template <>
475{
476 public:
479 : _si(0)
480 , _current(_si)
481 { }
482
485 {
486 if(_si)
487 {
488 _si->clear();
489 }
490
491 _si = &si;
492 _current = _si;
493 }
494
495 protected:
496 // inherit docs
497 void onSetId(const char* id, std::size_t len)
498 {
499 _current->setId(id, len);
500 }
501
502 // inherit docs
503 void onSetTypeName(const char* type, std::size_t len)
504 {
505 _current->setTypeName(type, len);
506 }
507
508 void onSetString(const Pt::Char* value, std::size_t len)
509 {
510 _current->setString(value, len);
511 }
512
513 // inherit docs
514 void onSetBinary(const char* data, std::size_t length)
515 {
516 _current->setBinary(data, length);
517 }
518
519 // inherit docs
520 void onSetChar(const Pt::Char& ch)
521 {
522 _current->setChar(ch);
523 }
524
525 // inherit docs
526 void onSetBool(bool value)
527 {
528 _current->setBool(value);
529 }
530
531 // inherit docs
533 {
534 _current->setInt64(value);
535 }
536
537 // inherit docs
539 {
540 _current->setUInt64(value);
541 }
542
543 // inherit docs
544 void onSetFloat(long double value)
545 {
546 _current->setLongDouble(value);
547 }
548
549 // inherit docs
550 void onSetReference(const char* id, std::size_t len)
551 {
552 _current->setReference(id, len);
553 }
554
555 // inherit docs
556 Composer* onBeginMember(const char* name, std::size_t len)
557 {
558 SerializationInfo& child = _current->addMember(name, len);
559 _current = &child;
560 return this;
561 }
562
563 // inherit docs
565 {
566 SerializationInfo& child = _current->addElement();
567 _current = &child;
568 return this;
569 }
570
571 // inherit docs
573 {
574 SerializationInfo& child = _current->addDictElement();
575 _current = &child;
576 return this;
577 }
578
579 // inherit docs
581 {
582 SerializationInfo& child = _current->addDictKey();
583 _current = &child;
584 return this;
585 }
586
587 // inherit docs
589 {
590 SerializationInfo& child = _current->addDictValue();
591 _current = &child;
592 return this;
593 }
594
595 // inherit docs
597 {
598 if( ! _current->parent() )
599 {
600 return parent();
601 }
602
603 _current = _current->parent();
604 return this;
605 }
606
607 private:
609 Pt::SerializationInfo* _current;
610};
611
612} // namespace Pt
613
614#endif
Composer * onBeginDictValue()
Begins composition of a dict value.
Definition Composer.h:588
void onSetString(const Pt::Char *value, std::size_t len)
Compose a string value.
Definition Composer.h:508
void onSetId(const char *id, std::size_t len)
Set reference ID.
Definition Composer.h:497
void onSetBool(bool value)
Compose a bool value.
Definition Composer.h:526
Composer * onBeginMember(const char *name, std::size_t len)
Begin composition os a struct member.
Definition Composer.h:556
void onSetReference(const char *id, std::size_t len)
Compose a reference.
Definition Composer.h:550
Composer * onBeginDictKey()
Begins composition of a dict key.
Definition Composer.h:580
Composer * onBeginDictElement()
Begins composition of a dict key.
Definition Composer.h:572
void onSetFloat(long double value)
Compose a floating point value.
Definition Composer.h:544
BasicComposer()
Construct with context.
Definition Composer.h:478
void onSetInt(Pt::int64_t value)
Compose a integer value.
Definition Composer.h:532
void onSetTypeName(const char *type, std::size_t len)
Set type name.
Definition Composer.h:503
Composer * onBeginElement()
Begins composition of a sequence member.
Definition Composer.h:564
void onSetBinary(const char *data, std::size_t length)
Compose a binary value.
Definition Composer.h:514
Composer * onFinish()
Finishes composition of a struct or sequence member.
Definition Composer.h:596
void onSetUInt(Pt::uint64_t value)
Compose a unsigned integer value.
Definition Composer.h:538
void onSetChar(const Pt::Char &ch)
Compose a character value.
Definition Composer.h:520
void begin(Pt::SerializationInfo &si)
Begin composing a type.
Definition Composer.h:484
Composer * onBeginDictValue()
Begins composition of a dict value.
Definition Composer.h:441
void onSetString(const Pt::Char *value, std::size_t len)
Compose a string value.
Definition Composer.h:361
void onSetId(const char *id, std::size_t len)
Set reference ID.
Definition Composer.h:350
void onSetBool(bool value)
Compose a bool value.
Definition Composer.h:379
Composer * onBeginMember(const char *name, std::size_t len)
Begin composition os a struct member.
Definition Composer.h:409
void onSetReference(const char *id, std::size_t len)
Compose a reference.
Definition Composer.h:403
void begin(T &type)
Begin composing a type.
Definition Composer.h:337
Composer * onBeginDictKey()
Begins composition of a dict key.
Definition Composer.h:433
Composer * onBeginDictElement()
Begins composition of a dict key.
Definition Composer.h:425
void onSetFloat(long double value)
Compose a floating point value.
Definition Composer.h:397
void onSetInt(Pt::int64_t value)
Compose a integer value.
Definition Composer.h:385
void onSetTypeName(const char *type, std::size_t len)
Set type name.
Definition Composer.h:356
Composer * onBeginElement()
Begins composition of a sequence member.
Definition Composer.h:417
void onSetBinary(const char *data, std::size_t length)
Compose a binary value.
Definition Composer.h:367
Composer * onFinish()
Finishes composition of a struct or sequence member.
Definition Composer.h:449
void onSetUInt(Pt::uint64_t value)
Compose a unsigned integer value.
Definition Composer.h:391
void onSetChar(const Pt::Char &ch)
Compose a character value.
Definition Composer.h:373
BasicComposer(SerializationContext *context=0)
Construct with context.
Definition Composer.h:330
Composes types during serialization.
Definition Composer.h:101
void setInt(Pt::int64_t value)
Composes a signed integer type.
Definition Composer.h:176
virtual void onSetReference(const char *, std::size_t)
Compose a reference.
Definition Composer.h:285
void setString(const Pt::String &value)
Composes a string value.
Definition Composer.h:148
void setId(const std::string &id)
Sets the reference id of the type to compose.
Definition Composer.h:136
virtual Composer * onBeginMember(const char *, std::size_t)
Begin composition os a struct member.
Definition Composer.h:289
Composer * beginElement()
Begins composition of a sequence member.
Definition Composer.h:214
void setId(const char *id, std::size_t len)
Sets the reference id of the type to compose.
Definition Composer.h:143
Composer * parent() const
Returns the parent composer.
Definition Composer.h:115
void setTypeName(const std::string &type)
Sets the type name of the type to compose.
Definition Composer.h:122
Composer()
Constructor.
Definition Composer.h:245
void setBinary(const char *data, std::size_t length)
Composes a binary value.
Definition Composer.h:158
virtual Composer * onBeginElement()
Begins composition of a sequence member.
Definition Composer.h:294
void setString(const Pt::Char *value, std::size_t len)
Composes a string value.
Definition Composer.h:153
virtual void onSetBinary(const char *, std::size_t)
Compose a binary value.
Definition Composer.h:261
Composer * beginDictKey()
Begins composition of a dict key.
Definition Composer.h:229
void setParent(Composer *parent)
Sets the parent composer.
Definition Composer.h:110
virtual void onSetFloat(long double)
Compose a floating point value.
Definition Composer.h:281
void setFloat(long double value)
Composes a float value.
Definition Composer.h:189
virtual ~Composer()
Destructor.
Definition Composer.h:105
virtual Composer * onBeginDictElement()
Begins composition of a dict key.
Definition Composer.h:299
void setUInt(Pt::int64_t value)
Composes an unsigned integer type.
Definition Composer.h:184
Composer * finish()
Finishes composition of a struct or sequence member.
Definition Composer.h:239
Composer * beginMember(const std::string &name)
Begins composition of a struct member.
Definition Composer.h:204
void setReference(const char *id, std::size_t len)
Composes a reference.
Definition Composer.h:199
virtual Composer * onFinish()
Finishes composition of a struct or sequence member.
Definition Composer.h:314
virtual void onSetBool(bool)
Compose a bool value.
Definition Composer.h:269
virtual void onSetInt(Pt::int64_t)
Compose a integer value.
Definition Composer.h:273
virtual Composer * onBeginDictKey()
Begins composition of a dict key.
Definition Composer.h:304
void setTypeName(const char *type, std::size_t len)
Sets the type name of the type to compose.
Definition Composer.h:129
void setChar(const Pt::Char &ch)
Composes a char value.
Definition Composer.h:163
void setReference(const std::string &id)
Composes a reference.
Definition Composer.h:194
virtual void onSetString(const Pt::Char *, std::size_t)
Compose a string value.
Definition Composer.h:257
void setBool(bool value)
Composes a boolean value.
Definition Composer.h:168
Composer * beginDictElement()
Begins composition of a dict key.
Definition Composer.h:224
virtual Composer * onBeginDictValue()
Begins composition of a dict value.
Definition Composer.h:309
Composer * beginDictValue()
Begins composition of a dict value.
Definition Composer.h:234
Composer * beginMember(const char *name, std::size_t len)
Begins composition of a struct member.
Definition Composer.h:209
virtual void onSetId(const char *id, std::size_t len)=0
Set reference ID.
virtual void onSetUInt(Pt::uint64_t)
Compose a unsigned integer value.
Definition Composer.h:277
virtual void onSetTypeName(const char *, std::size_t)
Set type name.
Definition Composer.h:250
virtual void onSetChar(const Pt::Char &)
Compose a character value.
Definition Composer.h:265
Context for the serialization of types.
Definition Api-SerializationContext.h:26
SerializationError(const std::string &msg)
Construct with message.
Represents arbitrary types during serialization.
Definition SerializationInfo.h:59
SerializationInfo & addElement()
Add a sequence element.
SerializationInfo & addDictValue()
Add a dict value.
SerializationInfo & addDictKey()
Add a dict key.
SerializationInfo & addDictElement()
Add a dict element.
SerializationInfo & addMember(const std::string &name)
Add a struct member.
Definition SerializationInfo.h:449
Unicode capable basic_string.
Definition Api-String.h:67
size_type size() const
Returns the length of the string.
Definition Api-String.h:263
const Pt::Char * c_str() const
Returns a null terminated C string.
Definition Api-String.h:288
int_type int64_t
Signed 64-bit integer type.
Definition Api-Types.h:59
uint_type uint64_t
Unsigned 64-bit integer type.
Definition Api-Types.h:66
Core module.
Definition Allocator.h:33
Unicode character type.
Definition String.h:67