TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::GUniqueID Class Reference

#include <GUniqueID.h>

Public Member Functions

 GUniqueID ()
 
GUniqueIDoperator= (const Any &a)
 
 GUniqueID (const Any &a)
 
Any toAny () const
 
std::string toString16 () const
 
bool uninitialized () const
 
uint16 tag () const
 
 operator uint64 () const
 
bool operator== (const GUniqueID &other) const
 
bool operator!= (const GUniqueID &other) const
 
void serialize (class BinaryOutput &b) const
 
void deserialize (class BinaryInput &b)
 
void serialize (class TextOutput &t) const
 
void deserialize (class TextInput &t)
 

Static Public Member Functions

static GUniqueID fromString16 (const std::string &s)
 
static GUniqueID NONE (uint16 tag)
 
static GUniqueID create (uint16 tag=0)
 

Private Attributes

uint64 id
 

Detailed Description

Globally unique identifiers. The probability of two different programs generating the same value from UniqueID::create is vanishingly small.

UniqueIDs optionally contain a 10-bit application specific tag that distinguishes their type.

Constructor & Destructor Documentation

G3D::GUniqueID::GUniqueID ( )
inline
See also
create
31 : id(0) {}
uint64 id
Definition: GUniqueID.h:26

+ Here is the caller graph for this function:

G3D::GUniqueID::GUniqueID ( const Any a)
inline
See also
create
36  {
37  *this = a;
38  }

Member Function Documentation

GUniqueID G3D::GUniqueID::create ( uint16  tag = 0)
static

Create a new ID

83  {
84  static uint64 counter = 0;
85  static uint64 systemID = 0;
86 
87  if (systemID == 0) {
88  // Create a unique ID for this machine/program instance
89 
90  // TODO: see ioctl(skfd, SIOCGIFHWADDR, &if_hwaddr)
91  Array<NetAddress> addr;
93  if (addr.size() > 0) {
94  systemID |= addr[0].ip();
95  }
96 
97  union {
98  float64 ft;
99  uint64 ut;
100  };
101  ft = System::time();
102  systemID = ut << 22;
103  systemID ^= ((uint64)iRandom(0, 32768)) << 8;
104 
105  systemID &= ~((uint64)1023 << 54);
106 
107  // Ensure that the systemID is non-zero (vanishingly small probability)
108  if (systemID == 0) {
109  systemID = 1;
110  }
111  }
112 
113  // No need for modulo; we'll all be dead before this counter
114  // overflows 54 bits
115  ++counter;
116 
117  GUniqueID i;
118 
119  i.id = (((uint64)(tag & 1023)) << 54) | (counter ^ systemID);
120 
121  return i;
122 }
double float64
Definition: g3dmath.h:173
GUniqueID()
Definition: GUniqueID.h:31
uint64_t uint64
Definition: g3dmath.h:170
void localHostAddresses(Array< NetAddress > &array) const
Definition: NetworkDevice.cpp:515
uint64_t uint64
Definition: Define.h:149
static RealTime time()
Definition: System.cpp:934
uint16 tag() const
Definition: GUniqueID.h:55
int iRandom(int low, int hi)
Definition: g3dmath.cpp:110
static NetworkDevice * instance()
Definition: NetworkDevice.cpp:109

+ Here is the call graph for this function:

void G3D::GUniqueID::deserialize ( class BinaryInput b)
38  {
39  id = b.readUInt64();
40 }

+ Here is the call graph for this function:

void G3D::GUniqueID::deserialize ( class TextInput t)
49  {
50  t.readSymbol("(");
51  id = (((uint64)t.readNumber()) << 32) + (uint64)t.readNumber();
52  t.readSymbol(")");
53 }
uint64_t uint64
Definition: g3dmath.h:170

+ Here is the call graph for this function:

GUniqueID G3D::GUniqueID::fromString16 ( const std::string &  s)
static
69  {
70  if (s.length() != 16) {
71  debugAssertM(false, "Corrupt 16-character string");
72  return GUniqueID();
73  }
74 
75  uint32 high = 0, low = 0;
76  sscanf(s.c_str(), "%08x%08x", &high, &low);
77  GUniqueID i;
78  i.id = (uint64(high) << 32) | low;
79  return i;
80 }
GUniqueID()
Definition: GUniqueID.h:31
uint64_t uint64
Definition: g3dmath.h:170
#define debugAssertM(exp, message)
Definition: debugAssert.h:161
uint32_t uint32
Definition: Define.h:150

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

GUniqueID G3D::GUniqueID::NONE ( uint16  tag)
static

Returns the ID that has the specified tag (so that it is not uninitialized), but which is a common sentinel "none" value.

56  {
57  GUniqueID i;
58  uint64 t = tag;
59  i.id = (t << 54);
60  return i;
61 }
GUniqueID()
Definition: GUniqueID.h:31
uint64_t uint64
Definition: Define.h:149
uint16 tag() const
Definition: GUniqueID.h:55

+ Here is the call graph for this function:

G3D::GUniqueID::operator uint64 ( ) const
inline
59  {
60  return id;
61  }
uint64 id
Definition: GUniqueID.h:26
bool G3D::GUniqueID::operator!= ( const GUniqueID other) const
inline
67  {
68  return id != other.id;
69  }
GUniqueID & G3D::GUniqueID::operator= ( const Any a)
15  {
16  a.verifyName("GUniqueID");
17  a.verifyType(Any::ARRAY);
18  a.verifySize(1);
19  std::string s = a[0];
20  a.verify(s.length() == 16);
22  return *this;
23 }
Definition: Any.h:187
static GUniqueID fromString16(const std::string &s)
Definition: GUniqueID.cpp:69

+ Here is the call graph for this function:

bool G3D::GUniqueID::operator== ( const GUniqueID other) const
inline
63  {
64  return id == other.id;
65  }
void G3D::GUniqueID::serialize ( class BinaryOutput b) const
33  {
34  b.writeUInt64(id);
35 }

+ Here is the call graph for this function:

void G3D::GUniqueID::serialize ( class TextOutput t) const
42  {
43  t.writeSymbol("(");
44  t.writeNumber((double)(id >> 32));
45  t.writeNumber((double)(id & 0xFFFFFFFF));
46  t.writeSymbol(")");
47 }

+ Here is the call graph for this function:

uint16 G3D::GUniqueID::tag ( ) const
inline
55  {
56  return id >> 54;
57  }

+ Here is the caller graph for this function:

Any G3D::GUniqueID::toAny ( ) const
26  {
27  Any a(Any::ARRAY, "GUniqueID");
28  a.append(toString16());
29  return a;
30 }
Definition: Any.h:187
std::string toString16() const
Definition: GUniqueID.cpp:64

+ Here is the call graph for this function:

std::string G3D::GUniqueID::toString16 ( ) const

Returns a 16-character string equivalent to this GUniqueID's uint64 value.

64  {
65  return format("%08x%08x", uint32(id >> 32), uint32(id & 0xFFFFFFFF));
66 }
std::string __cdecl format(const char *fmt...) G3D_CHECK_PRINTF_ARGS
uint32_t uint32
Definition: g3dmath.h:168

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool G3D::GUniqueID::uninitialized ( ) const
inline
51  {
52  return id == 0;
53  }

Member Data Documentation

uint64 G3D::GUniqueID::id
private

The documentation for this class was generated from the following files: