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

meta: refine checks on factory construction

skypjack 1 неделя назад
Родитель
Сommit
df71e64c29
2 измененных файлов с 16 добавлено и 15 удалено
  1. 1 4
      TODO
  2. 15 11
      src/entt/meta/factory.hpp

+ 1 - 4
TODO

@@ -34,11 +34,8 @@ TODO:
 * test trivially_destructible optimization
 * document stl and injections support (md)
 * document new meta multi support (md)
-* improve meta_reset and similar on multi contexts
-* review/refine/improve meta factory base for typed vs overloaded types (ctor and type)
 * fast lookup on meta reset
 * check for duplicates in basic_meta_factory constructor (with tests)
-* test (and fix) resolve where the type id conflicts with the type hash
-* assert and test type mismatch on meta factory construction and type(...) - ie id vs alias on overloaded and standard
+* check for alias validity (not a key, non an alias) in meta_factory::type - with tests (also for resolve and meta_reset)
 * use meta_any::type(value) with from_void and the like to avoid lookups in some cases
 * try to fully support uint16_t for entity types

+ 15 - 11
src/entt/meta/factory.hpp

@@ -51,10 +51,14 @@ class basic_meta_factory {
         return overload;
     }
 
+    bool unique_alias(const id_type alias) const noexcept {
+        return (stl::find_if(ctx->bucket.cbegin(), ctx->bucket.cend(), [alias](const auto &value) { return value.second->alias == alias; }) == ctx->bucket.cend());
+    }
+
 protected:
     void type(const id_type alias, const char *name) noexcept {
         state = mode::type;
-        ENTT_ASSERT((parent->alias == alias) || stl::find_if(ctx->bucket.cbegin(), ctx->bucket.cend(), [alias](const auto &value) { return value.second->alias == alias; }) == ctx->bucket.cend(), "Duplicate identifier");
+        ENTT_ASSERT((parent->alias == alias) || unique_alias(alias), "Duplicate identifier");
         parent->alias = alias;
         parent->name = name;
     }
@@ -138,8 +142,10 @@ public:
           bucket{},
           state{mode::type} {
         if(const auto it = ctx->bucket.find(id); it == ctx->bucket.cend()) {
+            ENTT_ASSERT(unique_alias(id), "Duplicate identifier");
             parent = ctx->bucket.emplace(id, stl::make_unique<meta_type_node>(stl::move(node))).first->second.get();
             parent->details = stl::make_unique<meta_type_descriptor>();
+            parent->alias = id;
         } else {
             parent = it->second.get();
         }
@@ -172,13 +178,6 @@ public:
     meta_factory() noexcept
         : meta_factory{locator<meta_ctx>::value_or()} {}
 
-    /**
-     * @brief Constructs an unconstrained type assigned to a given identifier.
-     * @param id A custom unique identifier.
-     */
-    meta_factory(const id_type id) noexcept
-        : meta_factory{locator<meta_ctx>::value_or(), id} {}
-
     /**
      * @brief Context aware constructor.
      * @param area The context into which to construct meta types.
@@ -186,15 +185,20 @@ public:
     meta_factory(meta_ctx &area) noexcept
         : base_type{area, internal::setup_node_for<element_type>(), type_hash<Type>::value()} {}
 
+    /**
+     * @brief Constructs an unconstrained type assigned to a given identifier.
+     * @param id A custom unique identifier.
+     */
+    meta_factory(const id_type id) noexcept
+        : meta_factory{locator<meta_ctx>::value_or(), id} {}
+
     /**
      * @brief Context aware constructor.
      * @param id A custom unique identifier.
      * @param area The context into which to construct meta types.
      */
     meta_factory(meta_ctx &area, const id_type id) noexcept
-        : base_type{area, internal::setup_node_for<element_type>(), id} {
-        base_type::type(id, nullptr);
-    }
+        : base_type{area, internal::setup_node_for<element_type>(), id} {}
 
     /**
      * @brief Assigns a custom unique identifier to a meta type.