TrinityCore
FlagsArray.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
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 FlagsArray_h__
19#define FlagsArray_h__
20
21#include "Define.h"
22#include <array>
23
24template<typename T, size_t N>
26{
27public:
28 constexpr FlagsArray operator&(FlagsArray const& right) const
29 {
30 FlagsArray temp = *this;
31 temp &= right;
32 return temp;
33 }
34
35 constexpr FlagsArray& operator&=(FlagsArray const& right)
36 {
37 for (size_t i = 0; i < N; ++i)
38 _storage[i] &= right[i];
39
40 return *this;
41 }
42
43 constexpr FlagsArray operator|(FlagsArray const& right) const
44 {
45 FlagsArray temp = *this;
46 temp |= right;
47 return temp;
48 }
49
50 constexpr FlagsArray& operator|=(FlagsArray const& right)
51 {
52 for (size_t i = 0; i < N; ++i)
53 _storage[i] |= right[i];
54
55 return *this;
56 }
57
58 constexpr FlagsArray operator~() const
59 {
60 FlagsArray temp = *this;
61 for (size_t i = 0; i < N; ++i)
62 temp._storage[i] = ~_storage[i];
63
64 return temp;
65 }
66
67 constexpr FlagsArray operator^(FlagsArray const& right) const
68 {
69 FlagsArray temp = *this;
70 temp ^= right;
71 return temp;
72 }
73
74 constexpr FlagsArray& operator^=(FlagsArray const& right)
75 {
76 for (size_t i = 0; i < N; ++i)
77 _storage[i] ^= right[i];
78
79 return *this;
80 }
81
82 constexpr explicit operator bool() const
83 {
84 for (size_t i = 0; i < N; ++i)
85 if (_storage[i])
86 return true;
87
88 return false;
89 }
90
91 constexpr bool operator !() const
92 {
93 return !(bool(*this));
94 }
95
96 constexpr T& operator[](size_t i)
97 {
98 return _storage[i];
99 }
100
101 constexpr T const& operator [](size_t i) const
102 {
103 return _storage[i];
104 }
105
106 constexpr FlagsArray& operator=(std::array<T, N> const& right)
107 {
108 _storage = right;
109 return *this;
110 }
111
112protected:
113 std::array<T, N> _storage = { };
114};
115
116class flag128 : public FlagsArray<uint32, 4>
117{
118public:
119 constexpr flag128(uint32 p1 = 0, uint32 p2 = 0, uint32 p3 = 0, uint32 p4 = 0)
120 {
121 _storage[0] = p1;
122 _storage[1] = p2;
123 _storage[2] = p3;
124 _storage[3] = p4;
125 }
126
127 constexpr bool IsEqual(uint32 p1 = 0, uint32 p2 = 0, uint32 p3 = 0, uint32 p4 = 0) const
128 {
129 return (_storage[0] == p1 && _storage[1] == p2 && _storage[2] == p3 && _storage[3] == p4);
130 }
131
132 constexpr bool HasFlag(uint32 p1 = 0, uint32 p2 = 0, uint32 p3 = 0, uint32 p4 = 0) const
133 {
134 return (_storage[0] & p1 || _storage[1] & p2 || _storage[2] & p3 || _storage[3] & p4);
135 }
136
137 constexpr void Set(uint32 p1 = 0, uint32 p2 = 0, uint32 p3 = 0, uint32 p4 = 0)
138 {
139 _storage[0] = p1;
140 _storage[1] = p2;
141 _storage[2] = p3;
142 _storage[3] = p4;
143 }
144};
145
146#endif // FlagsArray_h__
uint32_t uint32
Definition: Define.h:142
constexpr FlagsArray & operator|=(FlagsArray const &right)
Definition: FlagsArray.h:50
constexpr FlagsArray & operator&=(FlagsArray const &right)
Definition: FlagsArray.h:35
constexpr FlagsArray operator~() const
Definition: FlagsArray.h:58
constexpr T & operator[](size_t i)
Definition: FlagsArray.h:96
constexpr FlagsArray operator^(FlagsArray const &right) const
Definition: FlagsArray.h:67
constexpr FlagsArray operator|(FlagsArray const &right) const
Definition: FlagsArray.h:43
constexpr FlagsArray & operator=(std::array< T, N > const &right)
Definition: FlagsArray.h:106
constexpr bool operator!() const
Definition: FlagsArray.h:91
constexpr FlagsArray operator&(FlagsArray const &right) const
Definition: FlagsArray.h:28
std::array< T, N > _storage
Definition: FlagsArray.h:113
constexpr FlagsArray & operator^=(FlagsArray const &right)
Definition: FlagsArray.h:74
constexpr bool IsEqual(uint32 p1=0, uint32 p2=0, uint32 p3=0, uint32 p4=0) const
Definition: FlagsArray.h:127
constexpr flag128(uint32 p1=0, uint32 p2=0, uint32 p3=0, uint32 p4=0)
Definition: FlagsArray.h:119
constexpr bool HasFlag(uint32 p1=0, uint32 p2=0, uint32 p3=0, uint32 p4=0) const
Definition: FlagsArray.h:132
constexpr void Set(uint32 p1=0, uint32 p2=0, uint32 p3=0, uint32 p4=0)
Definition: FlagsArray.h:137