Browse files

Implement new guid write/read methods

Signed-off-by: Yaki Khadafi <elsoldollo@gmail.com>
  • Loading branch information...
1 parent 13498e9 commit 6962e859ab3312c0c007ad8f37784c6099d3c774 @Zakamurite Zakamurite committed Aug 3, 2012
Showing with 187 additions and 2 deletions.
  1. +99 −1 src/game/ObjectGuid.h
  2. +88 −1 src/shared/ByteBuffer.h
View
100 src/game/ObjectGuid.h
@@ -85,6 +85,8 @@ struct PackedGuidReader
ObjectGuid* m_guidPtr;
};
+#define NUM_GUID_BYTES 8
@LordJZ
LordJZ added a note Aug 3, 2012

sizeof(uint64)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
class MANGOS_DLL_SPEC ObjectGuid
{
public: // constructors
@@ -172,6 +174,28 @@ class MANGOS_DLL_SPEC ObjectGuid
bool operator!= (ObjectGuid const& guid) const { return GetRawValue() != guid.GetRawValue(); }
bool operator< (ObjectGuid const& guid) const { return GetRawValue() < guid.GetRawValue(); }
+ uint8& operator[] (uint8 index)
+ {
+ MANGOS_ASSERT(index < NUM_GUID_BYTES);
+
+#if MANGOS_ENDIAN == MANGOS_LITTLEENDIAN
+ return m_guidBytes[index];
+#else
+ return m_guidBytes[NUM_GUID_BYTES - 1 - index];
+#endif
+ }
+
+ uint8 const& operator[] (uint8 index) const
+ {
+ MANGOS_ASSERT(index < NUM_GUID_BYTES);
+
+#if MANGOS_ENDIAN == MANGOS_LITTLEENDIAN
+ return m_guidBytes[index];
+#else
+ return m_guidBytes[NUM_GUID_BYTES - 1 - index];
+#endif
+ }
+
public: // accessors - for debug
static char const* GetTypeName(HighGuid high);
char const* GetTypeName() const { return !IsEmpty() ? GetTypeName(GetHigh()) : "None"; }
@@ -203,7 +227,11 @@ class MANGOS_DLL_SPEC ObjectGuid
bool HasEntry() const { return HasEntry(GetHigh()); }
private: // fields
- uint64 m_guid;
+ union
+ {
+ uint64 m_guid;
+ uint8 m_guidBytes[NUM_GUID_BYTES];
+ };
};
typedef std::set<ObjectGuid> ObjectGuidSet;
@@ -277,4 +305,74 @@ HASH_NAMESPACE_START
HASH_NAMESPACE_END
+#define DEFINE_READGUIDMASK(T1, T2) template <T1> \
+ void ByteBuffer::ReadGuidMask(ObjectGuid& guid) \
+ { \
+ uint8 maskArr[] = { T2 }; \
+ for (uint8 i = 0; i < countof(maskArr); ++i) \
@LordJZ
LordJZ added a note Aug 3, 2012

countof is not in the standard

true, but it is supported by macro for mangos (and as it will come in c++11 i think, it is reasonable to use)

@LordJZ
LordJZ added a note Aug 4, 2012

Oh, wasn't aware of this. Thanks for correcting me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ guid[maskArr[i]] = ReadBit(); \
+ }
+
+#define DEFINE_WRITEGUIDMASK(T1, T2) template <T1> \
+ void ByteBuffer::WriteGuidMask(ObjectGuid& guid) \
+ { \
+ uint8 maskArr[] = { T2 }; \
+ for (uint8 i = 0; i < countof(maskArr); ++i) \
+ WriteBit(guid[maskArr[i]]); \
+ }
+
+#define DEFINE_READGUIDBYTES(T1, T2) template <T1> \
+ void ByteBuffer::ReadGuidBytes(ObjectGuid& guid) \
+ { \
+ uint8 maskArr[] = { T2 }; \
+ for (uint8 i = 0; i < countof(maskArr); ++i) \
+ if (guid[maskArr[i]] != 0) \
+ guid[maskArr[i]] ^= read<uint8>; \
+ }
+
+#define DEFINE_WRITEGUIDBYTES(T1, T2) template <T1> \
+ void ByteBuffer::WriteGuidBytes(ObjectGuid& guid) \
+ { \
+ uint8 maskArr[] = { T2 }; \
+ for (uint8 i = 0; i < countof(maskArr); ++i) \
+ if (guid[maskArr[i]] != 0) \
+ (*this) << uint8(guid[maskArr[i]] ^ 1); \
+ }
+
+DEFINE_READGUIDMASK(BITS_1, BIT_VALS_1)
+DEFINE_READGUIDMASK(BITS_2, BIT_VALS_2)
+DEFINE_READGUIDMASK(BITS_3, BIT_VALS_3)
+DEFINE_READGUIDMASK(BITS_4, BIT_VALS_4)
+DEFINE_READGUIDMASK(BITS_5, BIT_VALS_5)
+DEFINE_READGUIDMASK(BITS_6, BIT_VALS_6)
+DEFINE_READGUIDMASK(BITS_7, BIT_VALS_7)
+DEFINE_READGUIDMASK(BITS_8, BIT_VALS_8)
+
+DEFINE_WRITEGUIDMASK(BITS_1, BIT_VALS_1)
+DEFINE_WRITEGUIDMASK(BITS_2, BIT_VALS_2)
+DEFINE_WRITEGUIDMASK(BITS_3, BIT_VALS_3)
+DEFINE_WRITEGUIDMASK(BITS_4, BIT_VALS_4)
+DEFINE_WRITEGUIDMASK(BITS_5, BIT_VALS_5)
+DEFINE_WRITEGUIDMASK(BITS_6, BIT_VALS_6)
+DEFINE_WRITEGUIDMASK(BITS_7, BIT_VALS_7)
+DEFINE_WRITEGUIDMASK(BITS_8, BIT_VALS_8)
+
+DEFINE_READGUIDBYTES(BITS_1, BIT_VALS_1)
+DEFINE_READGUIDBYTES(BITS_2, BIT_VALS_2)
+DEFINE_READGUIDBYTES(BITS_3, BIT_VALS_3)
+DEFINE_READGUIDBYTES(BITS_4, BIT_VALS_4)
+DEFINE_READGUIDBYTES(BITS_5, BIT_VALS_5)
+DEFINE_READGUIDBYTES(BITS_6, BIT_VALS_6)
+DEFINE_READGUIDBYTES(BITS_7, BIT_VALS_7)
+DEFINE_READGUIDBYTES(BITS_8, BIT_VALS_8)
+
+DEFINE_WRITEGUIDBYTES(BITS_1, BIT_VALS_1)
+DEFINE_WRITEGUIDBYTES(BITS_2, BIT_VALS_2)
+DEFINE_WRITEGUIDBYTES(BITS_3, BIT_VALS_3)
+DEFINE_WRITEGUIDBYTES(BITS_4, BIT_VALS_4)
+DEFINE_WRITEGUIDBYTES(BITS_5, BIT_VALS_5)
+DEFINE_WRITEGUIDBYTES(BITS_6, BIT_VALS_6)
+DEFINE_WRITEGUIDBYTES(BITS_7, BIT_VALS_7)
+DEFINE_WRITEGUIDBYTES(BITS_8, BIT_VALS_8)
+
#endif
View
89 src/shared/ByteBuffer.h
@@ -23,6 +23,26 @@
#include "Log.h"
#include "Utilities/ByteConverter.h"
+#define BITS_1 uint8 _1
+#define BITS_2 BITS_1, uint8 _2
+#define BITS_3 BITS_2, uint8 _3
+#define BITS_4 BITS_3, uint8 _4
+#define BITS_5 BITS_4, uint8 _5
+#define BITS_6 BITS_5, uint8 _6
+#define BITS_7 BITS_6, uint8 _7
+#define BITS_8 BITS_7, uint8 _8
+
+#define BIT_VALS_1 _1
+#define BIT_VALS_2 BIT_VALS_1, _2
+#define BIT_VALS_3 BIT_VALS_2, _3
+#define BIT_VALS_4 BIT_VALS_3, _4
+#define BIT_VALS_5 BIT_VALS_4, _5
+#define BIT_VALS_6 BIT_VALS_5, _6
+#define BIT_VALS_7 BIT_VALS_6, _7
+#define BIT_VALS_8 BIT_VALS_7, _8
+
+class ObjectGuid;
+
class ByteBufferException
{
public:
@@ -206,6 +226,74 @@ class ByteBuffer
(*this) << uint8(guidByte[byteOrder[i + bytePos]] ^ 1);
}
+ template<BITS_1>
+ void ReadGuidMask(ObjectGuid& guid);
+ template<BITS_2>
+ void ReadGuidMask(ObjectGuid& guid);
+ template<BITS_3>
+ void ReadGuidMask(ObjectGuid& guid);
+ template<BITS_4>
+ void ReadGuidMask(ObjectGuid& guid);
+ template<BITS_5>
+ void ReadGuidMask(ObjectGuid& guid);
+ template<BITS_6>
+ void ReadGuidMask(ObjectGuid& guid);
+ template<BITS_7>
+ void ReadGuidMask(ObjectGuid& guid);
+ template<BITS_8>
+ void ReadGuidMask(ObjectGuid& guid);
+
+ template<BITS_1>
+ void WriteGuidMask(ObjectGuid& guid);
+ template<BITS_2>
+ void WriteGuidMask(ObjectGuid& guid);
+ template<BITS_3>
+ void WriteGuidMask(ObjectGuid& guid);
+ template<BITS_4>
+ void WriteGuidMask(ObjectGuid& guid);
+ template<BITS_5>
+ void WriteGuidMask(ObjectGuid& guid);
+ template<BITS_6>
+ void WriteGuidMask(ObjectGuid& guid);
+ template<BITS_7>
+ void WriteGuidMask(ObjectGuid& guid);
+ template<BITS_8>
+ void WriteGuidMask(ObjectGuid& guid);
+
+ template<BITS_1>
+ void ReadGuidBytes(ObjectGuid& guid);
+ template<BITS_2>
+ void ReadGuidBytes(ObjectGuid& guid);
+ template<BITS_3>
+ void ReadGuidBytes(ObjectGuid& guid);
+ template<BITS_4>
+ void ReadGuidBytes(ObjectGuid& guid);
+ template<BITS_5>
+ void ReadGuidBytes(ObjectGuid& guid);
+ template<BITS_6>
+ void ReadGuidBytes(ObjectGuid& guid);
+ template<BITS_7>
+ void ReadGuidBytes(ObjectGuid& guid);
+ template<BITS_8>
+ void ReadGuidBytes(ObjectGuid& guid);
+
+ template<BITS_1>
+ void WriteGuidBytes(ObjectGuid& guid);
+ template<BITS_2>
+ void WriteGuidBytes(ObjectGuid& guid);
+ template<BITS_3>
+ void WriteGuidBytes(ObjectGuid& guid);
+ template<BITS_4>
+ void WriteGuidBytes(ObjectGuid& guid);
+ template<BITS_5>
+ void WriteGuidBytes(ObjectGuid& guid);
+ template<BITS_6>
+ void WriteGuidBytes(ObjectGuid& guid);
+ template<BITS_7>
+ void WriteGuidBytes(ObjectGuid& guid);
+ template<BITS_8>
+ void WriteGuidBytes(ObjectGuid& guid);
+
template <typename T> void put(size_t pos, T value)
{
EndianConvert(value);
@@ -425,7 +513,6 @@ class ByteBuffer
return *this;
}
-
template <typename T> T read()
{
T r = read<T>(_rpos);

0 comments on commit 6962e85

Please sign in to comment.