TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PacketUtilities.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef PacketUtilities_h__
19 #define PacketUtilities_h__
20 
21 #include "ByteBuffer.h"
22 #include <G3D/Vector2.h>
23 #include <G3D/Vector3.h>
24 #include <sstream>
25 #include <array>
26 
27 inline ByteBuffer& operator<<(ByteBuffer& data, G3D::Vector2 const& v)
28 {
29  data << v.x << v.y;
30  return data;
31 }
32 
34 {
35  data >> v.x >> v.y;
36  return data;
37 }
38 
39 inline ByteBuffer& operator<<(ByteBuffer& data, G3D::Vector3 const& v)
40 {
41  data << v.x << v.y << v.z;
42  return data;
43 }
44 
46 {
47  data >> v.x >> v.y >> v.z;
48  return data;
49 }
50 
51 namespace WorldPackets
52 {
54  {
55  public:
56  PacketArrayMaxCapacityException(std::size_t requestedSize, std::size_t sizeLimit)
57  {
58  std::ostringstream builder;
59  builder << "Attempted to read more array elements from packet " << requestedSize << " than allowed " << sizeLimit;
60  message().assign(builder.str());
61  }
62  };
63 
67  template<typename T, std::size_t N = 1000 /*select a sane default limit*/>
68  class Array
69  {
70  typedef std::vector<T> storage_type;
71 
72  typedef typename storage_type::value_type value_type;
73  typedef typename storage_type::size_type size_type;
74  typedef typename storage_type::reference reference;
75  typedef typename storage_type::const_reference const_reference;
76  typedef typename storage_type::iterator iterator;
77  typedef typename storage_type::const_iterator const_iterator;
78 
79  public:
80  Array() : _limit(N) { }
81  Array(size_type limit) : _limit(limit) { }
82 
83  iterator begin() { return _storage.begin(); }
84  const_iterator begin() const { return _storage.begin(); }
85 
86  iterator end() { return _storage.end(); }
87  const_iterator end() const { return _storage.end(); }
88 
89  size_type size() const { return _storage.size(); }
90  bool empty() const { return _storage.empty(); }
91 
92  reference operator[](size_type i) { return _storage[i]; }
93  const_reference operator[](size_type i) const { return _storage[i]; }
94 
95  void resize(size_type newSize)
96  {
97  if (newSize > _limit)
99 
100  _storage.resize(newSize);
101  }
102 
103  void reserve(size_type newSize)
104  {
105  if (newSize > _limit)
106  throw PacketArrayMaxCapacityException(newSize, _limit);
107 
108  _storage.reserve(newSize);
109  }
110 
111  void push_back(value_type const& value)
112  {
113  if (_storage.size() >= _limit)
115 
116  _storage.push_back(value);
117  }
118 
119  void push_back(value_type&& value)
120  {
121  if (_storage.size() >= _limit)
123 
124  _storage.push_back(std::forward<value_type>(value));
125  }
126 
127  private:
128  storage_type _storage;
129  size_type _limit;
130  };
131 
132  template <typename T>
134  {
135  public:
136  CompactArray() : _mask(0) { }
137 
139  : _mask(right._mask), _contents(right._contents) { }
140 
142  : _mask(right._mask), _contents(std::move(right._contents))
143  {
144  right._mask = 0;
145  }
146 
148  {
149  _mask = right._mask;
150  _contents = right._contents;
151  return *this;
152  }
153 
155  {
156  _mask = right._mask;
157  right._mask = 0;
158  _contents = std::move(right._contents);
159  return *this;
160  }
161 
162  uint32 GetMask() const { return _mask; }
163  T const& operator[](size_t index) const { return _contents.at(index); }
164  size_t GetSize() const { return _contents.size(); }
165 
166  void Insert(size_t index, T const& value)
167  {
168  ASSERT(index < 0x20);
169 
170  _mask |= 1 << index;
171  if (_contents.size() <= index)
172  _contents.resize(index + 1);
173  _contents[index] = value;
174  }
175 
176  void Clear()
177  {
178  _mask = 0;
179  _contents.clear();
180  }
181 
182  bool operator==(CompactArray const& r) const
183  {
184  if (_mask != r._mask)
185  return false;
186 
187  return _contents == r._contents;
188  }
189 
190  bool operator!=(CompactArray const& r) const { return !(*this == r); }
191 
192  private:
194  std::vector<T> _contents;
195  };
196 
197  template <typename T>
198  ByteBuffer& operator<<(ByteBuffer& data, CompactArray<T> const& v)
199  {
200  uint32 mask = v.GetMask();
201  data << uint32(mask);
202  for (size_t i = 0; i < v.GetSize(); ++i)
203  {
204  if (mask & (1 << i))
205  data << v[i];
206  }
207 
208  return data;
209  }
210 
211  template <typename T>
213  {
214  uint32 mask;
215  data >> mask;
216 
217  for (size_t index = 0; mask != 0; mask >>= 1, ++index)
218  {
219  if ((mask & 1) != 0)
220  {
221  T value;
222  data >> value;
223  v.Insert(index, value);
224  }
225  }
226 
227  return data;
228  }
229 }
230 
231 #endif // PacketUtilities_h__
storage_type::reference reference
Definition: PacketUtilities.h:74
const_iterator begin() const
Definition: PacketUtilities.h:84
Definition: Vector2.h:40
uint32 _mask
Definition: PacketUtilities.h:193
bool empty() const
Definition: PacketUtilities.h:90
float x
Definition: Vector3.h:62
reference operator[](size_type i)
Definition: PacketUtilities.h:92
size_t GetSize() const
Definition: PacketUtilities.h:164
Definition: ByteBuffer.h:70
iterator begin()
Definition: PacketUtilities.h:83
CompactArray & operator=(CompactArray const &right)
Definition: PacketUtilities.h:147
void push_back(value_type const &value)
Definition: PacketUtilities.h:111
size_type _limit
Definition: PacketUtilities.h:129
std::string & message()
Definition: ByteBuffer.h:48
STL namespace.
storage_type::const_reference const_reference
Definition: PacketUtilities.h:75
std::vector< T > storage_type
Definition: PacketUtilities.h:70
storage_type::value_type value_type
Definition: PacketUtilities.h:72
bool operator!=(CompactArray const &r) const
Definition: PacketUtilities.h:190
float y
Definition: Vector3.h:62
Definition: Vector3.h:58
Definition: PacketUtilities.h:133
void push_back(value_type &&value)
Definition: PacketUtilities.h:119
storage_type::const_iterator const_iterator
Definition: PacketUtilities.h:77
float y
Definition: Vector2.h:50
ByteBuffer & operator<<(ByteBuffer &data, G3D::Vector2 const &v)
Definition: PacketUtilities.h:27
const_iterator end() const
Definition: PacketUtilities.h:87
PacketArrayMaxCapacityException(std::size_t requestedSize, std::size_t sizeLimit)
Definition: PacketUtilities.h:56
uint32 GetMask() const
Definition: PacketUtilities.h:162
CompactArray(CompactArray &&right)
Definition: PacketUtilities.h:141
CompactArray(CompactArray const &right)
Definition: PacketUtilities.h:138
T const & operator[](size_t index) const
Definition: PacketUtilities.h:163
uint32_t uint32
Definition: Define.h:150
void Insert(size_t index, T const &value)
Definition: PacketUtilities.h:166
float x
Definition: Vector2.h:49
void resize(size_type newSize)
Definition: PacketUtilities.h:95
storage_type::size_type size_type
Definition: PacketUtilities.h:73
Definition: PacketUtilities.h:53
void reserve(size_type newSize)
Definition: PacketUtilities.h:103
Definition: BattlegroundMgr.h:57
std::vector< T > _contents
Definition: PacketUtilities.h:194
ByteBuffer & operator>>(ByteBuffer &data, CompactArray< T > &v)
Definition: PacketUtilities.h:212
const_reference operator[](size_type i) const
Definition: PacketUtilities.h:93
bool operator==(CompactArray const &r) const
Definition: PacketUtilities.h:182
float z
Definition: Vector3.h:62
CompactArray()
Definition: PacketUtilities.h:136
Array(size_type limit)
Definition: PacketUtilities.h:81
Definition: ByteBuffer.h:40
#define ASSERT
Definition: Errors.h:55
const FieldDescriptor value
Definition: descriptor.h:1522
uint32_t uint32
Definition: g3dmath.h:168
Array()
Definition: PacketUtilities.h:80
storage_type _storage
Definition: PacketUtilities.h:128
ByteBuffer & operator>>(ByteBuffer &data, G3D::Vector2 &v)
Definition: PacketUtilities.h:33
storage_type::iterator iterator
Definition: PacketUtilities.h:76
iterator end()
Definition: PacketUtilities.h:86
void Clear()
Definition: PacketUtilities.h:176
size_type size() const
Definition: PacketUtilities.h:89
Definition: PacketUtilities.h:68