SDL Window Engine  20200905
swe_serialize.h
1 /***************************************************************************
2  * Copyright (C) 2017 by SWE team <sdl.window.engine@gmail.com> *
3  * *
4  * Part of the SWE: SDL Window Engine: *
5  * https://github.com/AndreyBarmaley/sdl-window-engine *
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 
23 #ifndef _SWE_SERIALIZE_
24 #define _SWE_SERIALIZE_
25 
26 #include <map>
27 #include <list>
28 #include <string>
29 #include <vector>
30 #include <unordered_map>
31 
32 #include "swe_types.h"
33 #include "swe_binarybuf.h"
34 
36 namespace SWE
37 {
38 
39  struct Point;
40  struct Rect;
41  struct Size;
42 
43  /* StreamBase */
44  class StreamBase : protected BitFlags
45  {
46  protected:
47  void setconstbuf(bool);
48  void setfail(bool) const;
49 
50  public:
51  StreamBase();
52  virtual ~StreamBase() {}
53 
54  void setbigendian(bool);
55 
56  bool isconstbuf(void) const;
57  bool fail(void) const;
58  bool bigendian(void) const;
59 
60  virtual int getBE16(void) const = 0;
61  virtual int getLE16(void) const = 0;
62  virtual int getBE32(void) const = 0;
63  virtual int getLE32(void) const = 0;
64  virtual s64 getBE64(void) const = 0;
65  virtual s64 getLE64(void) const = 0;
66 
67  virtual void putBE64(u64) = 0;
68  virtual void putLE64(u64) = 0;
69  virtual void putBE32(u32) = 0;
70  virtual void putLE32(u32) = 0;
71  virtual void putBE16(u16) = 0;
72  virtual void putLE16(u16) = 0;
73 
74  virtual BinaryBuf get(size_t = 0 /* all data */) const = 0;
75  virtual void put(const char*, size_t) = 0;
76 
77  bool wait(const std::string &);
78 
79  virtual int get8(void) const = 0;
80  int get16(void) const;
81  int get32(void) const;
82  s64 get64(void) const;
83 
84  virtual void put8(char) = 0;
85  void put16(u16);
86  void put32(u32);
87  void put64(u64);
88 
89  virtual size_t tell(void) const { return 0; }
90  virtual bool skip(size_t) const { return false; }
91  virtual bool seek(int offset, int whence = RW_SEEK_SET) const { return false; }
92 
93  const StreamBase & operator>> (bool &) const;
94  const StreamBase & operator>> (char &) const;
95  const StreamBase & operator>> (u8 &) const;
96  const StreamBase & operator>> (s8 &) const;
97  const StreamBase & operator>> (u16 &) const;
98  const StreamBase & operator>> (s16 &) const;
99  const StreamBase & operator>> (u32 &) const;
100  const StreamBase & operator>> (s32 &) const;
101  const StreamBase & operator>> (u64 &) const;
102  const StreamBase & operator>> (s64 &) const;
103  const StreamBase & operator>> (float &) const;
104  const StreamBase & operator>> (std::string &) const;
105 
106  const StreamBase & operator>> (Rect &) const;
107  const StreamBase & operator>> (Point &) const;
108  const StreamBase & operator>> (Size &) const;
109  const StreamBase & operator>> (BinaryBuf &) const;
110 
111  StreamBase & operator<< (const bool &);
112  StreamBase & operator<< (const char &);
113  StreamBase & operator<< (const u8 &);
114  StreamBase & operator<< (const s8 &);
115  StreamBase & operator<< (const u16 &);
116  StreamBase & operator<< (const s16 &);
117  StreamBase & operator<< (const u32 &);
118  StreamBase & operator<< (const s32 &);
119  StreamBase & operator<< (const u64 &);
120  StreamBase & operator<< (const s64 &);
121  StreamBase & operator<< (const float &);
122  StreamBase & operator<< (const std::string &);
123 
124  StreamBase & operator<< (const Rect &);
125  StreamBase & operator<< (const Point &);
126  StreamBase & operator<< (const Size &);
127  StreamBase & operator<< (const BinaryBuf &);
128 
129  template<class Type1, class Type2>
130  const StreamBase & operator>> (std::pair<Type1, Type2> & p) const
131  {
132  return *this >> p.first >> p.second;
133  }
134 
135  template<class Type>
136  const StreamBase & operator>> (std::vector<Type> & v) const
137  {
138  size_t size = get32();
139  v.clear(); v.reserve(size);
140  for(size_t it = 0; it < size; ++it)
141  {
142  Type t; *this >> t; v.emplace_back(t);
143  }
144  return *this;
145  }
146 
147  template<class Type>
148  const StreamBase & operator>> (std::list<Type> & v) const
149  {
150  size_t size = get32(); v.clear();
151  for(size_t it = 0; it < size; ++it)
152  {
153  Type t; *this >> t; v.emplace_back(t);
154  }
155  return *this;
156  }
157 
158  template<class Type1, class Type2>
159  const StreamBase & operator>> (std::map<Type1, Type2> & v) const
160  {
161  size_t size = get32(); v.clear();
162  for(size_t ii = 0; ii < size; ++ii)
163  {
164  Type1 t; *this >> t; *this >> v[t];
165  }
166  return *this;
167  }
168 
169  template<class Type1, class Type2, class... Args>
170  const StreamBase & operator>> (std::unordered_map<Type1, Type2, Args...> & v) const
171  {
172  size_t size = get32(); v.clear();
173  for(size_t ii = 0; ii < size; ++ii)
174  {
175  Type1 t; *this >> t; *this >> v[t];
176  }
177  return *this;
178  }
179 
180  template<class Type1, class Type2>
181  StreamBase & operator<< (const std::pair<Type1, Type2> & p)
182  {
183  return *this << p.first << p.second;
184  }
185 
186  template<typename InputIterator>
187  StreamBase & push(InputIterator first, InputIterator last)
188  {
189  for(auto it = first; it != last; ++it)
190  *this << *it;
191 
192  return *this;
193  }
194 
195  template<class Type>
196  StreamBase & operator<< (const std::vector<Type> & v)
197  {
198  put32(v.size());
199  return push(std::begin(v), std::end(v));
200  }
201 
202  template<class Type>
203  StreamBase & operator<< (const std::list<Type> & v)
204  {
205  put32(v.size());
206  return push(std::begin(v), std::end(v));
207  }
208 
209  template<class Type1, class Type2>
210  StreamBase & operator<< (const std::map<Type1, Type2> & v)
211  {
212  put32(v.size());
213  return push(std::begin(v), std::end(v));
214  }
215 
216  template<class Type1, class Type2, class... Args>
217  StreamBase & operator<< (const std::unordered_map<Type1, Type2, Args...> & v)
218  {
219  put32(v.size());
220  return push(std::begin(v), std::end(v));
221  }
222  };
223 
224  /* StreamRWops */
226  {
227  StreamRWops(const StreamRWops &) {};
228  StreamRWops & operator=(const StreamRWops &){ return *this; };
229 
230  protected:
231  SDL_RWops* rw;
232 
233  public:
234  StreamRWops(SDL_RWops* val = nullptr);
235  StreamRWops(StreamRWops && srw) noexcept;
236  ~StreamRWops();
237 
238  StreamRWops & operator=(StreamRWops && srw) noexcept;
239 
240  void close(void);
241 
242  size_t size(void) const;
243  size_t last(void) const;
244  size_t tell(void) const;
245  bool skip(size_t) const;
246  bool seek(int offset, int whence) const;
247 
248  bool isValid(void) const { return rw; }
249 
250  int get8(void) const;
251  int getBE16(void) const;
252  int getLE16(void) const;
253  int getBE32(void) const;
254  int getLE32(void) const;
255  s64 getBE64(void) const;
256  s64 getLE64(void) const;
257  BinaryBuf get(size_t = 0 /* all data */) const;
258 
259  bool put8(char);
260  bool putBE16(u16);
261  bool putLE16(u16);
262  bool putBE32(u32);
263  bool putLE32(u32);
264  bool putBE64(u64);
265  bool putLE64(u64);
266  bool put(const char*, size_t);
267  };
268 
269 } // SWE
270 #endif
Definition: swe_types.h:216
пространство SWE.
Definition: swe_binarybuf.cpp:30
класс прямоугольника
Definition: swe_rect.h:144
класс точки с двумя координатами
Definition: swe_rect.h:72
Definition: swe_serialize.h:225
класс бинарного массива
Definition: swe_binarybuf.h:35
класс двухмерной размерности
Definition: swe_rect.h:36
Definition: swe_serialize.h:44