8 #ifndef __WVSERIALIZE_H
9 #define __WVSERIALIZE_H
12 #include "wvstringlist.h"
16 # include <inttypes.h>
22 #include <netinet/in.h>
25 typedef __int8 int8_t;
26 typedef unsigned __int8 uint8_t;
27 typedef __int16 int16_t;
28 typedef unsigned __int16 uint16_t;
29 typedef __int32 int32_t;
30 typedef unsigned __int32 uint32_t;
31 typedef __int64 int64_t;
32 typedef unsigned __int64 uint64_t;
44 inline void wv_serialize(
WvBuf &buf,
const T &t)
46 _wv_serialize(buf, t);
54 inline int32_t _wv_htonl(int32_t i)
58 inline int16_t _wv_htons(int16_t i)
66 inline uint64_t ntohll(uint64_t n)
68 #ifdef WORDS_BIGENDIAN
71 return (((uint64_t)ntohl(n)) << 32) | ntohl(n >> 32);
75 inline uint64_t htonll(uint64_t n)
77 #ifdef WORDS_BIGENDIAN
80 return (((uint64_t)htonl(n)) << 32) | htonl(n >> 32);
94 void wv_serialize_scalar(
WvBuf &buf,
const T t)
98 int64_t i = htonll(t);
101 else if (
sizeof(T) == 4)
103 int32_t i = _wv_htonl(t);
106 else if (
sizeof(T) == 2)
108 int32_t i = _wv_htons(t);
111 else if (
sizeof(T) == 1)
117 inline void _wv_serialize(
WvBuf &buf,
long long i)
118 { wv_serialize_scalar(buf, i); }
119 inline void _wv_serialize(
WvBuf &buf,
unsigned long long i)
120 { wv_serialize_scalar(buf, i); }
121 inline void _wv_serialize(
WvBuf &buf,
long i)
122 { wv_serialize_scalar(buf, i); }
123 inline void _wv_serialize(
WvBuf &buf,
unsigned long i)
124 { wv_serialize_scalar(buf, i); }
125 inline void _wv_serialize(
WvBuf &buf,
int i)
126 { wv_serialize_scalar(buf, i); }
127 inline void _wv_serialize(
WvBuf &buf,
unsigned int i)
128 { wv_serialize_scalar(buf, i); }
129 inline void _wv_serialize(
WvBuf &buf,
short i)
130 { wv_serialize_scalar(buf, i); }
131 inline void _wv_serialize(
WvBuf &buf,
unsigned short i)
132 { wv_serialize_scalar(buf, i); }
133 inline void _wv_serialize(
WvBuf &buf,
bool i)
134 { wv_serialize_scalar(buf, i); }
137 inline void _wv_serialize(
WvBuf &buf,
char i)
138 { wv_serialize_scalar(buf, i); }
139 inline void _wv_serialize(
WvBuf &buf,
signed char i)
140 { wv_serialize_scalar(buf, i); }
141 inline void _wv_serialize(
WvBuf &buf,
unsigned char i)
142 { wv_serialize_scalar(buf, i); }
159 inline void wv_serialize(
WvBuf &buf,
const char *t)
161 _wv_serialize(buf, t);
169 inline void _wv_serialize(
WvBuf &buf,
const WvBuf &inbuf)
171 wv_serialize(buf, inbuf.
used());
172 buf.put(
const_cast<WvBuf *
>(&inbuf)->peek(0, inbuf.
used()), inbuf.
used());
181 template <
typename T>
185 _wv_serialize(buf, (
size_t)list.
count());
189 for (i.rewind(); i.next(); )
190 _wv_serialize(buf, *i);
196 template <
typename T>
197 T _wv_deserialize(
WvBuf &buf);
207 template <
typename T>
211 static T go(
WvBuf &buf)
212 {
return _wv_deserialize<T>(buf); }
230 template <
typename T>
234 static T *go(
WvBuf &buf)
235 {
return new T(_wv_deserialize<T>(buf)); }
259 template <
typename T>
260 inline T wv_deserialize(
WvBuf &buf)
270 inline int32_t _wv_ntohl(int32_t i)
274 inline int16_t _wv_ntohs(int16_t i)
285 template <
typename T>
286 inline T wv_deserialize_scalar(
WvBuf &buf)
288 if (buf.
used() <
sizeof(T))
292 return (T) ntohll(*(int64_t *)buf.
get(8));
293 else if (
sizeof(T) == 4)
294 return (T) _wv_ntohl(*(int32_t *)buf.
get(4));
295 else if (
sizeof(T) == 2)
296 return (T) _wv_ntohs(*(int16_t *)buf.
get(2));
297 else if (
sizeof(T) == 1)
298 return (T) *(int8_t *)buf.
get(1);
303 template <
typename T>
304 inline T xwv_deserialize_scalar(
WvBuf &buf)
310 inline long long _wv_deserialize<long long>(
WvBuf &buf)
311 {
return wv_deserialize_scalar<long long>(buf); }
313 inline unsigned long long _wv_deserialize<unsigned long long>(
WvBuf &buf)
314 {
return wv_deserialize_scalar<unsigned long long>(buf); }
316 inline long _wv_deserialize<long>(
WvBuf &buf)
317 {
return wv_deserialize_scalar<long>(buf); }
319 inline unsigned long _wv_deserialize<unsigned long>(
WvBuf &buf)
320 {
return wv_deserialize_scalar<unsigned long>(buf); }
322 inline int _wv_deserialize<int>(
WvBuf &buf)
323 {
return wv_deserialize_scalar<int>(buf); }
325 inline unsigned int _wv_deserialize<unsigned int>(
WvBuf &buf)
326 {
return wv_deserialize_scalar<unsigned int>(buf); }
328 inline short _wv_deserialize<short>(
WvBuf &buf)
329 {
return wv_deserialize_scalar<short>(buf); }
331 inline unsigned short _wv_deserialize<unsigned short>(
WvBuf &buf)
332 {
return wv_deserialize_scalar<unsigned short>(buf); }
334 inline bool _wv_deserialize<bool>(
WvBuf &buf)
335 {
return wv_deserialize_scalar<bool>(buf); }
337 inline char _wv_deserialize<char>(
WvBuf &buf)
338 {
return wv_deserialize_scalar<char>(buf); }
340 inline signed char _wv_deserialize<signed char>(
WvBuf &buf)
341 {
return wv_deserialize_scalar<signed char>(buf); }
343 inline unsigned char _wv_deserialize<unsigned char>(
WvBuf &buf)
344 {
return wv_deserialize_scalar<unsigned char>(buf); }
363 size_t len = wv_deserialize<size_t>(buf);
365 outbuf->
merge(buf, len);
372 template <
typename T>
379 size_t nelems = wv_deserialize<size_t>(buf);
381 for (
size_t count = 0; count < nelems; count++)
383 T t = wv_deserialize<T>(buf);
384 list->
append(
new T(t),
true);
398 size_t nelems = wv_deserialize<size_t>(buf);
400 for (
size_t count = 0; count < nelems; count++)
402 WvString str = wv_deserialize<WvString>(buf);
void merge(Buffer &inbuf, size_t count)
Efficiently moves count bytes from the specified buffer into this one.
const T * get(size_t count)
Reads exactly the specified number of elements and returns a pointer to a storage location owned by t...
size_t used() const
Returns the number of elements in the buffer currently available for reading.
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
void putstr(WvStringParm str)
Copies a WvString into the buffer, excluding the null-terminator.
Deserialize a complex templated object.
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
bool isnull() const
returns true if this string is null
The in place raw memory buffer type.
size_t count() const
Returns the number of elements in the list.
The iterator type for linked lists.
A linked list container class.
void append(T *data, bool autofree, const char *id=NULL)
Appends the element to the end of the list.
This is a WvList of WvStrings, and is a really handy way to parse strings.
WvString is an implementation of a simple and efficient printable-string class.