Formatter.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_Formatter_h
30 #define Pt_Formatter_h
31 
32 #include <Pt/Api.h>
33 #include <Pt/String.h>
34 #include <string>
35 #include <cstddef>
36 
37 namespace Pt {
38 
39 class Composer;
40 
45 class Formatter
46 {
47  public:
49  virtual ~Formatter()
50  { }
51 
54  void addString(const std::string& name, const std::string& type,
55  const Pt::String& value, const std::string& id)
56  { onAddString(name.c_str(), type.c_str(), value.c_str(), id.c_str()); }
57 
60  void addString(const char* name, const char* type,
61  const Pt::Char* value, const char* id)
62  { onAddString(name, type, value, id); }
63 
66  void addBinary(const std::string& name, const std::string& type,
67  const char* value, std::size_t length, const std::string& id)
68  { onAddBinary(name.c_str(), type.c_str(), value, length, id.c_str()); }
69 
72  void addBinary(const char* name, const char* type,
73  const char* value, std::size_t length, const char* id)
74  { onAddBinary(name, type, value, length, id); }
75 
78  void addBool(const std::string& name, bool value,
79  const std::string& id)
80  { onAddBool(name.c_str(), value, id.c_str()); }
81 
84  void addBool(const char* name, bool value,
85  const char* id)
86  { onAddBool(name, value, id); }
87 
90  void addChar(const std::string& name, const Pt::Char& value,
91  const std::string& id)
92  { onAddChar(name.c_str(), value, id.c_str()); }
93 
96  void addChar(const char* name, const Pt::Char& value,
97  const char* id)
98  { onAddChar(name, value, id); }
99 
102  void addInt8(const std::string& name, Pt::int8_t value,
103  const std::string& id)
104  { onAddInt8(name.c_str(), value, id.c_str()); }
105 
108  void addInt8(const char* name, Pt::int8_t value,
109  const char* id)
110  { onAddInt8(name, value, id); }
111 
114  void addInt16(const std::string& name, Pt::int16_t value,
115  const std::string& id)
116  { onAddInt16(name.c_str(), value, id.c_str()); }
117 
120  void addInt16(const char* name, Pt::int16_t value,
121  const char* id)
122  { onAddInt16(name, value, id); }
123 
126  void addInt32(const std::string& name, Pt::int32_t value,
127  const std::string& id)
128  { onAddInt32(name.c_str(), value, id.c_str()); }
129 
132  void addInt32(const char* name, Pt::int32_t value,
133  const char* id)
134  { onAddInt32(name, value, id); }
135 
138  void addInt64(const std::string& name, Pt::int64_t value,
139  const std::string& id)
140  { onAddInt64(name.c_str(), value, id.c_str()); }
141 
144  void addInt64(const char* name, Pt::int64_t value,
145  const char* id)
146  { onAddInt64(name, value, id); }
147 
150  void addUInt8(const std::string& name, Pt::uint8_t value,
151  const std::string& id)
152  { onAddUInt8(name.c_str(), value, id.c_str()); }
153 
156  void addUInt8(const char* name, Pt::uint8_t value,
157  const char* id)
158  { onAddUInt8(name, value, id); }
159 
162  void addUInt16(const std::string& name, Pt::uint16_t value,
163  const std::string& id)
164  { onAddUInt16(name.c_str(), value, id.c_str()); }
165 
168  void addUInt16(const char* name, Pt::uint16_t value,
169  const char* id)
170  { onAddUInt16(name, value, id); }
171 
174  void addUInt32(const std::string& name, Pt::uint32_t value,
175  const std::string& id)
176  { onAddUInt32(name.c_str(), value, id.c_str()); }
177 
180  void addUInt32(const char* name, Pt::uint32_t value,
181  const char* id)
182  { onAddUInt32(name, value, id); }
183 
186  void addUInt64(const std::string& name, Pt::uint64_t value,
187  const std::string& id)
188  { onAddUInt64(name.c_str(), value, id.c_str()); }
189 
192  void addUInt64(const char* name, Pt::uint64_t value,
193  const char* id)
194  { onAddUInt64(name, value, id); }
195 
198  void addFloat(const std::string& name, float value,
199  const std::string& id)
200  { onAddFloat(name.c_str(), value, id.c_str()); }
201 
204  void addFloat(const char* name, float value,
205  const char* id)
206  { onAddFloat(name, value, id); }
207 
210  void addDouble(const std::string& name, double value,
211  const std::string& id)
212  { onAddDouble(name.c_str(), value, id.c_str()); }
213 
216  void addDouble(const char* name, double value,
217  const char* id)
218  { onAddDouble(name, value, id); }
219 
222  void addLongDouble(const std::string& name, long double value,
223  const std::string& id)
224  { onAddLongDouble(name.c_str(), value, id.c_str()); }
225 
228  void addLongDouble(const char* name, long double value,
229  const char* id)
230  { onAddLongDouble(name, value, id); }
231 
234  void addReference(const std::string& name, const std::string& refId)
235  { onAddReference(name.c_str(), refId.c_str()); }
236 
239  void addReference(const char* name, const char* refId)
240  { onAddReference(name, refId); }
241 
244  void beginStruct(const std::string& name, const std::string& type,
245  const std::string& id)
246  { onBeginStruct(name.c_str(), type.c_str(), id.c_str()); }
247 
250  void beginStruct(const char* name, const char* type,
251  const char* id)
252  { onBeginStruct(name, type, id); }
253 
256  void beginMember(const std::string& name)
257  { onBeginMember(name.c_str()); }
258 
261  void beginMember(const char* name)
262  { onBeginMember(name); }
263 
267  { onFinishMember(); }
268 
272  { onFinishStruct(); }
273 
276  void beginSequence(const std::string& name, const std::string& type,
277  const std::string& id)
278  { onBeginSequence(name.c_str(), type.c_str(), id.c_str()); }
279 
282  void beginSequence(const char* name, const char* type,
283  const char* id)
284  { onBeginSequence(name, type, id); }
285 
289  { onBeginElement(); }
290 
294  { onFinishElement(); }
295 
299  { onFinishSequence(); }
300 
303  void beginDict(const std::string& name, const std::string& type,
304  const std::string& id)
305  { onBeginDict(name.c_str(), type.c_str(), id.c_str()); }
306 
309  void beginDict(const char* name, const char* type,
310  const char* id)
311  { onBeginDict(name, type, id); }
312 
316  { onBeginDictElement(); }
317 
321  { onFinishDictElement(); }
322 
326  { onBeginDictKey(); }
327 
331  { onFinishDictKey(); }
332 
336  { onBeginDictValue(); }
337 
341  { onFinishDictValue(); }
342 
345  void finishDict()
346  { onFinishDict(); }
347 
348  public:
350  void beginParse(Composer& composer)
351  { onBeginParse(composer); }
352 
354  bool parseSome()
355  { return onParseSome(); }
356 
358  void parse()
359  { onParse(); }
360 
361  protected:
364  virtual void onAddString(const char* name, const char* type,
365  const Pt::Char* value, const char* id) = 0;
366 
369  virtual void onAddBinary(const char* name, const char* type,
370  const char* value, std::size_t length, const char* id) = 0;
371 
374  virtual void onAddBool(const char* name, bool value,
375  const char* id) = 0;
376 
379  virtual void onAddChar(const char* name, const Pt::Char& value,
380  const char* id) = 0;
381 
384  virtual void onAddInt8(const char* name, Pt::int8_t value,
385  const char* id) = 0;
386 
389  virtual void onAddInt16(const char* name, Pt::int16_t value,
390  const char* id) = 0;
391 
394  virtual void onAddInt32(const char* name, Pt::int32_t value,
395  const char* id) = 0;
396 
399  virtual void onAddInt64(const char* name, Pt::int64_t value,
400  const char* id) = 0;
401 
404  virtual void onAddUInt8(const char* name, Pt::uint8_t value,
405  const char* id) = 0;
406 
409  virtual void onAddUInt16(const char* name, Pt::uint16_t value,
410  const char* id) = 0;
411 
414  virtual void onAddUInt32(const char* name, Pt::uint32_t value,
415  const char* id) = 0;
416 
419  virtual void onAddUInt64(const char* name, Pt::uint64_t value,
420  const char* id) = 0;
421 
424  virtual void onAddFloat(const char* name, float value,
425  const char* id) = 0;
426 
429  virtual void onAddDouble(const char* name, double value,
430  const char* id) = 0;
431 
434  virtual void onAddLongDouble(const char* name, long double value,
435  const char* id) = 0;
436 
439  virtual void onAddReference(const char* name, const char* refId) = 0;
440 
443  virtual void onBeginStruct(const char* name, const char* type,
444  const char* id) = 0;
445 
448  virtual void onBeginMember(const char* name) = 0;
449 
452  virtual void onFinishMember() = 0;
453 
456  virtual void onFinishStruct() = 0;
457 
460  virtual void onBeginSequence(const char* name, const char* type,
461  const char* id) = 0;
462 
465  virtual void onBeginElement() = 0;
466 
469  virtual void onFinishElement() = 0;
470 
473  virtual void onFinishSequence() = 0;
474 
477  virtual void onBeginDict(const char* name, const char* type,
478  const char* id)
479  { onBeginSequence(name, type, id); }
480 
483  virtual void onBeginDictElement()
484  { onBeginSequence("", "", ""); }
485 
488  virtual void onBeginDictKey()
489  { onBeginElement(); }
490 
493  virtual void onFinishDictKey()
494  { onFinishElement(); }
495 
498  virtual void onBeginDictValue()
499  { onBeginElement(); }
500 
503  virtual void onFinishDictValue()
504  { onFinishElement(); }
505 
508  virtual void onFinishDictElement()
509  { onFinishSequence(); }
510 
513  virtual void onFinishDict()
514  { onFinishSequence(); }
515 
516  protected:
518  virtual void onBeginParse(Composer& composer) = 0;
519 
521  virtual bool onParseSome() = 0;
522 
524  virtual void onParse() = 0;
525 
526  protected:
530  {}
531 };
532 
533 } // namespace Pt
534 
535 #endif
void addInt64(const std::string &name, Pt::int64_t value, const std::string &id)
Formats a 64-bit signed integer value.
Definition: Formatter.h:138
void beginElement()
Formats the begin of an array element.
Definition: Formatter.h:288
virtual void onFinishDictValue()
Formats the end of a dict value.
Definition: Formatter.h:503
virtual void onFinishMember()=0
Formats the end of a struct member.
uint_type uint16_t
Unsigned 16-bit integer type.
Definition: Types.h:30
void addUInt16(const std::string &name, Pt::uint16_t value, const std::string &id)
Formats a 16-bit unsigned integer value.
Definition: Formatter.h:162
void addInt16(const char *name, Pt::int16_t value, const char *id)
Formats a 16-bit signed integer value.
Definition: Formatter.h:120
void addBinary(const std::string &name, const std::string &type, const char *value, std::size_t length, const std::string &id)
Formats a binary value.
Definition: Formatter.h:66
void addUInt64(const char *name, Pt::uint64_t value, const char *id)
Formats a 64-bit unsigned integer value.
Definition: Formatter.h:192
void finishDict()
Formats the end of a dict.
Definition: Formatter.h:345
virtual void onAddDouble(const char *name, double value, const char *id)=0
Formats a double value.
void addUInt32(const std::string &name, Pt::uint32_t value, const std::string &id)
Formats a 32-bit unsigned integer value.
Definition: Formatter.h:174
virtual void onAddUInt64(const char *name, Pt::uint64_t value, const char *id)=0
Formats a 64-bit unsigned integer.
int_type int16_t
Signed 16-bit integer type.
Definition: Types.h:24
virtual bool onParseSome()=0
Returns true if composer completes, false if no more data available.
void addChar(const char *name, const Pt::Char &value, const char *id)
Formats a character value.
Definition: Formatter.h:96
virtual void onAddString(const char *name, const char *type, const Pt::Char *value, const char *id)=0
Formats a string value.
void addUInt32(const char *name, Pt::uint32_t value, const char *id)
Formats a 32-bit unsigned integer value.
Definition: Formatter.h:180
void addString(const char *name, const char *type, const Pt::Char *value, const char *id)
Formats a string value.
Definition: Formatter.h:60
void addLongDouble(const std::string &name, long double value, const std::string &id)
Formats a long double value.
Definition: Formatter.h:222
void beginDict(const char *name, const char *type, const char *id)
Formats the begin of a dict.
Definition: Formatter.h:309
Composes types during serialization.
Definition: Composer.h:42
void addLongDouble(const char *name, long double value, const char *id)
Formats a long double value.
Definition: Formatter.h:228
virtual void onFinishElement()=0
Formats the end of a sequence element.
void parse()
Parse until composer completes.
Definition: Formatter.h:358
void beginStruct(const char *name, const char *type, const char *id)
Formats the begin of a struct.
Definition: Formatter.h:250
void finishMember()
Formats the end of a struct member.
Definition: Formatter.h:266
virtual ~Formatter()
Destructor.
Definition: Formatter.h:49
void beginMember(const char *name)
Formats the begin of a struct member.
Definition: Formatter.h:261
void beginSequence(const char *name, const char *type, const char *id)
Formats the begin of an array.
Definition: Formatter.h:282
void addInt64(const char *name, Pt::int64_t value, const char *id)
Formats a 64-bit signed integer value.
Definition: Formatter.h:144
virtual void onBeginSequence(const char *name, const char *type, const char *id)=0
Formats the begin of a sequence.
void finishDictValue()
Formats the end of a dict value.
Definition: Formatter.h:340
void beginDictKey()
Formats the begin of a dict key.
Definition: Formatter.h:325
virtual void onBeginParse(Composer &composer)=0
Begin to parse to a composer.
void addString(const std::string &name, const std::string &type, const Pt::String &value, const std::string &id)
Formats a string value.
Definition: Formatter.h:54
virtual void onFinishStruct()=0
Formats the end of a struct.
virtual void onBeginElement()=0
Formats the begin of a sequence element.
virtual void onFinishDictElement()
Formats the end of a dict element.
Definition: Formatter.h:508
virtual void onAddInt8(const char *name, Pt::int8_t value, const char *id)=0
Formats a 8-bit integer.
void addReference(const std::string &name, const std::string &refId)
Formats a reference.
Definition: Formatter.h:234
void addBinary(const char *name, const char *type, const char *value, std::size_t length, const char *id)
Formats a binary value.
Definition: Formatter.h:72
const Pt::Char * c_str() const
Returns a null terminated C string.
Definition: String.h:264
void finishDictKey()
Formats the end of a dict key.
Definition: Formatter.h:330
virtual void onBeginDictElement()
Formats the begin of a dict element.
Definition: Formatter.h:483
int_type int32_t
Signed 32-bit integer type.
Definition: Types.h:36
virtual void onAddChar(const char *name, const Pt::Char &value, const char *id)=0
Formats a character value.
void addBool(const std::string &name, bool value, const std::string &id)
Formats a boolean value.
Definition: Formatter.h:78
void addUInt64(const std::string &name, Pt::uint64_t value, const std::string &id)
Formats a 64-bit unsigned integer value.
Definition: Formatter.h:186
Unicode character type.
Definition: String.h:66
void finishStruct()
Formats the end of a struct.
Definition: Formatter.h:271
int_type int64_t
Signed 64-bit integer type.
Definition: Types.h:48
void addUInt8(const std::string &name, Pt::uint8_t value, const std::string &id)
Formats a 8-bit unsigned integer value.
Definition: Formatter.h:150
void addFloat(const char *name, float value, const char *id)
Formats a float value.
Definition: Formatter.h:204
void beginMember(const std::string &name)
Formats the begin of a struct member.
Definition: Formatter.h:256
virtual void onBeginStruct(const char *name, const char *type, const char *id)=0
Formats the begin of a struct.
virtual void onBeginDictValue()
Formats the begin of a dict value.
Definition: Formatter.h:498
void addUInt16(const char *name, Pt::uint16_t value, const char *id)
Formats a 16-bit unsigned integer value.
Definition: Formatter.h:168
virtual void onFinishDict()
Formats the end of a dict.
Definition: Formatter.h:513
virtual void onAddBinary(const char *name, const char *type, const char *value, std::size_t length, const char *id)=0
Formats a binary value.
Formatter()
Default constructor.
Definition: Formatter.h:529
int_type int8_t
Signed 8-bit integer type.
Definition: Types.h:12
void addDouble(const std::string &name, double value, const std::string &id)
Formats a double value.
Definition: Formatter.h:210
void beginDictElement()
Formats the begin of a dict element.
Definition: Formatter.h:315
void addUInt8(const char *name, Pt::uint8_t value, const char *id)
Formats a 8-bit unsigned integer value.
Definition: Formatter.h:156
virtual void onFinishDictKey()
Formats the end of a dict key.
Definition: Formatter.h:493
virtual void onBeginMember(const char *name)=0
Formats the begin of a struct member.
virtual void onAddLongDouble(const char *name, long double value, const char *id)=0
Formats a long double value.
void finishDictElement()
Formats the end of a dict element.
Definition: Formatter.h:320
uint_type uint64_t
Unsigned 64-bit integer type.
Definition: Types.h:54
void beginDictValue()
Formats the begin of a dict value.
Definition: Formatter.h:335
void addInt32(const std::string &name, Pt::int32_t value, const std::string &id)
Formats a 32-bit signed integer value.
Definition: Formatter.h:126
Unicode capable basic_string.
Definition: String.h:42
void addInt8(const std::string &name, Pt::int8_t value, const std::string &id)
Formats a 8-bit signed integer value.
Definition: Formatter.h:102
void beginStruct(const std::string &name, const std::string &type, const std::string &id)
Formats the begin of a struct.
Definition: Formatter.h:244
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Types.h:42
virtual void onAddInt32(const char *name, Pt::int32_t value, const char *id)=0
Formats a 32-bit integer.
void addReference(const char *name, const char *refId)
Formats a reference.
Definition: Formatter.h:239
virtual void onFinishSequence()=0
Formats the end of a sequence.
void addInt16(const std::string &name, Pt::int16_t value, const std::string &id)
Formats a 16-bit signed integer value.
Definition: Formatter.h:114
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Types.h:18
virtual void onAddFloat(const char *name, float value, const char *id)=0
Formats a float value.
virtual void onAddUInt16(const char *name, Pt::uint16_t value, const char *id)=0
Formats a 16-bit unsigned integer.
void addDouble(const char *name, double value, const char *id)
Formats a double value.
Definition: Formatter.h:216
virtual void onAddInt16(const char *name, Pt::int16_t value, const char *id)=0
Formats a 16-bit integer.
virtual void onAddUInt32(const char *name, Pt::uint32_t value, const char *id)=0
Formats a 32-bit unsigned integer.
void addChar(const std::string &name, const Pt::Char &value, const std::string &id)
Formats a character value.
Definition: Formatter.h:90
void beginParse(Composer &composer)
Begin to parse to a composer.
Definition: Formatter.h:350
virtual void onAddUInt8(const char *name, Pt::uint8_t value, const char *id)=0
Formats a 8-bit unsigned integer.
bool parseSome()
Returns true if composer completes, false if no more data available.
Definition: Formatter.h:354
void beginDict(const std::string &name, const std::string &type, const std::string &id)
Formats the begin of a dict.
Definition: Formatter.h:303
void addBool(const char *name, bool value, const char *id)
Formats a boolean value.
Definition: Formatter.h:84
Support for serialization to different formats.
Definition: Formatter.h:45
virtual void onParse()=0
Parse until composer completes.
virtual void onAddBool(const char *name, bool value, const char *id)=0
Formats a bool value.
virtual void onAddInt64(const char *name, Pt::int64_t value, const char *id)=0
Formats a 64-bit integer.
virtual void onBeginDictKey()
Formats the begin of a dict key.
Definition: Formatter.h:488
void beginSequence(const std::string &name, const std::string &type, const std::string &id)
Formats the begin of an array.
Definition: Formatter.h:276
void addInt32(const char *name, Pt::int32_t value, const char *id)
Formats a 32-bit signed integer value.
Definition: Formatter.h:132
void finishSequence()
Formats the end of an array.
Definition: Formatter.h:298
void finishElement()
Formats the end of an array element.
Definition: Formatter.h:293
virtual void onAddReference(const char *name, const char *refId)=0
Formats a reference.
virtual void onBeginDict(const char *name, const char *type, const char *id)
Formats the begin of a dict.
Definition: Formatter.h:477
void addFloat(const std::string &name, float value, const std::string &id)
Formats a float value.
Definition: Formatter.h:198
void addInt8(const char *name, Pt::int8_t value, const char *id)
Formats a 8-bit signed integer value.
Definition: Formatter.h:108