Browse Source

meta: internal changes

Michele Caini 4 years ago
parent
commit
cb5e9393a4
3 changed files with 29 additions and 24 deletions
  1. 22 21
      src/entt/meta/meta.hpp
  2. 6 2
      src/entt/meta/node.hpp
  3. 1 1
      test/entt/meta/meta_ctor.cpp

+ 22 - 21
src/entt/meta/meta.hpp

@@ -361,12 +361,12 @@ public:
      */
     template<typename Type>
     [[nodiscard]] const Type * try_cast() const {
-        if(node) {
-            if(const auto info = type_id<Type>(); node->info == info) {
-                return any_cast<Type>(&storage);
-            } else if(const auto *base = internal::visit<&internal::meta_type_node::base>([info](const auto *curr) { return curr->type->info == info; }, node); base) {
-                return static_cast<const Type *>(base->cast(storage.data()));
-            }
+        if(!node) { return nullptr; }
+
+        if(const auto info = type_id<Type>(); node->info == info) {
+            return any_cast<Type>(&storage);
+        } else if(const auto *base = internal::visit<&internal::meta_type_node::base>([info](const auto *curr) { return curr->type->info == info; }, node); base) {
+            return static_cast<const Type *>(base->cast(storage.data()));
         }
 
         return nullptr;
@@ -378,12 +378,12 @@ public:
         if constexpr(std::is_const_v<Type>) {
             return std::as_const(*this).try_cast<Type>();
         } else {
-            if(node) {
-                if(const auto info = type_id<Type>(); node->info == info) {
-                    return any_cast<Type>(&storage);
-                } else if(const auto *base = internal::visit<&internal::meta_type_node::base>([info](const auto *curr) { return curr->type->info == info; }, node); base) {
-                    return const_cast<Type *>(static_cast<const Type *>(base->cast(storage.data())));
-                }
+            if(!node) { return nullptr; }
+
+            if(const auto info = type_id<Type>(); node->info == info) {
+                return any_cast<Type>(&storage);
+            } else if(const auto *base = internal::visit<&internal::meta_type_node::base>([info](const auto *curr) { return curr->type->info == info; }, node); base) {
+                return const_cast<Type *>(static_cast<const Type *>(base->cast(storage.data())));
             }
 
             return nullptr;
@@ -1299,7 +1299,7 @@ public:
             }
         }
 
-        return (!sz && node->factory) ? node->factory() : meta_any{};
+        return (!sz && node->default_constructor) ? node->default_constructor() : meta_any{};
     }
 
     /**
@@ -1501,12 +1501,12 @@ bool meta_any::set(const id_type id, Type &&value) {
 
 
 [[nodiscard]] inline meta_any meta_any::allow_cast(const meta_type &type) const {
-    if(node) {
-        if(const auto info = type.info(); node->info == info || internal::visit<&internal::meta_type_node::base>([info](const auto *curr) { return curr->type->info == info; }, node)) {
-            return as_ref();
-        } else if(const auto * const conv = internal::visit<&internal::meta_type_node::conv>([info](const auto *curr) { return curr->type->info == info; }, node); conv) {
-            return conv->conv(storage.data());
-        }
+    if(!node) { return {}; }
+
+    if(const auto info = type.info(); node->info == info || internal::visit<&internal::meta_type_node::base>([info](const auto *curr) { return curr->type->info == info; }, node)) {
+        return as_ref();
+    } else if(const auto * const conv = internal::visit<&internal::meta_type_node::conv>([info](const auto *curr) { return curr->type->info == info; }, node); conv) {
+        return conv->conv(storage.data());
     }
 
     return {};
@@ -1514,11 +1514,12 @@ bool meta_any::set(const id_type id, Type &&value) {
 
 
 inline bool meta_any::allow_cast(const meta_type &type) {
+    if(!node) { return false; }
+
     if(const auto info = type.info(); node->info == info || internal::visit<&internal::meta_type_node::base>([info](const auto *curr) { return curr->type->info == info; }, node)) {
         return true;
     } else if(const auto * const conv = internal::visit<&internal::meta_type_node::conv>([info](const auto *curr) { return curr->type->info == info; }, node); conv) {
-        *this = conv->conv(std::as_const(storage).data());
-        return true;
+        return static_cast<bool>(*this = conv->conv(std::as_const(storage).data()));
     }
 
     return false;

+ 6 - 2
src/entt/meta/node.hpp

@@ -121,7 +121,7 @@ struct meta_type_node {
     meta_prop_node * prop;
     const size_type size_of;
     meta_traits traits;
-    meta_any(* const factory)();
+    meta_any(* const default_constructor)();
     const meta_template_node *const templ;
     meta_ctor_node *ctor{nullptr};
     meta_base_node *base{nullptr};
@@ -140,7 +140,7 @@ template<typename Type>
 class ENTT_API meta_node {
     static_assert(std::is_same_v<Type, std::remove_cv_t<std::remove_reference_t<Type>>>, "Invalid type");
 
-    [[nodiscard]] static decltype(meta_type_node::factory) meta_default_constructor() ENTT_NOEXCEPT {
+    [[nodiscard]] static decltype(meta_type_node::default_constructor) meta_default_constructor() ENTT_NOEXCEPT {
         if constexpr(std::is_default_constructible_v<Type>) {
             return +[]() { return meta_any{std::in_place_type<Type>}; };
         } else {
@@ -203,6 +203,10 @@ template<typename... Args>
 
 template<auto Member, typename Op>
 [[nodiscard]] static std::decay_t<decltype(std::declval<internal::meta_type_node>().*Member)> visit(const Op &op, const internal::meta_type_node *node) {
+    if(!node) {
+        return nullptr;
+    }
+
     for(auto *curr = node->*Member; curr; curr = curr->next) {
         if(op(curr)) {
             return curr;

+ 1 - 1
test/entt/meta/meta_ctor.cpp

@@ -261,5 +261,5 @@ TEST_F(MetaCtor, ReRegistration) {
 
     ASSERT_NE(node->ctor, nullptr);
     // implicitly generated default constructor is not cleared
-    ASSERT_NE(node->factory, nullptr);
+    ASSERT_NE(node->default_constructor, nullptr);
 }