TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
UpdateMask.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef __UPDATEMASK_H
20 #define __UPDATEMASK_H
21 
22 #include "UpdateFields.h"
23 #include "Errors.h"
24 #include "ByteBuffer.h"
25 
27 {
28  public:
31 
33  {
35  };
36 
38 
39  UpdateMask(UpdateMask const& right) : _bits(NULL)
40  {
41  SetCount(right.GetCount());
42  memcpy(_bits, right._bits, sizeof(uint8) * _blockCount * 32);
43  }
44 
45  ~UpdateMask() { delete[] _bits; }
46 
47  void SetBit(uint32 index) { _bits[index] = 1; }
48  void UnsetBit(uint32 index) { _bits[index] = 0; }
49  bool GetBit(uint32 index) const { return _bits[index] != 0; }
50 
52  {
53  for (uint32 i = 0; i < GetBlockCount(); ++i)
54  {
55  ClientUpdateMaskType maskPart = 0;
56  for (uint32 j = 0; j < CLIENT_UPDATE_MASK_BITS; ++j)
57  if (_bits[CLIENT_UPDATE_MASK_BITS * i + j])
58  maskPart |= 1 << j;
59 
60  *data << maskPart;
61  }
62  }
63 
64  uint32 GetBlockCount() const { return _blockCount; }
65  uint32 GetCount() const { return _fieldCount; }
66 
67  void SetCount(uint32 valuesCount)
68  {
69  delete[] _bits;
70 
71  _fieldCount = valuesCount;
73 
74  if (!valuesCount)
75  {
76  _bits = nullptr;
77  return;
78  }
79 
81  memset(_bits, 0, sizeof(uint8) * _blockCount * CLIENT_UPDATE_MASK_BITS);
82  }
83 
84  void AddBlock()
85  {
86  uint8* curr = _bits;
88  ++_blockCount;
89 
92  if (curr)
93  {
94  memcpy(_bits, curr, sizeof(uint8) * (_blockCount - 1) * CLIENT_UPDATE_MASK_BITS);
95  delete[] curr;
96  }
97  }
98 
99  void Clear()
100  {
101  if (_bits)
102  memset(_bits, 0, sizeof(uint8) * _blockCount * CLIENT_UPDATE_MASK_BITS);
103  }
104 
106  {
107  if (this == &right)
108  return *this;
109 
110  SetCount(right.GetCount());
111  memcpy(_bits, right._bits, sizeof(uint8) * _blockCount * CLIENT_UPDATE_MASK_BITS);
112  return *this;
113  }
114 
116  {
117  ASSERT(right.GetCount() <= GetCount());
118  for (uint32 i = 0; i < _fieldCount; ++i)
119  _bits[i] &= right._bits[i];
120 
121  return *this;
122  }
123 
125  {
126  ASSERT(right.GetCount() <= GetCount());
127  for (uint32 i = 0; i < _fieldCount; ++i)
128  _bits[i] |= right._bits[i];
129 
130  return *this;
131  }
132 
134  {
135  UpdateMask ret(*this);
136  ret |= right;
137  return ret;
138  }
139 
140  private:
144 };
145 
146 #endif
147 
UpdateMask operator|(UpdateMask const &right)
Definition: UpdateMask.h:133
Definition: UpdateMask.h:26
void Clear()
Definition: UpdateMask.h:99
Definition: ByteBuffer.h:70
uint32 _blockCount
Definition: UpdateMask.h:142
UpdateMask & operator&=(UpdateMask const &right)
Definition: UpdateMask.h:115
UpdateMask()
Definition: UpdateMask.h:37
uint32 GetBlockCount() const
Definition: UpdateMask.h:64
arena_t NULL
Definition: jemalloc_internal.h:624
uint32 _fieldCount
Definition: UpdateMask.h:141
UpdateMaskCount
Definition: UpdateMask.h:32
void AppendToPacket(ByteBuffer *data)
Definition: UpdateMask.h:51
UpdateMask & operator=(UpdateMask const &right)
Definition: UpdateMask.h:105
~UpdateMask()
Definition: UpdateMask.h:45
void AddBlock()
Definition: UpdateMask.h:84
uint32_t uint32
Definition: Define.h:150
Definition: UpdateMask.h:34
uint32 GetCount() const
Definition: UpdateMask.h:65
uint8 * _bits
Definition: UpdateMask.h:143
uint32 ClientUpdateMaskType
Type representing how client reads update mask.
Definition: UpdateMask.h:30
void SetCount(uint32 valuesCount)
Definition: UpdateMask.h:67
uint8_t uint8
Definition: Define.h:152
#define ASSERT
Definition: Errors.h:55
void UnsetBit(uint32 index)
Definition: UpdateMask.h:48
bool GetBit(uint32 index) const
Definition: UpdateMask.h:49
UpdateMask(UpdateMask const &right)
Definition: UpdateMask.h:39
void SetBit(uint32 index)
Definition: UpdateMask.h:47
UpdateMask & operator|=(UpdateMask const &right)
Definition: UpdateMask.h:124