瀏覽代碼

Fix invoke method of EventManager class

Patrick-Christopher Mattulat 6 月之前
父節點
當前提交
443c21ae54

+ 1 - 0
CMakeLists.txt

@@ -339,6 +339,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/event/EventListenerTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/event/EventManagerTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/event/EventTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/event/ChangeColorEvent.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/event/OnClickEvent.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/event/Button.cpp)
 

+ 2 - 2
include/ls-std/event/EventManager.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2024-05-16
-* Changed:         2024-05-17
+* Changed:         2024-05-30
 *
 * */
 
@@ -36,7 +36,7 @@ namespace ls::std::event
 
       [[nodiscard]] ls::std::event::type::listener_id getNextProvisionId() const;
       [[nodiscard]] bool holdsListenerForEvent(ls::std::event::type::listener_id _id, const ls::std::event::Event &_event);
-      void invoke(const ls::std::event::Event &_event) const;
+      void invoke(const ls::std::event::Event &_event);
       [[nodiscard]] ls::std::event::type::listener_id requestListenerId();
       void subscribeListenerForEvent(::std::shared_ptr<ls::std::event::EventListener> _listener, const ls::std::event::Event &_event, ls::std::event::type::event_action _action);
       void unsubscribeListenerForEvent(const ::std::shared_ptr<ls::std::event::EventListener> &_listener, const ls::std::event::Event &_event);

+ 4 - 6
source/ls-std/event/EventManager.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2024-05-16
-* Changed:         2024-05-17
+* Changed:         2024-05-30
 *
 * */
 
@@ -54,7 +54,7 @@ bool EventManager::holdsListenerForEvent(listener_id _id, const Event &_event)
   return holdsListener;
 }
 
-void EventManager::invoke(const Event &_event) const
+void EventManager::invoke(const Event &_event)
 {
   if (!this->_observesEvent(_event))
   {
@@ -62,10 +62,8 @@ void EventManager::invoke(const Event &_event) const
   }
   else
   {
-    for (const auto &[eventName, listeners] : this->inventory)
-    {
-      EventManager::_notifyListeners(listeners);
-    }
+    const auto &listeners = this->inventory[_event.getClassName()];
+    EventManager::_notifyListeners(listeners);
   }
 }
 

+ 15 - 1
test/cases/event/EventManagerTest.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2024-05-16
-* Changed:         2024-05-17
+* Changed:         2024-05-30
 *
 * */
 
@@ -18,6 +18,7 @@ using ls::std::event::EventManager;
 using ::std::make_shared;
 using ::std::string;
 using test::event::Button;
+using test::event::ChangeColorEvent;
 using test::event::OnClickEvent;
 using testing::Test;
 
@@ -56,18 +57,31 @@ namespace
 
     auto myBlueButton = make_shared<Button>();
     myBlueButton->subscribe(OnClickEvent().of(eventManager), [myBlueButton]() mutable { myBlueButton->onClickEvent(); });
+    myBlueButton->subscribe(ChangeColorEvent().of(eventManager), [myBlueButton]() mutable { myBlueButton->onChangeColorEvent(); });
     auto myRedButton = make_shared<Button>();
     auto myGreenButton = make_shared<Button>();
+    myGreenButton->subscribe(ChangeColorEvent().of(eventManager), [myGreenButton]() mutable { myGreenButton->onChangeColorEvent(); });
+
+    // status without any event invoked
 
     ASSERT_FALSE(myBlueButton->isClicked());
+    ASSERT_STREQ("black", myBlueButton->getColor().c_str());
     ASSERT_FALSE(myRedButton->isClicked());
+    ASSERT_STREQ("black", myRedButton->getColor().c_str());
     ASSERT_FALSE(myGreenButton->isClicked());
+    ASSERT_STREQ("black", myGreenButton->getColor().c_str());
 
     eventManager->invoke(OnClickEvent());
+    eventManager->invoke(ChangeColorEvent());
+
+    // status with events invoked
 
     ASSERT_TRUE(myBlueButton->isClicked());
+    ASSERT_STREQ("blue", myBlueButton->getColor().c_str());
     ASSERT_FALSE(myRedButton->isClicked());
+    ASSERT_STREQ("black", myRedButton->getColor().c_str());
     ASSERT_FALSE(myGreenButton->isClicked());
+    ASSERT_STREQ("blue", myGreenButton->getColor().c_str());
   }
 
   TEST_F(EventManagerTest, invoke_event_not_known)

+ 13 - 2
test/classes/event/Button.cpp

@@ -3,25 +3,36 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2024-05-16
-* Changed:         2024-05-17
+* Changed:         2024-05-30
 *
 * */
 
 #include "Button.hpp"
 
 using ls::std::event::EventListener;
+using ::std::string;
 using test::event::Button;
 
-Button::Button() : EventListener()
+Button::Button() : EventListener(), color("black")
 {}
 
 Button::~Button() noexcept = default;
 
+string Button::getColor() const
+{
+  return this->color;
+}
+
 bool Button::isClicked() const
 {
   return this->clicked;
 }
 
+void Button::onChangeColorEvent()
+{
+  this->color = "blue";
+}
+
 void Button::onClickEvent()
 {
   this->clicked = !this->clicked;

+ 5 - 1
test/classes/event/Button.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2024-05-16
-* Changed:         2024-05-17
+* Changed:         2024-05-30
 *
 * */
 
@@ -11,6 +11,7 @@
 #define LS_STD_BUTTON_HPP
 
 #include <ls-std/ls-std-event.hpp>
+#include <string>
 
 namespace test::event
 {
@@ -21,12 +22,15 @@ namespace test::event
       explicit Button();
       ~Button() noexcept override;
 
+      [[nodiscard]] ::std::string getColor() const;
       [[nodiscard]] bool isClicked() const;
+      void onChangeColorEvent();
       void onClickEvent();
 
     private:
 
       bool clicked{};
+      ::std::string color{};
   };
 }
 

+ 18 - 0
test/classes/event/ChangeColorEvent.cpp

@@ -0,0 +1,18 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2024-05-30
+* Changed:         2024-05-30
+*
+* */
+
+#include "ChangeColorEvent.hpp"
+
+using ls::std::event::Event;
+using test::event::ChangeColorEvent;
+
+ChangeColorEvent::ChangeColorEvent() : Event("ChangeColorEvent")
+{}
+
+ChangeColorEvent::~ChangeColorEvent() noexcept = default;

+ 26 - 0
test/classes/event/ChangeColorEvent.hpp

@@ -0,0 +1,26 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2024-05-30
+* Changed:         2024-05-30
+*
+* */
+
+#ifndef LS_STD_CHANGE_COLOR_EVENT_HPP
+#define LS_STD_CHANGE_COLOR_EVENT_HPP
+
+#include <ls-std/event/Event.hpp>
+
+namespace test::event
+{
+  class ChangeColorEvent : public ls::std::event::Event
+  {
+    public:
+
+      explicit ChangeColorEvent();
+      ~ChangeColorEvent() noexcept override;
+  };
+}
+
+#endif

+ 2 - 1
test/ls-std-event-test.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-05-14
- * Changed:         2024-05-16
+ * Changed:         2024-05-30
  *
  * */
 
@@ -11,6 +11,7 @@
 #define LS_STD_LS_STD_EVENT_TEST_HPP
 
 #include <classes/event/Button.hpp>
+#include <classes/event/ChangeColorEvent.hpp>
 #include <classes/event/OnClickEvent.hpp>
 
 #endif