serializer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Tommi Maekitalo
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 CXXTOOLS_BIN_SERIALIZER_H
30 #define CXXTOOLS_BIN_SERIALIZER_H
31 
32 #include <cxxtools/bin/formatter.h>
33 #include <cxxtools/decomposer.h>
34 
35 namespace cxxtools
36 {
37  namespace bin
38  {
39  class Serializer
40  {
41  // make non copyable
42  Serializer(const Serializer&) { }
43  Serializer& operator=(const Serializer&) { return *this; }
44 
45  public:
46  enum TypeCode
47  {
48  TypeEmpty = 0x00,
49  TypeBool = 0x01,
50  TypeChar = 0x02,
51  TypeString = 0x03,
52  TypeInt = 0x04,
53  TypeBinary2 = 0x06, // followed by zero terminated, 2 byte length field + data
54  TypeBinary4 = 0x07, // followed by zero terminated, 4 byte length field + data
55  TypeInt8 = 0x10,
56  TypeInt16 = 0x11,
57  TypeInt32 = 0x12,
58  TypeInt64 = 0x13,
59  TypeUInt8 = 0x18,
60  TypeUInt16 = 0x19,
61  TypeUInt32 = 0x1a,
62  TypeUInt64 = 0x1b,
63  TypeBcdFloat = 0x20,
64  TypeShortFloat = 0x21, // 1 bit sign, 7 bit exponent, 16 bit mantissa (3 byte)
65  TypeMediumFloat = 0x22, // 1 bit sign, 7 bit exponent, 32 bit mantissa (5 byte)
66  TypeLongFloat = 0x23, // 1 bit sign, 15 bit exponent, 64 bit mantissa (10 byte)
67  TypePair = 0x30,
68  TypeArray = 0x31,
69  TypeVector = 0x32,
70  TypeList = 0x33,
71  TypeDeque = 0x34,
72  TypeSet = 0x35,
73  TypeMultiset = 0x36,
74  TypeMap = 0x37,
75  TypeMultimap = 0x38,
76  TypeOther = 0x3f, // followed by zero terminated type name, data is zero terminated
78  TypePlainBool = 0x41,
79  TypePlainChar = 0x42,
81  TypePlainInt = 0x44,
84  TypePlainInt8 = 0x50,
93  TypePlainShortFloat = 0x61, // 1 bit sign, 7 bit exponent, 16 bit mantissa
94  TypePlainMediumFloat = 0x62, // 1 bit sign, 7 bit exponent, 32 bit mantissa
95  TypePlainLongFloat = 0x63, // 1 bit sign, 15 bit exponent, 64 bit mantissa
96  TypePlainPair = 0x70,
99  TypePlainList = 0x73,
101  TypePlainSet = 0x75,
103  TypePlainMap = 0x77,
105  TypePlainOther = 0x7f, // followed by zero terminated type name, data is zero terminated
109  RpcRequest = 0xc0,
110  RpcResponse = 0xc1,
111  RpcException = 0xc2,
112  Eod = 0xff
113  };
114 
116  {
117  }
118 
119  explicit Serializer(std::ostream& out)
120  {
121  _formatter.begin(out);
122  }
123 
124  Serializer& begin(std::ostream& out)
125  {
126  _formatter.begin(out);
127  return *this;
128  }
129 
130  template <typename T>
131  Serializer& serialize(const T& v, const std::string& name)
132  {
133  Decomposer<T> s;
134  s.begin(v);
135  s.setName(name);
136  s.format(_formatter);
137  return *this;
138  }
139 
140  template <typename T>
141  Serializer& serialize(const T& v)
142  {
143  Decomposer<T> s;
144  s.begin(v);
145  s.format(_formatter);
146  return *this;
147  }
148 
149  void finish()
150  { }
151 
152  private:
153  Formatter _formatter;
154  };
155  }
156 }
157 
158 #endif // CXXTOOLS_BIN_SERIALIZER_H
159