This patchs adds a header file, "core/enum.hh"; Code which includes this header file will be able to use an enumerated type as the key in a hash table. The header file implements a hash function for *all* enumerated types, by using the standard hash function of the underlying integer type.
29 lines
722 B
C++
29 lines
722 B
C++
/*
|
|
* Copyright (C) 2015 Cloudius Systems, Ltd.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
/*
|
|
* This header file defines a hash function for all enum types, using the
|
|
* standard hash function of the underlying type (such as int). This makes
|
|
* it possible to use an enum type as a key for std::unordered_map, for
|
|
* example.
|
|
*/
|
|
|
|
#include <type_traits>
|
|
#include <functional>
|
|
#include <cstddef>
|
|
|
|
namespace std {
|
|
template<typename T>
|
|
class hash {
|
|
using sfinae = typename std::enable_if<std::is_enum<T>::value, T>::type;
|
|
public:
|
|
std::size_t operator()(const T& e) const {
|
|
using utype = typename std::underlying_type<T>::type;
|
|
return std::hash<utype>()(static_cast<utype>(e));
|
|
}
|
|
};
|
|
}
|