bigsnitch/addonhandler.h

62 lines
1.5 KiB
C
Raw Normal View History

2020-09-10 19:26:41 +00:00
#pragma once
#include <includes.h>
#include <dlfcn.h>
#include <functional>
template <typename T>
class DLWrapper {
private:
std::map<std::string, void*> funcs;
public:
void append(std::string name, void* ptr) {
if(ptr) {
funcs.insert({name, ptr});
}
}
void remove(std::string name) {
for(auto it = funcs.begin(); it != funcs.end(); ) {
if(it->first == name) {
it = funcs.erase(it);
return;
} else {
++it;
}
}
}
template<typename ...Args>
void operator()(Args... args) const {
for(auto fun : funcs) {
(T)(fun)(args...);
}
}
};
/*
the addon handler loads addon shared library objects and calls
in every loaded addon the implemented events, when they're implemented.
*/
class AddonHandler
{
private:
//list of loaded addons with library pointer
std::map<std::string, std::filesystem::path> loaded_addon_paths;
std::map<std::string, void*> loaded_addon_libs;
public:
AddonHandler();
// loads an addon and returns it's name, if successfully loaded
std::optional<std::string> load(std::filesystem::path path);
// unload an addon
void unload(std::string name);
// returns whether an addon with name is loaded
bool isLoaded(std::string name);
// returns the list of loaded addons (name, path)
std::map<std::string, std::filesystem::path> loadedAddons();
DLWrapper<void(*)(int)> on_request;
DLWrapper<void(*)(int)> on_response;
};