-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
404 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include "SharedLibrary.hpp" | ||
|
||
#include "Exception.hpp" | ||
|
||
namespace soup | ||
{ | ||
SharedLibrary::SharedLibrary(const std::string& path) | ||
{ | ||
load(path); | ||
} | ||
|
||
SharedLibrary::SharedLibrary(const char* path) | ||
{ | ||
load(path); | ||
} | ||
|
||
SharedLibrary::SharedLibrary(SharedLibrary&& b) | ||
: handle(b.handle) | ||
{ | ||
b.forget(); | ||
} | ||
|
||
SharedLibrary::~SharedLibrary() | ||
{ | ||
unload(); | ||
} | ||
|
||
void SharedLibrary::operator=(SharedLibrary&& b) | ||
{ | ||
unload(); | ||
handle = b.handle; | ||
b.forget(); | ||
} | ||
|
||
bool SharedLibrary::isLoaded() const noexcept | ||
{ | ||
return handle != nullptr; | ||
} | ||
|
||
bool SharedLibrary::load(const std::string& path) | ||
{ | ||
return load(path.c_str()); | ||
} | ||
|
||
bool SharedLibrary::load(const char* path) | ||
{ | ||
#if SOUP_WINDOWS | ||
handle = LoadLibraryA(path); | ||
#else | ||
handle = dlopen(path, RTLD_LAZY); | ||
#endif | ||
return isLoaded(); | ||
} | ||
|
||
void SharedLibrary::unload() | ||
{ | ||
if (isLoaded()) | ||
{ | ||
#if SOUP_WINDOWS | ||
FreeLibrary(handle); | ||
#else | ||
dlclose(handle); | ||
#endif | ||
forget(); | ||
} | ||
} | ||
|
||
void SharedLibrary::forget() | ||
{ | ||
handle = nullptr; | ||
} | ||
|
||
void* SharedLibrary::getAddress(const char* name) const noexcept | ||
{ | ||
SOUP_IF_UNLIKELY (!isLoaded()) | ||
{ | ||
return nullptr; | ||
} | ||
#if SOUP_WINDOWS | ||
return (void*)GetProcAddress(handle, name); | ||
#else | ||
return dlsym(handle, name); | ||
#endif | ||
} | ||
|
||
void* SharedLibrary::getAddressMandatory(const char* name) const | ||
{ | ||
SOUP_IF_LIKELY (auto addr = getAddress(name)) | ||
{ | ||
return addr; | ||
} | ||
std::string msg = "Failed to find mandatory symbol: "; | ||
msg.append(name); | ||
SOUP_THROW(Exception(std::move(msg))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
|
||
#include "base.hpp" | ||
|
||
#if SOUP_WINDOWS | ||
#include <Windows.h> | ||
#else | ||
#include <dlfcn.h> | ||
#endif | ||
|
||
#include <string> | ||
|
||
namespace soup | ||
{ | ||
struct SharedLibrary | ||
{ | ||
#if SOUP_WINDOWS | ||
using handle_t = HMODULE; | ||
#else | ||
using handle_t = void*; | ||
#endif | ||
|
||
handle_t handle = nullptr; | ||
|
||
explicit SharedLibrary() = default; | ||
explicit SharedLibrary(const std::string& path); | ||
explicit SharedLibrary(const char* path); | ||
explicit SharedLibrary(SharedLibrary&& b); | ||
~SharedLibrary(); | ||
|
||
void operator=(SharedLibrary&& b); | ||
|
||
[[nodiscard]] bool isLoaded() const noexcept; | ||
bool load(const std::string& path); | ||
bool load(const char* path); | ||
void unload(); | ||
void forget(); | ||
|
||
[[nodiscard]] void* getAddress(const char* name) const noexcept; | ||
[[nodiscard]] void* getAddressMandatory(const char* name) const; | ||
}; | ||
} |
Oops, something went wrong.