Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implement new guid write/read methods

Signed-off-by: Yaki Khadafi <elsoldollo@gmail.com>
  • Loading branch information...
commit 6962e859ab3312c0c007ad8f37784c6099d3c774 1 parent 13498e9
Yaki Khadafi authored August 03, 2012
100  src/game/ObjectGuid.h
@@ -85,6 +85,8 @@ struct PackedGuidReader
85 85
     ObjectGuid* m_guidPtr;
86 86
 };
87 87
 
  88
+#define NUM_GUID_BYTES 8
  89
+
88 90
 class MANGOS_DLL_SPEC ObjectGuid
89 91
 {
90 92
     public:                                                 // constructors
@@ -172,6 +174,28 @@ class MANGOS_DLL_SPEC ObjectGuid
172 174
         bool operator!= (ObjectGuid const& guid) const { return GetRawValue() != guid.GetRawValue(); }
173 175
         bool operator< (ObjectGuid const& guid) const { return GetRawValue() < guid.GetRawValue(); }
174 176
 
  177
+        uint8& operator[] (uint8 index)
  178
+        {
  179
+            MANGOS_ASSERT(index < NUM_GUID_BYTES);
  180
+
  181
+#if MANGOS_ENDIAN == MANGOS_LITTLEENDIAN
  182
+            return m_guidBytes[index];
  183
+#else
  184
+            return m_guidBytes[NUM_GUID_BYTES - 1 - index];
  185
+#endif
  186
+        }
  187
+
  188
+        uint8 const& operator[] (uint8 index) const
  189
+        {
  190
+            MANGOS_ASSERT(index < NUM_GUID_BYTES);
  191
+
  192
+#if MANGOS_ENDIAN == MANGOS_LITTLEENDIAN
  193
+            return m_guidBytes[index];
  194
+#else
  195
+            return m_guidBytes[NUM_GUID_BYTES - 1 - index];
  196
+#endif
  197
+        }
  198
+
175 199
     public:                                                 // accessors - for debug
176 200
         static char const* GetTypeName(HighGuid high);
177 201
         char const* GetTypeName() const { return !IsEmpty() ? GetTypeName(GetHigh()) : "None"; }
@@ -203,7 +227,11 @@ class MANGOS_DLL_SPEC ObjectGuid
203 227
         bool HasEntry() const { return HasEntry(GetHigh()); }
204 228
 
205 229
     private:                                                // fields
206  
-        uint64 m_guid;
  230
+        union
  231
+        {
  232
+            uint64 m_guid;
  233
+            uint8 m_guidBytes[NUM_GUID_BYTES];
  234
+        };
207 235
 };
208 236
 
209 237
 typedef std::set<ObjectGuid> ObjectGuidSet;
@@ -277,4 +305,74 @@ HASH_NAMESPACE_START
277 305
 
278 306
 HASH_NAMESPACE_END
279 307
 
  308
+#define DEFINE_READGUIDMASK(T1, T2) template <T1> \
  309
+    void ByteBuffer::ReadGuidMask(ObjectGuid& guid) \
  310
+    { \
  311
+        uint8 maskArr[] = { T2 }; \
  312
+        for (uint8 i = 0; i < countof(maskArr); ++i) \
  313
+            guid[maskArr[i]] = ReadBit(); \
  314
+    }
  315
+
  316
+#define DEFINE_WRITEGUIDMASK(T1, T2) template <T1> \
  317
+    void ByteBuffer::WriteGuidMask(ObjectGuid& guid) \
  318
+    { \
  319
+        uint8 maskArr[] = { T2 }; \
  320
+        for (uint8 i = 0; i < countof(maskArr); ++i) \
  321
+            WriteBit(guid[maskArr[i]]); \
  322
+    }
  323
+
  324
+#define DEFINE_READGUIDBYTES(T1, T2) template <T1> \
  325
+    void ByteBuffer::ReadGuidBytes(ObjectGuid& guid) \
  326
+    { \
  327
+        uint8 maskArr[] = { T2 }; \
  328
+        for (uint8 i = 0; i < countof(maskArr); ++i) \
  329
+            if (guid[maskArr[i]] != 0) \
  330
+                guid[maskArr[i]] ^= read<uint8>; \
  331
+    }
  332
+
  333
+#define DEFINE_WRITEGUIDBYTES(T1, T2) template <T1> \
  334
+    void ByteBuffer::WriteGuidBytes(ObjectGuid& guid) \
  335
