From ff277cfa6e1d24cfab6b4952bae2002d7a0daf0d Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sun, 23 Dec 2018 23:21:30 +0100 Subject: [PATCH] Prepare module management --- src/loader/module_loader.cpp | 23 +++++++++++++++++++++++ src/loader/module_loader.hpp | 31 +++++++++++++++++++++++++++++++ src/module/ceg.cpp | 18 ++++++++++++++++++ src/module/dw.cpp | 18 ++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 src/loader/module_loader.cpp create mode 100644 src/loader/module_loader.hpp create mode 100644 src/module/ceg.cpp create mode 100644 src/module/dw.cpp diff --git a/src/loader/module_loader.cpp b/src/loader/module_loader.cpp new file mode 100644 index 0000000..56e40bb --- /dev/null +++ b/src/loader/module_loader.cpp @@ -0,0 +1,23 @@ +#include +#include "module_loader.hpp" + +std::vector>* module_loader::modules_ = nullptr; + +void module_loader::register_module(std::unique_ptr&& module_) +{ + if(!module_loader::modules_) + { + module_loader::modules_ = new std::vector>(); + 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; +} diff --git a/src/loader/module_loader.hpp b/src/loader/module_loader.hpp new file mode 100644 index 0000000..50dff89 --- /dev/null +++ b/src/loader/module_loader.hpp @@ -0,0 +1,31 @@ +#pragma once +#include "module.hpp" + +class module_loader final +{ +public: + template + class installer + { + static_assert(std::is_base_of::value, "Module has invalid base class"); + + public: + installer() + { + register_module(std::make_unique()); + } + }; + + static void register_module(std::unique_ptr&& module); + +private: + static std::vector>* modules_; + + static void destroy_modules(); +}; + +#define REGISTER_MODULE(name) \ +namespace \ +{ \ + static module_loader::installer $_##name; \ +} diff --git a/src/module/ceg.cpp b/src/module/ceg.cpp new file mode 100644 index 0000000..6522fdb --- /dev/null +++ b/src/module/ceg.cpp @@ -0,0 +1,18 @@ +#include +#include "loader/module_loader.hpp" + +class ceg final : public module +{ +public: + ceg() + { + OutputDebugStringA("+ CEG\n"); + } + + ~ceg() + { + OutputDebugStringA("- CEG\n"); + } +}; + +REGISTER_MODULE(ceg) \ No newline at end of file diff --git a/src/module/dw.cpp b/src/module/dw.cpp new file mode 100644 index 0000000..2ccc52e --- /dev/null +++ b/src/module/dw.cpp @@ -0,0 +1,18 @@ +#include +#include "loader/module_loader.hpp" + +class dw final : public module +{ +public: + dw() + { + OutputDebugStringA("+ DW\n"); + } + + ~dw() + { + OutputDebugStringA("- DW\n"); + } +}; + +REGISTER_MODULE(dw) \ No newline at end of file