1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
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. |
---|
8 | * |
---|
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. |
---|
13 | * |
---|
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 |
---|
17 | */ |
---|
18 | |
---|
19 | #ifndef __UPDATEMASK_H |
---|
20 | #define __UPDATEMASK_H |
---|
21 | |
---|
22 | #include "UpdateFields.h" |
---|
23 | #include "Errors.h" |
---|
24 | |
---|
25 | class UpdateMask |
---|
26 | { |
---|
27 | public: |
---|
28 | UpdateMask( ) : mCount( 0 ), mBlocks( 0 ), mUpdateMask( 0 ) { } |
---|
29 | UpdateMask( const UpdateMask& mask ) : mUpdateMask( 0 ) { *this = mask; } |
---|
30 | |
---|
31 | ~UpdateMask( ) |
---|
32 | { |
---|
33 | if(mUpdateMask) |
---|
34 | delete [] mUpdateMask; |
---|
35 | } |
---|
36 | |
---|
37 | inline void SetBit (uint32 index) |
---|
38 | { |
---|
39 | ( (uint8 *)mUpdateMask )[ index >> 3 ] |= 1 << ( index & 0x7 ); |
---|
40 | } |
---|
41 | |
---|
42 | inline void UnsetBit (uint32 index) |
---|
43 | { |
---|
44 | ( (uint8 *)mUpdateMask )[ index >> 3 ] &= (0xff ^ (1 << ( index & 0x7 ) ) ); |
---|
45 | } |
---|
46 | |
---|
47 | inline bool GetBit (uint32 index) |
---|
48 | { |
---|
49 | return ( ( (uint8 *)mUpdateMask)[ index >> 3 ] & ( 1 << ( index & 0x7 ) )) != 0; |
---|
50 | } |
---|
51 | |
---|
52 | inline uint32 GetBlockCount() { return mBlocks; } |
---|
53 | inline uint32 GetLength() { return mBlocks << 2; } |
---|
54 | inline uint32 GetCount() { return mCount; } |
---|
55 | inline uint8* GetMask() { return (uint8*)mUpdateMask; } |
---|
56 | |
---|
57 | inline void SetCount (uint32 valuesCount) |
---|
58 | { |
---|
59 | if(mUpdateMask) |
---|
60 | delete [] mUpdateMask; |
---|
61 | |
---|
62 | mCount = valuesCount; |
---|
63 | mBlocks = (valuesCount + 31) / 32; |
---|
64 | |
---|
65 | mUpdateMask = new uint32[mBlocks]; |
---|
66 | memset(mUpdateMask, 0, mBlocks << 2); |
---|
67 | } |
---|
68 | |
---|
69 | inline void Clear() |
---|
70 | { |
---|
71 | if (mUpdateMask) |
---|
72 | memset(mUpdateMask, 0, mBlocks << 2); |
---|
73 | } |
---|
74 | |
---|
75 | inline UpdateMask& operator = ( const UpdateMask& mask ) |
---|
76 | { |
---|
77 | SetCount(mask.mCount); |
---|
78 | memcpy(mUpdateMask, mask.mUpdateMask, mBlocks << 2); |
---|
79 | |
---|
80 | return *this; |
---|
81 | } |
---|
82 | |
---|
83 | inline void operator &= ( const UpdateMask& mask ) |
---|
84 | { |
---|
85 | ASSERT(mask.mCount <= mCount); |
---|
86 | for (uint32 i = 0; i < mBlocks; i++) |
---|
87 | mUpdateMask[i] &= mask.mUpdateMask[i]; |
---|
88 | } |
---|
89 | |
---|
90 | inline void operator |= ( const UpdateMask& mask ) |
---|
91 | { |
---|
92 | ASSERT(mask.mCount <= mCount); |
---|
93 | for (uint32 i = 0; i < mBlocks; i++) |
---|
94 | mUpdateMask[i] |= mask.mUpdateMask[i]; |
---|
95 | } |
---|
96 | |
---|
97 | inline UpdateMask operator & ( const UpdateMask& mask ) const |
---|
98 | { |
---|
99 | ASSERT(mask.mCount <= mCount); |
---|
100 | |
---|
101 | UpdateMask newmask; |
---|
102 | newmask = *this; |
---|
103 | newmask &= mask; |
---|
104 | |
---|
105 | return newmask; |
---|
106 | } |
---|
107 | |
---|
108 | inline UpdateMask operator | ( const UpdateMask& mask ) const |
---|
109 | { |
---|
110 | ASSERT(mask.mCount <= mCount); |
---|
111 | |
---|
112 | UpdateMask newmask; |
---|
113 | newmask = *this; |
---|
114 | newmask |= mask; |
---|
115 | |
---|
116 | return newmask; |
---|
117 | } |
---|
118 | |
---|
119 | private: |
---|
120 | uint32 mCount; |
---|
121 | uint32 mBlocks; |
---|
122 | uint32 *mUpdateMask; |
---|
123 | }; |
---|
124 | #endif |
---|