ADD: improved calling interfacce

This commit is contained in:
Dominik Meyer 2021-08-04 12:04:30 +02:00
parent e06a5b9be5
commit 9a0efd0b62
No known key found for this signature in database
GPG Key ID: B4C312B600606B64
4 changed files with 41 additions and 9 deletions

View File

@ -28,7 +28,7 @@
// forward declaration of EventManager::Participant // forward declaration of EventManager::Participant
class Participant; class Participant;
class Manager class Manager : public std::enable_shared_from_this<Manager>
{ {
/// the thread the event manager is transmitting events in /// the thread the event manager is transmitting events in
std::unique_ptr<std::thread> mainThread_; std::unique_ptr<std::thread> mainThread_;
@ -185,6 +185,17 @@
*/ */
void schedule(std::shared_ptr<EventManager::Participant> plugin); 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 }; // class Manager
}; //namespace EventManager }; //namespace EventManager

View File

@ -64,6 +64,14 @@
*/ */
virtual void schedule_() { throw std::runtime_error(std::string(__PRETTY_FUNCTION__) + " not implemented");} 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: protected:
@ -141,6 +149,12 @@
*/ */
void schedule() {schedule_();}; 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 * @brief emit an event to this participant

View File

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

View File

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