diff --git a/src/uvw/emitter.h b/src/uvw/emitter.h index b5446e69..6776020c 100644 --- a/src/uvw/emitter.h +++ b/src/uvw/emitter.h @@ -8,22 +8,12 @@ #include #include #include -#include #include #include #include +#include "type_info.hpp" -#if defined __clang__ || defined __GNUC__ -# define UVW_PRETTY_FUNCTION __PRETTY_FUNCTION__ -# define UVW_PRETTY_FUNCTION_PREFIX '=' -# define UVW_PRETTY_FUNCTION_SUFFIX ']' -#elif defined _MSC_VER -# define UVW_PRETTY_FUNCTION __FUNCSIG__ -# define UVW_PRETTY_FUNCTION_PREFIX '<' -# define UVW_PRETTY_FUNCTION_SUFFIX '>' -#endif - namespace uvw { @@ -168,20 +158,15 @@ class Emitter { ListenerList onL{}; }; - template - std::string event_type() const noexcept { - return UVW_PRETTY_FUNCTION; - } - template Handler & handler() noexcept { - auto type = event_type(); + auto id = type(); - if(!handlers.count(type)) { - handlers[type] = std::make_unique>(); + if(!handlers.count(id)) { + handlers[id] = std::make_unique>(); } - return static_cast&>(*handlers.at(type)); + return static_cast&>(*handlers.at(id)); } protected: @@ -293,10 +278,10 @@ public: */ template bool empty() const noexcept { - auto type = event_type(); + auto id = type(); - return (!handlers.count(type) || - static_cast&>(*handlers.at(type)).empty()); + return (!handlers.count(id) || + static_cast&>(*handlers.at(id)).empty()); } /** @@ -310,7 +295,7 @@ public: } private: - std::unordered_map> handlers{}; + std::unordered_map> handlers{}; }; diff --git a/src/uvw/type_info.hpp b/src/uvw/type_info.hpp new file mode 100644 index 00000000..39a9d958 --- /dev/null +++ b/src/uvw/type_info.hpp @@ -0,0 +1,78 @@ +#ifndef UVW_TYPE_INFO_INCLUDE_HPP +#define UVW_TYPE_INFO_INCLUDE_HPP + + +#include +#include + + +namespace uvw { + + +/** + * @cond TURN_OFF_DOXYGEN + * Internal details not to be documented. + */ + + +namespace internal { + + +// Fowler–Noll–Vo hash function v. 1a - the good +[[nodiscard]] static constexpr std::uint32_t fnv1a(const char *curr) noexcept { + constexpr std::uint32_t offset = 2166136261; + constexpr std::uint32_t prime = 16777619; + auto value = offset; + + while(*curr != 0) { + value = (value ^ static_cast(*(curr++))) * prime; + } + + return value; +} + + +[[nodiscard]] static std::uint32_t counter() noexcept { + static std::uint32_t cnt{}; + return cnt++; +} + + +template +[[nodiscard]] static std::uint32_t fake() noexcept { + static std::uint32_t local = counter(); + return local; +} + + +} + + +/** + * Internal details not to be documented. + * @endcond + */ + + +/** + * @brief Returns a numerical identifier for a given type. + * @tparam Type The type for which to return the numerical identifier. + * @return The numerical identifier of the give type. + */ +template +[[nodiscard]] static constexpr std::uint32_t type() noexcept { +#if defined __clang__ || defined __GNUC__ + constexpr auto value = internal::fnv1a(__PRETTY_FUNCTION__); + return value; +#elif defined _MSC_VER + constexpr auto value = internal::fnv1a(__FUNCSIG__); + return value; +#else + return internal::fake(); +#endif +} + + +} + +#endif // UVW_TYPE_INFO_INCLUDE_HPP