Skip to content

Commit

Permalink
Merge pull request #82 from boostorg/construct-from-native
Browse files Browse the repository at this point in the history
Add constructor taking native_handle_t
  • Loading branch information
apolukhin authored Dec 21, 2024
2 parents 15d8fb4 + 7cbc0d4 commit 7ecf458
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
12 changes: 8 additions & 4 deletions include/boost/dll/detail/posix/shared_library_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class shared_library_impl {
typedef void* native_handle_t;

shared_library_impl() noexcept
: handle_(NULL)
: handle_(nullptr)
{}

~shared_library_impl() noexcept {
Expand All @@ -48,9 +48,13 @@ class shared_library_impl {
shared_library_impl(shared_library_impl&& sl) noexcept
: handle_(sl.handle_)
{
sl.handle_ = NULL;
sl.handle_ = nullptr;
}

explicit shared_library_impl(native_handle_t handle) noexcept
: handle_(handle)
{}

shared_library_impl & operator=(shared_library_impl&& sl) noexcept {
swap(sl);
return *this;
Expand Down Expand Up @@ -152,7 +156,7 @@ class shared_library_impl {
// returned handle is for the main program.
ec.clear();
boost::dll::detail::reset_dlerror();
handle_ = dlopen(NULL, native_mode);
handle_ = dlopen(nullptr, native_mode);
if (!handle_) {
ec = std::make_error_code(
std::errc::bad_file_descriptor
Expand Down Expand Up @@ -194,7 +198,7 @@ class shared_library_impl {
void* symbol_addr(const char* sb, std::error_code &ec) const noexcept {
// dlsym - obtain the address of a symbol from a dlopen object
void* const symbol = dlsym(handle_, sb);
if (symbol == NULL) {
if (symbol == nullptr) {
ec = std::make_error_code(
std::errc::invalid_seek
);
Expand Down
12 changes: 8 additions & 4 deletions include/boost/dll/detail/windows/shared_library_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class shared_library_impl {
typedef boost::winapi::HMODULE_ native_handle_t;

shared_library_impl() noexcept
: handle_(NULL)
: shared_library_impl(nullptr)
{}

~shared_library_impl() noexcept {
Expand All @@ -41,9 +41,13 @@ class shared_library_impl {
shared_library_impl(shared_library_impl&& sl) noexcept
: handle_(sl.handle_)
{
sl.handle_ = NULL;
sl.handle_ = nullptr;
}

explicit shared_library_impl(native_handle_t handle) noexcept
: handle_(handle)
{}

shared_library_impl & operator=(shared_library_impl&& sl) noexcept {
swap(sl);
return *this;
Expand Down Expand Up @@ -142,7 +146,7 @@ class shared_library_impl {
std::errc::operation_not_supported
);

return NULL;
return nullptr;
}

// Judging by the documentation of GetProcAddress
Expand All @@ -151,7 +155,7 @@ class shared_library_impl {
void* const symbol = boost::dll::detail::aggressive_ptr_cast<void*>(
boost::winapi::get_proc_address(handle_, sb)
);
if (symbol == NULL) {
if (symbol == nullptr) {
ec = boost::dll::detail::last_error_code();
}

Expand Down
9 changes: 9 additions & 0 deletions include/boost/dll/shared_library.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ class shared_library
shared_library::load(lib_path, mode, ec);
}

/*!
* Takes ownership of a loaded library.
*
* \param handle The native handle.
*/
explicit shared_library(native_handle_t handle) noexcept
: base_t(handle)
{}

/*!
* Assignment operator. If this->is_loaded() then calls this->unload(). Does not invalidate existing symbols and functions loaded from lib.
*
Expand Down

0 comments on commit 7ecf458

Please sign in to comment.