Core/Utils: Extract template base class out of flag128

This commit is contained in:
Shauren
2022-02-09 23:11:03 +01:00
parent ed2fa95d83
commit 5c417bec98
8 changed files with 153 additions and 136 deletions

View File

@@ -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__

View File

@@ -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,

View File

@@ -22,6 +22,7 @@
#include "SpellInfo.h"
#include "SpellMgr.h"
#include "Timer.h"
#include "Util.h"
#include <sstream>
LanguageMgr::LanguageMgr() : _langsMap(), _wordsMap() { }

View File

@@ -20,8 +20,8 @@
#include "Common.h"
#include "DBCEnums.h"
#include "FlagsArray.h"
#include "RaceMask.h"
#include "Util.h"
#pragma pack(push, 1)

View File

@@ -20,9 +20,9 @@
#include "Common.h"
#include "DB2Structure.h"
#include "Errors.h"
#include "SharedDefines.h"
#include <bitset>
#include <unordered_map>
#include <vector>
enum ItemModType

View File

@@ -17,10 +17,9 @@
#include "GarrisonPackets.h"
#include "DB2Structure.h"
#include "Errors.h"
namespace WorldPackets
{
namespace Garrison
namespace WorldPackets::Garrison
{
WorldPacket const* GarrisonCreateResult::Write()
{
@@ -454,4 +453,3 @@ WorldPacket const* GarrisonBuildingActivated::Write()
return &_worldPacket;
}
}
}

View File

@@ -20,7 +20,6 @@
#include "SharedDefines.h"
#include "DB2Structure.h"
#include "Util.h"
#include "Object.h"
#include "SpellAuraDefines.h"
#include "SpellDefines.h"

View File

@@ -23,10 +23,11 @@
#include "Define.h"
#include "DBCEnums.h"
#include "Duration.h"
#include "Errors.h"
#include "FlagsArray.h"
#include "IteratorPair.h"
#include "RaceMask.h"
#include "SharedDefines.h"
#include "Util.h"
#include <functional>
#include <map>