LogViewer
A modern log viewer with AI-assisted analysis (Qt 6)
Loading...
Searching...
No Matches
PluginFunctionRegistry.hpp
Go to the documentation of this file.
1
8#ifndef PLUGIN_FUNCTION_REGISTRY_HPP
9#define PLUGIN_FUNCTION_REGISTRY_HPP
10
11#include <functional>
12#include <memory>
13#include <stdexcept>
14#include <string>
15#include "Logger.hpp"
16
17#ifdef _WIN32
18 #include <windows.h>
19#else
20 #include <dlfcn.h>
21#endif
22
23namespace plugin {
24
50template <typename FnType>
52 public:
58 FunctionRegistry(void* handle, const std::string& symbolName)
60 {
62
63 if (!m_function) {
64 util::Logger::Debug("Plugin symbol not found: {}", symbolName);
65 } else {
66 util::Logger::Trace("Plugin symbol resolved: {}", symbolName);
67 }
68 }
69
74 explicit operator bool() const { return m_function != nullptr; }
75
80 FnType get() const { return m_function; }
81
99 template <typename... Args>
100 requires std::invocable<FnType, Args...>
101 auto call(Args&&... args) const
102 {
103 if (!m_function) {
104 throw std::runtime_error("Function not loaded: " + m_symbolName);
105 }
106
107 try {
108 return std::invoke(m_function, std::forward<Args>(args)...);
109 } catch (const std::exception& e) {
110 util::Logger::Error("Error calling plugin function {}: {}",
111 m_symbolName, e.what());
112 throw;
113 }
114 }
115
116 private:
117 void* m_handle;
118 std::string m_symbolName;
120
125 FnType ResolveSymbol() const
126 {
127#ifdef _WIN32
128 // FARPROC is a function pointer; cannot assign to void* — cast directly.
130 m_symbolName.c_str());
131 return reinterpret_cast<FnType>(proc);
132#else
133 // Clear any previous errors
134 dlerror();
135 void* ptr = dlsym(m_handle, m_symbolName.c_str());
136
137 if (!ptr) {
138 const char* error = dlerror();
139 if (error) {
140 util::Logger::Trace("dlsym error: {}", error);
141 }
142 }
143
144 return reinterpret_cast<FnType>(ptr);
145#endif
146 }
147};
148
149} // namespace plugin
150
151#endif // PLUGIN_FUNCTION_REGISTRY_HPP
Logging facade for application-wide logging.
Type-safe wrapper for dynamically loaded functions.
Definition PluginFunctionRegistry.hpp:51
FnType get() const
Gets raw function pointer.
Definition PluginFunctionRegistry.hpp:80
void * m_handle
Definition PluginFunctionRegistry.hpp:117
std::string m_symbolName
Definition PluginFunctionRegistry.hpp:118
auto call(Args &&... args) const
Calls the resolved function with type checking via concepts.
Definition PluginFunctionRegistry.hpp:101
FunctionRegistry(void *handle, const std::string &symbolName)
Constructs registry and attempts to resolve symbol.
Definition PluginFunctionRegistry.hpp:58
FnType ResolveSymbol() const
Resolves symbol from dynamic library.
Definition PluginFunctionRegistry.hpp:125
FnType m_function
Definition PluginFunctionRegistry.hpp:119
static void Trace(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logger.hpp:279
static void Debug(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logger.hpp:285
static void Error(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logger.hpp:303
Definition Error.hpp:17
Definition IFieldConversionPlugin.hpp:8