Update Modules Structure.

- Fully Functional programming.
- Restructured main
- Restructured Init.
- Currently unbuildable.
This commit is contained in:
realaltffour 2020-04-05 13:59:31 +03:00
parent f35201061d
commit 54f20fb0cf
No known key found for this signature in database
GPG Key ID: 05B35E2E8F56C5A6
4 changed files with 34 additions and 60 deletions

View File

@ -7,11 +7,12 @@
#include "module.h"
#include "modules/init.h"
Module* get_Mod(std::string name) {return nullptr;};
void dispatch_Mod(Module* mod, std::string args) {};
int main(int argc, const char* argv[]) {
using namespace boost::program_options;
using namespace std;
std::map<string, Module*> lmodules;
lmodules["init"] = new modules::init::mod;
try {
options_description general("General Options");
@ -37,26 +38,23 @@ int main(int argc, const char* argv[]) {
exit(0);
} else if (vm.count("help-module")) {
auto name = vm["help-module"].as<string>();
if (lmodules.find(name) == lmodules.end()) {
std::cout << "Module not found.\n";
exit(1);
auto module = get_Mod(name);
if (!module) {
std::cout << "Module not found.\n";
exit(1);
}
auto module = lmodules[name];
std::cout << module->desc << "\n";
} else if (vm.count("module")) {
auto name = vm["module"].as<string>();
if (lmodules.find(name) == lmodules.end()) {
std::cout << "Module not found.\n";
exit(1);
}
auto module = lmodules[name];
auto module = get_Mod(name);
if (!module) {
std::cout << "Module not found.\n";
exit(1);
}
if (vm.count("module-args")) {
auto args = vm["module-args"].as<string>();
module->dispatch(args);
} else
module->dispatch("");
auto args = vm["module-args"].as<string>();
dispatch_Mod(module, args);
} else dispatch_Mod(module, "");
} else {
std::cout << general << "\n";
exit(0);

View File

@ -5,31 +5,6 @@
#include <map>
#include <string>
#define MODFN(name, code) \
void name(std::string arguments) { code }
#define MODFNSIG(name) void name(std::string arguments);
#define MODFNIMPL(name, code) \
void name(std::string arguments) { code }
#define MODDISPATCHFNSIG() virtual void dispatch(std::string arguments) = 0;
#define MODDISPATCHFNIMPL(code) \
void mod::dispatch(std::string arguments) { code }
#define MODDISPATCHFN() void dispatch(std::string arguments) override;
#define MODARGS(code) \
struct args { \
code \
}
#define MAKEMOD(code) \
struct mod : args, Module { \
code \
}
#define DEFMOD(name_, desc_) \
mod() { \
this->name = name_; \
this->desc = desc_; \
}
using ModuleFn = std::function<void(std::string arguments)>;
using ModuleFnDispatcher = std::function<void(std::string arguments)>;
@ -37,7 +12,6 @@ struct Module {
std::string name;
std::string desc;
std::map<std::string, ModuleFn> funcs;
MODDISPATCHFNSIG();
};
#endif

View File

@ -2,9 +2,7 @@
#include "../data.h"
namespace modules {
namespace init {
MODFNIMPL(init_create, {
void init_create(init_args arg) {
std::cout << "Creating database in current directory." << std::endl;
DB db;
time_t t = time(0);
@ -24,8 +22,16 @@ MODFNIMPL(init_create, {
// TODO: Add event logging
writeDB(db, ".db");
});
}
MODDISPATCHFNIMPL({ init_create(arguments); });
} // namespace init
}; // namespace modules
void init_dispatch() {
init_args arg;
init_create(arg);
}
init_mod init_new() {
init_mod mod;
mod.name = "Init";
mod.desc = "Creates skidjular directory in $(pwd)";
return mod;
}

View File

@ -2,21 +2,17 @@
#define MODULE_INIT_H
#include <time.h>
#include <ctime>
#include <iostream>
#include "module.h"
namespace modules {
namespace init {
MODARGS();
MAKEMOD(MODFNSIG(init_create);
MODDISPATCHFN();
DEFMOD("init",
"Creates a skidjular directory in $(pwd)");
);
struct init_args {};
struct init_mod : init_args, Module {};
};
};
init_mod init_new();
void init_create(init_args arg);
void init_dispatch(std::string arguments);
#endif