looper/backend.hpp
2024-03-26 18:39:02 -07:00

48 lines
No EOL
1.5 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 void add_licenses() {
// Don't add any here, but leave this specified. That way, licenses specific to UI frontends are only added per UI frontend.
}
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();
virtual ~UIBackend();
};
void init_backends();