1
0
Эх сурвалжийг харах

emitter: the event isn't const by default anymore (close #547)

Michele Caini 5 жил өмнө
parent
commit
f689014c3e

+ 5 - 5
docs/md/signal.md

@@ -477,9 +477,9 @@ The full list of accepted types of events isn't required. Handlers are created
 internally on the fly and thus each type of event is accepted by default.
 
 Whenever an event is published, an emitter provides the listeners with a
-reference to itself along with a const reference to the event. Therefore
-listeners have an handy way to work with it without incurring in the need of
-capturing a reference to the emitter itself.<br/>
+reference to itself along with a reference to the event. Therefore listeners
+have an handy way to work with it without incurring in the need of capturing a
+reference to the emitter itself.<br/>
 In addition, an opaque object is returned each time a connection is established
 between an emitter and a listener, allowing the caller to disconnect them at a
 later time.<br/>
@@ -493,10 +493,10 @@ my_emitter emitter{};
 ```
 
 Listeners must be movable and callable objects (free functions, lambdas,
-functors, `std::function`s, whatever) whose function type is:
+functors, `std::function`s, whatever) whose function type is compatible with:
 
 ```cpp
-void(const Event &, my_emitter &)
+void(Event &, my_emitter &)
 ```
 
 Where `Event` is the type of event they want to listen.<br/>

+ 9 - 8
src/entt/signal/emitter.hpp

@@ -33,9 +33,9 @@ namespace entt {
  * Pools for the type of events are created internally on the fly. It's not
  * required to specify in advance the full list of accepted types.<br/>
  * Moreover, whenever an event is published, an emitter provides the listeners
- * with a reference to itself along with a const reference to the event.
- * Therefore listeners have an handy way to work with it without incurring in
- * the need of capturing a reference to the emitter.
+ * with a reference to itself along with a reference to the event. Therefore
+ * listeners have an handy way to work with it without incurring in the need of 
+ * capturing a reference to the emitter.
  *
  * @tparam Derived Actual type of emitter that extends the class template.
  */
@@ -50,7 +50,7 @@ class emitter {
 
     template<typename Event>
     struct pool_handler final: basic_pool {
-        using listener_type = std::function<void(const Event &, Derived &)>;
+        using listener_type = std::function<void(Event &, Derived &)>;
         using element_type = std::pair<bool, listener_type>;
         using container_type = std::list<element_type>;
         using connection_type = typename container_type::iterator;
@@ -95,7 +95,7 @@ class emitter {
             }
         }
 
-        void publish(const Event &event, Derived &ref) {
+        void publish(Event &event, Derived &ref) {
             container_type swap_list;
             once_list.swap(swap_list);
 
@@ -209,7 +209,8 @@ public:
      */
     template<typename Event, typename... Args>
     void publish(Args &&... args) {
-        assure<Event>().publish(Event{std::forward<Args>(args)...}, *static_cast<Derived *>(this));
+        Event instance{std::forward<Args>(args)...};
+        assure<Event>().publish(instance, *static_cast<Derived *>(this));
     }
 
     /**
@@ -221,7 +222,7 @@ public:
      * to be used later to disconnect the listener if required.
      *
      * The listener is as a callable object that can be moved and the type of
-     * which is `void(const Event &, Derived &)`.
+     * which is _compatible_ with `void(Event &, Derived &)`.
      *
      * @note
      * Whenever an event is emitted, the emitter provides the listener with a
@@ -246,7 +247,7 @@ public:
      * to be used later to disconnect the listener if required.
      *
      * The listener is as a callable object that can be moved and the type of
-     * which is `void(const Event &, Derived &)`.
+     * which is _compatible_ with `void(Event &, Derived &)`.
      *
      * @note
      * Whenever an event is emitted, the emitter provides the listener with a

+ 6 - 6
test/entt/signal/emitter.cpp

@@ -13,7 +13,7 @@ TEST(Emitter, Clear) {
 
     ASSERT_TRUE(emitter.empty());
 
-    emitter.on<foo_event>([](const auto &, const auto &){});
+    emitter.on<foo_event>([](auto &, const auto &){});
     emitter.once<quux_event>([](const auto &, const auto &){});
 
     ASSERT_FALSE(emitter.empty());
@@ -35,7 +35,7 @@ TEST(Emitter, Clear) {
     ASSERT_FALSE(emitter.empty<quux_event>());
     ASSERT_TRUE(emitter.empty<bar_event>());
 
-    emitter.on<foo_event>([](const auto &, const auto &){});
+    emitter.on<foo_event>([](auto &, const auto &){});
     emitter.on<bar_event>([](const auto &, const auto &){});
 
     ASSERT_FALSE(emitter.empty());
@@ -55,8 +55,8 @@ TEST(Emitter, ClearPublishing) {
 
     ASSERT_TRUE(emitter.empty());
 
-    emitter.once<foo_event>([](const auto &, auto &em){
-        em.template once<foo_event>([](const auto &, auto &){});
+    emitter.once<foo_event>([](auto &, auto &em){
+        em.template once<foo_event>([](auto &, auto &){});
         em.template clear<foo_event>();
     });
 
@@ -76,7 +76,7 @@ TEST(Emitter, ClearPublishing) {
 TEST(Emitter, On) {
     test_emitter emitter;
 
-    emitter.on<foo_event>([](const auto &, const auto &){});
+    emitter.on<foo_event>([](auto &, const auto &){});
 
     ASSERT_FALSE(emitter.empty());
     ASSERT_FALSE(emitter.empty<foo_event>());
@@ -104,7 +104,7 @@ TEST(Emitter, Once) {
 TEST(Emitter, OnceAndErase) {
     test_emitter emitter;
 
-    auto conn = emitter.once<foo_event>([](const auto &, const auto &){});
+    auto conn = emitter.once<foo_event>([](auto &, const auto &){});
 
     ASSERT_FALSE(emitter.empty());
     ASSERT_FALSE(emitter.empty<foo_event>());