Merge pull request 'Base implementation' (#1) from base-impl into master
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
+2
-1
@@ -4,8 +4,9 @@
|
|||||||
!/docs
|
!/docs
|
||||||
!/include
|
!/include
|
||||||
!/src
|
!/src
|
||||||
|
!/tests
|
||||||
|
|
||||||
# Include files
|
# Include files
|
||||||
!/.gitignore
|
!/.gitignore
|
||||||
!/CMakeLists.txt
|
!/CMakeLists.txt
|
||||||
|
!/Makefile
|
||||||
|
|||||||
@@ -3,10 +3,52 @@ project(magic_scrolls)
|
|||||||
|
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
|
find_package(SQLite3 REQUIRED)
|
||||||
|
|
||||||
file(GLOB_RECURSE sources
|
file(GLOB_RECURSE sources
|
||||||
"${CMAKE_SOURCE_DIR}/src/*.cpp")
|
"${CMAKE_SOURCE_DIR}/src/*.cpp")
|
||||||
|
|
||||||
add_executable(magic_scrolls ${sources})
|
add_executable(magic_scrolls ${sources})
|
||||||
|
|
||||||
target_include_directories(magic_scrolls PUBLIC "${CMAKE_SOURCE_DIR}/include")
|
target_include_directories(magic_scrolls PUBLIC "${CMAKE_SOURCE_DIR}/include")
|
||||||
|
target_link_libraries(magic_scrolls PRIVATE SQLite::SQLite3)
|
||||||
|
|
||||||
|
# -------
|
||||||
|
enable_testing()
|
||||||
|
|
||||||
|
add_executable(event_bus_test
|
||||||
|
tests/engine/event_bus_test.cpp
|
||||||
|
src/engine/core/event_bus.cpp
|
||||||
|
)
|
||||||
|
add_executable(db_provider_test
|
||||||
|
tests/engine/db_provider_test.cpp
|
||||||
|
src/engine/core/engine.cpp
|
||||||
|
src/engine/core/engine_db_provider.cpp
|
||||||
|
)
|
||||||
|
add_executable(engine_test
|
||||||
|
tests/engine/engine_test.cpp
|
||||||
|
src/engine/core/engine.cpp
|
||||||
|
src/engine/core/engine_db_provider.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(event_bus_test PUBLIC "${CMAKE_SOURCE_DIR}/include")
|
||||||
|
target_link_libraries(event_bus_test PRIVATE SQLite::SQLite3)
|
||||||
|
|
||||||
|
target_include_directories(db_provider_test PUBLIC "${CMAKE_SOURCE_DIR}/include")
|
||||||
|
target_link_libraries(db_provider_test PRIVATE SQLite::SQLite3)
|
||||||
|
|
||||||
|
target_include_directories(engine_test PUBLIC "${CMAKE_SOURCE_DIR}/include")
|
||||||
|
target_link_libraries(engine_test PRIVATE SQLite::SQLite3)
|
||||||
|
|
||||||
|
add_test(
|
||||||
|
NAME "Event Bus"
|
||||||
|
COMMAND event_bus_test
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME "Database Provider"
|
||||||
|
COMMAND db_provider_test
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME "Engine"
|
||||||
|
COMMAND engine_test
|
||||||
|
)
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
.PHONY: build test
|
||||||
|
|
||||||
|
build:
|
||||||
|
cmake -B build
|
||||||
|
cmake --build build
|
||||||
|
|
||||||
|
test:
|
||||||
|
ctest --test-dir build --output-on-failure --progress
|
||||||
@@ -1,17 +1,20 @@
|
|||||||
#ifndef __MCS_ENGINE_HPP__
|
#ifndef __MCS_ENGINE_HPP__
|
||||||
#define __MCS_ENGINE_HPP__
|
#define __MCS_ENGINE_HPP__
|
||||||
|
|
||||||
#include "engine/core/engine_module.hpp"
|
#include <engine/core/engine_module.hpp>
|
||||||
#include "engine/core/engine_module_api.hpp"
|
#include <engine/core/engine_module_api.hpp>
|
||||||
|
#include <engine/core/engine_db_provider.hpp>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
/// Magic Srolls engine core
|
/// Magic Srolls engine core
|
||||||
class MscEngine : public IMscEngineModuleAPI {
|
class MscEngine : public IMscEngineModuleAPI
|
||||||
|
{
|
||||||
private:
|
private:
|
||||||
/// Loaded modules list
|
/// Loaded modules list
|
||||||
std::vector<std::shared_ptr<MscEngineModule>> _modules;
|
std::vector<std::shared_ptr<MscEngineModule>> _modules;
|
||||||
|
MscEventBus _bus;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/// Actual version
|
/// Actual version
|
||||||
@@ -19,8 +22,11 @@ public:
|
|||||||
|
|
||||||
MscEngine(std::vector<std::shared_ptr<MscEngineModule>> modules);
|
MscEngine(std::vector<std::shared_ptr<MscEngineModule>> modules);
|
||||||
|
|
||||||
UNIMPL getBus() const override;
|
MscEventBus &getBus() override;
|
||||||
UNIMPL getLoadedModules() const override;
|
std::unordered_map<std::string, std::uint16_t> getLoadedModules() const override;
|
||||||
|
MscEngineModule &getModule(const std::string &name) override;
|
||||||
|
std::unique_ptr<MscEngineDBProvider> createDBProvider() override;
|
||||||
|
std::unique_ptr<MscEngineDBProvider> createDBProvider(const std::string &filepath) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // __MCS_ENGINE_HPP__
|
#endif // __MCS_ENGINE_HPP__
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
#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
|
||||||
|
bool isCanceled() const
|
||||||
|
{
|
||||||
|
return canceled;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Cancel current event
|
||||||
|
void cancel()
|
||||||
|
{
|
||||||
|
canceled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool canceled = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @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
|
||||||
|
SubscriptionId 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__
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
#ifndef __MSC_ENGINE_DB_PROVIDER_HPP__
|
||||||
|
#define __MSC_ENGINE_DB_PROVIDER_HPP__
|
||||||
|
|
||||||
|
#include <sqlite3.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
/// @brief Engine database provider
|
||||||
|
class MscEngineDBProvider
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
~MscEngineDBProvider();
|
||||||
|
|
||||||
|
/// @brief Create inmemory database
|
||||||
|
MscEngineDBProvider();
|
||||||
|
|
||||||
|
/// @brief Create file database
|
||||||
|
explicit MscEngineDBProvider(const std::string &filepath);
|
||||||
|
|
||||||
|
/// @brief Execute SQL statement
|
||||||
|
/// @param sql statement
|
||||||
|
void execute(const std::string &sql);
|
||||||
|
|
||||||
|
MscEngineDBProvider(const MscEngineDBProvider &) = delete;
|
||||||
|
MscEngineDBProvider &operator=(const MscEngineDBProvider &) = delete;
|
||||||
|
|
||||||
|
private:
|
||||||
|
sqlite3 *_db = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // __MSC_ENGINE_DB_PROVIDER_HPP__
|
||||||
@@ -2,12 +2,14 @@
|
|||||||
#define __MCS_ENGINE_MODULE_HPP__
|
#define __MCS_ENGINE_MODULE_HPP__
|
||||||
|
|
||||||
#include "engine/utils/rev_range.hpp"
|
#include "engine/utils/rev_range.hpp"
|
||||||
|
#include "engine/core/engine_module_api.hpp"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
/// Engine module base class
|
/// Engine module base class
|
||||||
class MscEngineModule {
|
class MscEngineModule
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~MscEngineModule() = default;
|
virtual ~MscEngineModule() = default;
|
||||||
|
|
||||||
@@ -22,7 +24,7 @@ public:
|
|||||||
getDependencies() const = 0;
|
getDependencies() const = 0;
|
||||||
|
|
||||||
/// Module initialization phase
|
/// Module initialization phase
|
||||||
virtual void onInitialize();
|
virtual void onInitialize(IMscEngineModuleAPI &engine) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // __MCS_ENGINE_MODULE_HPP__
|
#endif // __MCS_ENGINE_MODULE_HPP__
|
||||||
|
|||||||
@@ -1,17 +1,34 @@
|
|||||||
#ifndef __MSC_ENGINE_MODULE_API_HPP__
|
#ifndef __MSC_ENGINE_MODULE_API_HPP__
|
||||||
#define __MSC_ENGINE_MODULE_API_HPP__
|
#define __MSC_ENGINE_MODULE_API_HPP__
|
||||||
|
|
||||||
typedef void UNIMPL;
|
#include <engine/core/engine_bus.hpp>
|
||||||
|
#include <engine/core/engine_db_provider.hpp>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
class IMscEngineModuleAPI {
|
class MscEngineModule;
|
||||||
|
|
||||||
|
class IMscEngineModuleAPI
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~IMscEngineModuleAPI() = default;
|
virtual ~IMscEngineModuleAPI() = default;
|
||||||
|
|
||||||
/// Get access to engine event bus
|
/// @brief Get access to engine event bus
|
||||||
virtual UNIMPL getBus() const = 0;
|
virtual MscEventBus &getBus() = 0;
|
||||||
|
|
||||||
/// Get access to loaded module list
|
/// @brief Get access to loaded module list
|
||||||
virtual UNIMPL getLoadedModules() const = 0;
|
virtual std::unordered_map<std::string, std::uint16_t> getLoadedModules() const = 0;
|
||||||
|
|
||||||
|
/// @brief Access module by its name
|
||||||
|
virtual MscEngineModule &getModule(const std::string &name) = 0;
|
||||||
|
|
||||||
|
/// @brief Create database provider with in-memory database
|
||||||
|
virtual std::unique_ptr<MscEngineDBProvider> createDBProvider() = 0;
|
||||||
|
|
||||||
|
/// @brief Create database provider with file database
|
||||||
|
virtual std::unique_ptr<MscEngineDBProvider> createDBProvider(const std::string &filepath) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // __MSC_ENGINE_MODULE_API_HPP__
|
#endif // __MSC_ENGINE_MODULE_API_HPP__
|
||||||
|
|||||||
@@ -2,9 +2,11 @@
|
|||||||
#define __MSC_TEST_1_MODULE_HPP__
|
#define __MSC_TEST_1_MODULE_HPP__
|
||||||
|
|
||||||
#include "engine/core/engine_module.hpp"
|
#include "engine/core/engine_module.hpp"
|
||||||
|
#include "engine/core/engine_module_api.hpp"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
class Test1Module : public MscEngineModule {
|
class Test1Module : public MscEngineModule
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
~Test1Module() = default;
|
~Test1Module() = default;
|
||||||
|
|
||||||
@@ -13,7 +15,7 @@ public:
|
|||||||
const std::unordered_map<std::string, MscRevisionRange> &
|
const std::unordered_map<std::string, MscRevisionRange> &
|
||||||
getDependencies() const override;
|
getDependencies() const override;
|
||||||
|
|
||||||
void onInitialize() override;
|
void onInitialize(IMscEngineModuleAPI &engine) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // __MSC_TEST_1_MODULE_HPP__
|
#endif // __MSC_TEST_1_MODULE_HPP__
|
||||||
|
|||||||
@@ -2,12 +2,14 @@
|
|||||||
#define __MSC_REV_RANGE_HPP__
|
#define __MSC_REV_RANGE_HPP__
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
struct MscRevisionRange {
|
struct MscRevisionRange
|
||||||
|
{
|
||||||
uint16_t minRevision;
|
uint16_t minRevision;
|
||||||
uint16_t maxRevision;
|
uint16_t maxRevision;
|
||||||
|
|
||||||
/// Check provided revision to be in range
|
/// 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;
|
return rev >= this->minRevision && rev <= this->maxRevision;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
+52
-10
@@ -8,33 +8,40 @@
|
|||||||
|
|
||||||
uint16_t MscEngine::REV = 0;
|
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;
|
std::unordered_map<std::string, uint16_t> registry;
|
||||||
for (const auto &module : modules) {
|
for (const auto &module : modules)
|
||||||
|
{
|
||||||
if (!module)
|
if (!module)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
auto [it, inserted] =
|
auto [it, inserted] =
|
||||||
registry.try_emplace(module->getName(), module->getRev());
|
registry.try_emplace(module->getName(), module->getRev());
|
||||||
if (!inserted) {
|
if (!inserted)
|
||||||
|
{
|
||||||
throw std::runtime_error("Duplicate module detected: " +
|
throw std::runtime_error("Duplicate module detected: " +
|
||||||
module->getName());
|
module->getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto &module : modules) {
|
for (const auto &module : modules)
|
||||||
|
{
|
||||||
if (!module)
|
if (!module)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
for (const auto &[dep_name, range] : module->getDependencies()) {
|
for (const auto &[dep_name, range] : module->getDependencies())
|
||||||
|
{
|
||||||
auto it = registry.find(dep_name);
|
auto it = registry.find(dep_name);
|
||||||
if (it == registry.end()) {
|
if (it == registry.end())
|
||||||
|
{
|
||||||
throw std::runtime_error("Module " + module->getName() +
|
throw std::runtime_error("Module " + module->getName() +
|
||||||
" requires missing module: " + dep_name);
|
" requires missing module: " + dep_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
int current_rev = it->second;
|
int current_rev = it->second;
|
||||||
if (!range.isSatisfied(current_rev)) {
|
if (!range.isSatisfied(current_rev))
|
||||||
|
{
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"Module " + module->getName() + " requires " + dep_name +
|
"Module " + module->getName() + " requires " + dep_name +
|
||||||
" in range [" + std::to_string(range.minRevision) + " <-> " +
|
" in range [" + std::to_string(range.minRevision) + " <-> " +
|
||||||
@@ -47,8 +54,43 @@ MscEngine::MscEngine(std::vector<std::shared_ptr<MscEngineModule>> modules) {
|
|||||||
_modules = std::move(modules);
|
_modules = std::move(modules);
|
||||||
}
|
}
|
||||||
|
|
||||||
UNIMPL MscEngine::getBus() const { throw std::runtime_error("Unimplemented"); }
|
MscEventBus &MscEngine::getBus()
|
||||||
|
{
|
||||||
|
return _bus;
|
||||||
|
}
|
||||||
|
|
||||||
UNIMPL MscEngine::getLoadedModules() const {
|
std::unordered_map<std::string, std::uint16_t> MscEngine::getLoadedModules() const
|
||||||
throw std::runtime_error("Unimplemented");
|
{
|
||||||
|
std::unordered_map<std::string, std::uint16_t> result;
|
||||||
|
for (const auto &module : _modules)
|
||||||
|
{
|
||||||
|
if (!module)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
auto mod = module.get();
|
||||||
|
result[mod->getName()] = mod->getRev();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
MscEngineModule &MscEngine::getModule(const std::string &name)
|
||||||
|
{
|
||||||
|
for (auto &module : _modules)
|
||||||
|
{
|
||||||
|
if (module.get()->getName() == name)
|
||||||
|
return *module.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
throw std::runtime_error("Module not found: " + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<MscEngineDBProvider> MscEngine::createDBProvider()
|
||||||
|
{
|
||||||
|
return std::make_unique<MscEngineDBProvider>();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<MscEngineDBProvider> MscEngine::createDBProvider(const std::string &filepath)
|
||||||
|
{
|
||||||
|
return std::make_unique<MscEngineDBProvider>(filepath);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
#include <engine/core/engine_db_provider.hpp>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
MscEngineDBProvider::MscEngineDBProvider() : MscEngineDBProvider(":memory:")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
MscEngineDBProvider::MscEngineDBProvider(const std::string &filepath)
|
||||||
|
{
|
||||||
|
const int result = sqlite3_open(filepath.c_str(), &_db);
|
||||||
|
if (result != SQLITE_OK)
|
||||||
|
{
|
||||||
|
const std::string message = _db != nullptr
|
||||||
|
? sqlite3_errmsg(_db)
|
||||||
|
: "unknown SQLite error";
|
||||||
|
|
||||||
|
if (_db != nullptr)
|
||||||
|
{
|
||||||
|
sqlite3_close(_db);
|
||||||
|
_db = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw std::runtime_error("Failed to open SQLite database: " + message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MscEngineDBProvider::~MscEngineDBProvider()
|
||||||
|
{
|
||||||
|
if (_db != nullptr)
|
||||||
|
sqlite3_close(_db);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MscEngineDBProvider::execute(const std::string &sql)
|
||||||
|
{
|
||||||
|
char *errorMessage = nullptr;
|
||||||
|
const int result = sqlite3_exec(_db, sql.c_str(), nullptr, nullptr, &errorMessage);
|
||||||
|
|
||||||
|
if (result == SQLITE_OK)
|
||||||
|
return;
|
||||||
|
|
||||||
|
std::string message(errorMessage);
|
||||||
|
sqlite3_free(errorMessage);
|
||||||
|
|
||||||
|
throw std::runtime_error("Failed to execute SQL: " + message);
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
MscEventBus::SubscriptionId MscEventBus::subscribeOnce(const std::string &name, Listener listener)
|
||||||
|
{
|
||||||
|
return _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/modules/test1/test1.hpp"
|
||||||
|
#include "engine/core/engine_module_api.hpp"
|
||||||
#include "engine/utils/rev_range.hpp"
|
#include "engine/utils/rev_range.hpp"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -9,9 +10,13 @@ std::string Test1Module::getName() const { return "test1"; }
|
|||||||
uint16_t Test1Module::getRev() const { return 1; }
|
uint16_t Test1Module::getRev() const { return 1; }
|
||||||
|
|
||||||
const std::unordered_map<std::string, MscRevisionRange> &
|
const std::unordered_map<std::string, MscRevisionRange> &
|
||||||
Test1Module::getDependencies() const {
|
Test1Module::getDependencies() const
|
||||||
|
{
|
||||||
static const std::unordered_map<std::string, MscRevisionRange> deps{};
|
static const std::unordered_map<std::string, MscRevisionRange> deps{};
|
||||||
return deps;
|
return deps;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Test1Module::onInitialize() {}
|
void Test1Module::onInitialize(IMscEngineModuleAPI &engine)
|
||||||
|
{
|
||||||
|
(void)(engine);
|
||||||
|
}
|
||||||
|
|||||||
+2
-1
@@ -5,7 +5,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
auto engine =
|
auto engine =
|
||||||
std::make_unique<MscEngine>(std::vector<std::shared_ptr<MscEngineModule>>{
|
std::make_unique<MscEngine>(std::vector<std::shared_ptr<MscEngineModule>>{
|
||||||
std::make_shared<Test1Module>()});
|
std::make_shared<Test1Module>()});
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
#include <cassert>
|
||||||
|
#include <vector>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <engine/core/engine.hpp>
|
||||||
|
#include <engine/core/engine_db_provider.hpp>
|
||||||
|
|
||||||
|
/// @brief Проверка на создание экземпляра
|
||||||
|
void testProviderCreation()
|
||||||
|
{
|
||||||
|
MscEngine engine({});
|
||||||
|
|
||||||
|
auto dbp1 = engine.createDBProvider();
|
||||||
|
assert(dbp1.get() != nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Проверка на создание экземпляра с некорректным путём файла
|
||||||
|
void testIncorrectFilePath()
|
||||||
|
{
|
||||||
|
MscEngine engine({});
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
engine.createDBProvider("/i/am/an/incorrect/path/to/file.txt");
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
catch (const std::runtime_error &error)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Проверка на успешное выполнение корректного выражения
|
||||||
|
void testValidStatement()
|
||||||
|
{
|
||||||
|
MscEngine engine({});
|
||||||
|
auto dbp = engine.createDBProvider();
|
||||||
|
dbp.get()->execute("SELECT 'hello, world' AS message;");
|
||||||
|
}
|
||||||
|
|
||||||
|
void testInvalidStatement()
|
||||||
|
{
|
||||||
|
MscEngine engine({});
|
||||||
|
auto dbp = engine.createDBProvider();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
dbp.get()->execute("SELECT * FROM nonexistentdatabase ABC");
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
catch (const std::runtime_error &error)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
testProviderCreation();
|
||||||
|
testIncorrectFilePath();
|
||||||
|
testValidStatement();
|
||||||
|
testInvalidStatement();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,149 @@
|
|||||||
|
#include <cassert>
|
||||||
|
#include <string>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <memory>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <engine/utils/rev_range.hpp>
|
||||||
|
#include <engine/core/engine.hpp>
|
||||||
|
#include <engine/core/engine_module.hpp>
|
||||||
|
|
||||||
|
class FirstTestModuleR1 : public MscEngineModule
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
std::string getName() const override
|
||||||
|
{
|
||||||
|
return "first_tmod";
|
||||||
|
};
|
||||||
|
|
||||||
|
std::uint16_t getRev() const override
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::unordered_map<std::string, MscRevisionRange> &getDependencies() const override
|
||||||
|
{
|
||||||
|
static const std::unordered_map<std::string, MscRevisionRange> deps{};
|
||||||
|
return deps;
|
||||||
|
}
|
||||||
|
|
||||||
|
void onInitialize(IMscEngineModuleAPI &engine) override {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class FirstTestModuleR2 : public MscEngineModule
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
std::string getName() const override
|
||||||
|
{
|
||||||
|
return "first_tmod";
|
||||||
|
};
|
||||||
|
|
||||||
|
std::uint16_t getRev() const override
|
||||||
|
{
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::unordered_map<std::string, MscRevisionRange> &getDependencies() const override
|
||||||
|
{
|
||||||
|
static const std::unordered_map<std::string, MscRevisionRange> deps{};
|
||||||
|
return deps;
|
||||||
|
}
|
||||||
|
|
||||||
|
void onInitialize(IMscEngineModuleAPI &engine) override {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class SecondTestModuleE1 : public MscEngineModule
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
std::string getName() const override
|
||||||
|
{
|
||||||
|
return "second_tmod";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::uint16_t getRev() const override
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::unordered_map<std::string, MscRevisionRange> &getDependencies() const override
|
||||||
|
{
|
||||||
|
static const std::unordered_map<std::string, MscRevisionRange> deps{
|
||||||
|
{"first_tmod", MscRevisionRange{1, 1}}};
|
||||||
|
return deps;
|
||||||
|
}
|
||||||
|
|
||||||
|
void onInitialize(IMscEngineModuleAPI &engine) override {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class SecondTestModuleE2 : public MscEngineModule
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
std::string getName() const override
|
||||||
|
{
|
||||||
|
return "second_tmod";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::uint16_t getRev() const override
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::unordered_map<std::string, MscRevisionRange> &getDependencies() const override
|
||||||
|
{
|
||||||
|
static const std::unordered_map<std::string, MscRevisionRange> deps{
|
||||||
|
{"first_tmod", MscRevisionRange{1, 2}}};
|
||||||
|
return deps;
|
||||||
|
}
|
||||||
|
|
||||||
|
void onInitialize(IMscEngineModuleAPI &engine) override {}
|
||||||
|
};
|
||||||
|
|
||||||
|
void testSatisfiedModules()
|
||||||
|
{
|
||||||
|
MscEngine engine({std::make_shared<FirstTestModuleR1>(),
|
||||||
|
std::make_shared<SecondTestModuleE1>()});
|
||||||
|
}
|
||||||
|
|
||||||
|
void testDuplicatedModules()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
MscEngine engine({
|
||||||
|
std::make_shared<FirstTestModuleR1>(),
|
||||||
|
std::make_shared<FirstTestModuleR2>(),
|
||||||
|
});
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
catch (const std::runtime_error &error)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void testUnsatisfiedModules()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
MscEngine engine({std::make_shared<FirstTestModuleR2>(),
|
||||||
|
std::make_shared<SecondTestModuleE1>()});
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
catch (const std::runtime_error &error)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void testSatisfiedMultiversionModule()
|
||||||
|
{
|
||||||
|
MscEngine engine({std::make_shared<FirstTestModuleR2>(),
|
||||||
|
std::make_shared<SecondTestModuleE2>()});
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
testSatisfiedModules();
|
||||||
|
testDuplicatedModules();
|
||||||
|
testUnsatisfiedModules();
|
||||||
|
testSatisfiedMultiversionModule();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
#include <cassert>
|
||||||
|
#include <engine/core/engine_bus.hpp>
|
||||||
|
|
||||||
|
const std::string testEventName = "TestEvent";
|
||||||
|
int calls = 0;
|
||||||
|
|
||||||
|
class TestEvent : public MscEvent
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
std::string getName() const override
|
||||||
|
{
|
||||||
|
return testEventName;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @brief Проверка на работу pub-sub
|
||||||
|
void testEventPubSub()
|
||||||
|
{
|
||||||
|
calls = 0;
|
||||||
|
MscEventBus bus;
|
||||||
|
TestEvent event;
|
||||||
|
|
||||||
|
bus.subscribe(testEventName, [&](MscEvent &ev)
|
||||||
|
{
|
||||||
|
calls++;
|
||||||
|
|
||||||
|
auto *testEvent = dynamic_cast<TestEvent*>(&ev);
|
||||||
|
assert(testEvent != nullptr); });
|
||||||
|
|
||||||
|
bus.publish(event);
|
||||||
|
assert(calls == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Проверка на работу
|
||||||
|
void testEventOnce()
|
||||||
|
{
|
||||||
|
calls = 0;
|
||||||
|
MscEventBus bus;
|
||||||
|
TestEvent event;
|
||||||
|
|
||||||
|
bus.subscribeOnce(testEventName, [&](MscEvent &)
|
||||||
|
{ calls++; });
|
||||||
|
|
||||||
|
bus.publish(event);
|
||||||
|
bus.publish(event);
|
||||||
|
assert(calls == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Проверка работы отписки
|
||||||
|
void testUnsubscribe()
|
||||||
|
{
|
||||||
|
calls = 0;
|
||||||
|
MscEventBus bus;
|
||||||
|
TestEvent event;
|
||||||
|
|
||||||
|
auto id = bus.subscribe(testEventName, [&](MscEvent &)
|
||||||
|
{ calls++; });
|
||||||
|
|
||||||
|
bus.publish(event);
|
||||||
|
|
||||||
|
assert(bus.unsubscribe(id) == true);
|
||||||
|
assert(bus.unsubscribe(id) == false);
|
||||||
|
|
||||||
|
bus.publish(event);
|
||||||
|
|
||||||
|
assert(calls == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
testEventPubSub();
|
||||||
|
testEventOnce();
|
||||||
|
testUnsubscribe();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user