2024-05-31 13:12:28 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <filesystem>
|
2023-05-31 17:04:01 -04:00
|
|
|
#include <stdexcept>
|
2024-05-31 13:12:28 -04:00
|
|
|
#include <string>
|
2023-05-31 17:04:01 -04:00
|
|
|
#include <utility>
|
|
|
|
|
2024-05-31 13:12:28 -04:00
|
|
|
namespace fs = std::filesystem;
|
2023-05-31 17:04:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Dlhandle {
|
2024-05-31 13:12:28 -04:00
|
|
|
void *chandle = nullptr;
|
2023-05-31 17:04:01 -04:00
|
|
|
|
|
|
|
public:
|
|
|
|
class Exception : public std::runtime_error {
|
|
|
|
public:
|
|
|
|
using std::runtime_error::runtime_error;
|
|
|
|
};
|
|
|
|
|
2024-05-31 13:12:28 -04:00
|
|
|
Dlhandle() = default;
|
|
|
|
Dlhandle(const fs::path &fpath);
|
|
|
|
Dlhandle(const Dlhandle &o) = delete;
|
|
|
|
Dlhandle(Dlhandle &&o)
|
|
|
|
: chandle(o.chandle)
|
|
|
|
{
|
2023-05-31 17:04:01 -04:00
|
|
|
o.chandle = nullptr;
|
|
|
|
}
|
|
|
|
|
2024-05-31 13:12:28 -04:00
|
|
|
~Dlhandle();
|
2023-05-31 17:04:01 -04:00
|
|
|
|
2024-05-31 13:12:28 -04:00
|
|
|
Dlhandle &operator=(Dlhandle &&o) {
|
|
|
|
chandle = std::exchange(o.chandle, nullptr);
|
|
|
|
return *this;
|
2023-05-31 17:04:01 -04:00
|
|
|
}
|
|
|
|
|
2024-05-31 13:12:28 -04:00
|
|
|
template <typename T>
|
|
|
|
T *get(const std::string &symbol) const {
|
|
|
|
return reinterpret_cast<T *>(get_internal(symbol.c_str()));
|
2023-05-31 17:04:01 -04:00
|
|
|
}
|
|
|
|
|
2024-05-31 13:12:28 -04:00
|
|
|
auto get_fnc(const std::string &symbol) const {
|
|
|
|
return get<void*(...)>(symbol);
|
2023-05-31 17:04:01 -04:00
|
|
|
}
|
|
|
|
|
2024-05-31 13:12:28 -04:00
|
|
|
private:
|
|
|
|
void *get_internal(const char *symbol) const;
|
2023-05-31 17:04:01 -04:00
|
|
|
};
|