Skip to content

Commit

Permalink
[10366] Add portable way for hash specialization adding.
Browse files Browse the repository at this point in the history
Use it for ObjectGuid hash specialization
  • Loading branch information
VladimirMangos committed Aug 17, 2010
1 parent cdf5b03 commit ad0f083
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
61 changes: 48 additions & 13 deletions src/framework/Utilities/UnorderedMap.h
Expand Up @@ -35,33 +35,64 @@
#endif

#ifdef _STLPORT_VERSION
#define UNORDERED_MAP std::hash_map
# define UNORDERED_MAP std::hash_map
# define HASH_NAMESPACE_START namespace std {
# define HASH_NAMESPACE_END }
using std::hash_map;
#elif COMPILER == COMPILER_MICROSOFT && _MSC_VER >= 1500 && _HAS_TR1
#define UNORDERED_MAP std::tr1::unordered_map
# define UNORDERED_MAP std::tr1::unordered_map
# define HASH_NAMESPACE_START namespace std {
# define HASH_NAMESPACE_END }
#elif COMPILER == COMPILER_MICROSOFT && _MSC_VER >= 1300
#define UNORDERED_MAP stdext::hash_map
# define UNORDERED_MAP stdext::hash_map
# define HASH_NAMESPACE_START namespace stdext {
# define HASH_NAMESPACE_END }
using stdext::hash_map;

#if !_HAS_TRADITIONAL_STL

// can be not used by some platforms, so provide fake forward
HASH_NAMESPACE_START

template<class K>
class hash
{
public:
size_t operator() (K const&);
};

HASH_NAMESPACE_END

#endif

#elif COMPILER == COMPILER_INTEL
#define UNORDERED_MAP std::hash_map
# define UNORDERED_MAP std::hash_map
# define HASH_NAMESPACE_START namespace std {
# define HASH_NAMESPACE_END }
using std::hash_map;
#elif COMPILER == COMPILER_GNU && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3)
#define UNORDERED_MAP std::tr1::unordered_map
# define HASH_NAMESPACE_START namespace std { namespace tr1 {
# define HASH_NAMESPACE_END } }
#elif COMPILER == COMPILER_GNU && __GNUC__ >= 3
#define UNORDERED_MAP __gnu_cxx::hash_map
# define UNORDERED_MAP __gnu_cxx::hash_map
# define HASH_NAMESPACE_START namespace __gnu_cxx {
# define HASH_NAMESPACE_END }

HASH_NAMESPACE_START

namespace __gnu_cxx
{
template<>
struct hash<unsigned long long>
class hash<unsigned long long>
{
size_t operator()(const unsigned long long &__x) const { return (size_t)__x; }
public:
size_t operator()(const unsigned long long &__x) const { return (size_t)__x; }
};

template<typename T>
struct hash<T *>
class hash<T *>
{
size_t operator()(T * const &__x) const { return (size_t)__x; }
public:
size_t operator()(T * const &__x) const { return (size_t)__x; }
};

template<> struct hash<std::string>
Expand All @@ -71,10 +102,14 @@ namespace __gnu_cxx
return hash<const char *>()(__x.c_str());
}
};
};
HASH_NAMESPACE_END

#else
#define UNORDERED_MAP std::hash_map
# define UNORDERED_MAP std::hash_map
# define HASH_NAMESPACE_START namespace std {
# define HASH_NAMESPACE_END }
using std::hash_map;

#endif

#endif
17 changes: 17 additions & 0 deletions src/game/ObjectGuid.h
Expand Up @@ -22,6 +22,8 @@
#include "Common.h"
#include "ByteBuffer.h"

#include <functional>

enum TypeID
{
TYPEID_OBJECT = 0,
Expand Down Expand Up @@ -277,4 +279,19 @@ ByteBuffer& operator>> (ByteBuffer& buf, PackedGuidReader const& guid);

inline PackedGuid ObjectGuid::WriteAsPacked() const { return PackedGuid(*this); }

HASH_NAMESPACE_START

template<>
class hash<ObjectGuid>
{
public:

size_t operator() (ObjectGuid const& key) const
{
return hash<uint64>()(key.GetRawValue());
}
};

HASH_NAMESPACE_END

#endif
2 changes: 1 addition & 1 deletion src/shared/revision_nr.h
@@ -1,4 +1,4 @@
#ifndef __REVISION_NR_H__
#define __REVISION_NR_H__
#define REVISION_NR "10365"
#define REVISION_NR "10366"
#endif // __REVISION_NR_H__

1 comment on commit ad0f083

@VladimirMangos
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it allow use non standard types as unordered_map keys, for example in this case ObjectGuid.

Please sign in to comment.