diff options
| author | Shauren <shauren.trinity@gmail.com> | 2022-02-09 23:11:03 +0100 |
|---|---|---|
| committer | Shauren <shauren.trinity@gmail.com> | 2022-02-09 23:11:03 +0100 |
| commit | 5c417bec984c383702619ae05e867512100cb3e9 (patch) | |
| tree | d15712d9d93494ccbb0b682b5a324f696c1c4604 /src/common | |
| parent | ed2fa95d832d3a7a7a761123c939c3e409dcde26 (diff) | |
Core/Utils: Extract template base class out of flag128
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/Utilities/FlagsArray.h | 146 | ||||
| -rw-r--r-- | src/common/Utilities/Util.h | 128 |
2 files changed, 146 insertions, 128 deletions
diff --git a/src/common/Utilities/FlagsArray.h b/src/common/Utilities/FlagsArray.h new file mode 100644 index 00000000000..cfdb066c321 --- /dev/null +++ b/src/common/Utilities/FlagsArray.h @@ -0,0 +1,146 @@ +/* + * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef FlagsArray_h__ +#define FlagsArray_h__ + +#include "Define.h" +#include <array> + +template<typename T, size_t N> +class FlagsArray +{ +public: + constexpr FlagsArray operator&(FlagsArray const& right) const + { + FlagsArray temp = *this; + temp &= right; + return temp; + } + + constexpr FlagsArray& operator&=(FlagsArray const& right) + { + for (size_t i = 0; i < N; ++i) + _storage[i] &= right[i]; + + return *this; + } + + constexpr FlagsArray operator|(FlagsArray const& right) const + { + FlagsArray temp = *this; + temp |= right; + return temp; + } + + constexpr FlagsArray& operator|=(FlagsArray const& right) + { + for (size_t i = 0; i < N; ++i) + _storage[i] |= right[i]; + + return *this; + } + + constexpr FlagsArray operator~() const + { + FlagsArray temp = *this; + for (size_t i = 0; i < N; ++i) + temp._storage[i] = ~_storage[i]; + + return temp; + } + + constexpr FlagsArray operator^(FlagsArray const& right) const + { + FlagsArray temp = *this; + temp ^= right; + return temp; + } + + constexpr FlagsArray& operator^=(FlagsArray const& right) + { + for (size_t i = 0; i < N; ++i) + _storage[i] ^= right[i]; + + return *this; + } + + constexpr operator bool() const + { + for (size_t i = 0; i < N; ++i) + if (_storage[i]) + return true; + + return false; + } + + constexpr bool operator !() const + { + return !(bool(*this)); + } + + constexpr T& operator[](size_t i) + { + return _storage[i]; + } + + constexpr T const& operator [](size_t i) const + { + return _storage[i]; + } + + constexpr FlagsArray& operator=(std::array<T, N> const& right) + { + _storage = right; + return *this; + } + +protected: + std::array<T, N> _storage = { }; +}; + +class flag128 : public FlagsArray<uint32, 4> +{ +public: + constexpr flag128(uint32 p1 = 0, uint32 p2 = 0, uint32 p3 = 0, uint32 p4 = 0) + { + _storage[0] = p1; + _storage[1] = p2; + _storage[2] = p3; + _storage[3] = p4; + } + + constexpr bool IsEqual(uint32 p1 = 0, uint32 p2 = 0, uint32 p3 = 0, uint32 p4 = 0) const + { + return (_storage[0] == p1 && _storage[1] == p2 && _storage[2] == p3 && _storage[3] == p4); + } + + constexpr bool HasFlag(uint32 p1 = 0, uint32 p2 = 0, uint32 p3 = 0, uint32 p4 = 0) const + { + return (_storage[0] & p1 || _storage[1] & p2 || _storage[2] & p3 || _storage[3] & p4); + } + + constexpr void Set(uint32 p1 = 0, uint32 p2 = 0, uint32 p3 = 0, uint32 p4 = 0) + { + _storage[0] = p1; + _storage[1] = p2; + _storage[2] = p3; + _storage[3] = p4; + } +}; + +#endif // FlagsArray_h__ diff --git a/src/common/Utilities/Util.h b/src/common/Utilities/Util.h index 8a24981c837..6aeca639173 100644 --- a/src/common/Utilities/Util.h +++ b/src/common/Utilities/Util.h @@ -430,134 +430,6 @@ class HookList final } }; -class TC_COMMON_API flag128 -{ -private: - uint32 part[4]; - -public: - flag128(uint32 p1 = 0, uint32 p2 = 0, uint32 p3 = 0, uint32 p4 = 0) - { - part[0] = p1; - part[1] = p2; - part[2] = p3; - part[3] = p4; - } - - inline bool IsEqual(uint32 p1 = 0, uint32 p2 = 0, uint32 p3 = 0, uint32 p4 = 0) const - { - return (part[0] == p1 && part[1] == p2 && part[2] == p3 && part[3] == p4); - } - - inline bool HasFlag(uint32 p1 = 0, uint32 p2 = 0, uint32 p3 = 0, uint32 p4 = 0) const - { - return (part[0] & p1 || part[1] & p2 || part[2] & p3 || part[3] & p4); - } - - inline void Set(uint32 p1 = 0, uint32 p2 = 0, uint32 p3 = 0, uint32 p4 = 0) - { - part[0] = p1; - part[1] = p2; - part[2] = p3; - part[3] = p4; - } - - inline bool operator<(flag128 const& right) const - { - for (uint8 i = 4; i > 0; --i) - { - if (part[i - 1] < right.part[i - 1]) - return true; - else if (part[i - 1] > right.part[i - 1]) - return false; - } - return false; - } - - inline bool operator==(flag128 const& right) const - { - return - ( - part[0] == right.part[0] && - part[1] == right.part[1] && - part[2] == right.part[2] && - part[3] == right.part[3] - ); - } - - inline bool operator!=(flag128 const& right) const - { - return !(*this == right); - } - - inline flag128 operator&(flag128 const& right) const - { - return flag128(part[0] & right.part[0], part[1] & right.part[1], part[2] & right.part[2], part[3] & right.part[3]); - } - - inline flag128& operator&=(flag128 const& right) - { - part[0] &= right.part[0]; - part[1] &= right.part[1]; - part[2] &= right.part[2]; - part[3] &= right.part[3]; - return *this; - } - - inline flag128 operator|(flag128 const& right) const - { - return flag128(part[0] | right.part[0], part[1] | right.part[1], part[2] | right.part[2], part[3] | right.part[3]); - } - - inline flag128& operator |=(flag128 const& right) - { - part[0] |= right.part[0]; - part[1] |= right.part[1]; - part[2] |= right.part[2]; - part[3] |= right.part[3]; - return *this; - } - - inline flag128 operator~() const - { - return flag128(~part[0], ~part[1], ~part[2], ~part[3]); - } - - inline flag128 operator^(flag128 const& right) const - { - return flag128(part[0] ^ right.part[0], part[1] ^ right.part[1], part[2] ^ right.part[2], part[3] ^ right.part[3]); - } - - inline flag128& operator^=(flag128 const& right) - { - part[0] ^= right.part[0]; - part[1] ^= right.part[1]; - part[2] ^= right.part[2]; - part[3] ^= right.part[3]; - return *this; - } - - inline operator bool() const - { - return (part[0] != 0 || part[1] != 0 || part[2] != 0 || part[3] != 0); - } - - inline bool operator !() const - { - return !(bool(*this)); - } - - inline uint32& operator[](uint8 el) - { - return part[el]; - } - - inline uint32 const& operator [](uint8 el) const - { - return part[el]; - } -}; - enum ComparisionType { COMP_TYPE_EQ = 0, |
