Skip to content

Commit 8feeace

Browse files
committed
One common hash function for enums.
1 parent 14e952a commit 8feeace

4 files changed

Lines changed: 23 additions & 46 deletions

File tree

Sources/NodeUIEngine/NUIE_FeatureToggle.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
#include "NUIE_FeatureToggle.hpp"
2+
#include "NUIE_HashUtils.hpp"
23

34
#include <unordered_map>
45
#include <functional>
56

6-
namespace std
7-
{
8-
template <>
9-
struct hash<NUIE::Feature>
10-
{
11-
size_t operator() (const NUIE::Feature& featureId) const noexcept
12-
{
13-
return (size_t) featureId;
14-
}
15-
};
16-
}
17-
187
namespace NUIE
198
{
209

@@ -30,7 +19,7 @@ FeatureEnableGuard::~FeatureEnableGuard ()
3019
EnableFeature (featureId, oldEnabled);
3120
}
3221

33-
static std::unordered_map<Feature, bool> features;
22+
static std::unordered_map<Feature, bool, EnumHash> features;
3423

3524
void EnableFeature (Feature featureId, bool enabled)
3625
{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef NUIE_HASHUTILS_HPP
2+
#define NUIE_HASHUTILS_HPP
3+
4+
struct EnumHash
5+
{
6+
template <typename EnumType>
7+
size_t operator() (EnumType enumVal) const
8+
{
9+
return static_cast<size_t> (enumVal);
10+
}
11+
};
12+
13+
#endif

Sources/NodeUIEngine/NUIE_InputEventHandler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ ModifierKeys::ModifierKeys () :
1313

1414
}
1515

16-
ModifierKeys::ModifierKeys (const std::unordered_set<ModifierKeyCode>& keys) :
17-
keys (keys)
16+
ModifierKeys::ModifierKeys (const std::initializer_list<ModifierKeyCode>& keyCodes) :
17+
keys (keyCodes)
1818
{
1919

2020
}

Sources/NodeUIEngine/NUIE_InputEventHandler.hpp

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef NUIE_INPUTEVENTHANDLER_HPP
22
#define NUIE_INPUTEVENTHANDLER_HPP
33

4+
#include "NUIE_HashUtils.hpp"
45
#include "NUIE_Geometry.hpp"
56
#include "NUIE_NodeUIEnvironment.hpp"
67

@@ -34,44 +35,18 @@ enum class KeyCode
3435
Escape
3536
};
3637

37-
}
38-
39-
namespace std
40-
{
41-
template <>
42-
struct hash<NUIE::MouseButton>
43-
{
44-
size_t operator() (const NUIE::MouseButton& button) const noexcept
45-
{
46-
return (size_t) button;
47-
}
48-
};
49-
50-
template <>
51-
struct hash<NUIE::ModifierKeyCode>
52-
{
53-
size_t operator() (const NUIE::ModifierKeyCode& keyCode) const noexcept
54-
{
55-
return (size_t) keyCode;
56-
}
57-
};
58-
}
59-
60-
namespace NUIE
61-
{
62-
6338
class ModifierKeys
6439
{
6540
public:
6641
ModifierKeys ();
67-
ModifierKeys (const std::unordered_set<ModifierKeyCode>& keys);
42+
ModifierKeys (const std::initializer_list<ModifierKeyCode>& keyCodes);
6843
~ModifierKeys ();
6944

7045
void Insert (ModifierKeyCode keyCode);
7146
bool Contains (ModifierKeyCode keyCode) const;
7247

7348
private:
74-
std::unordered_set<ModifierKeyCode> keys;
49+
std::unordered_set<ModifierKeyCode, EnumHash> keys;
7550
};
7651

7752
extern const ModifierKeys EmptyModifierKeys;
@@ -109,9 +84,9 @@ class MouseEventTranslator
10984
void OnMouseMove (NodeUIEnvironment& env, const ModifierKeys& modifierKeys, const Point& position);
11085

11186
private:
112-
InputEventHandler& handler;
113-
std::unordered_map<MouseButton, Point> downMouseButtons;
114-
std::unordered_set<MouseButton> movingMouseButtons;
87+
InputEventHandler& handler;
88+
std::unordered_map<MouseButton, Point, EnumHash> downMouseButtons;
89+
std::unordered_set<MouseButton, EnumHash> movingMouseButtons;
11590
};
11691

11792
}

0 commit comments

Comments
 (0)