looper/backend.hpp

44 lines
No EOL
1.3 KiB
C++

#pragma once
#include <vector>
#include <string>
#include <map>
#include <optional>
#include "playback.h"
#include "dbus.hpp"
class UIBackend {
protected:
std::vector<std::string> args;
Playback *playback;
public:
DBusAPI *dbus_api;
virtual std::string get_id();
virtual std::string get_name();
UIBackend() = default;
virtual int run(std::vector<std::string> realArgs, int argc, char **argv);
static std::map<std::string, UIBackend*> backends;
static inline std::optional<UIBackend*> get_backend(std::string id) {
if (backends.contains(id)) {
return backends[id];
} else {
return {};
}
}
static inline UIBackend* get_first_backend() {
if (backends.empty()) {
return nullptr;
}
return (*backends.begin()).second;
}
template<class T>
static void register_backend() {
UIBackend *backend = new T();
std::string backend_id = backend->get_id();
if (backends.contains(backend_id)) { // Guard against potential memory leak due to reassigning a new pointer without deallocating the previous one
delete backend;
return;
}
backends[backend_id] = backend;
}
static void deinit_backends();
};
void init_backends();