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

meta: refine user defined traits support

skypjack 1 год назад
Родитель
Сommit
d16fa5c605
4 измененных файлов с 24 добавлено и 21 удалено
  1. 0 3
      TODO
  2. 6 8
      src/entt/meta/factory.hpp
  3. 3 10
      src/entt/meta/meta.hpp
  4. 15 0
      src/entt/meta/node.hpp

+ 0 - 3
TODO

@@ -37,11 +37,8 @@ TODO:
 * cleanup common view from tricks to handle single swap-only and in-place, if constexpr branches
 * cleanup common view from tricks to handle single swap-only and in-place, if constexpr branches
 * exploit ref/cref in any to avoid invoking the vtable if possible
 * exploit ref/cref in any to avoid invoking the vtable if possible
 * review meta properties and details, maybe a dense map is too much
 * review meta properties and details, maybe a dense map is too much
-* self assignment support for any and meta_any
-* meta support for use defined traits (room for them on meta traits)
 * entity based component_traits
 * entity based component_traits
 * investigate using any rather than shared<void> for meta properties
 * investigate using any rather than shared<void> for meta properties
-* refine user defined meta traits implementation
 * copy-and-swap for any and meta_any
 * copy-and-swap for any and meta_any
 * improve seek function for overloaded meta functions
 * improve seek function for overloaded meta functions
 * fix cmake warning about FetchContent_Populate
 * fix cmake warning about FetchContent_Populate

+ 6 - 8
src/entt/meta/factory.hpp

@@ -104,15 +104,13 @@ protected:
         }
         }
     }
     }
 
 
-    void traits(const std::underlying_type_t<internal::meta_traits> value) {
-        constexpr auto shift = popcount(static_cast<std::underlying_type_t<internal::meta_traits>>(internal::meta_traits::_user_defined_traits));
-
-        if(const internal::meta_traits data{value << shift}; bucket == parent) {
-            internal::meta_context::from(*ctx).value[parent].traits |= data;
+    void traits(const internal::meta_traits value) {
+        if(bucket == parent) {
+            internal::meta_context::from(*ctx).value[parent].traits |= value;
         } else if(is_data) {
         } else if(is_data) {
-            details->data[bucket].traits |= data;
+            details->data[bucket].traits |= value;
         } else {
         } else {
-            details->func[bucket].traits |= data;
+            details->func[bucket].traits |= value;
         }
         }
     }
     }
 
 
@@ -523,7 +521,7 @@ public:
     template<typename Value>
     template<typename Value>
     meta_factory traits(const Value value) {
     meta_factory traits(const Value value) {
         static_assert(std::is_enum_v<Value>, "Invalid enum type");
         static_assert(std::is_enum_v<Value>, "Invalid enum type");
-        base_type::traits(static_cast<std::underlying_type_t<internal::meta_traits>>(static_cast<std::underlying_type_t<Value>>(value)));
+        base_type::traits(internal::user_to_meta_traits(value));
         return *this;
         return *this;
     }
     }
 };
 };

+ 3 - 10
src/entt/meta/meta.hpp

@@ -9,7 +9,6 @@
 #include <utility>
 #include <utility>
 #include "../config/config.h"
 #include "../config/config.h"
 #include "../core/any.hpp"
 #include "../core/any.hpp"
-#include "../core/bit.hpp"
 #include "../core/fwd.hpp"
 #include "../core/fwd.hpp"
 #include "../core/iterator.hpp"
 #include "../core/iterator.hpp"
 #include "../core/type_info.hpp"
 #include "../core/type_info.hpp"
@@ -925,9 +924,7 @@ struct meta_data {
      */
      */
     template<typename Type>
     template<typename Type>
     [[nodiscard]] Type traits() const noexcept {
     [[nodiscard]] Type traits() const noexcept {
-        static_assert(std::is_enum_v<Type>, "Invalid enum type");
-        constexpr auto shift = popcount(static_cast<std::underlying_type_t<internal::meta_traits>>(internal::meta_traits::_user_defined_traits));
-        return Type{static_cast<std::underlying_type_t<Type>>(static_cast<std::underlying_type_t<internal::meta_traits>>(node->traits) >> shift)};
+        return internal::meta_to_user_traits<Type>(node->traits);
     }
     }
 
 
     /**
     /**
@@ -1059,9 +1056,7 @@ struct meta_func {
      */
      */
     template<typename Type>
     template<typename Type>
     [[nodiscard]] Type traits() const noexcept {
     [[nodiscard]] Type traits() const noexcept {
-        static_assert(std::is_enum_v<Type>, "Invalid enum type");
-        constexpr auto shift = popcount(static_cast<std::underlying_type_t<internal::meta_traits>>(internal::meta_traits::_user_defined_traits));
-        return Type{static_cast<std::underlying_type_t<Type>>(static_cast<std::underlying_type_t<internal::meta_traits>>(node->traits) >> shift)};
+        return internal::meta_to_user_traits<Type>(node->traits);
     }
     }
 
 
     /**
     /**
@@ -1535,9 +1530,7 @@ public:
      */
      */
     template<typename Type>
     template<typename Type>
     [[nodiscard]] Type traits() const noexcept {
     [[nodiscard]] Type traits() const noexcept {
-        static_assert(std::is_enum_v<Type>, "Invalid enum type");
-        constexpr auto shift = popcount(static_cast<std::underlying_type_t<internal::meta_traits>>(internal::meta_traits::_user_defined_traits));
-        return Type{static_cast<std::underlying_type_t<Type>>(static_cast<std::underlying_type_t<internal::meta_traits>>(node.traits) >> shift)};
+        return internal::meta_to_user_traits<Type>(node.traits);
     }
     }
 
 
     /**
     /**

+ 15 - 0
src/entt/meta/node.hpp

@@ -8,6 +8,7 @@
 #include "../config/config.h"
 #include "../config/config.h"
 #include "../container/dense_map.hpp"
 #include "../container/dense_map.hpp"
 #include "../core/attribute.h"
 #include "../core/attribute.h"
+#include "../core/bit.hpp"
 #include "../core/enum.hpp"
 #include "../core/enum.hpp"
 #include "../core/fwd.hpp"
 #include "../core/fwd.hpp"
 #include "../core/type_info.hpp"
 #include "../core/type_info.hpp"
@@ -43,6 +44,20 @@ enum class meta_traits : std::uint32_t {
     _entt_enum_as_bitmask = 0xFFFF
     _entt_enum_as_bitmask = 0xFFFF
 };
 };
 
 
+template<typename Type>
+[[nodiscard]] auto meta_to_user_traits(const meta_traits traits) noexcept {
+    static_assert(std::is_enum_v<Type>, "Invalid enum type");
+    constexpr auto shift = popcount(static_cast<std::underlying_type_t<meta_traits>>(meta_traits::_user_defined_traits));
+    return Type{static_cast<std::underlying_type_t<Type>>(static_cast<std::underlying_type_t<meta_traits>>(traits) >> shift)};
+}
+
+template<typename Type>
+[[nodiscard]] auto user_to_meta_traits(const Type value) noexcept {
+    static_assert(std::is_enum_v<Type>, "Invalid enum type");
+    constexpr auto shift = popcount(static_cast<std::underlying_type_t<meta_traits>>(meta_traits::_user_defined_traits));
+    return meta_traits{static_cast<std::underlying_type_t<internal::meta_traits>>(static_cast<std::underlying_type_t<Type>>(value)) << shift};
+}
+
 struct meta_type_node;
 struct meta_type_node;
 
 
 struct meta_prop_node {
 struct meta_prop_node {