Specs
Beautiful C++ Test Framework
Loading...
Searching...
No Matches
SpecsCpp::LibraryLoader Class Reference

#include <LibraryLoader.h>

Public Member Functions

 LibraryLoader (ISpecEnvironment *globalSpecEnvironment)
 
 ~LibraryLoader ()
 
ISpecGroupload (std::string_view libraryFilePath)
 
bool unload (std::string_view libraryFilePath)
 
void unload_all ()
 

Detailed Description

Definition at line 17 of file LibraryLoader.h.

Constructor & Destructor Documentation

◆ LibraryLoader()

SpecsCpp::LibraryLoader::LibraryLoader ( ISpecEnvironment globalSpecEnvironment)
inline

Definition at line 33 of file LibraryLoader.h.

34 : _globalSpecEnvironment(globalSpecEnvironment) {}

◆ ~LibraryLoader()

SpecsCpp::LibraryLoader::~LibraryLoader ( )
inline

Definition at line 36 of file LibraryLoader.h.

Member Function Documentation

◆ load()

ISpecGroup * SpecsCpp::LibraryLoader::load ( std::string_view  libraryFilePath)
inline

Definition at line 38 of file LibraryLoader.h.

38 {
39 if (!std::filesystem::exists(libraryFilePath))
40 throw std::runtime_error("File does not exist: " + std::string(libraryFilePath));
41
42#ifdef _WIN32
43 auto libraryHandle = LoadLibraryA(libraryFilePath.data());
44#else
45 auto libraryHandle = dlopen(libraryFilePath.data(), RTLD_LAZY);
46#endif
47
48 if (!libraryHandle)
49 throw std::runtime_error("Failed to load library: " + std::string(libraryFilePath));
50
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)
55 );
56
57 auto loadFunction = reinterpret_cast<LOAD_FUNCTION_SIGNATURE>(loadFunctionAddress);
58
59 if (auto* group = loadFunction(_globalSpecEnvironment)) {
60 _loadedLibraries.emplace(libraryFilePath, libraryHandle);
61 return group;
62 } else {
63 FreeLibrary(libraryHandle);
64 throw std::runtime_error(
65 "Failed to load group from library: " + std::string(libraryFilePath)
66 );
67 }
68 }

◆ unload()

bool SpecsCpp::LibraryLoader::unload ( std::string_view  libraryFilePath)
inline

Definition at line 70 of file LibraryLoader.h.

70 {
71 if (auto it = _loadedLibraries.find(libraryFilePath.data());
72 it != _loadedLibraries.end()) {
73#ifdef _WIN32
74 FreeLibrary(it->second);
75#else
76 dlclose(it->second);
77#endif
78
79 _loadedLibraries.erase(it);
80 return true;
81 }
82 return false;
83 }
void it(std::string_view description, std::function< void()> body)
Definition it.h:11

◆ unload_all()

void SpecsCpp::LibraryLoader::unload_all ( )
inline

Definition at line 85 of file LibraryLoader.h.

85 {
86 for (auto& [libraryFilePath, libraryHandle] : _loadedLibraries)
87 FreeLibrary(libraryHandle);
88 _loadedLibraries.clear();
89 }

Referenced by ~LibraryLoader().


The documentation for this class was generated from the following file: