core: (customizable) type_id[_v]

This commit is contained in:
Michele Caini
2019-12-12 16:02:58 +01:00
parent 023267ecab
commit f5ced7fe39
5 changed files with 73 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
#ifndef ENTT_CORE_TYPE_INFO_HPP
#define ENTT_CORE_TYPE_INFO_HPP
#include <type_traits>
#include "../config/config.h"
#include "hashed_string.hpp"
namespace entt {
/**
* @brief Types identifiers.
* @tparam Type Type for which to generate an identifier.
*/
template<typename... Type>
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<typename Type>
static constexpr auto type_id_v = type_id<Type>::value();
}
#endif // ENTT_CORE_TYPE_INFO_HPP

View File

@@ -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"

View File

@@ -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)

View File

@@ -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"],

View File

@@ -0,0 +1,9 @@
#include <gtest/gtest.h>
#include <entt/core/hashed_string.hpp>
#include <entt/core/type_info.hpp>
TEST(TypeId, Functionalities) {
ASSERT_NE(entt::type_id_v<int>, entt::type_id_v<const int>);
ASSERT_NE(entt::type_id_v<int>, entt::type_id_v<char>);
ASSERT_EQ(entt::type_id_v<int>, entt::type_id_v<int>);
}