Browse Source

emitter: truly const emitter, no lazy initialization

Michele Caini 5 years ago
parent
commit
c2d1859858
2 changed files with 13 additions and 14 deletions
  1. 0 3
      TODO
  2. 13 11
      src/entt/signal/emitter.hpp

+ 0 - 3
TODO

@@ -25,12 +25,9 @@ WIP:
 * add exclude-only views to combine with packs
 * deprecate non-owning groups in favor of owning views and view packs, introduce lazy owning views
 * HP: write documentation for custom storages and views!!
-* use views within the registry, do not duplicate the logic for some operations
-* view pack: optimize it and iterate only first min size hint entities
 * view pack: plain function as an alias for operator|, reverse iterators, rbegin and rend
 * what about using hashed string rather than hash values for meta types?
 * pagination doesn't work nicely across boundaries probably, give it a look. RO operations are fine, adding components maybe not.
-* make it easier to hook into the type system and describe how to do that to eg auto-generate meta types on first use
 * add observer functions aside observer class
 * snapshot: support for range-based archives
 * update snapshot documentation to describe alternatives

+ 13 - 11
src/entt/signal/emitter.hpp

@@ -122,7 +122,7 @@ class emitter {
     };
 
     template<typename Event>
-    [[nodiscard]] const pool_handler<Event> & assure() const {
+    [[nodiscard]] pool_handler<Event> * assure() {
         const auto index = type_seq<Event>::value();
 
         if(!(index < pools.size())) {
@@ -133,12 +133,13 @@ class emitter {
             pools[index].reset(new pool_handler<Event>{});
         }
 
-        return static_cast<pool_handler<Event> &>(*pools[index]);
+        return static_cast<pool_handler<Event> *>(pools[index].get());
     }
 
     template<typename Event>
-    [[nodiscard]] pool_handler<Event> & assure() {
-        return const_cast<pool_handler<Event> &>(std::as_const(*this).template assure<Event>());
+    [[nodiscard]] const pool_handler<Event> * assure() const {
+        const auto index = type_seq<Event>::value();
+        return (!(index < pools.size()) || !pools[index]) ? nullptr : static_cast<const pool_handler<Event> *>(pools[index].get());
     }
 
 public:
@@ -200,7 +201,7 @@ public:
     template<typename Event, typename... Args>
     void publish(Args &&... args) {
         Event instance{std::forward<Args>(args)...};
-        assure<Event>().publish(instance, *static_cast<Derived *>(this));
+        assure<Event>()->publish(instance, *static_cast<Derived *>(this));
     }
 
     /**
@@ -225,7 +226,7 @@ public:
      */
     template<typename Event>
     connection<Event> on(listener<Event> instance) {
-        return assure<Event>().on(std::move(instance));
+        return assure<Event>()->on(std::move(instance));
     }
 
     /**
@@ -250,7 +251,7 @@ public:
      */
     template<typename Event>
     connection<Event> once(listener<Event> instance) {
-        return assure<Event>().once(std::move(instance));
+        return assure<Event>()->once(std::move(instance));
     }
 
     /**
@@ -264,7 +265,7 @@ public:
      */
     template<typename Event>
     void erase(connection<Event> conn) {
-        assure<Event>().erase(std::move(conn));
+        assure<Event>()->erase(std::move(conn));
     }
 
     /**
@@ -277,7 +278,7 @@ public:
      */
     template<typename Event>
     void clear() {
-        assure<Event>().clear();
+        assure<Event>()->clear();
     }
 
     /**
@@ -301,7 +302,8 @@ public:
      */
     template<typename Event>
     [[nodiscard]] bool empty() const {
-        return assure<Event>().empty();
+        const auto *cpool = assure<Event>();
+        return !cpool || cpool->empty();
     }
 
     /**
@@ -315,7 +317,7 @@ public:
     }
 
 private:
-    mutable std::vector<std::unique_ptr<basic_pool>> pools{};
+    std::vector<std::unique_ptr<basic_pool>> pools{};
 };