Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cVarMap.h
Go to the documentation of this file.
1 //
5 //
6 
7 #ifndef _INC_cVarMap_H
8 #define _INC_cVarMap_H
9 #ifndef NO_PRAGMA_ONCE
10 #pragma once
11 #endif
12 
13 #include "cVariant.h"
14 #include "../Scriptable/IScriptableObj.h"
15 #ifdef USE_ISCRIPTABLE
16 #include "../Scriptable/cScriptableInterface.h"
17 #endif
18 #include "GrayCore/include/cAtom.h"
22 
23 namespace GrayLib
24 {
26 
28  : public cRefBase
29  , public cHeapObject
30  , public CObject
31  , public IScriptableSetter // for set from database.
32  , public cNonCopyable
33  {
40 
41  protected:
43  private:
44  cAtomRef m_aKey;
45  public:
47 
48  public:
50  : m_aKey(aKey)
51  {
52  }
53  cVarTuple(cAtomRef aKey, const cVariant& vVal)
54  : m_aKey(aKey), m_vVal(vVal)
55  {
56  }
57  virtual ~cVarTuple()
58  {
59  }
60 
61  virtual bool isValidCheck() const noexcept override // memory allocation and structure definitions are valid.
62  {
63  if (!COBJECT_IsValidCheck())
64  return false;
66  return false;
67  if (!m_aKey.isValidCheck())
68  return false;
69  if (!IS_TYPE_OF(cVarTuple, this)) // structure definitions are valid.
70  return false;
71  return true;
72  }
73 
74  ATOMCODE_t get_HashCode() const noexcept
75  {
76  DEBUG_CHECK(isValidCheck());
77  return m_aKey.get_HashCode();
78  }
79  cAtomRef get_KeyA() const noexcept
80  {
81  DEBUG_CHECK(isValidCheck());
82  return m_aKey;
83  }
85  {
86  m_vVal.SetNoType();
87  return S_OK;
88  }
89 
90  size_t GetHeapStats(OUT ITERATE_t& iAllocCount) const
91  {
93  return m_vVal.GetHeapStats(iAllocCount) + m_aKey.GetHeapStats(iAllocCount);
94  }
95 
96  HRESULT SerializeOutput(cStreamOutput& file) const;
97 
98  // If loading this as a NAME=VAL
99  STDMETHOD(s_Put)(const ATOMCHAR_t* pszPropName, const cVariant& vValSet) override;
100  };
101 
103 
105  : public IScriptableEnumerator
106  , public IScriptableGetter
107  , public IScriptableSetter
108  , public cVariantCommon
109  {
114  public:
116 
117  static const ATOMCHAR_t* k_pszKeyDefault;
118 
119 #ifdef USE_ISCRIPTABLE
121  enum M_TYPE_
122  {
123 #define cVarMapMethod(a,b,c,d) M_##a,
124 #include "cVarMapMethods.tbl"
125 #undef cVarMapMethod
127  };
128  static cScriptableMethod sm_Methods[M_QTY + 1];
129 #endif
130 
131  private:
132  HRESULT DumpTags(cStreamOutput* pOut, const ATOMCHAR_t* pszFormat, bool bValue) const;
133  HRESULT s_LoadTagType(const cVariant& vVal, VARTYPE_TYPE eVarTypeChange = VARTYPE_NoType);
134 
135  public:
137  {
138  }
139  cVarMap(const cVarMap& rMap)
140  : m_aTuples(rMap.m_aTuples)
141  {
142  // Copy
143  }
144 
145  // Parse the key name. Is it legal? Is an array? [] = find unused array entry.
146  static cAtomRef GRAYCALL MakeKeyNameRef(const ATOMCHAR_t*& pszKey, bool bMustExist, bool bAllowDots );
147 
148  size_t GetHeapStats(OUT ITERATE_t& iAllocCount) const;
149  HRESULT SerializeOutput(cStreamOutput& file) const;
150  HRESULT SerializeInput(cStreamInput& file);
151 
153  {
154  return m_aTuples.GetSize();
155  }
156 
158  {
159  return m_aTuples.GetAt(index);
160  }
161 
163  {
164  // Get just the value. ignore the tag name.
165  if (m_aTuples.IsValidIndex(index))
166  return m_aTuples.GetAt(index)->m_vVal;
167  return cVariant::k_VarNULL;
168  }
169 
170  // Search for a key.
171  cVarTuplePtr FindKeyPtrA(cAtomRef aKey) const;
172  cVarTuplePtr FindKeyPtr(const ATOMCHAR_t* pszKey) const;
173  cVarTuplePtr MakeKeyPtrA(cAtomRef aKey);
174  cVarTuplePtr MakeKeyPtr(const ATOMCHAR_t* pszKey);
175 
176  cVarTuplePtr FindKeyPtrX(const ATOMCHAR_t* pszKeyX) const;
177  cVarTuplePtr MakeKeyPtrX(const ATOMCHAR_t* pszKeyX);
178 
179  bool RemoveKeyA(cAtomRef aKey);
180  bool RemoveKey(const ATOMCHAR_t* pszKey);
181 
182  // Set/Create the Vars
183  HRESULT SetValA(cAtomRef aKey, const cVariant& vVal);
184  HRESULT SetValNULL(cAtomRef aKey);
185  HRESULT SetValVarN(const ATOMCHAR_t* pszKey, const cVariant& vVal);
186  HRESULT SetValVar(const ATOMCHAR_t* pszKey, const cVariant& vVal);
187  HRESULT SetValStr(const ATOMCHAR_t* pszKey, const ATOMCHAR_t* pszVal);
188 
189  // Get values for vars.
190  // Just get it's value immediately. default to 0 or "" if missing.
191  const cVariant& GetValVar(const ATOMCHAR_t* pszKey) const
192  {
193  cVarTuplePtr pVar = FindKeyPtr(pszKey);
194  if (pVar != nullptr)
195  return pVar->m_vVal;
196  return cVariant::k_VarNULL;
197  }
198  const cVariant& operator[](const ATOMCHAR_t* pszKey) const
199  {
200  return GetValVar(pszKey);
201  }
202 
203  cVariant& RefValVar(const ATOMCHAR_t* pszKey);
205  {
206  return RefValVar(pszKey);
207  }
208 
209  cStringA GetValAsStr(const ATOMCHAR_t* pszKey) const;
210  UINT32 GetValAsDWord(const ATOMCHAR_t* pszKey, UINT32 uValDefault = 0) const;
211  int GetValAsInt(const ATOMCHAR_t* pszKey, int iValDefault = 0) const;
212  float GetValAsFloat(const ATOMCHAR_t* pszKey, float fValDefault = 0.0f) const;
213 
214  // IScriptableSetter, IScriptableGetter
215  STDMETHOD(s_GetEnum)(ITERATE_t i, OUT cStringA& rsPropName, OUT cVariant& rvValRet);
216  STDMETHOD(s_GetByName)(const ATOMCHAR_t* pszPropName, OUT cVariant& vValRet);
217  STDMETHOD(s_Put)(const ATOMCHAR_t* pszPropName, const cVariant& vValSet);
218 
219  // HTML parsing helpers
220  StrLen_t AddHtmlArgsLen(const ATOMCHAR_t* pszArgs);
221 
222  // Search for a value (not a key)
223  ITERATE_t FindValAsStr(const ATOMCHAR_t* pszVal) const;
224  ITERATE_t FindValAsInt(int iVal, const ATOMCHAR_t* pszPrefix = nullptr) const;
225 
226  // Use these to extend a IScriptableObj
227  void s_WriteTags(IScriptableSetter& s, const ATOMCHAR_t* pszKeyPrefix = nullptr) const;
228  ITERATE_t AddTuple(cVarTuple* pVar);
229 
230 #ifdef USE_ISCRIPTABLE
231  HRESULT s_InvokeExtProp(const cVariant& vArgs, cVariant& vValRet);
233 #endif
234 
235  HRESULT DumpTagsHead(cStreamOutput* pOut, const ATOMCHAR_t* pszName) const;
236  void FixWeird();
237 
238  StrLen_t GetStr(ATOMCHAR_t* pszArgs, StrLen_t nLenMax, const ATOMCHAR_t* pszLineBreak = nullptr) const;
239  HRESULT SetStr(const ATOMCHAR_t* pszArgs);
240  COMPARE_t Compare(const cVarMap* pMap) const;
241 
242  HRESULT SetFromArrays(int iQty, const char** ppszNames, const char** ppszVals);
243 
245  };
246 
247  class GRAYLIB_LINK cVariantMap : public cVarMap, public cRefBase
248  {
252 
253  public:
255  {
256  }
257  cVariantMap(const cVarMap& rMap)
258  : cVarMap(rMap)
259  {
260  }
261  };
262 }
263 
264 #ifdef GRAY_DLL // force implementation/instantiate for DLL/SO.
265 namespace Gray
266 {
267  template class GRAYLIB_LINK CArray< cRefPtr<GrayLib::cVarTuple>, GrayLib::cVarTuple* >;
268  template class GRAYLIB_LINK cArrayTyped< cRefPtr<GrayLib::cVarTuple>, GrayLib::cVarTuple* >;
269  template class GRAYLIB_LINK cArraySorted< cRefPtr<GrayLib::cVarTuple>, GrayLib::cVarTuple*, ATOMCODE_t >;
270  template class GRAYLIB_LINK cArraySortRef<GrayLib::cVarTuple, ATOMCODE_t>;
271  template class GRAYLIB_LINK cArraySortHash<GrayLib::cVarTuple, ATOMCODE_t>;
272 }
273 #endif
274 
275 #endif // _INC_cVarMap_H
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define IS_TYPE_OF(t, p)
Definition: PtrCast.h:23
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define DEBUG_CHECK(exp)
Definition: cDebugAssert.h:90
#define COBJECT_IsValidCheck()
Definition: cObject.h:64
#define CSCRIPT_CLASS_DEF0(TYPE)
Definition: cScriptableInterface.h:271
Definition: cScriptableInterface.h:97
Definition: cVarMap.h:109
cArraySortHash< cVarTuple, ATOMCODE_t > m_aTuples
Hash sorted by ATOMCODE_t. NOT alphabetic sort.
Definition: cVarMap.h:115
cVarMap()
Definition: cVarMap.h:136
M_TYPE_
Definition: cVarMap.h:122
@ M_QTY
Definition: cVarMap.h:126
HRESULT LoadTuple(IScriptableEnumerator &section)
cVarMap(const cVarMap &rMap)
Definition: cVarMap.h:139
cVariant & operator[](const ATOMCHAR_t *pszKey)
Definition: cVarMap.h:204
const cVariant & GetValVar(const ATOMCHAR_t *pszKey) const
Definition: cVarMap.h:191
static const ATOMCHAR_t * k_pszKeyDefault
Default name for keys with not valid names. "_" (or no name)
Definition: cVarMap.h:117
ITERATE_t get_KeyQty() const
Definition: cVarMap.h:152
cVarTuplePtr GetMapElement(ITERATE_t index) const
Definition: cVarMap.h:157
const cVariant & GetMapElementVal(ITERATE_t index) const
Definition: cVarMap.h:162
UNITTEST2_PREDEF(cVarMap)
const cVariant & operator[](const ATOMCHAR_t *pszKey) const
Definition: cVarMap.h:198
cVarTuplePtr FindKeyPtrX(const ATOMCHAR_t *pszKeyX) const
HRESULT s_InvokeExtProp(const cVariant &vArgs, cVariant &vValRet)
Definition: cVarMap.h:33
virtual bool isValidCheck() const noexcept override
Definition: cVarMap.h:61
cVariant m_vVal
Equate the m_aKey name to this cVariant Val.
Definition: cVarMap.h:46
size_t GetHeapStats(OUT ITERATE_t &iAllocCount) const
Definition: cVarMap.h:90
CHEAPOBJECT_IMPL
Definition: cVarMap.h:42
ATOMCODE_t get_HashCode() const noexcept
Definition: cVarMap.h:74
cAtomRef get_KeyA() const noexcept
Definition: cVarMap.h:79
virtual ~cVarTuple()
Definition: cVarMap.h:57
HRESULT DisposeThis()
Definition: cVarMap.h:84
cVarTuple(cAtomRef aKey, const cVariant &vVal)
Definition: cVarMap.h:53
cVarTuple(cAtomRef aKey)
Definition: cVarMap.h:49
Definition: cVarMap.h:248
cVariantMap(const cVarMap &rMap)
Definition: cVarMap.h:257
cVariantMap()
Definition: cVarMap.h:254
Definition: cVariant.h:26
static const cVariant k_VarNULL
VARTYPE_NoType isNoType()
Definition: cVariant.h:39
size_t GetHeapStats(OUT ITERATE_t &iAllocCount) const
Definition: cVariant.cpp:74
void SetNoType()
empty example of the type.
Definition: cVariant.h:208
ITERATE_t GetSize() const noexcept
Definition: cArray.h:137
Definition: cObject.h:67
REF_t GetAt(ITERATE_t index) const noexcept
Definition: cArraySort.h:440
bool IsValidIndex(ITERATE_t i) const noexcept
Definition: cArraySort.h:432
Definition: cArraySortRef.h:60
Definition: cAtom.h:63
bool isValidCheck() const noexcept
Definition: cAtom.h:133
ATOMCODE_t get_HashCode() const noexcept
Definition: cAtom.h:112
size_t GetHeapStats(OUT ITERATE_t &iAllocCount) const
Definition: cAtom.cpp:171
Definition: cHeapObject.h:38
virtual bool isValidCheck() const noexcept
Definition: cHeapObject.h:94
Definition: cNonCopyable.h:17
Definition: cRefPtr.h:22
Definition: cRefPtr.h:225
Definition: cStream.h:306
Definition: cStream.h:126
Definition: IScriptableObj.h:38
Definition: IScriptableObj.h:51
Definition: IScriptableObj.h:62
Definition: cMesh.h:22
VARTYPE_TYPE
< define types of structure/record elements. (COM uses VARTYPE=VARENUM for this) stored as BYTE
Definition: cVariantType.h:19
@ VARTYPE_NoType
Definition: cVariantType.h:24
UNITTEST2_PREDEF(cQuadtree)
cRefPtr< cVarTuple > cVarTuplePtr
Definition: cVarMap.h:102
< 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
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
int ITERATE_t
like size_t but signed
Definition: Index.h:28
char ATOMCHAR_t
the char form (UNICODE or not) for an atom. (for symbolic names)
Definition: StrConst.h:15
uint16 index
Definition: sample3.cpp:29
Definition: cVariantData.h:37