+    { \
  336
+        uint8 maskArr[] = { T2 }; \
  337
+        for (uint8 i = 0; i < countof(maskArr); ++i) \
  338
+            if (guid[maskArr[i]] != 0) \
  339
+                (*this) << uint8(guid[maskArr[i]] ^ 1); \
  340
+    }
  341
+
  342
+DEFINE_READGUIDMASK(BITS_1, BIT_VALS_1)
  343
+DEFINE_READGUIDMASK(BITS_2, BIT_VALS_2)
  344
+DEFINE_READGUIDMASK(BITS_3, BIT_VALS_3)
  345
+DEFINE_READGUIDMASK(BITS_4, BIT_VALS_4)
  346
+DEFINE_READGUIDMASK(BITS_5, BIT_VALS_5)
  347
+DEFINE_READGUIDMASK(BITS_6, BIT_VALS_6)
  348
+DEFINE_READGUIDMASK(BITS_7, BIT_VALS_7)
  349
+DEFINE_READGUIDMASK(BITS_8, BIT_VALS_8)
  350
+
  351
+DEFINE_WRITEGUIDMASK(BITS_1, BIT_VALS_1)
  352
+DEFINE_WRITEGUIDMASK(BITS_2, BIT_VALS_2)
  353
+DEFINE_WRITEGUIDMASK(BITS_3, BIT_VALS_3)
  354
+DEFINE_WRITEGUIDMASK(BITS_4, BIT_VALS_4)
  355
+DEFINE_WRITEGUIDMASK(BITS_5, BIT_VALS_5)
  356
+DEFINE_WRITEGUIDMASK(BITS_6, BIT_VALS_6)
  357
+DEFINE_WRITEGUIDMASK(BITS_7, BIT_VALS_7)
  358
+DEFINE_WRITEGUIDMASK(BITS_8, BIT_VALS_8)
  359
+
  360
+DEFINE_READGUIDBYTES(BITS_1, BIT_VALS_1)
  361
+DEFINE_READGUIDBYTES(BITS_2, BIT_VALS_2)
  362
+DEFINE_READGUIDBYTES(BITS_3, BIT_VALS_3)
  363
+DEFINE_READGUIDBYTES(BITS_4, BIT_VALS_4)
  364
+DEFINE_READGUIDBYTES(BITS_5, BIT_VALS_5)
  365
+DEFINE_READGUIDBYTES(BITS_6, BIT_VALS_6)
  366
+DEFINE_READGUIDBYTES(BITS_7, BIT_VALS_7)
  367
+DEFINE_READGUIDBYTES(BITS_8, BIT_VALS_8)
  368
+
  369
+DEFINE_WRITEGUIDBYTES(BITS_1, BIT_VALS_1)
  370
+DEFINE_WRITEGUIDBYTES(BITS_2, BIT_VALS_2)
  371
+DEFINE_WRITEGUIDBYTES(BITS_3, BIT_VALS_3)
  372
+DEFINE_WRITEGUIDBYTES(BITS_4, BIT_VALS_4)
  373
+DEFINE_WRITEGUIDBYTES(BITS_5, BIT_VALS_5)
  374
+DEFINE_WRITEGUIDBYTES(BITS_6, BIT_VALS_6)
  375
+DEFINE_WRITEGUIDBYTES(BITS_7, BIT_VALS_7)
  376
+DEFINE_WRITEGUIDBYTES(BITS_8, BIT_VALS_8)
  377
+
280 378
 #endif
89  src/shared/ByteBuffer.h
@@ -23,6 +23,26 @@
23 23
 #include "Log.h"
24 24
 #include "Utilities/ByteConverter.h"
25 25
 
  26
+#define BITS_1 uint8 _1
  27
+#define BITS_2 BITS_1, uint8 _2
  28
+#define BITS_3 BITS_2, uint8 _3
  29
+#define BITS_4 BITS_3, uint8 _4
  30
+#define BITS_5 BITS_4, uint8 _5
  31
+#define BITS_6 BITS_5, uint8 _6
  32
+#define BITS_7 BITS_6, uint8 _7
  33
+#define BITS_8 BITS_7, uint8 _8
  34
+
  35
+#define BIT_VALS_1 _1
  36
+#define BIT_VALS_2 BIT_VALS_1, _2
  37
+#define BIT_VALS_3 BIT_VALS_2, _3
  38
+#define BIT_VALS_4 BIT_VALS_3, _4
  39
+#define BIT_VALS_5 BIT_VALS_4, _5
  40
