XnStringsHash.h
Go to the documentation of this file.
/****************************************************************************
* *
* 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;
}
};
#define XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR_DECL(decl, ValueType, ClassName, ValueTranslator) \
XN_DECLARE_HASH_DECL(decl, const XnChar*, ValueType, ClassName, XnStringsKeyTranslator, ValueTranslator, XnStringsKeyManager) \
#define XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR(ValueType, ClassName, ValueTranslator) \
XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR_DECL(, ValueType, ClassName, ValueTranslator)
#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)) \
#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
XnValue
void * XnValue
Definition XnDataTypes.h:36
XnHash.h
XnHashValue
XnUInt8 XnHashValue
Definition XnHash.h:46
XnOS.h
xnOSMalloc
XN_C_API void *XN_C_DECL xnOSMalloc(const XnSizeT nAllocSize)
xnOSFree
XN_C_API void XN_C_DECL xnOSFree(const void *pMemBlock)
xnOSStrCopy
XN_C_API XnStatus XN_C_DECL xnOSStrCopy(XnChar *cpDestString, const XnChar *cpSrcString, const XnUInt32 nDestLength)
xnOSStrLen
XN_C_API XnUInt32 XN_C_DECL xnOSStrLen(const XnChar *cpStr)
xnOSStrCRC32
XN_C_API XnStatus XN_C_DECL xnOSStrCRC32(const XnChar *cpString, XnUInt32 *nCRC32)
XN_DECLARE_STRINGS_HASH
#define XN_DECLARE_STRINGS_HASH(ValueType, ClassName)
Definition XnStringsHash.h:112
XnStringsKeyManager
Definition XnStringsHash.h:35
XnStringsKeyManager::Compare
static XnInt32 Compare(const XnChar *const &key1, const XnChar *const &key2)
Definition XnStringsHash.h:46
XnStringsKeyManager::Hash
static XnHashValue Hash(const XnChar *const &key)
Definition XnStringsHash.h:37
XnStringsKeyTranslator
Definition XnStringsHash.h:53
XnStringsKeyTranslator::CreateValueCopy
static XnValue CreateValueCopy(const XnChar *const &orig)
Definition XnStringsHash.h:55
XnStringsKeyTranslator::GetFromValue
static const XnChar *& GetFromValue(XnValue &Value)
Definition XnStringsHash.h:80
XnStringsKeyTranslator::GetAsValue
static XnValue GetAsValue(const XnChar *const &orig)
Definition XnStringsHash.h:70
XnStringsKeyTranslator::GetFromValue
static const XnChar *const & GetFromValue(const XnValue &Value)
Definition XnStringsHash.h:75
XnStringsKeyTranslator::FreeValue
static void FreeValue(XnValue &Value)
Definition XnStringsHash.h:64