Просмотр исходного кода

core: (customizable) type_id[_v]

Michele Caini 6 лет назад
Родитель
Сommit
f5ced7fe39
5 измененных файлов с 73 добавлено и 0 удалено
  1. 52 0
      src/entt/core/type_info.hpp
  2. 1 0
      src/entt/entt.hpp
  3. 1 0
      test/CMakeLists.txt
  4. 10 0
      test/entt/core/BUILD.bazel
  5. 9 0
      test/entt/core/type_info.cpp

+ 52 - 0
src/entt/core/type_info.hpp

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

+ 1 - 0
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"

+ 1 - 0
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)
 

+ 10 - 0
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"],

+ 9 - 0
test/entt/core/type_info.cpp

@@ -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>);
+}