OpenNI 1.5.4
XnStringsHash.h
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * OpenNI 1.x Alpha *
4 * Copyright (C) 2011 PrimeSense Ltd. *
5 * *
6 * This file is part of OpenNI. *
7 * *
8 * OpenNI is free software: you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published *
10 * by the Free Software Foundation, either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * OpenNI is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with OpenNI. If not, see <http://www.gnu.org/licenses/>. *
20 * *
21 ****************************************************************************/
22 #ifndef _XN_STRINGS_HASH_H
23 #define _XN_STRINGS_HASH_H
24 
25 //---------------------------------------------------------------------------
26 // Includes
27 //---------------------------------------------------------------------------
28 #include "XnHash.h"
29 #include <XnOS.h>
30 
31 //---------------------------------------------------------------------------
32 // Types
33 //---------------------------------------------------------------------------
35 {
36 public:
37  static XnHashValue Hash(const XnChar* const& key)
38  {
39  XnUInt32 nCRC = 0;
40  xnOSStrCRC32(key, &nCRC);
41 
42  // convert from UINT32 to XnHashValue
43  return nCRC % (1 << (sizeof(XnHashValue)*8));
44  }
45 
46  static XnInt32 Compare(const XnChar* const& key1, const XnChar* const& key2)
47  {
48  return strcmp(key1, key2);
49  }
50 };
51 
53 {
54 public:
55  static XnValue CreateValueCopy(const XnChar* const& orig)
56  {
57  // we should copy string, so we can keep the key
58  XnUInt32 nLen = xnOSStrLen(orig) + 1; // with null termination
59  XnChar* pcKey = (XnChar*)xnOSMalloc(nLen);
60  xnOSStrCopy(pcKey, orig, nLen);
61  return (pcKey);
62  }
63 
64  static void FreeValue(XnValue& Value)
65  {
66  XnChar* pcKey = (XnChar*)Value;
67  xnOSFree(pcKey);
68  }
69 
70  static XnValue GetAsValue(const XnChar* const& orig)
71  {
72  return (XnValue)orig;
73  }
74 
75  static const XnChar* const& GetFromValue(const XnValue& Value)
76  {
77  return (const XnChar* const&)Value;
78  }
79 
80  static const XnChar*& GetFromValue(XnValue& Value)
81  {
82  return (const XnChar*&)Value;
83  }
84 };
85 
90 #define XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR_DECL(decl, ValueType, ClassName, ValueTranslator) \
91  XN_DECLARE_HASH_DECL(decl, const XnChar*, ValueType, ClassName, XnStringsKeyTranslator, ValueTranslator, XnStringsKeyManager) \
92 
93 
97 #define XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR(ValueType, ClassName, ValueTranslator) \
98  XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR_DECL(, ValueType, ClassName, ValueTranslator)
99 
104 #define XN_DECLARE_STRINGS_HASH_DECL(decl, ValueType, ClassName) \
105  XN_DECLARE_DEFAULT_VALUE_TRANSLATOR_DECL(decl, ValueType, XN_DEFAULT_TRANSLATOR_NAME(ClassName)) \
106  XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR_DECL(decl, ValueType, ClassName, XN_DEFAULT_TRANSLATOR_NAME(ClassName)) \
107 
108 
112 #define XN_DECLARE_STRINGS_HASH(ValueType, ClassName) \
113  XN_DECLARE_STRINGS_HASH_DECL(, ValueType, ClassName)
114 
115 XN_DECLARE_STRINGS_HASH(XnValue, XnStringsHash)
116 
117 
118 #endif //_XN_STRINGS_HASH_H
XnHashValue
XnUInt8 XnHashValue
Definition: XnHash.h:45
XnOS.h
XnStringsKeyManager::Compare
static XnInt32 Compare(const XnChar *const &key1, const XnChar *const &key2)
Definition: XnStringsHash.h:65
xnOSMalloc
XN_C_API void *XN_C_DECL xnOSMalloc(const XnSizeT nAllocSize)
xnOSStrLen
XN_C_API XnUInt32 XN_C_DECL xnOSStrLen(const XnChar *cpStr)
XnStringsKeyManager::Hash
static XnHashValue Hash(const XnChar *const &key)
Definition: XnStringsHash.h:56
xnOSFree
XN_C_API void XN_C_DECL xnOSFree(const void *pMemBlock)
XnStringsKeyTranslator::FreeValue
static void FreeValue(XnValue &Value)
Definition: XnStringsHash.h:64
XnStringsKeyTranslator::GetFromValue
static const XnChar *const & GetFromValue(const XnValue &Value)
Definition: XnStringsHash.h:75
XN_DECLARE_STRINGS_HASH
#define XN_DECLARE_STRINGS_HASH(ValueType, ClassName)
Definition: XnStringsHash.h:112
XnStringsKeyTranslator::CreateValueCopy
static XnValue CreateValueCopy(const XnChar *const &orig)
Definition: XnStringsHash.h:55
XnStringsKeyTranslator::GetAsValue
static XnValue GetAsValue(const XnChar *const &orig)
Definition: XnStringsHash.h:70
XnHash.h
xnOSStrCopy
XN_C_API XnStatus XN_C_DECL xnOSStrCopy(XnChar *cpDestString, const XnChar *cpSrcString, const XnUInt32 nDestLength)
XnStringsKeyManager
Definition: XnStringsHash.h:34
XnStringsKeyTranslator
Definition: XnStringsHash.h:52
xnOSStrCRC32
XN_C_API XnStatus XN_C_DECL xnOSStrCRC32(const XnChar *cpString, XnUInt32 *nCRC32)
XnStringsKeyTranslator::GetFromValue
static const XnChar *& GetFromValue(XnValue &Value)
Definition: XnStringsHash.h:80
XnValue
void * XnValue
Definition: XnDataTypes.h:36