9#include <collections.h>
21 using LibraryPtr = HMODULE;
23 using LibraryPtr =
void*;
28 static constexpr auto LOAD_FUNCTION_NAME =
"SpecsCpp_Load";
30 collections_map<std::string, LibraryPtr> _loadedLibraries;
34 : _globalSpecEnvironment(globalSpecEnvironment) {}
39 if (!std::filesystem::exists(libraryFilePath))
40 throw std::runtime_error(
"File does not exist: " + std::string(libraryFilePath));
43 auto libraryHandle = LoadLibraryA(libraryFilePath.data());
45 auto libraryHandle = dlopen(libraryFilePath.data(), RTLD_LAZY);
49 throw std::runtime_error(
"Failed to load library: " + std::string(libraryFilePath));
51 auto loadFunctionAddress = GetProcAddress(libraryHandle, LOAD_FUNCTION_NAME);
52 if (!loadFunctionAddress)
53 throw std::runtime_error(
54 "Failed to get load function address: " + std::string(LOAD_FUNCTION_NAME)
57 auto loadFunction =
reinterpret_cast<LOAD_FUNCTION_SIGNATURE
>(loadFunctionAddress);
59 if (
auto* group = loadFunction(_globalSpecEnvironment)) {
60 _loadedLibraries.emplace(libraryFilePath, libraryHandle);
63 FreeLibrary(libraryHandle);
64 throw std::runtime_error(
65 "Failed to load group from library: " + std::string(libraryFilePath)
70 bool unload(std::string_view libraryFilePath) {
71 if (
auto it = _loadedLibraries.find(libraryFilePath.data());
72 it != _loadedLibraries.end()) {
74 FreeLibrary(it->second);
79 _loadedLibraries.erase(it);
86 for (
auto& [libraryFilePath, libraryHandle] : _loadedLibraries)
87 FreeLibrary(libraryHandle);
88 _loadedLibraries.clear();
bool unload(std::string_view libraryFilePath)
LibraryLoader(ISpecEnvironment *globalSpecEnvironment)
ISpecGroup * load(std::string_view libraryFilePath)