zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Serialization.h
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * zenilib is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
36 #ifndef ZENI_SERIALIZATION_H
37 #define ZENI_SERIALIZATION_H
38 
39 #include <Zeni/String.h>
40 
41 #include <SDL/SDL_net.h>
42 
43 #include <iostream>
44 #include <list>
45 #include <set>
46 #include <vector>
47 
48 /*** IPaddress functions ***/
49 
50 ZENI_DLL bool operator==(const IPaddress &lhs, const IPaddress &rhs);
51 ZENI_DLL bool operator!=(const IPaddress &lhs, const IPaddress &rhs);
52 ZENI_DLL bool operator<(const IPaddress &lhs, const IPaddress &rhs);
53 ZENI_DLL bool operator<=(const IPaddress &lhs, const IPaddress &rhs);
54 ZENI_DLL bool operator>(const IPaddress &lhs, const IPaddress &rhs);
55 ZENI_DLL bool operator>=(const IPaddress &lhs, const IPaddress &rhs);
56 
57 namespace Zeni {
58  /*** Simple Helper Functions ***/
59 
60  ZENI_DLL String ustoa(const unsigned short &number);
61  ZENI_DLL String stoa(const short &number);
62  ZENI_DLL String uitoa(const unsigned int &number);
63  ZENI_DLL String itoa(const int &number);
64  ZENI_DLL String ultoa(const unsigned long &number);
65  ZENI_DLL String ltoa(const long &number);
66 #ifdef _WINDOWS
67  ZENI_DLL String ulltoa(const unsigned long long &number);
68  ZENI_DLL String lltoa(const long long &number);
69 #else
70  ZENI_DLL String ulltoa(const unsigned long &number);
71  ZENI_DLL String lltoa(const long &number);
72 #endif
73  ZENI_DLL String ftoa(const float &number);
74  ZENI_DLL String ftoa(const float &number, const unsigned int &precision);
75  ZENI_DLL String dtoa(const double &number);
76  ZENI_DLL String dtoa(const double &number, const unsigned int &precision);
77  ZENI_DLL String ldtoa(const long double &number);
78  ZENI_DLL String iptoa(const IPaddress &address);
79 
80  ZENI_DLL size_t grab_bytes(std::istream &is, char * const &store, const size_t &num_bytes);
81  ZENI_DLL size_t grab_bytes(std::istream &is, String &store, const size_t &num_bytes);
82 
83  class ZENI_DLL Serializable {
84  public:
85  Serializable() : m_size(0) {}
86  virtual ~Serializable() {}
87 
88  virtual Uint16 size() const {return sizeof(m_size) + m_size;}
89 
90  virtual std::ostream & serialize(std::ostream &os) const = 0;
91  virtual std::istream & unserialize(std::istream &is) = 0;
92 
93  protected:
95  };
96 
97  /*** Stand-Alone serialization/unserialization functions ***/
98 
99  inline std::ostream & serialize(std::ostream &os, const Serializable &value) {return value.serialize(os);}
100 
101  ZENI_DLL std::ostream & serialize(std::ostream &os, const Sint32 &value);
102  ZENI_DLL std::ostream & serialize(std::ostream &os, const Uint32 &value);
103  ZENI_DLL std::ostream & serialize(std::ostream &os, const Sint16 &value);
104  ZENI_DLL std::ostream & serialize(std::ostream &os, const Uint16 &value);
105  ZENI_DLL std::ostream & serialize(std::ostream &os, const Sint8 &value);
106  ZENI_DLL std::ostream & serialize(std::ostream &os, const char &value);
107  ZENI_DLL std::ostream & serialize(std::ostream &os, const unsigned char &value);
108  ZENI_DLL std::ostream & serialize(std::ostream &os, const float &value);
109  ZENI_DLL std::ostream & serialize(std::ostream &os, const double &value);
110  //ZENI_DLL std::ostream & serialize(std::ostream &os, const bool &value);
111  ZENI_DLL std::ostream & serialize(std::ostream &os, const IPaddress &address);
112  ZENI_DLL std::ostream & serialize(std::ostream &os, const String &string);
113 
114  inline std::istream & unserialize(std::istream &is, Serializable &value) {return value.unserialize(is);}
115 
116  ZENI_DLL std::istream & unserialize(std::istream &is, Sint32 &value);
117  ZENI_DLL std::istream & unserialize(std::istream &is, Uint32 &value);
118  ZENI_DLL std::istream & unserialize(std::istream &is, Sint16 &value);
119  ZENI_DLL std::istream & unserialize(std::istream &is, Uint16 &value);
120  ZENI_DLL std::istream & unserialize(std::istream &is, Sint8 &value);
121  ZENI_DLL std::istream & unserialize(std::istream &is, char &value);
122  ZENI_DLL std::istream & unserialize(std::istream &is, unsigned char &value);
123  ZENI_DLL std::istream & unserialize(std::istream &is, float &value);
124  ZENI_DLL std::istream & unserialize(std::istream &is, double &value);
125  //ZENI_DLL std::istream & unserialize(std::istream &is, bool &value);
126  ZENI_DLL std::istream & unserialize(std::istream &is, IPaddress &address);
127  ZENI_DLL std::istream & unserialize(std::istream &is, String &string);
128 
129  template <typename TYPE>
130  std::ostream & serialize(std::ostream &os, const std::list<TYPE> &list_) {
131  Zeni::serialize(os, static_cast<Uint16>(list_.size()));
132  for(typename std::list<TYPE>::const_iterator it = list_.begin(); it != list_.end(); ++it)
133  if(!serialize(os, *it))
134  break;
135  return os;
136  }
137 
138  template <typename TYPE>
139  std::istream & unserialize(std::istream &is, std::list<TYPE> &list_) {
140  list_.clear();
141 
142  Uint16 size;
143  if(Zeni::unserialize(is, size)) {
144  TYPE el;
145  for(Uint16 i = 0u; i != size; ++i) {
146  if(!unserialize(is, el))
147  break;
148  list_.push_back(el);
149  }
150  }
151  return is;
152  }
153 
154  template <typename TYPE>
155  std::ostream & serialize(std::ostream &os, const std::set<TYPE> &list_) {
156  Zeni::serialize(os, static_cast<Uint16>(list_.size()));
157  for(typename std::set<TYPE>::const_iterator it = list_.begin(); it != list_.end(); ++it)
158  if(!serialize(os, *it))
159  break;
160  return os;
161  }
162 
163  template <typename TYPE>
164  std::istream & unserialize(std::istream &is, std::set<TYPE> &list_) {
165  list_.clear();
166 
167  Uint16 size;
168  if(Zeni::unserialize(is, size)) {
169  TYPE el;
170  for(Uint16 i = 0u; i != size; ++i) {
171  if(!unserialize(is, el))
172  break;
173  list_.insert(el);
174  }
175  }
176  return is;
177  }
178 
179  template <typename TYPE>
180  std::ostream & serialize(std::ostream &os, const std::vector<TYPE> &list_) {
181  if(Zeni::serialize(os, static_cast<Uint16>(list_.size())))
182  for(typename std::vector<TYPE>::const_iterator it = list_.begin(); it != list_.end(); ++it)
183  if(!serialize(os, *it))
184  break;
185  return os;
186  }
187 
188  template <typename TYPE>
189  std::istream & unserialize(std::istream &is, std::vector<TYPE> &list_) {
190  list_.clear();
191 
192  Uint16 size;
193  if(Zeni::unserialize(is, size)) {
194  TYPE el;
195  list_.reserve(size);
196  for(Uint16 i = 0u; i != size; ++i) {
197  if(!unserialize(is, el))
198  break;
199  list_.push_back(el);
200  }
201  }
202  return is;
203  }
204 
205 }
206 
207 #endif
bool operator>(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:279
int32_t Sint32
A signed 32-bit integer type.
Definition: SDL_stdinc.h:141
String stoa(const short &number)
String uitoa(const unsigned int &number)
String ustoa(const unsigned short &number)
bool operator<(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:273
virtual Uint16 size() const
Definition: Serialization.h:88
bool operator==(const SourceLocation &lhs, const SourceLocation &rhs)
String ltoa(const long &number)
String lltoa(const long &number)
String dtoa(const double &number)
std::istream & unserialize(std::istream &is, Color &value)
Definition: Color.cpp:72
std::ostream & serialize(std::ostream &os, const Color &value)
Definition: Color.cpp:68
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:145
virtual std::istream & unserialize(std::istream &is)=0
ALuint u
Definition: alMain.h:58
String ftoa(const float &number)
String itoa(const int &number)
GLuint GLuint64EXT address
Definition: glew.h:13266
size_t grab_bytes(std::istream &is, char *const &store, const size_t &num_bytes)
bool operator!=(const SourceLocation &lhs, const SourceLocation &rhs)
String iptoa(const IPaddress &address)
virtual ~Serializable()
Definition: Serialization.h:86
EGLSurface EGLint void ** value
Definition: eglext.h:301
String ldtoa(const long double &number)
bool operator>=(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:281
int16_t Sint16
A signed 16-bit integer type.
Definition: SDL_stdinc.h:133
A base class for objects that can be sent over the network.
Definition: Serialization.h:83
virtual std::ostream & serialize(std::ostream &os) const =0
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:137
int i
Definition: pngrutil.c:1377
GLenum GLint GLint * precision
Definition: glew.h:3391
bool operator<=(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:280
Definition: infutil.h:15
String ulltoa(const unsigned long &number)
String ultoa(const unsigned long &number)
int8_t Sint8
A signed 8-bit integer type.
Definition: SDL_stdinc.h:125
GLsizei size
Definition: gl2ext.h:1467