소스 검색

emitter: make it run smoothly across boundaries

Michele Caini 4 년 전
부모
커밋
8e2a6470ac
4개의 변경된 파일15개의 추가작업 그리고 80개의 파일을 삭제
  1. 12 18
      src/entt/signal/emitter.hpp
  2. 0 13
      test/lib/emitter_plugin/main.cpp
  3. 3 21
      test/lib/emitter_plugin/plugin.cpp
  4. 0 28
      test/lib/emitter_plugin/type_context.h

+ 12 - 18
src/entt/signal/emitter.hpp

@@ -8,8 +8,8 @@
 #include <memory>
 #include <type_traits>
 #include <utility>
-#include <vector>
 #include "../config/config.h"
+#include "../container/dense_hash_map.hpp"
 #include "../core/fwd.hpp"
 #include "../core/type_info.hpp"
 
@@ -120,23 +120,19 @@ class emitter {
 
     template<typename Event>
     [[nodiscard]] pool_handler<Event> *assure() {
-        const auto index = type_index<Event>::value();
-
-        if(!(index < pools.size())) {
-            pools.resize(std::size_t(index) + 1u);
-        }
-
-        if(!pools[index]) {
-            pools[index].reset(new pool_handler<Event>{});
+        if(auto &&ptr = pools[type_id<Event>().hash()]; !ptr) {
+            auto *cpool = new pool_handler<Event>{};
+            ptr.reset(cpool);
+            return cpool;
+        } else {
+            return static_cast<pool_handler<Event> *>(ptr.get());
         }
-
-        return static_cast<pool_handler<Event> *>(pools[index].get());
     }
 
     template<typename Event>
     [[nodiscard]] const pool_handler<Event> *assure() const {
-        const auto index = type_index<Event>::value();
-        return (!(index < pools.size()) || !pools[index]) ? nullptr : static_cast<const pool_handler<Event> *>(pools[index].get());
+        const auto it = pools.find(type_id<Event>().hash());
+        return (it == pools.cend()) ? nullptr : static_cast<const pool_handler<Event> *>(it->second.get());
     }
 
 public:
@@ -285,9 +281,7 @@ public:
      */
     void clear() ENTT_NOEXCEPT {
         for(auto &&cpool: pools) {
-            if(cpool) {
-                cpool->clear();
-            }
+            cpool.second->clear();
         }
     }
 
@@ -308,12 +302,12 @@ public:
      */
     [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
         return std::all_of(pools.cbegin(), pools.cend(), [](auto &&cpool) {
-            return !cpool || cpool->empty();
+            return cpool.second->empty();
         });
     }
 
 private:
-    std::vector<std::unique_ptr<basic_pool>> pools{};
+    dense_hash_map<id_type, std::unique_ptr<basic_pool>> pools{};
 };
 
 } // namespace entt

+ 0 - 13
test/lib/emitter_plugin/main.cpp

@@ -2,19 +2,9 @@
 
 #include <gtest/gtest.h>
 #include <cr.h>
-#include <entt/core/type_info.hpp>
 #include <entt/signal/emitter.hpp>
-#include "type_context.h"
 #include "types.h"
 
-template<typename Type>
-struct entt::type_index<Type> {
-    [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
-        static const entt::id_type value = type_context::instance()->value(entt::type_hash<Type>::value());
-        return value;
-    }
-};
-
 TEST(Lib, Emitter) {
     test_emitter emitter;
     int value{};
@@ -26,9 +16,6 @@ TEST(Lib, Emitter) {
     cr_plugin ctx;
     cr_plugin_load(ctx, PLUGIN);
 
-    ctx.userdata = type_context::instance();
-    cr_plugin_update(ctx);
-
     ctx.userdata = &emitter;
     cr_plugin_update(ctx);
 

+ 3 - 21
test/lib/emitter_plugin/plugin.cpp

@@ -1,31 +1,13 @@
 #include <cr.h>
-#include <entt/core/type_info.hpp>
 #include <entt/signal/emitter.hpp>
-#include "type_context.h"
 #include "types.h"
 
-struct ctx {
-    inline static type_context *ref;
-};
-
-template<typename Type>
-struct entt::type_index<Type> {
-    [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
-        static const entt::id_type value = ctx::ref->value(entt::type_hash<Type>::value());
-        return value;
-    }
-};
-
 CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {
     switch(operation) {
     case CR_STEP:
-        if(!ctx::ref) {
-            ctx::ref = static_cast<type_context *>(ctx->userdata);
-        } else {
-            static_cast<test_emitter *>(ctx->userdata)->publish<event>();
-            static_cast<test_emitter *>(ctx->userdata)->publish<message>(42);
-            static_cast<test_emitter *>(ctx->userdata)->publish<message>(3);
-        }
+        static_cast<test_emitter *>(ctx->userdata)->publish<event>();
+        static_cast<test_emitter *>(ctx->userdata)->publish<message>(42);
+        static_cast<test_emitter *>(ctx->userdata)->publish<message>(3);
         break;
     case CR_CLOSE:
     case CR_LOAD:

+ 0 - 28
test/lib/emitter_plugin/type_context.h

@@ -1,28 +0,0 @@
-#ifndef ENTT_LIB_EMITTER_PLUGIN_TYPE_CONTEXT_H
-#define ENTT_LIB_EMITTER_PLUGIN_TYPE_CONTEXT_H
-
-#include <unordered_map>
-#include <entt/core/fwd.hpp>
-
-class type_context {
-    type_context() = default;
-
-public:
-    inline entt::id_type value(const entt::id_type name) {
-        if(name_to_index.find(name) == name_to_index.cend()) {
-            name_to_index[name] = entt::id_type(name_to_index.size());
-        }
-
-        return name_to_index[name];
-    }
-
-    static type_context *instance() {
-        static type_context self{};
-        return &self;
-    }
-
-private:
-    std::unordered_map<entt::id_type, entt::id_type> name_to_index{};
-};
-
-#endif