Explorar o código

Add possibility to provide event parameter

Patrick-Christopher Mattulat hai 5 meses
pai
achega
bb7e42dbf2

+ 3 - 1
CMakeLists.txt

@@ -206,7 +206,8 @@ set(SOURCE_FILES_ENCODING
 set(SOURCE_FILES_EVENT
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/event/Event.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/event/EventListener.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/event/EventManager.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/event/EventManager.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/event/EventParameter.cpp)
 
 set(SOURCE_FILES_IO
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/evaluator/FileExistenceEvaluator.cpp
@@ -338,6 +339,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
     set(UNIT_TEST_FILES_EVENT
             ${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/EventParameterTest.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

+ 7 - 1
include/ls-std/event/EventListener.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_EVENT_LISTENER_HPP
 
 #include "Event.hpp"
+#include "EventParameter.hpp"
 #include <ls-std/core/Class.hpp>
 #include <ls-std/event/type/EventTypes.hpp>
 #include <ls-std/os/dynamic-goal.hpp>
@@ -26,9 +27,14 @@ namespace ls::std::event
 
       [[nodiscard]] ls::std::event::type::listener_id getId() const;
       void setId(ls::std::event::type::listener_id _id);
+      void setParameter(const ls::std::event::EventParameter &_parameter);
       [[maybe_unused]] bool subscribe(const ls::std::event::Event &_event, const ls::std::event::type::event_action &_action);
       [[maybe_unused]] bool unsubscribe(const ls::std::event::Event &_event);
 
+    protected:
+
+    ls::std::event::EventParameter parameter{};
+
     private:
 
       ls::std::event::type::listener_id id{};

+ 4 - 0
include/ls-std/event/EventManager.hpp

@@ -12,6 +12,8 @@
 
 #include "Event.hpp"
 #include "EventListener.hpp"
+#include "EventParameter.hpp"
+
 #include <list>
 #include <ls-std/core/Class.hpp>
 #include <ls-std/event/type/EventTypes.hpp>
@@ -37,6 +39,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);
+      void invoke(const ls::std::event::Event &_event, const ls::std::event::EventParameter &_parameter);
       [[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);
@@ -46,6 +49,7 @@ namespace ls::std::event
       ls::std::event::type::event_observability_inventory inventory{};
       ls::std::event::type::listener_id provisionId = 1;
 
+      static void _giveListenersParameter(const ls::std::event::type::event_listeners &_listeners, const ls::std::event::EventParameter &_parameter);
       static void _notifyListeners(const ls::std::event::type::event_listeners &_listeners);
       bool _observesEvent(const ls::std::event::Event &_event) const;
   };

+ 35 - 0
include/ls-std/event/EventParameter.hpp

@@ -0,0 +1,35 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2024-05-30
+* Changed:         2024-05-30
+*
+* */
+
+#ifndef EVENT_PARAMETER_HPP
+#define EVENT_PARAMETER_HPP
+
+#include <list>
+#include <ls-std/os/dynamic-goal.hpp>
+#include <string>
+
+namespace ls::std::event
+{
+  class LS_STD_DYNAMIC_GOAL EventParameter
+  {
+    public:
+
+      EventParameter();
+      ~EventParameter();
+
+      void addParameter(const ::std::string &_parameter);
+      [[nodiscard]] ::std::list<::std::string> get() const;
+
+    private:
+
+      ::std::list<::std::string> parameters{};
+  };
+}
+
+#endif

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

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-05-14
- * Changed:         2024-05-17
+ * Changed:         2024-05-30
  *
  * */
 
@@ -15,5 +15,6 @@
 #include <ls-std/event/Event.hpp>
 #include <ls-std/event/EventListener.hpp>
 #include <ls-std/event/EventManager.hpp>
+#include <ls-std/event/EventParameter.hpp>
 
 #endif

+ 7 - 1
source/ls-std/event/EventListener.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
 *
 * */
 
@@ -16,6 +16,7 @@ using ls::std::core::NullPointerEvaluator;
 using ls::std::event::Event;
 using ls::std::event::EventListener;
 using ls::std::event::EventManager;
+using ls::std::event::EventParameter;
 using ls::std::event::type::event_action;
 using ls::std::event::type::listener_id;
 using ::std::dynamic_pointer_cast;
@@ -36,6 +37,11 @@ void EventListener::setId(listener_id _id)
   this->id = _id;
 }
 
+void EventListener::setParameter(const EventParameter &_parameter)
+{
+  this->parameter = _parameter;
+}
+
 bool EventListener::subscribe(const Event &_event, const event_action &_action)
 {
   NullPointerEvaluator(_event.getManager(), "no event manager is provided for " + _event.getClassName()).evaluate();

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

@@ -10,13 +10,13 @@
 #include <ls-std/core/exception/EventNotHandledException.hpp>
 #include <ls-std/event/EventListener.hpp>
 #include <ls-std/event/EventManager.hpp>
-#include <utility>
 
 using ls::std::core::Class;
 using ls::std::core::EventNotHandledException;
 using ls::std::event::Event;
 using ls::std::event::EventListener;
 using ls::std::event::EventManager;
+using ls::std::event::EventParameter;
 using ls::std::event::type::event_action;
 using ls::std::event::type::event_listeners;
 using ls::std::event::type::listener_id;
@@ -60,11 +60,21 @@ void EventManager::invoke(const Event &_event)
   {
     throw EventNotHandledException("event " + _event.getClassName() + " is not known by event manager");
   }
-  else
+
+  const auto &listeners = this->inventory[_event.getClassName()];
+  EventManager::_notifyListeners(listeners);
+}
+
+void EventManager::invoke(const Event &_event, const EventParameter &_parameter)
+{
+  if (!this->_observesEvent(_event))
   {
-    const auto &listeners = this->inventory[_event.getClassName()];
-    EventManager::_notifyListeners(listeners);
+    throw EventNotHandledException("event " + _event.getClassName() + " is not known by event manager");
   }
+
+  const auto &listeners = this->inventory[_event.getClassName()];
+  EventManager::_giveListenersParameter(listeners, _parameter);
+  EventManager::_notifyListeners(listeners);
 }
 
 listener_id EventManager::requestListenerId()
@@ -101,6 +111,14 @@ void EventManager::unsubscribeListenerForEvent(const shared_ptr<EventListener> &
   }
 }
 
+void EventManager::_giveListenersParameter(const event_listeners &_listeners, const EventParameter &_parameter)
+{
+  for (const auto &[listener, action] : _listeners)
+  {
+    listener->setParameter(_parameter);
+  }
+}
+
 void EventManager::_notifyListeners(const event_listeners &_listeners)
 {
   for (const auto &[listener, eventAction] : _listeners)

+ 28 - 0
source/ls-std/event/EventParameter.cpp

@@ -0,0 +1,28 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2024-05-30
+* Changed:         2024-05-30
+*
+* */
+
+#include <ls-std/event/EventParameter.hpp>
+
+using ls::std::event::EventParameter;
+using ::std::list;
+using ::std::string;
+
+EventParameter::EventParameter() = default;
+
+EventParameter::~EventParameter() = default;
+
+void EventParameter::addParameter(const string &_parameter)
+{
+  this->parameters.push_back(_parameter);
+}
+
+list<string> EventParameter::get() const
+{
+  return this->parameters;
+}

+ 12 - 3
test/cases/event/EventManagerTest.cpp

@@ -15,6 +15,7 @@
 
 using ls::std::core::EventNotHandledException;
 using ls::std::event::EventManager;
+using ls::std::event::EventParameter;
 using ::std::make_shared;
 using ::std::string;
 using test::event::Button;
@@ -30,6 +31,14 @@ namespace
 
       EventManagerTest() = default;
       ~EventManagerTest() override = default;
+
+      static EventParameter generateParameter(const string &_color)
+      {
+        EventParameter parameter{};
+        parameter.addParameter(_color);
+
+        return parameter;
+      }
   };
 
   TEST_F(EventManagerTest, getClassName)
@@ -72,16 +81,16 @@ namespace
     ASSERT_STREQ("black", myGreenButton->getColor().c_str());
 
     eventManager->invoke(OnClickEvent());
-    eventManager->invoke(ChangeColorEvent());
+    eventManager->invoke(ChangeColorEvent(), EventManagerTest::generateParameter("pink"));
 
     // status with events invoked
 
     ASSERT_TRUE(myBlueButton->isClicked());
-    ASSERT_STREQ("blue", myBlueButton->getColor().c_str());
+    ASSERT_STREQ("pink", 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());
+    ASSERT_STREQ("pink", myGreenButton->getColor().c_str());
   }
 
   TEST_F(EventManagerTest, invoke_event_not_known)

