the gui problem^TM
This commit is contained in:
262
src/App.cpp
262
src/App.cpp
@@ -1,160 +1,164 @@
|
||||
#include "App.h"
|
||||
|
||||
App* App::instance = nullptr;
|
||||
Archimedes::App* Archimedes::App::instance = nullptr;
|
||||
|
||||
App::App(const int& argc, char* argv[]) {
|
||||
namespace Archimedes {
|
||||
|
||||
|
||||
App::App(const int& argc, char* argv[]) {
|
||||
|
||||
if(instance != nullptr) {
|
||||
std::cout << "App already exists\nThere can only be one!\n";
|
||||
std::abort();
|
||||
}
|
||||
|
||||
std::cout << "Initializing...\n";
|
||||
|
||||
instance = this;
|
||||
|
||||
handleArgs(argc, argv);
|
||||
|
||||
if(instance != nullptr) {
|
||||
std::cout << "App already exists\nThere can only be one!\n";
|
||||
std::abort();
|
||||
}
|
||||
|
||||
std::cout << "Initializing...\n";
|
||||
App::~App() {
|
||||
|
||||
instance = this;
|
||||
|
||||
handleArgs(argc, argv);
|
||||
|
||||
}
|
||||
|
||||
App::~App() {
|
||||
|
||||
std::cout << "\nExiting...\n";
|
||||
for(auto it = modules.begin(); it != modules.end(); it++) {
|
||||
void* handle = (*it)->getHandle();
|
||||
delete *it;
|
||||
dlclose(handle);
|
||||
it = modules.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool App::load(std::string lib, std::list<std::string> blacklist = {}) {
|
||||
|
||||
|
||||
std::cout << "print twice!\n";
|
||||
void* h = dlopen(lib.c_str(), RTLD_NOW);
|
||||
|
||||
if(!h) {
|
||||
std::cout << "could not open lib at: \"" << lib.c_str() << "\"\nError: " << dlerror() << std::endl;
|
||||
return false;
|
||||
std::cout << "\nExiting...\n";
|
||||
for(auto it = modules.begin(); it != modules.end(); it++) {
|
||||
void* handle = (*it)->getHandle();
|
||||
delete *it;
|
||||
dlclose(handle);
|
||||
it = modules.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
Module::create_t* create = (Module::create_t*) dlsym(h, "create");
|
||||
|
||||
if(dlerror()) {
|
||||
std::cout << "error finding create function in file: " << lib << std::endl;
|
||||
}
|
||||
|
||||
Module* m = create(h, App::Get());
|
||||
bool App::load(std::string lib, std::list<std::string> blacklist = {}) {
|
||||
|
||||
for(auto it = blacklist.begin(); it != blacklist.end(); it++) {
|
||||
if(*it == m->getName()) {
|
||||
std::cout << "Module \"" << *it << "\" is already loaded!\n";
|
||||
delete m;
|
||||
dlclose(h);
|
||||
|
||||
std::cout << "print twice!\n";
|
||||
void* h = dlopen(lib.c_str(), RTLD_NOW);
|
||||
|
||||
if(!h) {
|
||||
std::cout << "could not open lib at: \"" << lib.c_str() << "\"\nError: " << dlerror() << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
blacklist.push_back(m->getName());
|
||||
|
||||
bool skip = false;
|
||||
for(auto it = m->deps.begin(); it != m->deps.end(); it++) {
|
||||
for(std::string s : blacklist) {
|
||||
if(it->first == s)
|
||||
skip = true;
|
||||
Module::create_t* create = (Module::create_t*) dlsym(h, "create");
|
||||
|
||||
if(dlerror()) {
|
||||
std::cout << "error finding create function in file: " << lib << std::endl;
|
||||
}
|
||||
if(skip) {
|
||||
skip = false;
|
||||
continue;
|
||||
} else {
|
||||
load(it->second, blacklist);
|
||||
|
||||
Module* m = create(h, App::Get());
|
||||
|
||||
for(auto it = blacklist.begin(); it != blacklist.end(); it++) {
|
||||
if(*it == m->getName()) {
|
||||
std::cout << "Module \"" << *it << "\" is already loaded!\n";
|
||||
delete m;
|
||||
dlclose(h);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
blacklist.push_back(m->getName());
|
||||
|
||||
bool skip = false;
|
||||
for(auto it = m->deps.begin(); it != m->deps.end(); it++) {
|
||||
for(std::string s : blacklist) {
|
||||
if(it->first == s)
|
||||
skip = true;
|
||||
}
|
||||
if(skip) {
|
||||
skip = false;
|
||||
continue;
|
||||
} else {
|
||||
load(it->second, blacklist);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
modules.push_back(m);
|
||||
m->setSelf(--modules.end());
|
||||
modules.end()++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void App::unload(std::list<Module*>::iterator it) {
|
||||
|
||||
modules.push_back(m);
|
||||
m->setSelf(--modules.end());
|
||||
modules.end()++;
|
||||
Module* m = *it;
|
||||
void* h = m->getHandle();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void App::unload(std::list<Module*>::iterator it) {
|
||||
modules.erase(m->self);
|
||||
delete m;
|
||||
|
||||
Module* m = *it;
|
||||
void* h = m->getHandle();
|
||||
dlclose(h);
|
||||
|
||||
modules.erase(m->self);
|
||||
delete m;
|
||||
|
||||
dlclose(h);
|
||||
|
||||
}
|
||||
|
||||
void App::stopModule(std::list<Module*>::iterator it) {
|
||||
toClose.push_back(*it);
|
||||
}
|
||||
|
||||
void App::startModule(std::string lib) {
|
||||
toOpen.push_back(lib);
|
||||
}
|
||||
|
||||
void App::handleArgs(const int& argc, char* argv[]) {
|
||||
|
||||
int i = 1;
|
||||
|
||||
if(argc == 1) {
|
||||
printHelp();
|
||||
end();
|
||||
}
|
||||
|
||||
while(i < argc) {
|
||||
if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
|
||||
|
||||
void App::stopModule(std::list<Module*>::iterator it) {
|
||||
toClose.push_back(*it);
|
||||
}
|
||||
|
||||
void App::startModule(std::string lib) {
|
||||
toOpen.push_back(lib);
|
||||
}
|
||||
|
||||
void App::handleArgs(const int& argc, char* argv[]) {
|
||||
|
||||
int i = 1;
|
||||
|
||||
if(argc == 1) {
|
||||
printHelp();
|
||||
end();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
|
||||
while(i < argc) {
|
||||
if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
|
||||
printHelp();
|
||||
end();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
while(i < argc) {
|
||||
std::cout << "Attempting to load: " << argv[i] << std::endl;
|
||||
load(argv[i]);
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
while(i < argc) {
|
||||
std::cout << "Attempting to load: " << argv[i] << std::endl;
|
||||
load(argv[i]);
|
||||
void App::printHelp() {
|
||||
std::cout << "archimedes [ -h | --help ] /path/to/some/library.so\n";
|
||||
}
|
||||
|
||||
|
||||
void App::run() {
|
||||
std::cout << "\nTesting...\n";
|
||||
|
||||
for(auto* m : modules)
|
||||
m->onLoad();
|
||||
|
||||
// Main loop
|
||||
while (!done && !modules.empty()) {
|
||||
|
||||
for(auto* m : modules) {
|
||||
m->run();
|
||||
}
|
||||
|
||||
for(auto it = toClose.begin(); it != toClose.end(); it++) {
|
||||
unload(it);
|
||||
}
|
||||
toClose.clear();
|
||||
|
||||
for(std::string s : toOpen) {
|
||||
load(s, getBlacklist());
|
||||
}
|
||||
toOpen.clear();
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void App::printHelp() {
|
||||
std::cout << "archimedes [ -h | --help ] /path/to/some/library.so\n";
|
||||
}
|
||||
|
||||
|
||||
void App::run() {
|
||||
std::cout << "\nTesting...\n";
|
||||
|
||||
for(auto* m : modules)
|
||||
m->onLoad();
|
||||
|
||||
// Main loop
|
||||
while (!done && !modules.empty()) {
|
||||
|
||||
for(auto* m : modules) {
|
||||
m->run();
|
||||
}
|
||||
|
||||
for(auto it = toClose.begin(); it != toClose.end(); it++) {
|
||||
unload(it);
|
||||
}
|
||||
toClose.clear();
|
||||
|
||||
for(std::string s : toOpen) {
|
||||
load(s, getBlacklist());
|
||||
}
|
||||
toOpen.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
60
src/App.h
60
src/App.h
@@ -5,44 +5,48 @@
|
||||
#include "Module.h"
|
||||
#include "GuiModule.h"
|
||||
|
||||
class App {
|
||||
namespace Archimedes {
|
||||
|
||||
private:
|
||||
static App* instance;
|
||||
class App {
|
||||
|
||||
bool done = false;
|
||||
private:
|
||||
static App* instance;
|
||||
|
||||
std::list<Module*> modules;
|
||||
std::list<Module*> toClose;
|
||||
std::list<std::string> toOpen;
|
||||
|
||||
bool load(std::string, std::list<std::string>);
|
||||
void unload(std::list<Module*>::iterator);
|
||||
|
||||
void handleArgs(const int&, char*[]);
|
||||
bool done = false;
|
||||
|
||||
void printHelp();
|
||||
std::list<Module*> modules;
|
||||
std::list<Module*> toClose;
|
||||
std::list<std::string> toOpen;
|
||||
|
||||
std::list<std::string> getBlacklist() {
|
||||
std::list<std::string> l;
|
||||
for(Module* m : modules)
|
||||
l.push_back(m->getName());
|
||||
return l;
|
||||
}
|
||||
bool load(std::string, std::list<std::string>);
|
||||
void unload(std::list<Module*>::iterator);
|
||||
|
||||
public:
|
||||
App(const int&, char*[]);
|
||||
~App();
|
||||
void handleArgs(const int&, char*[]);
|
||||
|
||||
static App& Get() { return *instance; }
|
||||
void printHelp();
|
||||
|
||||
void run();
|
||||
std::list<std::string> getBlacklist() {
|
||||
std::list<std::string> l;
|
||||
for(Module* m : modules)
|
||||
l.push_back(m->getName());
|
||||
return l;
|
||||
}
|
||||
|
||||
void stopModule(std::list<Module*>::iterator);
|
||||
public:
|
||||
App(const int&, char*[]);
|
||||
~App();
|
||||
|
||||
void startModule(std::string);
|
||||
static App& Get() { return *instance; }
|
||||
|
||||
void end() { done = true; }
|
||||
};
|
||||
void run();
|
||||
|
||||
void stopModule(std::list<Module*>::iterator);
|
||||
|
||||
void startModule(std::string);
|
||||
|
||||
void end() { done = true; }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -4,21 +4,24 @@
|
||||
#include "Module.h"
|
||||
#include "Window/Window.h"
|
||||
|
||||
class GuiModule : Module {
|
||||
namespace Archimedes {
|
||||
|
||||
public:
|
||||
typedef GuiModule* create_t();
|
||||
class GuiModule : public Module {
|
||||
|
||||
GuiModule(void* h, App& a) : Module(h, a) {
|
||||
deps["Window"] = "";
|
||||
}
|
||||
virtual ~GuiModule() {}
|
||||
virtual void load() = 0;
|
||||
virtual void run() = 0;
|
||||
public:
|
||||
typedef GuiModule* create_t(void*, App&);
|
||||
|
||||
private:
|
||||
Window window;
|
||||
};
|
||||
GuiModule(void* h, App& a) : Module(h, a) {}
|
||||
virtual ~GuiModule() {}
|
||||
virtual void onLoad() = 0;
|
||||
virtual void run() = 0;
|
||||
|
||||
Window* getWindow() { return window; }
|
||||
|
||||
private:
|
||||
Window* window;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
17
src/Module.h
17
src/Module.h
@@ -3,13 +3,15 @@
|
||||
|
||||
#include "pch.hpp"
|
||||
|
||||
class App;
|
||||
namespace Archimedes {
|
||||
|
||||
class Module {
|
||||
class App;
|
||||
|
||||
friend class App;
|
||||
class Module {
|
||||
|
||||
public:
|
||||
friend class App;
|
||||
|
||||
public:
|
||||
typedef Module* create_t(void*, App&);
|
||||
|
||||
Module(void* h, App& a) : handle(h), app(a) {};
|
||||
@@ -21,8 +23,8 @@ class Module {
|
||||
void* getHandle() { return handle; }
|
||||
|
||||
void setSelf(std::list<Module*>::iterator s) { self = s; }
|
||||
|
||||
protected:
|
||||
|
||||
protected:
|
||||
std::string name;
|
||||
void* handle;
|
||||
std::list<Module*>::iterator self;
|
||||
@@ -30,6 +32,7 @@ class Module {
|
||||
App& app;
|
||||
|
||||
std::unordered_map<std::string, std::string> deps;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
#include "App.h"
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
App app(argc, argv);
|
||||
Archimedes::App app(argc, argv);
|
||||
app.run();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user