+#define BIT_VALS_6 BIT_VALS_5, _6
  41
+#define BIT_VALS_7 BIT_VALS_6, _7
  42
+#define BIT_VALS_8 BIT_VALS_7, _8
  43
+
  44
+class ObjectGuid;
  45
+
26 46
 class ByteBufferException
27 47
 {
28 48
     public:
@@ -206,6 +226,74 @@ class ByteBuffer
206 226
                     (*this) << uint8(guidByte[byteOrder[i + bytePos]] ^ 1);
207 227
         }
208 228
 
  229
+        template<BITS_1>
  230
+            void ReadGuidMask(ObjectGuid& guid);
  231
+        template<BITS_2>
  232
+            void ReadGuidMask(ObjectGuid& guid);
  233
+        template<BITS_3>
  234
+            void ReadGuidMask(ObjectGuid& guid);
  235
+        template<BITS_4>
  236
+            void ReadGuidMask(ObjectGuid& guid);
  237
+        template<BITS_5>
  238
+            void ReadGuidMask(ObjectGuid& guid);
  239
+        template<BITS_6>
  240
+            void ReadGuidMask(ObjectGuid& guid);
  241
+        template<BITS_7>
  242
+            void ReadGuidMask(ObjectGuid& guid);
  243
+        template<BITS_8>
  244
+            void ReadGuidMask(ObjectGuid& guid);
  245
+
  246
+        template<BITS_1>
  247
+            void WriteGuidMask(ObjectGuid& guid);
  248
+        template<BITS_2>
  249
+            void WriteGuidMask(ObjectGuid& guid);
  250
+        template<BITS_3>
  251
+            void WriteGuidMask(ObjectGuid& guid);
  252
+        template<BITS_4>
  253
+            void WriteGuidMask(ObjectGuid& guid);
  254
+        template<BITS_5>
  255
+            void WriteGuidMask(ObjectGuid& guid);
  256
+        template<BITS_6>
  257
+            void WriteGuidMask(ObjectGuid& guid);
  258
+        template<BITS_7>
  259
+            void WriteGuidMask(ObjectGuid& guid);
  260
+        template<BITS_8>
  261
+            void WriteGuidMask(ObjectGuid& guid);
  262
+
  263
+        template<BITS_1>
  264
+            void ReadGuidBytes(ObjectGuid& guid);
  265
+        template<BITS_2>
  266
+            void ReadGuidBytes(ObjectGuid& guid);
  267
+        template<BITS_3>
  268
+            void ReadGuidBytes(ObjectGuid& guid);
  269
+        template<BITS_4>
  270
+            void ReadGuidBytes(ObjectGuid& guid);
  271
+        template<BITS_5>
  272
+            void ReadGuidBytes(ObjectGuid& guid);
  273
+        template<BITS_6>
  274
+            void ReadGuidBytes(ObjectGuid& guid);
  275
+        template<BITS_7>
  276
+            void ReadGuidBytes(ObjectGuid& guid);
  277
+        template<BITS_8>
  278
+            void ReadGuidBytes(ObjectGuid& guid);
  279
+
  280
+        template<BITS_1>
  281
+            void WriteGuidBytes(ObjectGuid& guid);
  282
+        template<BITS_2>
  283
+            void WriteGuidBytes(ObjectGuid& guid);
  284
+        template<BITS_3>
  285
+            void WriteGuidBytes(ObjectGuid& guid);
  286
+        template<BITS_4>
  287
+            void WriteGuidBytes(ObjectGuid& guid);
  288
+        template<BITS_5>
  289
+            void WriteGuidBytes(ObjectGuid& guid);
  290
+        template<BITS_6>
  291
+            void WriteGuidBytes(ObjectGuid& guid);
  292
+        template<BITS_7>
  293
+            void WriteGuidBytes(ObjectGuid& guid);
  294
+        template<BITS_8>
  295
+            void WriteGuidBytes(ObjectGuid& guid);
  296
+
209 297
         template <typename T> void put(size_t pos, T value)
210 298
         {
211 299
             EndianConvert(value);
@@ -425,7 +513,6 @@ class ByteBuffer
425 513
             return *this;
426 514
         }
427 515
 
428  
-
429 516
         template <typename T> T read()
430 517
         {
431 518
             T r = read<T>(_rpos);

0 notes on commit 6962e85

countof is not in the standard

Schmoozerd

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

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

Please sign in to comment.
Something went wrong with that request. Please try again.