summaryrefslogtreecommitdiffstats
path: root/tests/kinectsurface/QtKinectWrapper/OpenNI/Include/XnStringsHash.h
blob: ec8751335ee6f5906fe8d613aea51afd60215871 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/****************************************************************************
*                                                                           *
*  OpenNI 1.x Alpha                                                         *
*  Copyright (C) 2011 PrimeSense Ltd.                                       *
*                                                                           *
*  This file is part of OpenNI.                                             *
*                                                                           *
*  OpenNI is free software: you can redistribute it and/or modify           *
*  it under the terms of the GNU Lesser General Public License as published *
*  by the Free Software Foundation, either version 3 of the License, or     *
*  (at your option) any later version.                                      *
*                                                                           *
*  OpenNI is distributed in the hope that it will be useful,                *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             *
*  GNU Lesser General Public License for more details.                      *
*                                                                           *
*  You should have received a copy of the GNU Lesser General Public License *
*  along with OpenNI. If not, see <http://www.gnu.org/licenses/>.           *
*                                                                           *
****************************************************************************/
#ifndef _XN_STRINGS_HASH_H
#define _XN_STRINGS_HASH_H

//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include "XnHash.h"
#include <XnOS.h>

//---------------------------------------------------------------------------
// Types
//---------------------------------------------------------------------------
class XnStringsKeyManager
{
public:
	static XnHashValue Hash(const XnChar* const& key)
	{
		XnUInt32 nCRC = 0;
		xnOSStrCRC32(key, &nCRC);

		// convert from UINT32 to XnHashValue
		return nCRC % (1 << (sizeof(XnHashValue)*8));
	}

	static XnInt32 Compare(const XnChar* const& key1, const XnChar* const& key2)
	{
		return strcmp(key1, key2);
	}
};

class XnStringsKeyTranslator
{
public:
	static XnValue CreateValueCopy(const XnChar* const& orig)
	{
		// we should copy string, so we can keep the key
		XnUInt32 nLen = xnOSStrLen(orig) + 1; // with null termination
		XnChar* pcKey = (XnChar*)xnOSMalloc(nLen);
		xnOSStrCopy(pcKey, orig, nLen);
		return (pcKey);
	}

	static void FreeValue(XnValue& Value)
	{
		XnChar* pcKey = (XnChar*)Value;
		xnOSFree(pcKey);
	}

	static XnValue GetAsValue(const XnChar* const& orig)
	{
		return (XnValue)orig;
	}

	static const XnChar* const& GetFromValue(const XnValue& Value)
	{
		return (const XnChar* const&)Value;
	}

	static const XnChar*& GetFromValue(XnValue& Value)
	{
		return (const XnChar*&)Value;
	}
};

/**
* Declares a hash table from strings to @a ValueType that's named @a ClassName and uses @a ValueTranslator
* to translate values. It is declared using the @a decl declspec.
*/
#define XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR_DECL(decl, ValueType, ClassName, ValueTranslator)								\
	XN_DECLARE_HASH_DECL(decl, const XnChar*, ValueType, ClassName, XnStringsKeyTranslator, ValueTranslator, XnStringsKeyManager)	\

/**
* Declares a hash table from strings to @a ValueType that's named @a ClassName and uses @a ValueTranslator
* to translate values.
*/
#define XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR(ValueType, ClassName, ValueTranslator)								\
	XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR_DECL(, ValueType, ClassName, ValueTranslator)

/**
* Declares a hash table from strings to @a ValueType that's named @a ClassName and uses default translator
* to translate values. It is declared using the @a decl declspec.
*/
#define XN_DECLARE_STRINGS_HASH_DECL(decl, ValueType, ClassName)														\
	XN_DECLARE_DEFAULT_VALUE_TRANSLATOR_DECL(decl, ValueType, XN_DEFAULT_TRANSLATOR_NAME(ClassName))					\
	XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR_DECL(decl, ValueType, ClassName, XN_DEFAULT_TRANSLATOR_NAME(ClassName))	\

/**
* Declares a hash table from strings to @a ValueType that's named @a ClassName and uses default translator
* to translate values.
*/
#define XN_DECLARE_STRINGS_HASH(ValueType, ClassName)		\
	XN_DECLARE_STRINGS_HASH_DECL(, ValueType, ClassName)

XN_DECLARE_STRINGS_HASH(XnValue, XnStringsHash)


#endif //_XN_STRINGS_HASH_H