If you really want that approach, without having to dig into the engine and modify things, then you could modify the Source/Urho3D/Math/MathDefs.h
file and simply add near the SDBMHash
function the following:
/// Retrieve the lowercase version of an ASCII character.
inline constexpr int tolower_cx(int c) { return (c > 64 && c < 91) ? (c + 32) : c; }
/// Retrieve the uppercase version of an ASCII character.
inline constexpr int toupper_cx(int c) { return (c > 96 && c < 123) ? (c - 32) : c; }
/// Calculate the hash of the given ASCII string using the SDBM algorithm at compile time.
inline constexpr unsigned SDBMHash_Cx(const char* s, unsigned i, unsigned n, unsigned h) {
return i < n ? SDBMHash_Cx(s, i + 1, n, tolower_cx(s[i]) + (h << 6) + (h << 16) - h) : h;
}
/// User defined literal for converting strings to hashes at compile time.
inline constexpr unsigned operator "" _H(const char* str, size_t len) { return SDBMHash_Cx(str, 0, len, 0); }
Or even add it to your files if that’s all you need. Doesn’t matter where you add it as long as it can be used.
And then use the event name directly instead of it’s ID:
switch (eventType.Value())
{
case "KeyUp"_H:
break;
case "KeyDown"_H:
break;
default:
};
And if you want, you could change the user defined literal name from _H
to _E
or _Event
if you want it to make sense. Or you could keep all of them since they’ll be discarded after compile.
There are a bunch of places where Urho could’ve make use of constexpr
. But it’s using C++11 constexpr
. Which is somewhat limited. Wish the engine would’ve jumped straight to C++14 since that’s the sweet spot between compiler support and language features. 17 would be too high but 11 is too low.
C++14 removes some constexpr
limitations and would’ve made some things easier and probably better looking.
But at this point, the engine would require some major changes as it wasn’t initially designed with that in mind.