Преглед изворни кода

type_info: add conversion operators to underlying types (close #664)

Michele Caini пре 5 година
родитељ
комит
de35cbf4af
2 измењених фајлова са 13 додато и 0 уклоњено
  1. 9 0
      src/entt/core/type_info.hpp
  2. 4 0
      test/entt/core/type_info.cpp

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

@@ -97,6 +97,9 @@ struct ENTT_API type_seq final {
         static const id_type value = internal::type_seq::next();
         static const id_type value = internal::type_seq::next();
         return value;
         return value;
     }
     }
+
+    /*! @brief Conversion operator to the underlying type. */
+    [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
 };
 };
 
 
 
 
@@ -118,6 +121,9 @@ struct type_hash final {
         return type_seq<Type>::value();
         return type_seq<Type>::value();
 #endif
 #endif
     }
     }
+
+    /*! @brief Conversion operator to the underlying type. */
+    [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
 };
 };
 
 
 
 
@@ -134,6 +140,9 @@ struct type_name final {
     [[nodiscard]] static constexpr std::string_view value() ENTT_NOEXCEPT {
     [[nodiscard]] static constexpr std::string_view value() ENTT_NOEXCEPT {
         return internal::type_name<Type>(0);
         return internal::type_name<Type>(0);
     }
     }
+
+    /*! @brief Conversion operator to the underlying type. */
+    [[nodiscard]] constexpr operator std::string_view() const ENTT_NOEXCEPT { return value(); }
 };
 };
 
 
 
 

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

@@ -10,12 +10,14 @@ TEST(TypeSeq, Functionalities) {
     ASSERT_NE(entt::type_seq<int>::value(), entt::type_seq<char>::value());
     ASSERT_NE(entt::type_seq<int>::value(), entt::type_seq<char>::value());
     ASSERT_NE(entt::type_seq<int>::value(), entt::type_seq<int &&>::value());
     ASSERT_NE(entt::type_seq<int>::value(), entt::type_seq<int &&>::value());
     ASSERT_NE(entt::type_seq<int &>::value(), entt::type_seq<const int &>::value());
     ASSERT_NE(entt::type_seq<int &>::value(), entt::type_seq<const int &>::value());
+    ASSERT_EQ(static_cast<entt::id_type>(entt::type_seq<int>{}), entt::type_seq<int>::value());
 }
 }
 
 
 TEST(TypeHash, Functionalities) {
 TEST(TypeHash, Functionalities) {
     ASSERT_NE(entt::type_hash<int>::value(), entt::type_hash<const int>::value());
     ASSERT_NE(entt::type_hash<int>::value(), entt::type_hash<const int>::value());
     ASSERT_NE(entt::type_hash<int>::value(), entt::type_hash<char>::value());
     ASSERT_NE(entt::type_hash<int>::value(), entt::type_hash<char>::value());
     ASSERT_EQ(entt::type_hash<int>::value(), entt::type_hash<int>::value());
     ASSERT_EQ(entt::type_hash<int>::value(), entt::type_hash<int>::value());
+    ASSERT_EQ(static_cast<entt::id_type>(entt::type_hash<int>{}), entt::type_hash<int>::value());
 }
 }
 
 
 TEST(TypeName, Functionalities) {
 TEST(TypeName, Functionalities) {
@@ -27,6 +29,8 @@ TEST(TypeName, Functionalities) {
 
 
     ASSERT_TRUE(((entt::type_name<entt::type_list<entt::type_list<int, char>, double>>::value()) == std::string_view{"entt::type_list<entt::type_list<int, char>, double>"})
     ASSERT_TRUE(((entt::type_name<entt::type_list<entt::type_list<int, char>, double>>::value()) == std::string_view{"entt::type_list<entt::type_list<int, char>, double>"})
         || ((entt::type_name<entt::type_list<entt::type_list<int, char>, double>>::value()) == std::string_view{"struct entt::type_list<struct entt::type_list<int,char>,double>"}));
         || ((entt::type_name<entt::type_list<entt::type_list<int, char>, double>>::value()) == std::string_view{"struct entt::type_list<struct entt::type_list<int,char>,double>"}));
+
+    ASSERT_EQ(static_cast<std::string_view>(entt::type_name<int>{}), entt::type_name<int>::value());
 }
 }
 
 
 TEST(TypeInfo, Functionalities) {
 TEST(TypeInfo, Functionalities) {