From f5ced7fe3963a1239b49a29be018388ff1b41f2a Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Thu, 12 Dec 2019 16:02:58 +0100 Subject: [PATCH] core: (customizable) type_id[_v] --- src/entt/core/type_info.hpp | 52 ++++++++++++++++++++++++++++++++++++ src/entt/entt.hpp | 1 + test/CMakeLists.txt | 1 + test/entt/core/BUILD.bazel | 10 +++++++ test/entt/core/type_info.cpp | 9 +++++++ 5 files changed, 73 insertions(+) create mode 100644 src/entt/core/type_info.hpp create mode 100644 test/entt/core/type_info.cpp diff --git a/src/entt/core/type_info.hpp b/src/entt/core/type_info.hpp new file mode 100644 index 000000000..9e75eab65 --- /dev/null +++ b/src/entt/core/type_info.hpp @@ -0,0 +1,52 @@ +#ifndef ENTT_CORE_TYPE_INFO_HPP +#define ENTT_CORE_TYPE_INFO_HPP + + +#include +#include "../config/config.h" +#include "hashed_string.hpp" + + +namespace entt { + + +/** + * @brief Types identifiers. + * @tparam Type Type for which to generate an identifier. + */ +template +struct type_id { + static_assert(sizeof...(Type) == ((bool{true || sizeof(Type)}) + ...)); + +#if defined _MSC_VER + /** + * @brief Returns the numeric representation of a given type. + * @return The numeric representation of the given type. + */ + static constexpr ENTT_ID_TYPE value() ENTT_NOEXCEPT { + return entt::hashed_string{__FUNCSIG__}; + } +#elif defined __GNUC__ + /** + * @brief Returns the numeric representation of a given type. + * @return The numeric representation of the given type. + */ + static constexpr ENTT_ID_TYPE value() ENTT_NOEXCEPT { + return entt::hashed_string{__PRETTY_FUNCTION__}; + } +#endif +}; + + +/** + * @brief Helper variable template. + * @tparam Type Type for which to generate an identifier. + */ +template +static constexpr auto type_id_v = type_id::value(); + + +} + + +#endif // ENTT_CORE_TYPE_INFO_HPP diff --git a/src/entt/entt.hpp b/src/entt/entt.hpp index 02eba4548..3065a3093 100644 --- a/src/entt/entt.hpp +++ b/src/entt/entt.hpp @@ -4,6 +4,7 @@ #include "core/hashed_string.hpp" #include "core/ident.hpp" #include "core/monostate.hpp" +#include "core/type_info.hpp" #include "core/type_traits.hpp" #include "core/utility.hpp" #include "entity/actor.hpp" diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c24008f24..7a628fefa 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -131,6 +131,7 @@ SETUP_BASIC_TEST(family entt/core/family.cpp) SETUP_BASIC_TEST(hashed_string entt/core/hashed_string.cpp) SETUP_BASIC_TEST(ident entt/core/ident.cpp) SETUP_BASIC_TEST(monostate entt/core/monostate.cpp) +SETUP_BASIC_TEST(type_info entt/core/type_info.cpp) SETUP_BASIC_TEST(type_traits entt/core/type_traits.cpp) SETUP_BASIC_TEST(utility entt/core/utility.cpp) diff --git a/test/entt/core/BUILD.bazel b/test/entt/core/BUILD.bazel index bf2d4934a..adea042a9 100644 --- a/test/entt/core/BUILD.bazel +++ b/test/entt/core/BUILD.bazel @@ -50,6 +50,16 @@ cc_test( ], ) +cc_test( + name = "type_info", + srcs = ["type_info.cpp"], + copts = entt_copts, + deps = [ + "//:entt", + "@com_google_googletest//:gtest_main", + ], +) + cc_test( name = "type_traits", srcs = ["type_traits.cpp"], diff --git a/test/entt/core/type_info.cpp b/test/entt/core/type_info.cpp new file mode 100644 index 000000000..a0f91293c --- /dev/null +++ b/test/entt/core/type_info.cpp @@ -0,0 +1,9 @@ +#include +#include +#include + +TEST(TypeId, Functionalities) { + ASSERT_NE(entt::type_id_v, entt::type_id_v); + ASSERT_NE(entt::type_id_v, entt::type_id_v); + ASSERT_EQ(entt::type_id_v, entt::type_id_v); +}