OpenNI 1.5.7
XnStringsHash.h
Go to the documentation of this file.
1 /*****************************************************************************
2 * *
3 * OpenNI 1.x Alpha *
4 * Copyright (C) 2012 PrimeSense Ltd. *
5 * *
6 * This file is part of OpenNI. *
7 * *
8 * Licensed under the Apache License, Version 2.0 (the "License"); *
9 * you may not use this file except in compliance with the License. *
10 * You may obtain a copy of the License at *
11 * *
12 * http://www.apache.org/licenses/LICENSE-2.0 *
13 * *
14 * Unless required by applicable law or agreed to in writing, software *
15 * distributed under the License is distributed on an "AS IS" BASIS, *
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
17 * See the License for the specific language governing permissions and *
18 * limitations under the License. *
19 * *
20 *****************************************************************************/
21 #ifndef _XN_STRINGS_HASH_H
22 #define _XN_STRINGS_HASH_H
23 
24 //---------------------------------------------------------------------------
25 // Includes
26 //---------------------------------------------------------------------------
27 #include "XnHash.h"
28 #include <XnOS.h>
29 
30 //---------------------------------------------------------------------------
31 // Types
32 //---------------------------------------------------------------------------
34 {
35 public:
36  static XnHashValue Hash(const XnChar* const& key)
37  {
38  XnUInt32 nCRC = 0;
39  xnOSStrCRC32(key, &nCRC);
40 
41  // convert from UINT32 to XnHashValue
42  return nCRC % (1 << (sizeof(XnHashValue)*8));
43  }
44 
45  static XnInt32 Compare(const XnChar* const& key1, const XnChar* const& key2)
46  {
47  return strcmp(key1, key2);
48  }
49 };
50 
52 {
53 public:
54  static XnValue CreateValueCopy(const XnChar* const& orig)
55  {
56  // we should copy string, so we can keep the key
57  XnUInt32 nLen = xnOSStrLen(orig) + 1; // with null termination
58  XnChar* pcKey = (XnChar*)xnOSMalloc(nLen);
59  xnOSStrCopy(pcKey, orig, nLen);
60  return (pcKey);
61  }
62 
63  static void FreeValue(XnValue& Value)
64  {
65  XnChar* pcKey = (XnChar*)Value;
66  xnOSFree(pcKey);
67  }
68 
69  static XnValue GetAsValue(const XnChar* const& orig)
70  {
71  return (XnValue)orig;
72  }
73 
74  static const XnChar* const& GetFromValue(const XnValue& Value)
75  {
76  return (const XnChar* const&)Value;
77  }
78 
79  static const XnChar*& GetFromValue(XnValue& Value)
80  {
81  return (const XnChar*&)Value;
82  }
83 };
84 
89 #define XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR_DECL(decl, ValueType, ClassName, ValueTranslator) \
90  XN_DECLARE_HASH_DECL(decl, const XnChar*, ValueType, ClassName, XnStringsKeyTranslator, ValueTranslator, XnStringsKeyManager) \
91 
92 
96 #define XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR(ValueType, ClassName, ValueTranslator) \
97  XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR_DECL(, ValueType, ClassName, ValueTranslator)
98 
103 #define XN_DECLARE_STRINGS_HASH_DECL(decl, ValueType, ClassName) \
104  XN_DECLARE_DEFAULT_VALUE_TRANSLATOR_DECL(decl, ValueType, XN_DEFAULT_TRANSLATOR_NAME(ClassName)) \
105  XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR_DECL(decl, ValueType, ClassName, XN_DEFAULT_TRANSLATOR_NAME(ClassName)) \
106 
107 
111 #define XN_DECLARE_STRINGS_HASH(ValueType, ClassName) \
112  XN_DECLARE_STRINGS_HASH_DECL(, ValueType, ClassName)
113 
114 XN_DECLARE_STRINGS_HASH(XnValue, XnStringsHash)
115 
116 
117 #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:64
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:55
xnOSFree
XN_C_API void XN_C_DECL xnOSFree(const void *pMemBlock)
XnStringsKeyTranslator::FreeValue
static void FreeValue(XnValue &Value)
Definition: XnStringsHash.h:63
XnStringsKeyTranslator::GetFromValue
static const XnChar *const & GetFromValue(const XnValue &Value)
Definition: XnStringsHash.h:74
XN_DECLARE_STRINGS_HASH
#define XN_DECLARE_STRINGS_HASH(ValueType, ClassName)
Definition: XnStringsHash.h:111
XnStringsKeyTranslator::CreateValueCopy
static XnValue CreateValueCopy(const XnChar *const &orig)
Definition: XnStringsHash.h:54
XnStringsKeyTranslator::GetAsValue
static XnValue GetAsValue(const XnChar *const &orig)
Definition: XnStringsHash.h:69
XnHash.h
xnOSStrCopy
XN_C_API XnStatus XN_C_DECL xnOSStrCopy(XnChar *cpDestString, const XnChar *cpSrcString, const XnUInt32 nDestLength)
XnStringsKeyManager
Definition: XnStringsHash.h:33
XnStringsKeyTranslator
Definition: XnStringsHash.h:51
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:79
XnValue
void * XnValue
Definition: XnDataTypes.h:35