forked from maddouri/dynalo
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
a69019a
commit 7348d7f
Showing
3 changed files
with
74 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
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,70 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <stdexcept> | ||
|
||
#include <dlfcn.h> | ||
|
||
namespace dynalo { namespace detail | ||
{ | ||
|
||
inline | ||
std::string last_error() | ||
{ | ||
return std::string(::dlerror()); | ||
} | ||
|
||
namespace native | ||
{ | ||
|
||
using handle = void*; | ||
|
||
inline handle invalid_handle() { return nullptr; } | ||
|
||
namespace name | ||
{ | ||
|
||
inline std::string prefix() { return std::string("lib"); } | ||
inline std::string suffix() { return std::string(); } | ||
inline std::string extension() { return std::string("dylib"); } | ||
|
||
} | ||
|
||
} | ||
|
||
inline | ||
native::handle open(const std::string& dyn_lib_path) | ||
{ | ||
native::handle lib_handle = ::dlopen(dyn_lib_path.c_str(), RTLD_LAZY); | ||
if (lib_handle == nullptr) | ||
{ | ||
throw std::runtime_error(std::string("Failed to open [dyn_lib_path:") + dyn_lib_path + "]: " + last_error()); | ||
} | ||
|
||
return lib_handle; | ||
} | ||
|
||
inline | ||
void close(native::handle lib_handle) | ||
{ | ||
const int rc = ::dlclose(lib_handle); | ||
if (rc != 0) | ||
{ | ||
throw std::runtime_error(std::string("Failed to close the dynamic library: ") + last_error()); | ||
} | ||
} | ||
|
||
template <typename FunctionSignature> | ||
inline | ||
FunctionSignature* get_function(native::handle lib_handle, const std::string& func_name) | ||
{ | ||
void* func_ptr = ::dlsym(lib_handle, func_name.c_str()); | ||
if (func_ptr == nullptr) | ||
{ | ||
throw std::runtime_error(std::string("Failed to get [func_name:") + func_name + "]: " + last_error()); | ||
} | ||
|
||
return reinterpret_cast<FunctionSignature*>(func_ptr); | ||
} | ||
|
||
}} |
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