OpenNI 1.5.7
XnStringsHash.h
Go to the documentation of this file.
00001 /*****************************************************************************
00002 *                                                                            *
00003 *  OpenNI 1.x Alpha                                                          *
00004 *  Copyright (C) 2012 PrimeSense Ltd.                                        *
00005 *                                                                            *
00006 *  This file is part of OpenNI.                                              *
00007 *                                                                            *
00008 *  Licensed under the Apache License, Version 2.0 (the "License");           *
00009 *  you may not use this file except in compliance with the License.          *
00010 *  You may obtain a copy of the License at                                   *
00011 *                                                                            *
00012 *      http://www.apache.org/licenses/LICENSE-2.0                            *
00013 *                                                                            *
00014 *  Unless required by applicable law or agreed to in writing, software       *
00015 *  distributed under the License is distributed on an "AS IS" BASIS,         *
00016 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
00017 *  See the License for the specific language governing permissions and       *
00018 *  limitations under the License.                                            *
00019 *                                                                            *
00020 *****************************************************************************/
00021 #ifndef _XN_STRINGS_HASH_H
00022 #define _XN_STRINGS_HASH_H
00023 
00024 //---------------------------------------------------------------------------
00025 // Includes
00026 //---------------------------------------------------------------------------
00027 #include "XnHash.h"
00028 #include <XnOS.h>
00029 
00030 //---------------------------------------------------------------------------
00031 // Types
00032 //---------------------------------------------------------------------------
00033 class XnStringsKeyManager
00034 {
00035 public:
00036     static XnHashValue Hash(const XnChar* const& key)
00037     {
00038         XnUInt32 nCRC = 0;
00039         xnOSStrCRC32(key, &nCRC);
00040 
00041         // convert from UINT32 to XnHashValue
00042         return nCRC % (1 << (sizeof(XnHashValue)*8));
00043     }
00044 
00045     static XnInt32 Compare(const XnChar* const& key1, const XnChar* const& key2)
00046     {
00047         return strcmp(key1, key2);
00048     }
00049 };
00050 
00051 class XnStringsKeyTranslator
00052 {
00053 public:
00054     static XnValue CreateValueCopy(const XnChar* const& orig)
00055     {
00056         // we should copy string, so we can keep the key
00057         XnUInt32 nLen = xnOSStrLen(orig) + 1; // with null termination
00058         XnChar* pcKey = (XnChar*)xnOSMalloc(nLen);
00059         xnOSStrCopy(pcKey, orig, nLen);
00060         return (pcKey);
00061     }
00062 
00063     static void FreeValue(XnValue& Value)
00064     {
00065         XnChar* pcKey = (XnChar*)Value;
00066         xnOSFree(pcKey);
00067     }
00068 
00069     static XnValue GetAsValue(const XnChar* const& orig)
00070     {
00071         return (XnValue)orig;
00072     }
00073 
00074     static const XnChar* const& GetFromValue(const XnValue& Value)
00075     {
00076         return (const XnChar* const&)Value;
00077     }
00078 
00079     static const XnChar*& GetFromValue(XnValue& Value)
00080     {
00081         return (const XnChar*&)Value;
00082     }
00083 };
00084 
00089 #define XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR_DECL(decl, ValueType, ClassName, ValueTranslator)                               \
00090     XN_DECLARE_HASH_DECL(decl, const XnChar*, ValueType, ClassName, XnStringsKeyTranslator, ValueTranslator, XnStringsKeyManager)   \
00091 
00092 
00096 #define XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR(ValueType, ClassName, ValueTranslator)                              \
00097     XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR_DECL(, ValueType, ClassName, ValueTranslator)
00098 
00103 #define XN_DECLARE_STRINGS_HASH_DECL(decl, ValueType, ClassName)                                                        \
00104     XN_DECLARE_DEFAULT_VALUE_TRANSLATOR_DECL(decl, ValueType, XN_DEFAULT_TRANSLATOR_NAME(ClassName))                    \
00105     XN_DECLARE_STRINGS_HASH_WITH_TRANSLATOR_DECL(decl, ValueType, ClassName, XN_DEFAULT_TRANSLATOR_NAME(ClassName)) \
00106 
00107 
00111 #define XN_DECLARE_STRINGS_HASH(ValueType, ClassName)       \
00112     XN_DECLARE_STRINGS_HASH_DECL(, ValueType, ClassName)
00113 
00114 XN_DECLARE_STRINGS_HASH(XnValue, XnStringsHash)
00115 
00116 
00117 #endif //_XN_STRINGS_HASH_H