1 //
2 // MessagePack for C++ serializing routine
3 //
4 // Copyright (C) 2008-2010 FURUHASHI Sadayuki
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18 #ifndef MSGPACK_PACK_HPP__
19 #define MSGPACK_PACK_HPP__
20
21 #include "pack_define.h"
22 #include <stdexcept>
23 #include <limits.h>
24
25 namespace msgpack {
26
27
28 template <typename Stream>
29 class packer {
30 public:
31 packer(Stream* s);
32 packer(Stream& s);
33 ~packer();
34
35 public:
36 template <typename T>
37 packer<Stream>& pack(const T& v);
38
39 packer<Stream>& pack_uint8(uint8_t d);
40 packer<Stream>& pack_uint16(uint16_t d);
41 packer<Stream>& pack_uint32(uint32_t d);
42 packer<Stream>& pack_uint64(uint64_t d);
43 packer<Stream>& pack_int8(int8_t d);
44 packer<Stream>& pack_int16(int16_t d);
45 packer<Stream>& pack_int32(int32_t d);
46 packer<Stream>& pack_int64(int64_t d);
47
48 packer<Stream>& pack_fix_uint8(uint8_t d);
49 packer<Stream>& pack_fix_uint16(uint16_t d);
50 packer<Stream>& pack_fix_uint32(uint32_t d);
51 packer<Stream>& pack_fix_uint64(uint64_t d);
52 packer<Stream>& pack_fix_int8(int8_t d);
53 packer<Stream>& pack_fix_int16(int16_t d);
54 packer<Stream>& pack_fix_int32(int32_t d);
55 packer<Stream>& pack_fix_int64(int64_t d);
56
57 packer<Stream>& pack_short(short d);
58 packer<Stream>& pack_int(int d);
59 packer<Stream>& pack_long(long d);
60 packer<Stream>& pack_long_long(long long d);
61 packer<Stream>& pack_unsigned_short(unsigned short d);
62 packer<Stream>& pack_unsigned_int(unsigned int d);
63 packer<Stream>& pack_unsigned_long(unsigned long d);
64 packer<Stream>& pack_unsigned_long_long(unsigned long long d);
65
66 packer<Stream>& pack_float(float d);
67 packer<Stream>& pack_double(double d);
68
69 packer<Stream>& pack_nil();
70 packer<Stream>& pack_true();
71 packer<Stream>& pack_false();
72
73 packer<Stream>& pack_array(unsigned int n);
74
75 packer<Stream>& pack_map(unsigned int n);
76
77 packer<Stream>& pack_raw(size_t l);
78 packer<Stream>& pack_raw_body(const char* b, size_t l);
79
80 private:
81 static void _pack_uint8(Stream& x, uint8_t d);
82 static void _pack_uint16(Stream& x, uint16_t d);
83 static void _pack_uint32(Stream& x, uint32_t d);
84 static void _pack_uint64(Stream& x, uint64_t d);
85 static void _pack_int8(Stream& x, int8_t d);
86 static void _pack_int16(Stream& x, int16_t d);
87 static void _pack_int32(Stream& x, int32_t d);
88 static void _pack_int64(Stream& x, int64_t d);
89
90 static void _pack_fix_uint8(Stream& x, uint8_t d);
91 static void _pack_fix_uint16(Stream& x, uint16_t d);
92 static void _pack_fix_uint32(Stream& x, uint32_t d);
93 static void _pack_fix_uint64(Stream& x, uint64_t d);
94 static void _pack_fix_int8(Stream& x, int8_t d);
95 static void _pack_fix_int16(Stream& x, int16_t d);
96 static void _pack_fix_int32(Stream& x, int32_t d);
97 static void _pack_fix_int64(Stream& x, int64_t d);
98
99 static void _pack_short(Stream& x, short d);
100 static void _pack_int(Stream& x, int d);
101 static void _pack_long(Stream& x, long d);
102 static void _pack_long_long(Stream& x, long long d);
103 static void _pack_unsigned_short(Stream& x, unsigned short d);
104 static void _pack_unsigned_int(Stream& x, unsigned int d);
105 static void _pack_unsigned_long(Stream& x, unsigned long d);
106 static void _pack_unsigned_long_long(Stream& x, unsigned long long d);
107
108 static void _pack_float(Stream& x, float d);
109 static void _pack_double(Stream& x, double d);
110
111 static void _pack_nil(Stream& x);
112 static void _pack_true(Stream& x);
113 static void _pack_false(Stream& x);
114
115 static void _pack_array(Stream& x, unsigned int n);
116
117 static void _pack_map(Stream& x, unsigned int n);
118
119 static void _pack_raw(Stream& x, size_t l);
120 static void _pack_raw_body(Stream& x, const void* b, size_t l);
121
append_buffer(Stream & x,const unsigned char * buf,unsigned int len)122 static void append_buffer(Stream& x, const unsigned char* buf, unsigned int len)
123 { x.write((const char*)buf, len); }
124
125 private:
126 Stream& m_stream;
127
128 private:
129 packer();
130 };
131
132
133 template <typename Stream, typename T>
pack(Stream * s,const T & v)134 inline void pack(Stream* s, const T& v)
135 {
136 packer<Stream>(s).pack(v);
137 }
138
139 template <typename Stream, typename T>
pack(Stream & s,const T & v)140 inline void pack(Stream& s, const T& v)
141 {
142 packer<Stream>(s).pack(v);
143 }
144
145
146 #define msgpack_pack_inline_func(name) \
147 template <typename Stream> \
148 inline void packer<Stream>::_pack ## name
149
150 #define msgpack_pack_inline_func_cint(name) \
151 template <typename Stream> \
152 inline void packer<Stream>::_pack ## name
153
154 #define msgpack_pack_inline_func_fixint(name) \
155 template <typename Stream> \
156 inline void packer<Stream>::_pack_fix ## name
157
158 #define msgpack_pack_user Stream&
159
160 #define msgpack_pack_append_buffer append_buffer
161
162 #include "pack_template.h"
163
164
165 template <typename Stream>
packer(Stream * s)166 packer<Stream>::packer(Stream* s) : m_stream(*s) { }
167
168 template <typename Stream>
packer(Stream & s)169 packer<Stream>::packer(Stream& s) : m_stream(s) { }
170
171 template <typename Stream>
~packer()172 packer<Stream>::~packer() { }
173
174
175 template <typename Stream>
pack_uint8(uint8_t d)176 inline packer<Stream>& packer<Stream>::pack_uint8(uint8_t d)
177 { _pack_uint8(m_stream, d); return *this; }
178
179 template <typename Stream>
pack_uint16(uint16_t d)180 inline packer<Stream>& packer<Stream>::pack_uint16(uint16_t d)
181 { _pack_uint16(m_stream, d); return *this; }
182
183 template <typename Stream>
pack_uint32(uint32_t d)184 inline packer<Stream>& packer<Stream>::pack_uint32(uint32_t d)
185 { _pack_uint32(m_stream, d); return *this; }
186
187 template <typename Stream>
pack_uint64(uint64_t d)188 inline packer<Stream>& packer<Stream>::pack_uint64(uint64_t d)
189 { _pack_uint64(m_stream, d); return *this; }
190
191 template <typename Stream>
pack_int8(int8_t d)192 inline packer<Stream>& packer<Stream>::pack_int8(int8_t d)
193 { _pack_int8(m_stream, d); return *this; }
194
195 template <typename Stream>
pack_int16(int16_t d)196 inline packer<Stream>& packer<Stream>::pack_int16(int16_t d)
197 { _pack_int16(m_stream, d); return *this; }
198
199 template <typename Stream>
pack_int32(int32_t d)200 inline packer<Stream>& packer<Stream>::pack_int32(int32_t d)
201 { _pack_int32(m_stream, d); return *this; }
202
203 template <typename Stream>
pack_int64(int64_t d)204 inline packer<Stream>& packer<Stream>::pack_int64(int64_t d)
205 { _pack_int64(m_stream, d); return *this;}
206
207
208 template <typename Stream>
pack_fix_uint8(uint8_t d)209 inline packer<Stream>& packer<Stream>::pack_fix_uint8(uint8_t d)
210 { _pack_fix_uint8(m_stream, d); return *this; }
211
212 template <typename Stream>
pack_fix_uint16(uint16_t d)213 inline packer<Stream>& packer<Stream>::pack_fix_uint16(uint16_t d)
214 { _pack_fix_uint16(m_stream, d); return *this; }
215
216 template <typename Stream>
pack_fix_uint32(uint32_t d)217 inline packer<Stream>& packer<Stream>::pack_fix_uint32(uint32_t d)
218 { _pack_fix_uint32(m_stream, d); return *this; }
219
220 template <typename Stream>
pack_fix_uint64(uint64_t d)221 inline packer<Stream>& packer<Stream>::pack_fix_uint64(uint64_t d)
222 { _pack_fix_uint64(m_stream, d); return *this; }
223
224 template <typename Stream>
pack_fix_int8(int8_t d)225 inline packer<Stream>& packer<Stream>::pack_fix_int8(int8_t d)
226 { _pack_fix_int8(m_stream, d); return *this; }
227
228 template <typename Stream>
pack_fix_int16(int16_t d)229 inline packer<Stream>& packer<Stream>::pack_fix_int16(int16_t d)
230 { _pack_fix_int16(m_stream, d); return *this; }
231
232 template <typename Stream>
pack_fix_int32(int32_t d)233 inline packer<Stream>& packer<Stream>::pack_fix_int32(int32_t d)
234 { _pack_fix_int32(m_stream, d); return *this; }
235
236 template <typename Stream>
pack_fix_int64(int64_t d)237 inline packer<Stream>& packer<Stream>::pack_fix_int64(int64_t d)
238 { _pack_fix_int64(m_stream, d); return *this;}
239
240
241 template <typename Stream>
pack_short(short d)242 inline packer<Stream>& packer<Stream>::pack_short(short d)
243 { _pack_short(m_stream, d); return *this; }
244
245 template <typename Stream>
pack_int(int d)246 inline packer<Stream>& packer<Stream>::pack_int(int d)
247 { _pack_int(m_stream, d); return *this; }
248
249 template <typename Stream>
pack_long(long d)250 inline packer<Stream>& packer<Stream>::pack_long(long d)
251 { _pack_long(m_stream, d); return *this; }
252
253 template <typename Stream>
pack_long_long(long long d)254 inline packer<Stream>& packer<Stream>::pack_long_long(long long d)
255 { _pack_long_long(m_stream, d); return *this; }
256
257 template <typename Stream>
pack_unsigned_short(unsigned short d)258 inline packer<Stream>& packer<Stream>::pack_unsigned_short(unsigned short d)
259 { _pack_unsigned_short(m_stream, d); return *this; }
260
261 template <typename Stream>
pack_unsigned_int(unsigned int d)262 inline packer<Stream>& packer<Stream>::pack_unsigned_int(unsigned int d)
263 { _pack_unsigned_int(m_stream, d); return *this; }
264
265 template <typename Stream>
pack_unsigned_long(unsigned long d)266 inline packer<Stream>& packer<Stream>::pack_unsigned_long(unsigned long d)
267 { _pack_unsigned_long(m_stream, d); return *this; }
268
269 template <typename Stream>
pack_unsigned_long_long(unsigned long long d)270 inline packer<Stream>& packer<Stream>::pack_unsigned_long_long(unsigned long long d)
271 { _pack_unsigned_long_long(m_stream, d); return *this; }
272
273
274 template <typename Stream>
pack_float(float d)275 inline packer<Stream>& packer<Stream>::pack_float(float d)
276 { _pack_float(m_stream, d); return *this; }
277
278 template <typename Stream>
pack_double(double d)279 inline packer<Stream>& packer<Stream>::pack_double(double d)
280 { _pack_double(m_stream, d); return *this; }
281
282
283 template <typename Stream>
pack_nil()284 inline packer<Stream>& packer<Stream>::pack_nil()
285 { _pack_nil(m_stream); return *this; }
286
287 template <typename Stream>
pack_true()288 inline packer<Stream>& packer<Stream>::pack_true()
289 { _pack_true(m_stream); return *this; }
290
291 template <typename Stream>
pack_false()292 inline packer<Stream>& packer<Stream>::pack_false()
293 { _pack_false(m_stream); return *this; }
294
295
296 template <typename Stream>
pack_array(unsigned int n)297 inline packer<Stream>& packer<Stream>::pack_array(unsigned int n)
298 { _pack_array(m_stream, n); return *this; }
299
300
301 template <typename Stream>
pack_map(unsigned int n)302 inline packer<Stream>& packer<Stream>::pack_map(unsigned int n)
303 { _pack_map(m_stream, n); return *this; }
304
305
306 template <typename Stream>
pack_raw(size_t l)307 inline packer<Stream>& packer<Stream>::pack_raw(size_t l)
308 { _pack_raw(m_stream, l); return *this; }
309
310 template <typename Stream>
pack_raw_body(const char * b,size_t l)311 inline packer<Stream>& packer<Stream>::pack_raw_body(const char* b, size_t l)
312 { _pack_raw_body(m_stream, b, l); return *this; }
313
314
315 } // namespace msgpack
316
317 #endif /* msgpack/pack.hpp */
318
319