Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using hash_type = std::uint64_t;
- constexpr auto fnv_basis = 14695981039346656037ull;
- constexpr auto fnv_prime = 109951162821ull;
- constexpr hash_type hash(const char *str, hash_type h = fnv_basis)
- {
- return *str ? hash(str + 1, (h ^ *str) * fnv_prime) : h;
- }
- // all unregistered event types will call this function
- template <hash_type Hash>
- constexpr int checkRegistered()
- {
- static_assert(false, "Event type is not registered");
- return 0; // because we can't do constexpr void
- }
- // "registration" is done by template specialization
- #define REGISTER_EVENT_TYPE(type) \
- template <> \
- constexpr int checkRegistered<hash(type)>() \
- { \
- return 0; \
- } \
- REGISTER_EVENT_TYPE("LevelChangedEvent")
- template <hash_type EventType>
- void doStuff()
- {
- checkRegistered<EventType>(); // compile time check if type is registered!
- ... // do stuff
- }
- int main()
- {
- doStuff<hash("LevelChangedEvent")>(); // ok
- doStuff<hash("HpChangedEvent")>(); // static_assert expected here
- }
Advertisement
Add Comment
Please, Sign In to add comment