From e6644d6b235005de9ba1b9884472fa5f5d8d6074 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 20 Mar 2022 18:49:49 +0100 Subject: refactor: update event system terminology & structure --- src/interfaces/command.hpp | 11 ++++++++++- src/interfaces/engine.hpp | 2 -- src/interfaces/input.hpp | 20 +++++++++++++------- src/interfaces/observable.hpp | 20 -------------------- src/interfaces/publisher.hpp | 20 ++++++++++++++++++++ src/interfaces/subscriber.hpp | 8 ++++++++ 6 files changed, 51 insertions(+), 30 deletions(-) delete mode 100644 src/interfaces/observable.hpp create mode 100644 src/interfaces/publisher.hpp create mode 100644 src/interfaces/subscriber.hpp (limited to 'src/interfaces') diff --git a/src/interfaces/command.hpp b/src/interfaces/command.hpp index 245bdd6..62d7a59 100644 --- a/src/interfaces/command.hpp +++ b/src/interfaces/command.hpp @@ -1,9 +1,18 @@ #pragma once -class ICommand +#include "interfaces/subscriber.hpp" + +#include + +class ICommand : public ISubscriber { public: virtual ~ICommand() = default; virtual void execute() noexcept = 0; + + void update(const std::nullptr_t & /*context*/) noexcept override + { + execute(); + }; }; diff --git a/src/interfaces/engine.hpp b/src/interfaces/engine.hpp index 498ff41..6830340 100644 --- a/src/interfaces/engine.hpp +++ b/src/interfaces/engine.hpp @@ -1,7 +1,5 @@ #pragma once -#include "interfaces/observable.hpp" - #include class ICLIGameEngine diff --git a/src/interfaces/input.hpp b/src/interfaces/input.hpp index 82ad57c..795ade2 100644 --- a/src/interfaces/input.hpp +++ b/src/interfaces/input.hpp @@ -1,21 +1,27 @@ #pragma once -#include "interfaces/command.hpp" -#include "interfaces/observable.hpp" +#include "interfaces/publisher.hpp" +#include "interfaces/subscriber.hpp" +#include #include -class IInputHandler : public IObservable +class IInputHandler : public IPublisher { public: + using Event = char; + using Context = std::nullptr_t; + ~IInputHandler() noexcept override = default; - void listen() const noexcept override = 0; + virtual void listen() const noexcept = 0; - void attach(const char &key, - const std::shared_ptr &command) noexcept override = 0; + void subscribe( + const Event &event, + const std::shared_ptr> &subscriber) noexcept override = 0; - void notify(const char &key) const noexcept override = 0; + void notify_subscribers(const Event &event, + const Context &context) const noexcept override = 0; virtual void enter_raw_mode() noexcept = 0; diff --git a/src/interfaces/observable.hpp b/src/interfaces/observable.hpp deleted file mode 100644 index 21fed76..0000000 --- a/src/interfaces/observable.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include "interfaces/command.hpp" - -#include -#include - -template -class IObservable -{ -public: - virtual ~IObservable() noexcept = default; - - virtual void listen() const noexcept = 0; - - virtual void attach(const char &event, - const std::shared_ptr &command) noexcept = 0; - - virtual void notify(const Event &event) const noexcept = 0; -}; diff --git a/src/interfaces/publisher.hpp b/src/interfaces/publisher.hpp new file mode 100644 index 0000000..14766ed --- /dev/null +++ b/src/interfaces/publisher.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include "interfaces/subscriber.hpp" + +#include +#include + +template +class IPublisher +{ +public: + virtual ~IPublisher() noexcept = default; + + virtual void + subscribe(const Event &event, + const std::shared_ptr> &subscriber) noexcept = 0; + + virtual void notify_subscribers(const Event &event, + const Context &context) const noexcept = 0; +}; diff --git a/src/interfaces/subscriber.hpp b/src/interfaces/subscriber.hpp new file mode 100644 index 0000000..17d87ef --- /dev/null +++ b/src/interfaces/subscriber.hpp @@ -0,0 +1,8 @@ +#pragma once + +template +class ISubscriber +{ +public: + virtual void update(const Context &context) noexcept = 0; +}; -- cgit v1.2.3-18-g5258