versioned interfaces

This commit is contained in:
Green Sky 2024-01-18 00:26:18 +01:00
parent b502690621
commit 87b3d15a2b
No known key found for this signature in database
3 changed files with 45 additions and 15 deletions

View File

@ -5,23 +5,24 @@
#include <iostream> #include <iostream>
// def // def
std::map<std::string, void*> g_instance_map {}; std::map<std::string, std::map<std::string, void*>> g_instance_map {};
extern "C" { extern "C" {
void* g_resolveInstance__internal(const char* id) { void* g_resolveInstance__internal(const char* id, const char* version) {
if (auto it = g_instance_map.find(id); it != g_instance_map.end()) { if (auto it = g_instance_map.find(id); it != g_instance_map.end()) {
return it->second; if (auto it_v = it->second.find(version); it_v != it->second.end()) {
return it_v->second;
}
} }
return nullptr; return nullptr;
} }
void g_provideInstance__internal(const char* id, const char* plugin_name, void* instance) { void g_provideInstance__internal(const char* id, const char* version, const char* plugin_name, void* instance) {
g_instance_map[id] = instance; g_instance_map[id][version] = instance;
std::cout << "PLM '" << plugin_name << "' provided '" << id << "'\n"; std::cout << "PLM: '" << plugin_name << "' provided '" << id << "' version '" << version << "'\n";
} }
} // extern C } // extern C
PluginManager::~PluginManager(void) { PluginManager::~PluginManager(void) {

View File

@ -10,20 +10,50 @@
#include <string> #include <string>
// TODO: save plug name to instance // TODO: save plug name to instance
extern std::map<std::string, void*> g_instance_map; // id -> version -> ptr
extern std::map<std::string, std::map<std::string, void*>> g_instance_map;
extern "C" { extern "C" {
void* g_resolveInstance__internal(const char* id); void* g_resolveInstance__internal(const char* id, const char* version);
void g_provideInstance__internal(const char* id, const char* plugin_name, void* instance); void g_provideInstance__internal(const char* id, const char* version, const char* plugin_name, void* instance);
} // extern C } // extern C
namespace internal {
template<typename T>
class g_type_version {
typedef char yes[1];
typedef char no [2];
template<typename C> static yes& test_version(decltype(&C::version));
template<typename C> static no& test_version(...);
static bool const has_version = sizeof(test_version<T>(nullptr)) == sizeof(yes);
static constexpr const char* get_version(void) {
if constexpr (has_version) {
return T::version;
} else {
return "UNK"; // default version
}
}
public:
static constexpr const char* version = get_version();
};
} // internal
// templated helper, use or make sure vtable is right // templated helper, use or make sure vtable is right
template<typename T>
void g_provideInstance(const char* id, const char* version, const char* plugin_name, T* instance) {
g_provideInstance__internal(id, version, plugin_name, instance);
}
template<typename T> template<typename T>
void g_provideInstance(const char* id, const char* plugin_name, T* instance) { void g_provideInstance(const char* id, const char* plugin_name, T* instance) {
g_provideInstance__internal(id, plugin_name, instance); g_provideInstance__internal(id, internal::g_type_version<T>::version, plugin_name, instance);
} }
// only on host! // only on host!

View File

@ -3,7 +3,7 @@
#include <stdint.h> #include <stdint.h>
#define SOLANA_PLUGIN_VERSION 8 #define SOLANA_PLUGIN_VERSION 9
#if defined(_MSC_VER) || defined(__MINGW32__) #if defined(_MSC_VER) || defined(__MINGW32__)
#define SOLANA_PLUGIN_EXPORT __declspec(dllexport) #define SOLANA_PLUGIN_EXPORT __declspec(dllexport)
@ -27,10 +27,9 @@ extern "C" {
// public plugin stuff here // public plugin stuff here
struct SolanaAPI { struct SolanaAPI {
void* (*resolveInstance)(const char* id); void* (*resolveInstance)(const char* id, const char* version);
// resolve_all_instances(const char* id) // resolve_all_instances(const char* id)
void (*provideInstance)(const char* id, const char* plugin_name, void* instance); void (*provideInstance)(const char* id, const char* version, const char* plugin_name, void* instance);
}; };
// ---------- info ---------- // ---------- info ----------