Prepare module management

This commit is contained in:
momo5502 2018-12-23 23:21:30 +01:00
parent ba39587135
commit ff277cfa6e
4 changed files with 90 additions and 0 deletions

View File

@ -0,0 +1,23 @@
#include <std_include.hpp>
#include "module_loader.hpp"
std::vector<std::unique_ptr<module>>* module_loader::modules_ = nullptr;
void module_loader::register_module(std::unique_ptr<module>&& module_)
{
if(!module_loader::modules_)
{
module_loader::modules_ = new std::vector<std::unique_ptr<module>>();
atexit(module_loader::destroy_modules);
}
module_loader::modules_->push_back(std::move(module_));
}
void module_loader::destroy_modules()
{
if(!module_loader::modules_) return;
delete module_loader::modules_;
module_loader::modules_ = nullptr;
}

View File

@ -0,0 +1,31 @@
#pragma once
#include "module.hpp"
class module_loader final
{
public:
template <typename T>
class installer
{
static_assert(std::is_base_of<module, T>::value, "Module has invalid base class");
public:
installer()
{
register_module(std::make_unique<T>());
}
};
static void register_module(std::unique_ptr<module>&& module);
private:
static std::vector<std::unique_ptr<module>>* modules_;
static void destroy_modules();
};
#define REGISTER_MODULE(name) \
namespace \
{ \
static module_loader::installer<name> $_##name; \
}

18
src/module/ceg.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <std_include.hpp>
#include "loader/module_loader.hpp"
class ceg final : public module
{
public:
ceg()
{
OutputDebugStringA("+ CEG\n");
}
~ceg()
{
OutputDebugStringA("- CEG\n");
}
};
REGISTER_MODULE(ceg)

18
src/module/dw.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <std_include.hpp>
#include "loader/module_loader.hpp"
class dw final : public module
{
public:
dw()
{
OutputDebugStringA("+ DW\n");
}
~dw()
{
OutputDebugStringA("- DW\n");
}
};
REGISTER_MODULE(dw)