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