mirror of
https://github.com/AlexandreRouma/SDRPlusPlus.git
synced 2025-06-26 04:17:50 +02:00
Code clean up + added inter module communication
This commit is contained in:
@ -40,6 +40,7 @@ namespace core {
|
||||
ConfigManager configManager;
|
||||
ScriptManager scriptManager;
|
||||
ModuleManager moduleManager;
|
||||
ModuleComManager modComManager;
|
||||
|
||||
void setInputSampleRate(double samplerate) {
|
||||
// NOTE: Zoom controls won't work
|
||||
|
@ -3,11 +3,13 @@
|
||||
#include <module.h>
|
||||
#include <scripting.h>
|
||||
#include <module.h>
|
||||
#include <module_com.h>
|
||||
|
||||
namespace core {
|
||||
SDRPP_EXPORT ConfigManager configManager;
|
||||
SDRPP_EXPORT ScriptManager scriptManager;
|
||||
SDRPP_EXPORT ModuleManager moduleManager;
|
||||
SDRPP_EXPORT ModuleComManager modComManager;
|
||||
|
||||
void setInputSampleRate(double samplerate);
|
||||
};
|
||||
|
51
core/src/module_com.cpp
Normal file
51
core/src/module_com.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
#include <module_com.h>
|
||||
|
||||
bool ModuleComManager::registerInterface(std::string moduleName, std::string name, void (*handler)(int code, void* in, void* out, void* ctx), void* ctx) {
|
||||
std::lock_guard<std::mutex> lck(mtx);
|
||||
if (interfaces.find(name) != interfaces.end()) {
|
||||
spdlog::error("Tried creating module interface with an existing name: {0}", name);
|
||||
return false;
|
||||
}
|
||||
ModuleComInterface iface;
|
||||
iface.moduleName = moduleName;
|
||||
iface.handler = handler;
|
||||
iface.ctx = ctx;
|
||||
interfaces[name] = iface;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ModuleComManager::unregisterInterface(std::string name) {
|
||||
std::lock_guard<std::mutex> lck(mtx);
|
||||
if (interfaces.find(name) == interfaces.end()) {
|
||||
spdlog::error("Tried to erase module interface with unknown name: {0}", name);
|
||||
return false;
|
||||
}
|
||||
interfaces.erase(name);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ModuleComManager::interfaceExists(std::string name) {
|
||||
std::lock_guard<std::mutex> lck(mtx);
|
||||
if (interfaces.find(name) == interfaces.end()) { return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string ModuleComManager::getModuleName(std::string name) {
|
||||
std::lock_guard<std::mutex> lck(mtx);
|
||||
if (interfaces.find(name) == interfaces.end()) {
|
||||
spdlog::error("Tried to call unknown module interface: {0}", name);
|
||||
return "";
|
||||
}
|
||||
return interfaces[name].moduleName;
|
||||
}
|
||||
|
||||
bool ModuleComManager::callInterface(std::string name, int code, void* in, void* out) {
|
||||
std::lock_guard<std::mutex> lck(mtx);
|
||||
if (interfaces.find(name) == interfaces.end()) {
|
||||
spdlog::error("Tried to call unknown module interface: {0}", name);
|
||||
return false;
|
||||
}
|
||||
ModuleComInterface iface = interfaces[name];
|
||||
iface.handler(code, in, out, iface.ctx);
|
||||
return true;
|
||||
}
|
24
core/src/module_com.h
Normal file
24
core/src/module_com.h
Normal file
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
struct ModuleComInterface {
|
||||
std::string moduleName;
|
||||
void* ctx;
|
||||
void (*handler)(int code, void* in, void* out, void* ctx);
|
||||
};
|
||||
|
||||
class ModuleComManager {
|
||||
public:
|
||||
bool registerInterface(std::string moduleName, std::string name, void (*handler)(int code, void* in, void* out, void* ctx), void* ctx);
|
||||
bool unregisterInterface(std::string name);
|
||||
bool interfaceExists(std::string name);
|
||||
std::string getModuleName(std::string name);
|
||||
bool callInterface(std::string name, int code, void* in, void* out);
|
||||
|
||||
private:
|
||||
std::mutex mtx;
|
||||
std::map<std::string, ModuleComInterface> interfaces;
|
||||
};
|
Reference in New Issue
Block a user