aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/engine/engine.cpp2
-rw-r--r--src/engine/user/input.cpp24
-rw-r--r--src/engine/user/input.hpp17
-rw-r--r--src/interfaces/command.hpp11
-rw-r--r--src/interfaces/engine.hpp2
-rw-r--r--src/interfaces/input.hpp20
-rw-r--r--src/interfaces/observable.hpp20
-rw-r--r--src/interfaces/publisher.hpp20
-rw-r--r--src/interfaces/subscriber.hpp8
9 files changed, 74 insertions, 50 deletions
diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp
index ed81ed3..e463f28 100644
--- a/src/engine/engine.cpp
+++ b/src/engine/engine.cpp
@@ -49,6 +49,6 @@ void CLIGameEngine::_configure_input(
auto key = config_pair.first;
auto command = config_pair.second;
- _input_handler->attach(key, command);
+ _input_handler->subscribe(key, command);
}
}
diff --git a/src/engine/user/input.cpp b/src/engine/user/input.cpp
index f1685fd..5e6f517 100644
--- a/src/engine/user/input.cpp
+++ b/src/engine/user/input.cpp
@@ -8,25 +8,26 @@ void InputHandler::listen() const noexcept
while (read(STDIN_FILENO, &character, 1) == 1)
{
- notify(character);
+ notify_subscribers(character, nullptr);
}
}
-void InputHandler::attach(const char &key,
- const std::shared_ptr<ICommand> &command) noexcept
+void InputHandler::subscribe(
+ const Event &event, const std::shared_ptr<ISubscriber<Context>> &subscriber) noexcept
{
- auto key_index = _key_as_index(key);
+ auto event_index = _event_as_index(event);
- _key_commands.at(key_index).push_back(command);
+ _subscribers.at(event_index).push_back(subscriber);
}
-void InputHandler::notify(const char &key) const noexcept
+void InputHandler::notify_subscribers(const Event &event,
+ const Context &context) const noexcept
{
- auto key_index = _key_as_index(key);
+ auto event_index = _event_as_index(event);
- for (const auto &command : _key_commands.at(key_index))
+ for (const auto &subscriber : _subscribers.at(event_index))
{
- command->execute();
+ subscriber->update(context);
}
}
@@ -58,7 +59,8 @@ void InputHandler::leave_raw_mode() noexcept
_original_termios = nullptr;
}
-InputHandler::_KeyCommandsSizeType InputHandler::_key_as_index(const char &key) noexcept
+InputHandler::_SubscribersSizeType
+InputHandler::_event_as_index(const Event &event) noexcept
{
- return static_cast<_KeyCommandsSizeType>(static_cast<uint8_t>(key));
+ return static_cast<_SubscribersSizeType>(static_cast<uint8_t>(event));
}
diff --git a/src/engine/user/input.hpp b/src/engine/user/input.hpp
index d2aa447..7a3c88e 100644
--- a/src/engine/user/input.hpp
+++ b/src/engine/user/input.hpp
@@ -1,9 +1,8 @@
#pragma once
#include "DI/auto_wirable.hpp"
-#include "interfaces/command.hpp"
#include "interfaces/input.hpp"
-#include "interfaces/observable.hpp"
+#include "interfaces/subscriber.hpp"
#include <limits>
#include <memory>
@@ -18,21 +17,23 @@ public:
void listen() const noexcept override;
- void attach(const char &key,
- const std::shared_ptr<ICommand> &command) noexcept override;
+ void
+ subscribe(const Event &event,
+ const std::shared_ptr<ISubscriber<Context>> &subscriber) noexcept override;
- void notify(const char &key) const noexcept override;
+ void notify_subscribers(const Event &event,
+ const Context &context) const noexcept override;
void enter_raw_mode() noexcept override;
void leave_raw_mode() noexcept override;
private:
- std::array<std::vector<std::shared_ptr<ICommand>>, CHAR_MAX> _key_commands;
+ std::array<std::vector<std::shared_ptr<ISubscriber<Context>>>, CHAR_MAX> _subscribers;
std::shared_ptr<termios> _original_termios = nullptr;
- using _KeyCommandsSizeType = decltype(_key_commands)::size_type;
+ using _SubscribersSizeType = decltype(_subscribers)::size_type;
- static _KeyCommandsSizeType _key_as_index(const char &key) noexcept;
+ static _SubscribersSizeType _event_as_index(const Event &event) noexcept;
};
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 <cstddef>
+
+class ICommand : public ISubscriber<std::nullptr_t>
{
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 <unordered_map>
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 <cstddef>
#include <memory>
-class IInputHandler : public IObservable<char>
+class IInputHandler : public IPublisher<char, std::nullptr_t>
{
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<ICommand> &command) noexcept override = 0;
+ void subscribe(
+ const Event &event,
+ const std::shared_ptr<ISubscriber<Context>> &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 <functional>
-#include <memory>
-
-template <typename Event>
-class IObservable
-{
-public:
- virtual ~IObservable() noexcept = default;
-
- virtual void listen() const noexcept = 0;
-
- virtual void attach(const char &event,
- const std::shared_ptr<ICommand> &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 <functional>
+#include <memory>
+
+template <typename Event, typename Context>
+class IPublisher
+{
+public:
+ virtual ~IPublisher() noexcept = default;
+
+ virtual void
+ subscribe(const Event &event,
+ const std::shared_ptr<ISubscriber<Context>> &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 <typename Context>
+class ISubscriber
+{
+public:
+ virtual void update(const Context &context) noexcept = 0;
+};