[7833] Implement levelup spells for non-hunter pets.
[getmangos.git] / src / shared / ByteBuffer.h
blob6458976776ce5d8b8982cd967062c3cb14562a99
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program 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 General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef _BYTEBUFFER_H
20 #define _BYTEBUFFER_H
22 #include "Common.h"
23 #include "Errors.h"
24 #include "Log.h"
25 #include "Utilities/ByteConverter.h"
27 class ByteBuffer
29 public:
30 const static size_t DEFAULT_SIZE = 0x1000;
32 // constructor
33 ByteBuffer(): _rpos(0), _wpos(0)
35 _storage.reserve(DEFAULT_SIZE);
37 // constructor
38 ByteBuffer(size_t res): _rpos(0), _wpos(0)
40 _storage.reserve(res);
42 // copy constructor
43 ByteBuffer(const ByteBuffer &buf): _rpos(buf._rpos), _wpos(buf._wpos), _storage(buf._storage) { }
45 void clear()
47 _storage.clear();
48 _rpos = _wpos = 0;
51 template <typename T> void append(T value)
53 EndianConvert(value);
54 append((uint8 *)&value, sizeof(value));
57 template <typename T> void put(size_t pos,T value)
59 EndianConvert(value);
60 put(pos,(uint8 *)&value,sizeof(value));
63 ByteBuffer &operator<<(uint8 value)
65 append<uint8>(value);
66 return *this;
68 ByteBuffer &operator<<(uint16 value)
70 append<uint16>(value);
71 return *this;
73 ByteBuffer &operator<<(uint32 value)
75 append<uint32>(value);
76 return *this;
78 ByteBuffer &operator<<(uint64 value)
80 append<uint64>(value);
81 return *this;
84 // signed as in 2e complement
85 ByteBuffer &operator<<(int8 value)
87 append<int8>(value);
88 return *this;
90 ByteBuffer &operator<<(int16 value)
92 append<int16>(value);
93 return *this;
95 ByteBuffer &operator<<(int32 value)
97 append<int32>(value);
98 return *this;
100 ByteBuffer &operator<<(int64 value)
102 append<int64>(value);
103 return *this;
106 // floating points
107 ByteBuffer &operator<<(float value)
109 append<float>(value);
110 return *this;
112 ByteBuffer &operator<<(double value)
114 append<double>(value);
115 return *this;
117 ByteBuffer &operator<<(const std::string &value)
119 append((uint8 const *)value.c_str(), value.length());
120 append((uint8)0);
121 return *this;
123 ByteBuffer &operator<<(const char *str)
125 append((uint8 const *)str, str ? strlen(str) : 0);
126 append((uint8)0);
127 return *this;
130 ByteBuffer &operator>>(bool &value)
132 value = read<char>() > 0 ? true : false;
133 return *this;
136 ByteBuffer &operator>>(uint8 &value)
138 value = read<uint8>();
139 return *this;
141 ByteBuffer &operator>>(uint16 &value)
143 value = read<uint16>();
144 return *this;
146 ByteBuffer &operator>>(uint32 &value)
148 value = read<uint32>();
149 return *this;
151 ByteBuffer &operator>>(uint64 &value)
153 value = read<uint64>();
154 return *this;
157 //signed as in 2e complement
158 ByteBuffer &operator>>(int8 &value)
160 value = read<int8>();
161 return *this;
163 ByteBuffer &operator>>(int16 &value)
165 value = read<int16>();
166 return *this;
168 ByteBuffer &operator>>(int32 &value)
170 value = read<int32>();
171 return *this;
173 ByteBuffer &operator>>(int64 &value)
175 value = read<int64>();
176 return *this;
179 ByteBuffer &operator>>(float &value)
181 value = read<float>();
182 return *this;
184 ByteBuffer &operator>>(double &value)
186 value = read<double>();
187 return *this;
189 ByteBuffer &operator>>(std::string& value)
191 value.clear();
192 while (rpos() < size()) // prevent crash at wrong string format in packet
194 char c=read<char>();
195 if (c==0)
196 break;
197 value+=c;
199 return *this;
202 uint8 operator[](size_t pos) const
204 return read<uint8>(pos);
207 size_t rpos() const { return _rpos; }
209 size_t rpos(size_t rpos_)
211 _rpos = rpos_;
212 return _rpos;
215 size_t wpos() const { return _wpos; }
217 size_t wpos(size_t wpos_)
219 _wpos = wpos_;
220 return _wpos;
223 template <typename T> T read()
225 T r=read<T>(_rpos);
226 _rpos += sizeof(T);
227 return r;
229 template <typename T> T read(size_t pos) const
231 ASSERT(pos + sizeof(T) <= size() || PrintPosError(false,pos,sizeof(T)));
232 T val = *((T const*)&_storage[pos]);
233 EndianConvert(val);
234 return val;
237 void read(uint8 *dest, size_t len)
239 ASSERT(_rpos + len <= size() || PrintPosError(false,_rpos,len));
240 memcpy(dest, &_storage[_rpos], len);
241 _rpos += len;
244 bool readPackGUID(uint64& guid)
246 if(rpos()+1 > size())
247 return false;
249 guid = 0;
251 uint8 guidmark=0;
252 (*this) >> guidmark;
254 for(int i=0;i<8;i++)
256 if(guidmark & (uint8(1) << i))
258 if(rpos()+1 > size())
259 return false;
261 uint8 bit;
262 (*this) >> bit;
263 guid |= (uint64(bit) << (i*8));
267 return true;
270 const uint8 *contents() const { return &_storage[0]; }
272 size_t size() const { return _storage.size(); }
273 bool empty() const { return _storage.empty(); }
275 void resize(size_t newsize)
277 _storage.resize(newsize);
278 _rpos = 0;
279 _wpos = size();
281 void reserve(size_t ressize)
283 if (ressize > size()) _storage.reserve(ressize);
286 void append(const std::string& str)
288 append((uint8 const*)str.c_str(),str.size() + 1);
290 void append(const char *src, size_t cnt)
292 return append((const uint8 *)src, cnt);
294 template<class T>
295 void append(const T *src, size_t cnt)
297 return append((const uint8 *)src, cnt*sizeof(T));
299 void append(const uint8 *src, size_t cnt)
301 if (!cnt) return;
303 ASSERT(size() < 10000000);
305 if (_storage.size() < _wpos + cnt)
306 _storage.resize(_wpos + cnt);
307 memcpy(&_storage[_wpos], src, cnt);
308 _wpos += cnt;
310 void append(const ByteBuffer& buffer)
312 if(buffer.size()) append(buffer.contents(),buffer.size());
315 void appendPackGUID(uint64 guid)
317 size_t mask_position = wpos();
318 *this << uint8(0);
319 for(uint8 i = 0; i < 8; i++)
321 if(guid & 0xFF)
323 _storage[mask_position] |= uint8(1<<i);
324 *this << uint8(guid & 0xFF);
327 guid >>= 8;
331 void put(size_t pos, const uint8 *src, size_t cnt)
333 ASSERT(pos + cnt <= size() || PrintPosError(true,pos,cnt));
334 memcpy(&_storage[pos], src, cnt);
336 void print_storage() const
338 if(!sLog.IsOutDebug()) // optimize disabled debug output
339 return;
341 sLog.outDebug("STORAGE_SIZE: %lu", (unsigned long)size() );
342 for(uint32 i = 0; i < size(); i++)
343 sLog.outDebugInLine("%u - ", read<uint8>(i) );
344 sLog.outDebug(" ");
347 void textlike() const
349 if(!sLog.IsOutDebug()) // optimize disabled debug output
350 return;
352 sLog.outDebug("STORAGE_SIZE: %lu", (unsigned long)size() );
353 for(uint32 i = 0; i < size(); i++)
354 sLog.outDebugInLine("%c", read<uint8>(i) );
355 sLog.outDebug(" ");
358 void hexlike() const
360 if(!sLog.IsOutDebug()) // optimize disabled debug output
361 return;
363 uint32 j = 1, k = 1;
364 sLog.outDebug("STORAGE_SIZE: %lu", (unsigned long)size() );
366 if(sLog.IsIncludeTime())
367 sLog.outDebugInLine(" ");
369 for(uint32 i = 0; i < size(); i++)
371 if ((i == (j*8)) && ((i != (k*16))))
373 if (read<uint8>(i) < 0x0F)
375 sLog.outDebugInLine("| 0%X ", read<uint8>(i) );
377 else
379 sLog.outDebugInLine("| %X ", read<uint8>(i) );
381 ++j;
383 else if (i == (k*16))
385 if (read<uint8>(i) < 0x0F)
387 sLog.outDebugInLine("\n");
388 if(sLog.IsIncludeTime())
389 sLog.outDebugInLine(" ");
391 sLog.outDebugInLine("0%X ", read<uint8>(i) );
393 else
395 sLog.outDebugInLine("\n");
396 if(sLog.IsIncludeTime())
397 sLog.outDebugInLine(" ");
399 sLog.outDebugInLine("%X ", read<uint8>(i) );
402 ++k;
403 ++j;
405 else
407 if (read<uint8>(i) < 0x0F)
409 sLog.outDebugInLine("0%X ", read<uint8>(i) );
411 else
413 sLog.outDebugInLine("%X ", read<uint8>(i) );
417 sLog.outDebugInLine("\n");
420 protected:
421 bool PrintPosError(bool add, size_t pos, size_t esize) const
423 sLog.outError("ERROR: Attempt %s in ByteBuffer (pos: %lu size: %lu) value with size: %lu",(add ? "put" : "get"),(unsigned long)pos, (unsigned long)size(), (unsigned long)esize);
425 // assert must fail after function call
426 return false;
429 size_t _rpos, _wpos;
430 std::vector<uint8> _storage;
433 template <typename T> ByteBuffer &operator<<(ByteBuffer &b, std::vector<T> v)
435 b << (uint32)v.size();
436 for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++)
438 b << *i;
440 return b;
443 template <typename T> ByteBuffer &operator>>(ByteBuffer &b, std::vector<T> &v)
445 uint32 vsize;
446 b >> vsize;
447 v.clear();
448 while(vsize--)
450 T t;
451 b >> t;
452 v.push_back(t);
454 return b;
457 template <typename T> ByteBuffer &operator<<(ByteBuffer &b, std::list<T> v)
459 b << (uint32)v.size();
460 for (typename std::list<T>::iterator i = v.begin(); i != v.end(); i++)
462 b << *i;
464 return b;
467 template <typename T> ByteBuffer &operator>>(ByteBuffer &b, std::list<T> &v)
469 uint32 vsize;
470 b >> vsize;
471 v.clear();
472 while(vsize--)
474 T t;
475 b >> t;
476 v.push_back(t);
478 return b;
481 template <typename K, typename V> ByteBuffer &operator<<(ByteBuffer &b, std::map<K, V> &m)
483 b << (uint32)m.size();
484 for (typename std::map<K, V>::iterator i = m.begin(); i != m.end(); i++)
486 b << i->first << i->second;
488 return b;
491 template <typename K, typename V> ByteBuffer &operator>>(ByteBuffer &b, std::map<K, V> &m)
493 uint32 msize;
494 b >> msize;
495 m.clear();
496 while(msize--)
498 K k;
499 V v;
500 b >> k >> v;
501 m.insert(make_pair(k, v));
503 return b;
505 #endif