+ 40 - 0
test/cases/event/EventParameterTest.cpp

@@ -0,0 +1,40 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2024-05-30
+* Changed:         2024-05-30
+*
+* */
+
+#include <gtest/gtest.h>
+#include <ls-std/ls-std-event.hpp>
+
+using ls::std::event::EventParameter;
+using testing::Test;
+
+namespace
+{
+  class EventParameterTest : public Test
+  {
+    public:
+
+      EventParameterTest() = default;
+      ~EventParameterTest() override = default;
+  };
+
+  TEST_F(EventParameterTest, addParameter)
+  {
+    auto parameter = EventParameter();
+    ASSERT_TRUE(parameter.get().empty());
+
+    parameter.addParameter("blue");
+    ASSERT_EQ(1, parameter.get().size());
+    ASSERT_STREQ("blue", parameter.get().front().c_str());
+  }
+
+  TEST_F(EventParameterTest, get)
+  {
+    ASSERT_TRUE(EventParameter().get().empty());
+  }
+}

+ 1 - 1
test/classes/event/Button.cpp

@@ -30,7 +30,7 @@ bool Button::isClicked() const
 
 void Button::onChangeColorEvent()
 {
-  this->color = "blue";
+  this->color = this->parameter.get().front();
 }
 
 void Button::onClickEvent()