Compare commits
14
Commits
8ea4bf45ba
...
lifecycle
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8c65860153 | ||
|
|
1a2722da57 | ||
|
|
12deed7aee | ||
|
|
33941a823c | ||
|
|
ca7db648f8 | ||
|
|
7a158a0bf8 | ||
|
|
a6676997d4 | ||
|
|
160e932f2e | ||
|
|
62f844a582 | ||
|
|
e756bb1d73 | ||
|
|
ff0453e81d | ||
|
|
045fe533d2 | ||
|
|
df7a8855d4 | ||
|
|
1978a24d1e |
@@ -0,0 +1,3 @@
|
|||||||
|
BasedOnStyle: LLVM
|
||||||
|
IndentWidth: 2
|
||||||
|
BreakBeforeBraces: Allman
|
||||||
+3
-1
@@ -4,8 +4,10 @@
|
|||||||
!/docs
|
!/docs
|
||||||
!/include
|
!/include
|
||||||
!/src
|
!/src
|
||||||
|
!/tests
|
||||||
|
|
||||||
# Include files
|
# Include files
|
||||||
|
!/.clang-format
|
||||||
!/.gitignore
|
!/.gitignore
|
||||||
!/CMakeLists.txt
|
!/CMakeLists.txt
|
||||||
|
!/Makefile
|
||||||
|
|||||||
@@ -3,10 +3,65 @@ 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
|
||||||
|
)
|
||||||
|
add_executable(lifecycle_test
|
||||||
|
tests/engine/lifecycle_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)
|
||||||
|
|
||||||
|
target_include_directories(lifecycle_test PUBLIC "${CMAKE_SOURCE_DIR}/include")
|
||||||
|
target_link_libraries(lifecycle_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
|
||||||
|
)
|
||||||
|
add_test(
|
||||||
|
NAME "Lifecycle"
|
||||||
|
COMMAND lifecycle_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,27 +1,50 @@
|
|||||||
#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_api.hpp"
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <engine/core/engine_db_provider.hpp>
|
||||||
|
#include <engine/core/engine_module.hpp>
|
||||||
|
#include <engine/core/engine_module_api.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
/// Magic Srolls engine core
|
/// Ядро движка Magic Srolls
|
||||||
class MscEngine : public IMscEngineModuleAPI
|
class MscEngine : public IMscEngineModuleAPI
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
/// Loaded modules list
|
/// @brief Загруженные модули
|
||||||
std::vector<std::shared_ptr<MscEngineModule>> _modules;
|
std::vector<std::shared_ptr<MscEngineModule>> _modules;
|
||||||
|
|
||||||
|
/// @brief Общая событийная шина движка
|
||||||
|
MscEventBus _bus;
|
||||||
|
|
||||||
|
/// @brief Флаг работы движка
|
||||||
|
bool _isRunning = false;
|
||||||
|
|
||||||
|
/// @brief Время последнего тика
|
||||||
|
clock::time_point _lastUpdate;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/// Actual version
|
/// @brief Номер ревизии
|
||||||
static uint16_t REV;
|
static uint16_t REV;
|
||||||
|
|
||||||
MscEngine(std::vector<std::shared_ptr<MscEngineModule>> modules);
|
MscEngine(std::vector<std::shared_ptr<MscEngineModule>> modules);
|
||||||
|
~MscEngine() = default;
|
||||||
|
|
||||||
UNIMPL getBus() const override;
|
/// @brief Запустить движок
|
||||||
UNIMPL getLoadedModules() const override;
|
void start();
|
||||||
|
|
||||||
|
/// @brief Остановить движок
|
||||||
|
void stop();
|
||||||
|
|
||||||
|
MscEventBus &getBus() override;
|
||||||
|
std::unordered_map<std::string, std::uint16_t>
|
||||||
|
getLoadedModules() const override;
|
||||||
|
std::shared_ptr<MscEngineModule> &getModule(const std::string &name) override;
|
||||||
|
std::unique_ptr<MscEngineDBProvider> createDBProvider() override;
|
||||||
|
std::unique_ptr<MscEngineDBProvider>
|
||||||
|
createDBProvider(const std::string &filepath) override;
|
||||||
|
void requestStop() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // __MCS_ENGINE_HPP__
|
#endif // __MCS_ENGINE_HPP__
|
||||||
|
|||||||
@@ -18,10 +18,19 @@ public:
|
|||||||
virtual std::string getName() const = 0;
|
virtual std::string getName() const = 0;
|
||||||
|
|
||||||
/// @brief Is current event is canceled
|
/// @brief Is current event is canceled
|
||||||
virtual bool isCanceled() const = 0;
|
bool isCanceled() const
|
||||||
|
{
|
||||||
|
return canceled;
|
||||||
|
}
|
||||||
|
|
||||||
/// @brief Cancel current event
|
/// @brief Cancel current event
|
||||||
virtual void cancel() = 0;
|
void cancel()
|
||||||
|
{
|
||||||
|
canceled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool canceled = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// @brief Engine event bus
|
/// @brief Engine event bus
|
||||||
@@ -47,7 +56,7 @@ public:
|
|||||||
/// @brief Subscribe to specified event and execute once on publish
|
/// @brief Subscribe to specified event and execute once on publish
|
||||||
/// @param name event name
|
/// @param name event name
|
||||||
/// @param listener event handler
|
/// @param listener event handler
|
||||||
void subscribeOnce(const std::string &name, Listener listener);
|
SubscriptionId subscribeOnce(const std::string &name, Listener listener);
|
||||||
|
|
||||||
/// @brief Unsubscribe listener by its own id
|
/// @brief Unsubscribe listener by its own id
|
||||||
/// @param id subscription id
|
/// @param id subscription id
|
||||||
|
|||||||
@@ -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__
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
#ifndef __MCS_ENGINE_MODULE_HPP__
|
#ifndef __MCS_ENGINE_MODULE_HPP__
|
||||||
#define __MCS_ENGINE_MODULE_HPP__
|
#define __MCS_ENGINE_MODULE_HPP__
|
||||||
|
|
||||||
#include "engine/utils/rev_range.hpp"
|
|
||||||
#include "engine/core/engine_module_api.hpp"
|
#include "engine/core/engine_module_api.hpp"
|
||||||
|
#include "engine/utils/rev_range.hpp"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
@@ -23,8 +23,27 @@ public:
|
|||||||
virtual const std::unordered_map<std::string, MscRevisionRange> &
|
virtual const std::unordered_map<std::string, MscRevisionRange> &
|
||||||
getDependencies() const = 0;
|
getDependencies() const = 0;
|
||||||
|
|
||||||
/// Module initialization phase
|
/// @brief Module initialization
|
||||||
|
/// @param engine engine public API for modules
|
||||||
virtual void onInitialize(IMscEngineModuleAPI &engine) = 0;
|
virtual void onInitialize(IMscEngineModuleAPI &engine) = 0;
|
||||||
|
|
||||||
|
/// @brief Engine start event handler
|
||||||
|
/// @param engine engine public API for modules
|
||||||
|
virtual void onStart(IMscEngineModuleAPI &engine) = 0;
|
||||||
|
|
||||||
|
/// @brief Engine update cycle step
|
||||||
|
/// @param engine engine public API for modules
|
||||||
|
/// @param dt loop delta
|
||||||
|
virtual void onUpdate(IMscEngineModuleAPI &engine,
|
||||||
|
IMscEngineModuleAPI::clock::duration dt) = 0;
|
||||||
|
|
||||||
|
/// @brief Engine stop event handler
|
||||||
|
/// @param engine engine public API for modules
|
||||||
|
virtual void onStop(IMscEngineModuleAPI &engine) = 0;
|
||||||
|
|
||||||
|
/// @brief Module destroying
|
||||||
|
/// @param engine engine public API for modules
|
||||||
|
virtual void onDestory(IMscEngineModuleAPI &engine) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // __MCS_ENGINE_MODULE_HPP__
|
#endif // __MCS_ENGINE_MODULE_HPP__
|
||||||
|
|||||||
@@ -1,18 +1,43 @@
|
|||||||
#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 <cstdint>
|
||||||
|
#include <engine/core/engine_bus.hpp>
|
||||||
|
#include <engine/core/engine_db_provider.hpp>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
class MscEngineModule;
|
||||||
|
|
||||||
|
/// @brief Публичный API движка для модулей
|
||||||
class IMscEngineModuleAPI
|
class IMscEngineModuleAPI
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
using clock = std::chrono::steady_clock;
|
||||||
|
|
||||||
virtual ~IMscEngineModuleAPI() = default;
|
virtual ~IMscEngineModuleAPI() = default;
|
||||||
|
|
||||||
/// Get access to engine event bus
|
/// @brief Получить доступ к событийной шине
|
||||||
virtual UNIMPL getBus() const = 0;
|
virtual MscEventBus &getBus() = 0;
|
||||||
|
|
||||||
/// Get access to loaded module list
|
/// @brief Получить список загруженных модулей
|
||||||
virtual UNIMPL getLoadedModules() const = 0;
|
virtual std::unordered_map<std::string, std::uint16_t>
|
||||||
|
getLoadedModules() const = 0;
|
||||||
|
|
||||||
|
/// @brief Получить прямой доступ к модулю
|
||||||
|
virtual std::shared_ptr<MscEngineModule> &
|
||||||
|
getModule(const std::string &name) = 0;
|
||||||
|
|
||||||
|
/// @brief Создать провайдер БД с хранением в памяти
|
||||||
|
virtual std::unique_ptr<MscEngineDBProvider> createDBProvider() = 0;
|
||||||
|
|
||||||
|
/// @brief Создать провайдер БД с указанным файлом
|
||||||
|
virtual std::unique_ptr<MscEngineDBProvider>
|
||||||
|
createDBProvider(const std::string &filepath) = 0;
|
||||||
|
|
||||||
|
/// @brief Запросить остановку движка
|
||||||
|
virtual void requestStop() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // __MSC_ENGINE_MODULE_API_HPP__
|
#endif // __MSC_ENGINE_MODULE_API_HPP__
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
#ifndef __MSC_TEST_1_MODULE_HPP__
|
|
||||||
#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
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
~Test1Module() = default;
|
|
||||||
|
|
||||||
std::string getName() const override;
|
|
||||||
uint16_t getRev() const override;
|
|
||||||
const std::unordered_map<std::string, MscRevisionRange> &
|
|
||||||
getDependencies() const override;
|
|
||||||
|
|
||||||
void onInitialize(IMscEngineModuleAPI &engine) override;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // __MSC_TEST_1_MODULE_HPP__
|
|
||||||
+103
-5
@@ -1,12 +1,16 @@
|
|||||||
|
#include "engine/core/engine_module_api.hpp"
|
||||||
|
#include <chrono>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <engine/core/engine.hpp>
|
#include <engine/core/engine.hpp>
|
||||||
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
uint16_t MscEngine::REV = 0;
|
std::uint16_t MscEngine::REV = 1;
|
||||||
|
|
||||||
MscEngine::MscEngine(std::vector<std::shared_ptr<MscEngineModule>> modules)
|
MscEngine::MscEngine(std::vector<std::shared_ptr<MscEngineModule>> modules)
|
||||||
{
|
{
|
||||||
@@ -49,14 +53,108 @@ MscEngine::MscEngine(std::vector<std::shared_ptr<MscEngineModule>> modules)
|
|||||||
std::to_string(current_rev));
|
std::to_string(current_rev));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.get()->onInitialize(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
_modules = std::move(modules);
|
_modules = std::move(modules);
|
||||||
}
|
}
|
||||||
|
|
||||||
UNIMPL MscEngine::getBus() const { throw std::runtime_error("Unimplemented"); }
|
void MscEngine::start()
|
||||||
|
|
||||||
UNIMPL MscEngine::getLoadedModules() const
|
|
||||||
{
|
{
|
||||||
throw std::runtime_error("Unimplemented");
|
if (_isRunning)
|
||||||
|
return;
|
||||||
|
|
||||||
|
_isRunning = true;
|
||||||
|
_lastUpdate = clock::now();
|
||||||
|
|
||||||
|
for (auto &m : _modules)
|
||||||
|
m->onStart(*this);
|
||||||
|
|
||||||
|
constexpr auto tickDuration = std::chrono::milliseconds(50);
|
||||||
|
auto nextTick = clock::now();
|
||||||
|
|
||||||
|
using UpdateFn = std::function<void(IMscEngineModuleAPI & engine,
|
||||||
|
IMscEngineModuleAPI::clock::duration dt)>;
|
||||||
|
std::vector<UpdateFn> updateFns;
|
||||||
|
updateFns.reserve(_modules.size());
|
||||||
|
|
||||||
|
for (auto &m : _modules)
|
||||||
|
updateFns.emplace_back([m](IMscEngineModuleAPI &engine,
|
||||||
|
IMscEngineModuleAPI::clock::duration dt)
|
||||||
|
{ m->onUpdate(engine, dt); });
|
||||||
|
|
||||||
|
while (_isRunning)
|
||||||
|
{
|
||||||
|
nextTick += tickDuration;
|
||||||
|
const auto currentTick = clock::now();
|
||||||
|
const auto dt = currentTick - _lastUpdate;
|
||||||
|
|
||||||
|
// TODO: process
|
||||||
|
|
||||||
|
for (auto upd : updateFns)
|
||||||
|
upd(*this, dt);
|
||||||
|
|
||||||
|
_lastUpdate = currentTick;
|
||||||
|
|
||||||
|
std::this_thread::sleep_until(nextTick);
|
||||||
|
if (clock::now() > nextTick + std::chrono::seconds(1))
|
||||||
|
nextTick = clock::now();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto &m : _modules)
|
||||||
|
m->onStop(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MscEngine::stop()
|
||||||
|
{
|
||||||
|
if (!_isRunning)
|
||||||
|
return;
|
||||||
|
|
||||||
|
_isRunning = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
MscEventBus &MscEngine::getBus() { return _bus; }
|
||||||
|
|
||||||
|
std::unordered_map<std::string, std::uint16_t>
|
||||||
|
MscEngine::getLoadedModules() const
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<MscEngineModule> &MscEngine::getModule(const std::string &name)
|
||||||
|
{
|
||||||
|
for (auto &module : _modules)
|
||||||
|
{
|
||||||
|
if (module.get()->getName() == name)
|
||||||
|
return module;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MscEngine::requestStop()
|
||||||
|
{
|
||||||
|
this->stop(); // TODO: заменить на флаг
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
@@ -48,9 +48,9 @@ MscEventBus::SubscriptionId MscEventBus::subscribe(const std::string &name, List
|
|||||||
return _subscribe(name, std::move(listener), false);
|
return _subscribe(name, std::move(listener), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MscEventBus::subscribeOnce(const std::string &name, Listener listener)
|
MscEventBus::SubscriptionId MscEventBus::subscribeOnce(const std::string &name, Listener listener)
|
||||||
{
|
{
|
||||||
_subscribe(name, std::move(listener), true);
|
return _subscribe(name, std::move(listener), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MscEventBus::unsubscribe(SubscriptionId id)
|
bool MscEventBus::unsubscribe(SubscriptionId id)
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
#include "engine/modules/test1/test1.hpp"
|
|
||||||
#include "engine/core/engine_module_api.hpp"
|
|
||||||
#include "engine/utils/rev_range.hpp"
|
|
||||||
#include <cstdint>
|
|
||||||
#include <string>
|
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
std::string Test1Module::getName() const { return "test1"; }
|
|
||||||
|
|
||||||
uint16_t Test1Module::getRev() const { return 1; }
|
|
||||||
|
|
||||||
const std::unordered_map<std::string, MscRevisionRange> &
|
|
||||||
Test1Module::getDependencies() const
|
|
||||||
{
|
|
||||||
static const std::unordered_map<std::string, MscRevisionRange> deps{};
|
|
||||||
return deps;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Test1Module::onInitialize(IMscEngineModuleAPI &engine)
|
|
||||||
{
|
|
||||||
(void)(engine);
|
|
||||||
}
|
|
||||||
+1
-8
@@ -1,16 +1,9 @@
|
|||||||
#include "engine/modules/test1/test1.hpp"
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <engine/core/engine.hpp>
|
#include <engine/core/engine.hpp>
|
||||||
#include <memory>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
auto engine =
|
printf("Hello, engine!\n");
|
||||||
std::make_unique<MscEngine>(std::vector<std::shared_ptr<MscEngineModule>>{
|
|
||||||
std::make_shared<Test1Module>()});
|
|
||||||
|
|
||||||
printf("Hello, CMake, %p!\n", &engine);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,157 @@
|
|||||||
|
#include <cassert>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <engine/core/engine.hpp>
|
||||||
|
#include <engine/core/engine_module.hpp>
|
||||||
|
#include <engine/utils/rev_range.hpp>
|
||||||
|
#include <memory>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
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 {}
|
||||||
|
void onStart(IMscEngineModuleAPI &engine) override {}
|
||||||
|
void onUpdate(IMscEngineModuleAPI &engine,
|
||||||
|
IMscEngineModuleAPI::clock::duration dt) override
|
||||||
|
{
|
||||||
|
}
|
||||||
|
void onStop(IMscEngineModuleAPI &engine) override {}
|
||||||
|
void onDestory(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 {}
|
||||||
|
void onStart(IMscEngineModuleAPI &engine) override {}
|
||||||
|
void onUpdate(IMscEngineModuleAPI &engine,
|
||||||
|
IMscEngineModuleAPI::clock::duration dt) override
|
||||||
|
{
|
||||||
|
}
|
||||||
|
void onStop(IMscEngineModuleAPI &engine) override {}
|
||||||
|
void onDestory(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 {}
|
||||||
|
void onStart(IMscEngineModuleAPI &engine) override {}
|
||||||
|
void onUpdate(IMscEngineModuleAPI &engine,
|
||||||
|
IMscEngineModuleAPI::clock::duration dt) override
|
||||||
|
{
|
||||||
|
}
|
||||||
|
void onStop(IMscEngineModuleAPI &engine) override {}
|
||||||
|
void onDestory(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 onStart(IMscEngineModuleAPI &engine) override {}
|
||||||
|
void onUpdate(IMscEngineModuleAPI &engine,
|
||||||
|
IMscEngineModuleAPI::clock::duration dt) override
|
||||||
|
{
|
||||||
|
}
|
||||||
|
void onStop(IMscEngineModuleAPI &engine) override {}
|
||||||
|
void onDestory(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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
#include "engine/core/engine.hpp"
|
||||||
|
#include "engine/core/engine_module.hpp"
|
||||||
|
#include "engine/utils/rev_range.hpp"
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
bool onInitWasCalled = false;
|
||||||
|
bool onStartWasCalled = false;
|
||||||
|
bool onUpdateWasCalled = false;
|
||||||
|
bool onStopWasCalled = false;
|
||||||
|
|
||||||
|
class TestModule : public MscEngineModule
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
std::string getName() const override { return "test_mod"; }
|
||||||
|
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
|
||||||
|
{
|
||||||
|
onInitWasCalled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void onStart(IMscEngineModuleAPI &engine) override
|
||||||
|
{
|
||||||
|
onStartWasCalled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void onUpdate(IMscEngineModuleAPI &engine,
|
||||||
|
IMscEngineModuleAPI::clock::duration dt) override
|
||||||
|
{
|
||||||
|
onUpdateWasCalled = true;
|
||||||
|
engine.requestStop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void onStop(IMscEngineModuleAPI &engine) override { onStopWasCalled = true; }
|
||||||
|
|
||||||
|
void onDestory(IMscEngineModuleAPI &engine) override {}
|
||||||
|
};
|
||||||
|
|
||||||
|
void testStates()
|
||||||
|
{
|
||||||
|
MscEngine engine({std::make_shared<TestModule>()});
|
||||||
|
engine.start();
|
||||||
|
|
||||||
|
assert(onInitWasCalled);
|
||||||
|
assert(onStartWasCalled);
|
||||||
|
assert(onUpdateWasCalled);
|
||||||
|
assert(onStopWasCalled);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
testStates();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user