Я пересоздал репозиторий из-за большого количества мусора в прошлом
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
set(MODULE_LIB_NAME ModuleLib)
|
||||
|
||||
file(GLOB_RECURSE SRC "*.cpp" "*.c" "*.h" "*.hpp")
|
||||
|
||||
add_library(${MODULE_LIB_NAME} STATIC ${SRC})
|
||||
|
||||
target_include_directories(${MODULE_LIB_NAME} PRIVATE
|
||||
${PROJECT_SOURCE_DIR}/Core
|
||||
${PROJECT_SOURCE_DIR}/Delegate
|
||||
)
|
||||
|
||||
if(UNIX AND NOT APPLE)
|
||||
target_compile_options(${MODULE_LIB_NAME} PRIVATE -fPIC)
|
||||
endif()
|
||||
@@ -0,0 +1,68 @@
|
||||
#include "ModuleInstance.h"
|
||||
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
ModuleInstance::ModuleInstance(CoreInstance& core) : core_(core)
|
||||
{
|
||||
// Init instance
|
||||
}
|
||||
|
||||
void ModuleInstance::start()
|
||||
{
|
||||
// Create thread
|
||||
}
|
||||
|
||||
void ModuleInstance::stop()
|
||||
{
|
||||
onStop.Call();
|
||||
}
|
||||
|
||||
|
||||
extern ModuleInstance* Factory(CoreInstance&);
|
||||
|
||||
static std::mutex mModuleInstance;
|
||||
static ModuleInstance* module = nullptr;
|
||||
static std::thread* moduleThread;
|
||||
|
||||
void InitModule(CoreInstance& core)
|
||||
{
|
||||
mModuleInstance.lock();
|
||||
if(module == nullptr)
|
||||
module = Factory(core);
|
||||
mModuleInstance.unlock();
|
||||
}
|
||||
|
||||
void StartModule()
|
||||
{
|
||||
mModuleInstance.lock();
|
||||
if (module != nullptr)
|
||||
moduleThread = new std::thread([&] {module->start(); });
|
||||
mModuleInstance.unlock();
|
||||
}
|
||||
|
||||
void StopModule()
|
||||
{
|
||||
mModuleInstance.lock();
|
||||
if(module != nullptr)
|
||||
{
|
||||
delete module;
|
||||
module = nullptr;
|
||||
moduleThread->detach();
|
||||
delete moduleThread;
|
||||
}
|
||||
mModuleInstance.unlock();
|
||||
}
|
||||
|
||||
void QuitModule()
|
||||
{
|
||||
mModuleInstance.lock();
|
||||
if(module != nullptr)
|
||||
{
|
||||
module->stop();
|
||||
moduleThread->join();
|
||||
delete module;
|
||||
module = nullptr;
|
||||
}
|
||||
mModuleInstance.unlock();
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include "Delegate/Delegate.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#define EXPORT __declspec(dllexport)
|
||||
#elif __linux__
|
||||
#define EXPORT
|
||||
#else
|
||||
#error message("Unknow platform")
|
||||
#endif
|
||||
/*
|
||||
* This macro protects
|
||||
* module callbacks from GB
|
||||
*/
|
||||
#define PROTECTION_FROM_GB(Class) \
|
||||
extern "C" {\
|
||||
EXPORT void* protect_init() {return reinterpret_cast<void*>(&InitModule);} \
|
||||
EXPORT void* protect_start() {return reinterpret_cast<void*>(&StartModule);} \
|
||||
EXPORT void* protect_stop() {return reinterpret_cast<void*>(&StopModule);} \
|
||||
EXPORT void* protect_quit() {return reinterpret_cast<void*>(&QuitModule);} \
|
||||
}\
|
||||
ModuleInstance* Factory(CoreInstance& core) { return static_cast<ModuleInstance*>(new Class(core)); }
|
||||
|
||||
|
||||
class CoreInstance;
|
||||
|
||||
class ModuleInstance {
|
||||
CoreInstance& core_;
|
||||
|
||||
public:
|
||||
Delegate<void> onStop;
|
||||
|
||||
ModuleInstance(CoreInstance& core);
|
||||
virtual ~ModuleInstance() = default;
|
||||
|
||||
CoreInstance& getCore() const { return core_; }
|
||||
|
||||
virtual void start();
|
||||
void stop();
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
// Create module instance
|
||||
EXPORT void InitModule(CoreInstance& core);
|
||||
|
||||
// Start module instance
|
||||
EXPORT void StartModule();
|
||||
|
||||
// Destroy module instance when core unloads there
|
||||
EXPORT void StopModule();
|
||||
|
||||
// Stop module when core calls this
|
||||
EXPORT void QuitModule();
|
||||
}
|
||||
Reference in New Issue
Block a user