Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cUID.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cUID_H
7 #define _INC_cUID_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "../GrayLibBase.h"
15 
16 namespace GrayLib
17 {
18  typedef HASHCODE32_t UID_t;
19 
20  class cUID
21  {
25 
26  protected:
29 
30  public:
32  static const UID_t k_CLEAR = ((UID_t)0);
33  static const UID_t k_BAD = ((UID_t)UINT_MAX);
34 
35  static const UID_t k_ROOT = ((UID_t)0x80000000);
36  static const UID_t k_CONTEXT = ((UID_t)0);
37 
38 #define cUID_PREFIX "#0"
39 #define cUID_FORMAT "#0%x"
40 
41  cUID(UID_t u) noexcept
42  : m_nUIDValue(u)
43  {
44  }
45 
46  static inline bool IsValidUID(UID_t uid) noexcept
47  {
49  return (uid != k_CLEAR) && (uid != k_BAD) ;
50  }
51  bool isValidUID() const noexcept
52  {
53  return IsValidUID(m_nUIDValue);
54  }
55  UID_t get_HashCode() const noexcept
56  {
57  return m_nUIDValue;
58  }
59  UID_t get_UIDValue() const noexcept
60  {
61  return m_nUIDValue;
62  }
63  operator UID_t() const noexcept
64  {
65  return m_nUIDValue;
66  }
67  };
68 
69  typedef int UIDINDEX_t;
70 
72  {
78 
79  public:
83 
84  public:
85  cUIDRangeMask(UID_t nUIDMaskType = 0x80000000, UID_t nUIDMaskIndex = 0x7FFFFFFF, UIDINDEX_t dwIndexStart = 1)
86  : m_nUIDMaskType(nUIDMaskType) // high bits.
87  , m_nUIDMaskIndex(nUIDMaskIndex) // index mask ex. 0xFFFF
88  , m_nIndexStart(dwIndexStart) // don't allow 0 to be a valid index normally.
89  {
92  ASSERT(isValidRangeMask());
93  ASSERT(IsValidIndex(m_nIndexStart));
94  }
95  void InitRangeMask(UID_t nUIDMaskType, UID_t nUIDMaskIndex, UIDINDEX_t dwIndexStart = 1)
96  {
97  m_nUIDMaskType = nUIDMaskType;
98  m_nUIDMaskIndex = nUIDMaskIndex;
99  m_nIndexStart = dwIndexStart;
100  ASSERT(isValidRangeMask());
101  ASSERT(IsValidIndex(m_nIndexStart));
102  }
103  bool isValidRangeMask() const noexcept
104  {
105  if ((m_nUIDMaskType & m_nUIDMaskIndex) != k_HASHCODE_CLEAR) // bits must not overlap.
106  {
107  return false;
108  }
109  return true;
110  }
111  bool IsValidQty(UIDINDEX_t index) const noexcept
112  {
113  if (((UID_t)index) > (UID_t)(m_nUIDMaskIndex - m_nIndexStart))
114  return false;
115  return true;
116  }
117  bool IsValidIndex(UIDINDEX_t index) const noexcept
118  {
120  if (index < m_nIndexStart)
121  return false;
122  if (((UID_t)index) >= m_nUIDMaskIndex) // NOT allowed to equal mask!
123  return false;
124  return true;
125  }
126  bool IsValidUID(UID_t uid) const noexcept
127  {
129  if ((uid & ~m_nUIDMaskIndex) != m_nUIDMaskType)
130  return false;
131  // Check index range as well ?
132  return IsValidIndex(U2I_NoCheck(uid));
133  }
134  UIDINDEX_t U2I_NoCheck(UID_t uid) const noexcept
135  {
137  return((UIDINDEX_t)(uid & m_nUIDMaskIndex));
138  }
139  UIDINDEX_t U2I(UID_t uid) const
140  {
142  ASSERT(IsValidUID(uid));
143  return U2I_NoCheck(uid);
144  }
145 
147  {
149  ASSERT(IsValidIndex(index));
150  return(((UID_t)index) | m_nUIDMaskType);
151  }
152  };
153 }
154 
155 #endif // _INC_cUID_H
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define ASSERT(exp)
Definition: cDebugAssert.h:87
Definition: cUID.h:72
UIDINDEX_t m_nIndexStart
Start of non static indexes. inside m_nUIDMaskIndex. typically 1.
Definition: cUID.h:82
void InitRangeMask(UID_t nUIDMaskType, UID_t nUIDMaskIndex, UIDINDEX_t dwIndexStart=1)
Definition: cUID.h:95
cUIDRangeMask(UID_t nUIDMaskType=0x80000000, UID_t nUIDMaskIndex=0x7FFFFFFF, UIDINDEX_t dwIndexStart=1)
Definition: cUID.h:85
UID_t m_nUIDMaskType
'or' this onto UID to mark type. high bits outside range.
Definition: cUID.h:80
bool isValidRangeMask() const noexcept
Definition: cUID.h:103
bool IsValidIndex(UIDINDEX_t index) const noexcept
Definition: cUID.h:117
UIDINDEX_t U2I_NoCheck(UID_t uid) const noexcept
Definition: cUID.h:134
bool IsValidUID(UID_t uid) const noexcept
Definition: cUID.h:126
bool IsValidQty(UIDINDEX_t index) const noexcept
Definition: cUID.h:111
UID_t m_nUIDMaskIndex
'and' bits for index range. ex. 0xffff
Definition: cUID.h:81
UIDINDEX_t U2I(UID_t uid) const
Definition: cUID.h:139
UID_t I2U(UIDINDEX_t index) const
Definition: cUID.h:146
Definition: cUID.h:21
UID_t m_nUIDValue
Definition: cUID.h:28
UID_t get_UIDValue() const noexcept
Definition: cUID.h:59
static const UID_t k_BAD
not valid assigned UID.
Definition: cUID.h:33
static bool IsValidUID(UID_t uid) noexcept
Definition: cUID.h:46
cUID(UID_t u) noexcept
Definition: cUID.h:41
static const UID_t k_CONTEXT
special hard coded UID for global cScriptContextObj. k_HASHCODE_CLEAR
Definition: cUID.h:36
bool isValidUID() const noexcept
Definition: cUID.h:51
static const UID_t k_ROOT
special hard coded UID for the root cXObject.
Definition: cUID.h:35
UID_t get_HashCode() const noexcept
Definition: cUID.h:55
static const UID_t k_CLEAR
Hard coded UID_t values.
Definition: cUID.h:32
Definition: cMesh.h:22
int UIDINDEX_t
non dressed (partial) UID_t. just an index in a range inside the UID_t. k_HASHCODE_CLEAR
Definition: cUID.h:69
HASHCODE32_t UID_t
Sort of the same as a HASHCODE_t. not the same as UIDINDEX_t.
Definition: cUID.h:18
const HASHCODE_t k_HASHCODE_CLEAR
not a valid index.
Definition: GrayCore.h:118
UINT32 HASHCODE32_t
always 32 bits.
Definition: GrayCore.h:117
uint16 index
Definition: sample3.cpp:29