Browse Source

ADD: improved calling interfacce

main
Dominik Meyer 3 months ago
parent
commit
9a0efd0b62
Signed by: byterazor GPG Key ID: B4C312B600606B64
  1. 13
      include/EventManager/Manager.hpp
  2. 14
      include/EventManager/Participant.hpp
  3. 7
      src/EventManager/Manager.cpp
  4. 16
      tests/test_basic.cpp

13
include/EventManager/Manager.hpp

@ -28,7 +28,7 @@
// forward declaration of EventManager::Participant
class Participant;
class Manager
class Manager : public std::enable_shared_from_this<Manager>
{
/// the thread the event manager is transmitting events in
std::unique_ptr<std::thread> mainThread_;
@ -185,6 +185,17 @@
*/
void schedule(std::shared_ptr<EventManager::Participant> plugin);
/**
* @brief method to connect a particpant to the manager
*/
void connect(std::shared_ptr<EventManager::Participant> participant);
/**
* @brief method to disconnect a particpant from the manager
*/
void disconnect(std::shared_ptr<EventManager::Participant> participant);
}; // class Manager
}; //namespace EventManager

14
include/EventManager/Participant.hpp

@ -64,6 +64,14 @@
*/
virtual void schedule_() { throw std::runtime_error(std::string(__PRETTY_FUNCTION__) + " not implemented");}
/**
* @brief method called by the EventManager::Manager on connecting this particpant
*
* This method need to be implemented by each child class of EventManager::Participant
* Its the best location for subscribing to events and enable scheduling if required.
*/
virtual void init_() {throw std::runtime_error(std::string(__PRETTY_FUNCTION__) + " not implemented");}
protected:
@ -141,6 +149,12 @@
*/
void schedule() {schedule_();};
/**
* @brief method called by the EventManager::Manager when connecting the particpant
*
* method calls virtual _init method which has to be implemented by each particpants child class
*/
void init() {init_();}
/**
* @brief emit an event to this participant

7
src/EventManager/Manager.cpp

@ -324,3 +324,10 @@
schedulingParticipants_.push_back(participant);
}
}
void EventManager::Manager::connect(std::shared_ptr<EventManager::Participant> participant)
{
participant->setManager(shared_from_this());
participant->init();
}

16
tests/test_basic.cpp

@ -33,6 +33,11 @@ class myParticipant : public EventManager::Participant
_unsubscribe();
}
void init_() {
_subscribe(eventType_);
_enableScheduling();
}
void schedule_() {
std::shared_ptr<EventManager::Event> event = nullptr;
@ -67,10 +72,7 @@ class myParticipant : public EventManager::Participant
bool eventReceived() const { return receivedEvent_;}
void init() {
_subscribe(eventType_);
_enableScheduling();
}
};
@ -91,8 +93,7 @@ SCENARIO("Basic Usage of EventManager", "[Manager]")
REQUIRE_NOTHROW([&]()
{
participant0 = std::make_shared<myParticipant>(0,TEST_EVENT0);
participant0->setManager(manager);
participant0->init();
manager->connect(participant0);
}());
REQUIRE(manager->empty() == false);
@ -101,8 +102,7 @@ SCENARIO("Basic Usage of EventManager", "[Manager]")
REQUIRE_NOTHROW([&]()
{
participant1 = std::make_shared<myParticipant>(1,TEST_EVENT1);
participant1->setManager(manager);
participant1->init();
manager->connect(participant1);
}());
REQUIRE(manager->empty() == false);

Loading…
Cancel
Save