Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8ea4bf45ba | ||
|
|
433407cc9d |
@@ -8,7 +8,8 @@
|
||||
#include <vector>
|
||||
|
||||
/// Magic Srolls engine core
|
||||
class MscEngine : public IMscEngineModuleAPI {
|
||||
class MscEngine : public IMscEngineModuleAPI
|
||||
{
|
||||
private:
|
||||
/// Loaded modules list
|
||||
std::vector<std::shared_ptr<MscEngineModule>> _modules;
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
#ifndef __MSC_ENGINE_BUS_HPP__
|
||||
#define __MSC_ENGINE_BUS_HPP__
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
#include <functional>
|
||||
#include <cstdint>
|
||||
|
||||
/// @brief Event base class
|
||||
class MscEvent
|
||||
{
|
||||
public:
|
||||
virtual ~MscEvent() = default;
|
||||
|
||||
/// @brief Get event name
|
||||
virtual std::string getName() const = 0;
|
||||
|
||||
/// @brief Is current event is canceled
|
||||
virtual bool isCanceled() const = 0;
|
||||
|
||||
/// @brief Cancel current event
|
||||
virtual void cancel() = 0;
|
||||
};
|
||||
|
||||
/// @brief Engine event bus
|
||||
class MscEventBus
|
||||
{
|
||||
public:
|
||||
/// @brief Event handler alias
|
||||
using Listener = std::function<void(MscEvent &)>;
|
||||
|
||||
/// @brief Subscription identifier alias
|
||||
using SubscriptionId = std::uint64_t;
|
||||
|
||||
/// @brief Emit event
|
||||
/// @param event event context
|
||||
void publish(MscEvent &event);
|
||||
|
||||
/// @brief Subscribe to specified event
|
||||
/// @param name event name
|
||||
/// @param listener event handler
|
||||
/// @returns subscription identifier to make it possitble to remove listener
|
||||
SubscriptionId subscribe(const std::string &name, Listener listener);
|
||||
|
||||
/// @brief Subscribe to specified event and execute once on publish
|
||||
/// @param name event name
|
||||
/// @param listener event handler
|
||||
void subscribeOnce(const std::string &name, Listener listener);
|
||||
|
||||
/// @brief Unsubscribe listener by its own id
|
||||
/// @param id subscription id
|
||||
/// @return `true` if listener has been deleted, otherwise `false`
|
||||
bool unsubscribe(SubscriptionId id);
|
||||
|
||||
private:
|
||||
using ListenerWithId = struct
|
||||
{
|
||||
SubscriptionId id;
|
||||
Listener listener;
|
||||
bool once;
|
||||
};
|
||||
|
||||
std::unordered_map<std::string, std::vector<ListenerWithId>> _listeners;
|
||||
std::unordered_map<SubscriptionId, std::string> _listenersPaths;
|
||||
std::uint64_t _lastId = 0;
|
||||
std::mutex _listenersMutex;
|
||||
|
||||
SubscriptionId _subscribe(const std::string &name, Listener listener, bool once);
|
||||
};
|
||||
|
||||
#endif // __MSC_ENGINE_BUS_HPP__
|
||||
@@ -2,12 +2,14 @@
|
||||
#define __MCS_ENGINE_MODULE_HPP__
|
||||
|
||||
#include "engine/utils/rev_range.hpp"
|
||||
#include "engine/core/engine_module_api.hpp"
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
/// Engine module base class
|
||||
class MscEngineModule {
|
||||
class MscEngineModule
|
||||
{
|
||||
public:
|
||||
virtual ~MscEngineModule() = default;
|
||||
|
||||
@@ -22,7 +24,7 @@ public:
|
||||
getDependencies() const = 0;
|
||||
|
||||
/// Module initialization phase
|
||||
virtual void onInitialize();
|
||||
virtual void onInitialize(IMscEngineModuleAPI &engine) = 0;
|
||||
};
|
||||
|
||||
#endif // __MCS_ENGINE_MODULE_HPP__
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
|
||||
typedef void UNIMPL;
|
||||
|
||||
class IMscEngineModuleAPI {
|
||||
class IMscEngineModuleAPI
|
||||
{
|
||||
public:
|
||||
virtual ~IMscEngineModuleAPI() = default;
|
||||
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
#define __MSC_TEST_1_MODULE_HPP__
|
||||
|
||||
#include "engine/core/engine_module.hpp"
|
||||
#include "engine/core/engine_module_api.hpp"
|
||||
#include <cstdint>
|
||||
|
||||
class Test1Module : public MscEngineModule {
|
||||
class Test1Module : public MscEngineModule
|
||||
{
|
||||
public:
|
||||
~Test1Module() = default;
|
||||
|
||||
@@ -13,7 +15,7 @@ public:
|
||||
const std::unordered_map<std::string, MscRevisionRange> &
|
||||
getDependencies() const override;
|
||||
|
||||
void onInitialize() override;
|
||||
void onInitialize(IMscEngineModuleAPI &engine) override;
|
||||
};
|
||||
|
||||
#endif // __MSC_TEST_1_MODULE_HPP__
|
||||
|
||||
@@ -2,12 +2,14 @@
|
||||
#define __MSC_REV_RANGE_HPP__
|
||||
|
||||
#include <cstdint>
|
||||
struct MscRevisionRange {
|
||||
struct MscRevisionRange
|
||||
{
|
||||
uint16_t minRevision;
|
||||
uint16_t maxRevision;
|
||||
|
||||
/// Check provided revision to be in range
|
||||
bool isSatisfied(uint16_t rev) const {
|
||||
bool isSatisfied(uint16_t rev) const
|
||||
{
|
||||
return rev >= this->minRevision && rev <= this->maxRevision;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -8,33 +8,40 @@
|
||||
|
||||
uint16_t MscEngine::REV = 0;
|
||||
|
||||
MscEngine::MscEngine(std::vector<std::shared_ptr<MscEngineModule>> modules) {
|
||||
MscEngine::MscEngine(std::vector<std::shared_ptr<MscEngineModule>> modules)
|
||||
{
|
||||
std::unordered_map<std::string, uint16_t> registry;
|
||||
for (const auto &module : modules) {
|
||||
for (const auto &module : modules)
|
||||
{
|
||||
if (!module)
|
||||
continue;
|
||||
|
||||
auto [it, inserted] =
|
||||
registry.try_emplace(module->getName(), module->getRev());
|
||||
if (!inserted) {
|
||||
if (!inserted)
|
||||
{
|
||||
throw std::runtime_error("Duplicate module detected: " +
|
||||
module->getName());
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &module : modules) {
|
||||
for (const auto &module : modules)
|
||||
{
|
||||
if (!module)
|
||||
continue;
|
||||
|
||||
for (const auto &[dep_name, range] : module->getDependencies()) {
|
||||
for (const auto &[dep_name, range] : module->getDependencies())
|
||||
{
|
||||
auto it = registry.find(dep_name);
|
||||
if (it == registry.end()) {
|
||||
if (it == registry.end())
|
||||
{
|
||||
throw std::runtime_error("Module " + module->getName() +
|
||||
" requires missing module: " + dep_name);
|
||||
}
|
||||
|
||||
int current_rev = it->second;
|
||||
if (!range.isSatisfied(current_rev)) {
|
||||
if (!range.isSatisfied(current_rev))
|
||||
{
|
||||
throw std::runtime_error(
|
||||
"Module " + module->getName() + " requires " + dep_name +
|
||||
" in range [" + std::to_string(range.minRevision) + " <-> " +
|
||||
@@ -49,6 +56,7 @@ MscEngine::MscEngine(std::vector<std::shared_ptr<MscEngineModule>> modules) {
|
||||
|
||||
UNIMPL MscEngine::getBus() const { throw std::runtime_error("Unimplemented"); }
|
||||
|
||||
UNIMPL MscEngine::getLoadedModules() const {
|
||||
UNIMPL MscEngine::getLoadedModules() const
|
||||
{
|
||||
throw std::runtime_error("Unimplemented");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
#include <engine/core/engine_bus.hpp>
|
||||
#include <utility>
|
||||
#include <cstddef>
|
||||
|
||||
void MscEventBus::publish(MscEvent &event)
|
||||
{
|
||||
std::vector<ListenerWithId> listeners;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_listenersMutex);
|
||||
auto it = _listeners.find(event.getName());
|
||||
if (it == _listeners.end())
|
||||
return;
|
||||
|
||||
for (const auto &entry : it->second)
|
||||
listeners.push_back(entry);
|
||||
}
|
||||
|
||||
for (const auto &entry : listeners)
|
||||
{
|
||||
if (event.isCanceled())
|
||||
break;
|
||||
|
||||
entry.listener(event);
|
||||
if (entry.once)
|
||||
unsubscribe(entry.id);
|
||||
}
|
||||
}
|
||||
|
||||
MscEventBus::SubscriptionId MscEventBus::_subscribe(const std::string &name, MscEventBus::Listener listener, bool once)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_listenersMutex);
|
||||
|
||||
const SubscriptionId id = ++_lastId;
|
||||
auto listenerWithId = ListenerWithId{
|
||||
.id = id,
|
||||
.listener = std::move(listener),
|
||||
.once = once};
|
||||
|
||||
_listeners[name].push_back(std::move(listenerWithId));
|
||||
_listenersPaths[id] = name;
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
MscEventBus::SubscriptionId MscEventBus::subscribe(const std::string &name, Listener listener)
|
||||
{
|
||||
return _subscribe(name, std::move(listener), false);
|
||||
}
|
||||
|
||||
void MscEventBus::subscribeOnce(const std::string &name, Listener listener)
|
||||
{
|
||||
_subscribe(name, std::move(listener), true);
|
||||
}
|
||||
|
||||
bool MscEventBus::unsubscribe(SubscriptionId id)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_listenersMutex);
|
||||
|
||||
auto it = _listenersPaths.find(id);
|
||||
if (it == _listenersPaths.end())
|
||||
return false;
|
||||
|
||||
auto &listeners = _listeners.at(it->second);
|
||||
for (std::size_t i = 0; i < listeners.size(); i++)
|
||||
{
|
||||
if (listeners.at(i).id != id)
|
||||
continue;
|
||||
|
||||
listeners.erase(listeners.begin() + i);
|
||||
_listenersPaths.erase(id);
|
||||
|
||||
if (listeners.empty())
|
||||
_listeners.erase(it->second);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "engine/modules/test1/test1.hpp"
|
||||
#include "engine/core/engine_module_api.hpp"
|
||||
#include "engine/utils/rev_range.hpp"
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
@@ -9,9 +10,13 @@ std::string Test1Module::getName() const { return "test1"; }
|
||||
uint16_t Test1Module::getRev() const { return 1; }
|
||||
|
||||
const std::unordered_map<std::string, MscRevisionRange> &
|
||||
Test1Module::getDependencies() const {
|
||||
Test1Module::getDependencies() const
|
||||
{
|
||||
static const std::unordered_map<std::string, MscRevisionRange> deps{};
|
||||
return deps;
|
||||
}
|
||||
|
||||
void Test1Module::onInitialize() {}
|
||||
void Test1Module::onInitialize(IMscEngineModuleAPI &engine)
|
||||
{
|
||||
(void)(engine);
|
||||
}
|
||||
|
||||
+2
-1
@@ -5,7 +5,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <vector>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
auto engine =
|
||||
std::make_unique<MscEngine>(std::vector<std::shared_ptr<MscEngineModule>>{
|
||||
std::make_shared<Test1Module>()});
|
||||
|
||||
Reference in New Issue
Block a user