Formatter.h
1 /*
2  * Copyright (C) 2014 by Dr. 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_Soap_Formatter_h
30 #define Pt_Soap_Formatter_h
31 
32 #include <Pt/Soap/Api.h>
33 #include <Pt/Xml/XmlReader.h>
34 #include <Pt/Composer.h>
35 #include <Pt/Formatter.h>
36 #include <Pt/String.h>
37 #include <Pt/NonCopyable.h>
38 #include <Pt/Types.h>
39 #include <string>
40 #include <iostream>
41 
42 namespace Pt {
43 
44 namespace Soap {
45 
46 class Parameter;
47 
50 class PT_SOAP_API Formatter : public Pt::Formatter
51  , private NonCopyable
52 {
53  public:
54  Formatter(std::basic_ostream<Char>& os);
55 
56  ~Formatter();
57 
58  void setParameter(const Parameter& p);
59 
60  void attach(Xml::XmlReader& reader);
61 
62  void attach(std::basic_ostream<Char>& os);
63 
67  bool advance(const Pt::Xml::Node& node);
68 
69  protected:
70  void onAddString(const char* name, const char* type,
71  const Pt::Char* value, const char* id);
72 
73  void onAddBool(const char* name, bool value,
74  const char* id);
75 
76  void onAddChar(const char* name, const Pt::Char& value,
77  const char* id);
78 
79  void onAddInt8(const char* name, Pt::int8_t value,
80  const char* id);
81 
82  void onAddInt16(const char* name, Pt::int16_t value,
83  const char* id);
84 
85  void onAddInt32(const char* name, Pt::int32_t value,
86  const char* id);
87 
88  void onAddInt64(const char* name, Pt::int64_t value,
89  const char* id);
90 
91  void onAddUInt8(const char* name, Pt::uint8_t value, const char* id);
92 
93  void onAddUInt16(const char* name, Pt::uint16_t value, const char* id);
94 
95  void onAddUInt32(const char* name, Pt::uint32_t value, const char* id);
96 
97  void onAddUInt64(const char* name, Pt::uint64_t value, const char* id);
98 
99  void onAddFloat(const char* name, float value,
100  const char* id);
101 
102  void onAddDouble(const char* name, double value,
103  const char* id);
104 
105  void onAddLongDouble(const char* name, long double value,
106  const char* id);
107 
108  void onAddBinary(const char* name, const char* type,
109  const char* value, std::size_t length, const char* id);
110 
111  void onAddReference(const char* name, const char* id);
112 
113  void onBeginSequence(const char* name, const char* type,
114  const char* id);
115 
116  virtual void onBeginElement();
117 
118  virtual void onFinishElement();
119 
120  void onFinishSequence();
121 
122  void onBeginStruct(const char* name, const char* type,
123  const char* id);
124 
125  void onBeginMember(const char* name);
126 
127  void onFinishMember();
128 
129  void onFinishStruct();
130 
131  virtual void onBeginDict(const char* name, const char* type,
132  const char* id);
133 
134  virtual void onBeginDictElement();
135 
136  virtual void onBeginDictKey();
137 
138  virtual void onFinishDictKey();
139 
140  virtual void onBeginDictValue();
141 
142  virtual void onFinishDictValue();
143 
144  virtual void onFinishDictElement();
145 
146  virtual void onFinishDict();
147 
148  protected:
149  void onBeginParse(Composer& composer);
150 
151  bool onParseSome();
152 
153  void onParse();
154 
155  private:
156  enum State
157  {
158  OnBegin,
159  OnStartElement,
160  OnCharacters,
161  OnDictElement,
162  OnEndElement
163  };
164 
165  State _state;
166  Xml::XmlReader* _reader;
167  std::vector<const Parameter*> _paramStack;
168  Composer* _composer;
169 
170  std::basic_ostream<Char>* _os;
171  Pt::String _str;
172  std::vector<char> _data;
173 
174  Pt::varint_t _r1;
175  Pt::varint_t _r2;
176 };
177 
178 } // namespace Soap
179 
180 } // namespace Pt
181 
182 #endif // Pt_Soap_Formatter_h
uint_type uint16_t
Unsigned 16-bit integer type.
Definition: Types.h:30
int_type int16_t
Signed 16-bit integer type.
Definition: Types.h:24
int_type int32_t
Signed 32-bit integer type.
Definition: Types.h:36
Unicode character type.
Definition: String.h:66
int_type int64_t
Signed 64-bit integer type.
Definition: Types.h:48
int_type int8_t
Signed 8-bit integer type.
Definition: Types.h:12
uint_type uint64_t
Unsigned 64-bit integer type.
Definition: Types.h:54
Unicode capable basic_string.
Definition: String.h:42
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Types.h:42
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Types.h:18
XML document node.
Definition: Node.h:50
Support for serialization to different formats.
Definition: Formatter.h:45