Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cAtom.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cAtom_H
7 #define _INC_cAtom_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cString.h"
13 #include "HResult.h"
14 #include "cUnitTestDecl.h"
15 
16 namespace Gray
17 {
18  UNITTEST2_PREDEF(cAtom);
19 
21 
22 #define CATOM_STR(a) a
23 #define CATOM_CAT(a,b) a##b
24 #define CATOM_STATIC(a) #a
25 #define CATOM_STATSTR(a) #a
26 
28  {
32 
33  friend class cAtomRef;
34  friend class cAtomManager;
36 
37  private:
38  cStringA m_s;
39  ATOMCODE_t m_nHashCode;
40 
41  private:
42  cAtomDef(cStringA s) noexcept
43  : m_s(s)
44  , m_nHashCode(StrT::GetHashCode32<ATOMCHAR_t>(s, k_StrLen_UNK, 0))
45  {
46  // Private construct.
47  }
48 
49  public:
50  ATOMCODE_t get_HashCode() const noexcept
51  {
53  return m_nHashCode;
54  }
55  const ATOMCHAR_t* get_Name() const noexcept
56  {
57  return m_s;
58  }
59  };
61 
63  {
68 
69  friend class cAtomManager;
70  typedef cStringA STR_t;
71  typedef cAtomRef THIS_t;
73 
74  private:
75  cAtomDefPtr m_pDef;
76 
77  private:
78  static cAtomDefPtr GRAYCALL FindorCreateAtomStr(const ATOMCHAR_t* pszText);
79  static cAtomDefPtr GRAYCALL FindorCreateAtomStr(const STR_t& sText);
80 
81  explicit inline cAtomRef(cAtomDef* pDef) // cAtomManager only.
82  : m_pDef(pDef)
83  {
84  }
85 
86  void EmptyAtom(bool isLast);
87 
88  public:
89  cAtomRef(const THIS_t& ref) noexcept
90  : m_pDef(ref.m_pDef)
91  {
92  // copy
93  DEBUG_CHECK(m_pDef != nullptr);
94  }
95  cAtomRef(const STR_t& sName) noexcept
96  : m_pDef(FindorCreateAtomStr(sName))
97  {
98  DEBUG_CHECK(m_pDef != nullptr);
99  }
100  cAtomRef(const ATOMCHAR_t* pszName = "") noexcept
101  : m_pDef(FindorCreateAtomStr(pszName))
102  {
103  DEBUG_CHECK(m_pDef != nullptr);
104  }
106  {
107  EmptyAtom(true);
108  }
109 
110  size_t GetHeapStats(OUT ITERATE_t& iAllocCount) const;
111 
112  ATOMCODE_t get_HashCode() const noexcept
113  {
116  DEBUG_CHECK(m_pDef != nullptr);
117  return m_pDef->get_HashCode();
118  }
119 
120  const STR_t& get_StrA() const noexcept
121  {
122  return m_pDef->m_s;
123  }
124  const ATOMCHAR_t* get_CPtr() const noexcept
125  {
126  return m_pDef->m_s;
127  }
128  operator const ATOMCHAR_t* () const noexcept
129  {
130  return m_pDef->m_s;
131  }
132 
133  bool isValidCheck() const noexcept
134  {
135  return m_pDef != nullptr && m_pDef->m_s.isValidCheck();
136  }
137  bool IsEmpty() const
138  {
139  return m_pDef->m_s.IsEmpty();
140  }
142  {
143  return m_pDef->m_s.GetLength();
144  }
145 
146  bool operator==(const cAtomRef& atom) const noexcept
147  {
148  return(m_pDef == atom.m_pDef);
149  }
150  COMPARE_t CompareNoCase(const ATOMCHAR_t* pStr) const
151  {
152  return m_pDef->m_s.CompareNoCase(pStr);
153  }
154  bool operator==(const ATOMCHAR_t* pStr) const
155  {
156  return !CompareNoCase(pStr);
157  }
158 
159  const THIS_t& operator=(const THIS_t& atom)
160  {
161  if (m_pDef != atom.m_pDef)
162  {
163  EmptyAtom(true);
164  m_pDef = atom.m_pDef;
165  }
166  return *this;
167  }
168  const THIS_t& operator=(const ATOMCHAR_t* pStr)
169  {
170  if (CompareNoCase(pStr) != COMPARE_Equal)
171  {
172  EmptyAtom(true);
173  m_pDef = FindorCreateAtomStr(pStr);
174  }
175  return *this;
176  }
177  const THIS_t& operator=(const STR_t& sStr)
178  {
179  if (CompareNoCase(sStr) != COMPARE_Equal)
180  {
181  EmptyAtom(true);
182  m_pDef = FindorCreateAtomStr(sStr);
183  }
184  return *this;
185  }
186 
187  void EmptyAtom()
188  {
189  EmptyAtom(false);
190  }
191  void SetAtomStatic();
192 
193  static void GRAYCALL CreateStaticAtoms(const ATOMCHAR_t** ppAtoms);
194  static cAtomRef GRAYCALL FindAtomStr(const ATOMCHAR_t* pszText);
195  static cAtomRef GRAYCALL FindAtomHashCode(ATOMCODE_t idAtomCode);
196 
197  static HRESULT GRAYCALL CheckSymbolicStr(const ATOMCHAR_t* pszTag, bool bAllowDots = false);
198  static StrLen_t GRAYCALL GetSymbolicStr(OUT ATOMCHAR_t* pszTag, const ATOMCHAR_t* pszExp, bool bAllowDots = false);
199 
200 #ifdef _DEBUG
201  static HRESULT GRAYCALL DebugDumpFile(const FILECHAR_t* pszFilePath);
202 #endif
203  };
204 }
205 #endif // _INC_cAtom_H
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define DEBUG_CHECK(exp)
Definition: cDebugAssert.h:90
#define UNITTEST2_PREDEF(x)
Definition: cUnitTestDecl.h:19
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
Definition: cAtom.h:28
const ATOMCHAR_t * get_Name() const noexcept
Definition: cAtom.h:55
ATOMCODE_t get_HashCode() const noexcept
Definition: cAtom.h:50
Definition: cAtomManager.h:21
Definition: cAtom.h:63
cAtomRef(const STR_t &sName) noexcept
Definition: cAtom.h:95
bool IsEmpty() const
Definition: cAtom.h:137
StrLen_t GetLength() const
Definition: cAtom.h:141
const STR_t & get_StrA() const noexcept
Definition: cAtom.h:120
~cAtomRef()
Definition: cAtom.h:105
bool isValidCheck() const noexcept
Definition: cAtom.h:133
bool operator==(const cAtomRef &atom) const noexcept
Definition: cAtom.h:146
ATOMCODE_t get_HashCode() const noexcept
Definition: cAtom.h:112
COMPARE_t CompareNoCase(const ATOMCHAR_t *pStr) const
Definition: cAtom.h:150
const ATOMCHAR_t * get_CPtr() const noexcept
< as a C string
Definition: cAtom.h:124
const THIS_t & operator=(const STR_t &sStr)
Definition: cAtom.h:177
const THIS_t & operator=(const THIS_t &atom)
Definition: cAtom.h:159
bool operator==(const ATOMCHAR_t *pStr) const
Definition: cAtom.h:154
cAtomRef(const THIS_t &ref) noexcept
Definition: cAtom.h:89
void EmptyAtom()
Definition: cAtom.h:187
cAtomRef(const ATOMCHAR_t *pszName="") noexcept
Definition: cAtom.h:100
const THIS_t & operator=(const ATOMCHAR_t *pStr)
Definition: cAtom.h:168
Definition: cRefPtr.h:22
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
int COMPARE_t
result of compare. 0=same, 1=a>b, -1=a<b
Definition: cValT.h:17
const StrLen_t k_StrLen_UNK
use the default/current length of the string argument.
Definition: StrConst.h:34
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
HASHCODE32_t ATOMCODE_t
Encode a atom as a 32 bit hashcode instead of using its name/pointer. StrT::GetHashCode32()
Definition: cAtom.h:18
cRefPtr< cAtomDef > cAtomDefPtr
Definition: cAtom.h:60
int ITERATE_t
like size_t but signed
Definition: Index.h:28
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22
char ATOMCHAR_t
the char form (UNICODE or not) for an atom. (for symbolic names)
Definition: StrConst.h:15
UINT32 HASHCODE32_t
always 32 bits.
Definition: GrayCore.h:117
@ COMPARE_Equal
VARCMP_EQ.
Definition: cValT.h:23