Skip to content

Commit

Permalink
Switch to std::error_code from boost::system::error_code where possib…
Browse files Browse the repository at this point in the history
…le (#76)
  • Loading branch information
apolukhin authored Dec 17, 2024
1 parent 46da0d1 commit c66dfab
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 102 deletions.
14 changes: 4 additions & 10 deletions include/boost/dll/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#endif

#ifdef BOOST_DLL_DOXYGEN
/// Define this macro to make Boost.DLL use C++17's std::filesystem::path, std::system_error and std::error_code.
/// Define this macro to make Boost.DLL use C++17's std::filesystem::path and std::system_error.
#define BOOST_DLL_USE_STD_FS BOOST_DLL_USE_STD_FS

/// This namespace contains aliases to the Boost or C++17 classes. Aliases are configured using BOOST_DLL_USE_STD_FS macro.
Expand All @@ -38,38 +38,32 @@ using system_error = std::conditional_t<BOOST_DLL_USE_STD_FS, std::system_error,

#endif


#ifdef BOOST_DLL_USE_STD_FS
#include <filesystem>

#include <system_error>

namespace boost { namespace dll { namespace fs {

using namespace std::filesystem;

using std::error_code;
using std::system_error;
using std::make_error_code;
using std::errc;
using std::system_category;

}}}

#else // BOOST_DLL_USE_STD_FS

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/system/error_code.hpp>
#include <boost/system/system_error.hpp>
#include <boost/system/error_code.hpp>

namespace boost { namespace dll { namespace fs {

using namespace boost::filesystem;

using boost::system::error_code;
using boost::system::system_error;
using boost::system::errc::make_error_code;
namespace errc = boost::system::errc;
using boost::system::system_category;

}}}

Expand Down
14 changes: 7 additions & 7 deletions include/boost/dll/detail/posix/path_from_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace boost { namespace dll { namespace detail {
);
}

inline boost::dll::fs::path path_from_handle(void* handle, boost::dll::fs::error_code &ec) {
inline boost::dll::fs::path path_from_handle(void* handle, std::error_code &ec) {
handle = strip_handle(handle);

// Iterate through all images currently in memory
Expand All @@ -53,8 +53,8 @@ namespace boost { namespace dll { namespace detail {
}

boost::dll::detail::reset_dlerror();
ec = boost::dll::fs::make_error_code(
boost::dll::fs::errc::bad_file_descriptor
ec = std::make_error_code(
std::errc::bad_file_descriptor
);

return boost::dll::fs::path();
Expand All @@ -78,7 +78,7 @@ namespace boost { namespace dll { namespace detail {
// ... // Ignoring remaning parts of the structure
};

inline boost::dll::fs::path path_from_handle(const void* handle, boost::dll::fs::error_code &ec) {
inline boost::dll::fs::path path_from_handle(const void* handle, std::error_code &ec) {
static const std::size_t work_around_b_24465209__offset = 128;
const struct soinfo* si = reinterpret_cast<const struct soinfo*>(
static_cast<const char*>(handle) + work_around_b_24465209__offset
Expand Down Expand Up @@ -119,7 +119,7 @@ namespace boost { namespace dll { namespace detail {
};
#endif // #if BOOST_OS_QNX

inline boost::dll::fs::path path_from_handle(void* handle, boost::dll::fs::error_code &ec) {
inline boost::dll::fs::path path_from_handle(void* handle, std::error_code &ec) {
// RTLD_DI_LINKMAP (RTLD_DI_ORIGIN returns only folder and is not suitable for this case)
// Obtain the Link_map for the handle that is specified.
// The p argument points to a Link_map pointer (Link_map
Expand All @@ -144,8 +144,8 @@ namespace boost { namespace dll { namespace detail {
#endif
if (!link_map) {
boost::dll::detail::reset_dlerror();
ec = boost::dll::fs::make_error_code(
boost::dll::fs::errc::bad_file_descriptor
ec = std::make_error_code(
std::errc::bad_file_descriptor
);

return boost::dll::fs::path();
Expand Down
27 changes: 15 additions & 12 deletions include/boost/dll/detail/posix/program_location_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <mach-o/dyld.h>

namespace boost { namespace dll { namespace detail {
inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code &ec) {
inline boost::dll::fs::path program_location_impl(std::error_code &ec) {
ec.clear();

char path[1024];
Expand All @@ -31,8 +31,8 @@ namespace boost { namespace dll { namespace detail {

char *p = new char[size];
if (_NSGetExecutablePath(p, &size) != 0) {
ec = boost::dll::fs::make_error_code(
boost::dll::fs::errc::bad_file_descriptor
ec = std::make_error_code(
std::errc::bad_file_descriptor
);
}

Expand All @@ -46,7 +46,7 @@ namespace boost { namespace dll { namespace detail {

#include <stdlib.h>
namespace boost { namespace dll { namespace detail {
inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code& ec) {
inline boost::dll::fs::path program_location_impl(std::error_code& ec) {
ec.clear();

return boost::dll::fs::path(getexecname());
Expand All @@ -60,7 +60,7 @@ namespace boost { namespace dll { namespace detail {
#include <stdlib.h>

namespace boost { namespace dll { namespace detail {
inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code& ec) {
inline boost::dll::fs::path program_location_impl(std::error_code& ec) {
ec.clear();

int mib[4];
Expand All @@ -81,7 +81,7 @@ namespace boost { namespace dll { namespace detail {
#elif BOOST_OS_BSD_NET

namespace boost { namespace dll { namespace detail {
inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code &ec) {
inline boost::dll::fs::path program_location_impl(std::error_code &ec) {
return boost::dll::fs::read_symlink("/proc/curproc/exe", ec);
}
}}} // namespace boost::dll::detail
Expand All @@ -90,7 +90,7 @@ namespace boost { namespace dll { namespace detail {


namespace boost { namespace dll { namespace detail {
inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code &ec) {
inline boost::dll::fs::path program_location_impl(std::error_code &ec) {
return boost::dll::fs::read_symlink("/proc/curproc/file", ec);
}
}}} // namespace boost::dll::detail
Expand All @@ -100,16 +100,16 @@ namespace boost { namespace dll { namespace detail {
#include <fstream>
#include <string> // for std::getline
namespace boost { namespace dll { namespace detail {
inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code &ec) {
inline boost::dll::fs::path program_location_impl(std::error_code &ec) {
ec.clear();

std::string s;
std::ifstream ifs("/proc/self/exefile");
std::getline(ifs, s);

if (ifs.fail() || s.empty()) {
ec = boost::dll::fs::make_error_code(
boost::dll::fs::errc::bad_file_descriptor
ec = std::make_error_code(
std::errc::bad_file_descriptor
);
}

Expand All @@ -120,12 +120,15 @@ namespace boost { namespace dll { namespace detail {
#else // BOOST_OS_LINUX || BOOST_OS_UNIX || BOOST_OS_HPUX || BOOST_OS_ANDROID

namespace boost { namespace dll { namespace detail {
inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code &ec) {
inline boost::dll::fs::path program_location_impl(std::error_code &ec) {
// We can not use
// boost::dll::detail::path_from_handle(dlopen(NULL, RTLD_LAZY | RTLD_LOCAL), ignore);
// because such code returns empty path.

return boost::dll::fs::read_symlink("/proc/self/exe", ec); // Linux specific
boost::dll::fs::error_code fs_errc;
auto result = boost::dll::fs::read_symlink("/proc/self/exe", fs_errc); // Linux specific
ec = fs_errc;
return result;
}
}}} // namespace boost::dll::detail

Expand Down
26 changes: 13 additions & 13 deletions include/boost/dll/detail/posix/shared_library_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ class shared_library_impl {
return actual_path;
}

void load(boost::dll::fs::path sl, load_mode::type portable_mode, boost::dll::fs::error_code &ec) {
void load(boost::dll::fs::path sl, load_mode::type portable_mode, std::error_code &ec) {
typedef int native_mode_t;
native_mode_t native_mode = static_cast<native_mode_t>(portable_mode);
unload();

// Do not allow opening NULL paths. User must use program_location() instead
if (sl.empty()) {
boost::dll::detail::reset_dlerror();
ec = boost::dll::fs::make_error_code(
boost::dll::fs::errc::bad_file_descriptor
ec = std::make_error_code(
std::errc::bad_file_descriptor
);

return;
Expand Down Expand Up @@ -122,8 +122,8 @@ class shared_library_impl {
boost::dll::fs::path loc = boost::dll::detail::program_location_impl(prog_loc_err);
if (boost::dll::fs::exists(actual_path) && !boost::dll::fs::equivalent(sl, loc, prog_loc_err)) {
// decorated path exists : current error is not a bad file descriptor and we are not trying to load the executable itself
ec = boost::dll::fs::make_error_code(
boost::dll::fs::errc::executable_format_error
ec = std::make_error_code(
std::errc::executable_format_error
);
return;
}
Expand All @@ -136,8 +136,8 @@ class shared_library_impl {
return;
}

ec = boost::dll::fs::make_error_code(
boost::dll::fs::errc::bad_file_descriptor
ec = std::make_error_code(
std::errc::bad_file_descriptor
);

// Maybe user wanted to load the executable itself? Checking...
Expand All @@ -154,8 +154,8 @@ class shared_library_impl {
boost::dll::detail::reset_dlerror();
handle_ = dlopen(NULL, native_mode);
if (!handle_) {
ec = boost::dll::fs::make_error_code(
boost::dll::fs::errc::bad_file_descriptor
ec = std::make_error_code(
std::errc::bad_file_descriptor
);
}
}
Expand All @@ -178,7 +178,7 @@ class shared_library_impl {
boost::core::invoke_swap(handle_, rhs.handle_);
}

boost::dll::fs::path full_module_path(boost::dll::fs::error_code &ec) const {
boost::dll::fs::path full_module_path(std::error_code &ec) const {
return boost::dll::detail::path_from_handle(handle_, ec);
}

Expand All @@ -191,12 +191,12 @@ class shared_library_impl {
#endif
}

void* symbol_addr(const char* sb, boost::dll::fs::error_code &ec) const noexcept {
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) {
ec = boost::dll::fs::make_error_code(
boost::dll::fs::errc::invalid_seek
ec = std::make_error_code(
std::errc::invalid_seek
);
}

Expand Down
2 changes: 1 addition & 1 deletion include/boost/dll/detail/system_error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace boost { namespace dll { namespace detail {
#endif
}

inline void report_error(const boost::dll::fs::error_code& ec, const char* message) {
inline void report_error(const std::error_code& ec, const char* message) {
#if !BOOST_OS_WINDOWS
const char* const error_txt = dlerror();
if (error_txt) {
Expand Down
8 changes: 4 additions & 4 deletions include/boost/dll/detail/windows/path_from_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@

namespace boost { namespace dll { namespace detail {

inline boost::dll::fs::error_code last_error_code() BOOST_NOEXCEPT {
inline std::error_code last_error_code() BOOST_NOEXCEPT {
boost::winapi::DWORD_ err = boost::winapi::GetLastError();
return boost::dll::fs::error_code(
return std::error_code(
static_cast<int>(err),
boost::dll::fs::system_category()
std::system_category()
);
}

inline boost::dll::fs::path path_from_handle(boost::winapi::HMODULE_ handle, boost::dll::fs::error_code &ec) {
inline boost::dll::fs::path path_from_handle(boost::winapi::HMODULE_ handle, std::error_code &ec) {
BOOST_STATIC_CONSTANT(boost::winapi::DWORD_, ERROR_INSUFFICIENT_BUFFER_ = 0x7A);
BOOST_STATIC_CONSTANT(boost::winapi::DWORD_, DEFAULT_PATH_SIZE_ = 260);

Expand Down
12 changes: 6 additions & 6 deletions include/boost/dll/detail/windows/shared_library_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class shared_library_impl {
return actual_path;
}

void load(boost::dll::fs::path sl, load_mode::type portable_mode, boost::dll::fs::error_code &ec) {
void load(boost::dll::fs::path sl, load_mode::type portable_mode, std::error_code &ec) {
typedef boost::winapi::DWORD_ native_mode_t;
native_mode_t native_mode = static_cast<native_mode_t>(portable_mode);
unload();
Expand Down Expand Up @@ -125,21 +125,21 @@ class shared_library_impl {
boost::core::invoke_swap(handle_, rhs.handle_);
}

boost::dll::fs::path full_module_path(boost::dll::fs::error_code &ec) const {
boost::dll::fs::path full_module_path(std::error_code &ec) const {
return boost::dll::detail::path_from_handle(handle_, ec);
}

static boost::dll::fs::path suffix() {
return L".dll";
}

void* symbol_addr(const char* sb, boost::dll::fs::error_code &ec) const noexcept {
void* symbol_addr(const char* sb, std::error_code &ec) const noexcept {
if (is_resource()) {
// `GetProcAddress` could not be called for libraries loaded with
// `LOAD_LIBRARY_AS_DATAFILE`, `LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE`
// or `LOAD_LIBRARY_AS_IMAGE_RESOURCE`.
ec = boost::dll::fs::make_error_code(
boost::dll::fs::errc::operation_not_supported
ec = std::make_error_code(
std::errc::operation_not_supported
);

return NULL;
Expand All @@ -164,7 +164,7 @@ class shared_library_impl {

private:
// Returns true if this load attempt should be the last one.
bool load_impl(const boost::dll::fs::path &load_path, boost::winapi::DWORD_ mode, boost::dll::fs::error_code &ec) {
bool load_impl(const boost::dll::fs::path &load_path, boost::winapi::DWORD_ mode, std::error_code &ec) {
handle_ = boost::winapi::LoadLibraryExW(load_path.c_str(), 0, mode);
if (handle_) {
return true;
Expand Down
12 changes: 4 additions & 8 deletions include/boost/dll/import_class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,8 @@ inline std::unique_ptr<T, detail::deleter<T>> imported_class<T>::make_data(const

if (!ctor.has_allocating() || !dtor.has_deleting())
{
boost::dll::fs::error_code ec;

ec = boost::dll::fs::make_error_code(
boost::dll::fs::errc::bad_file_descriptor
std::error_code ec = std::make_error_code(
std::errc::bad_file_descriptor
);

// report_error() calls dlsym, do not use it here!
Expand All @@ -289,10 +287,8 @@ inline std::unique_ptr<T, detail::deleter<T>> imported_class<T>::make_data(const

if (!ctor.has_standard() || !dtor.has_standard())
{
boost::dll::fs::error_code ec;

ec = boost::dll::fs::make_error_code(
boost::dll::fs::errc::bad_file_descriptor
std::error_code ec = std::make_error_code(
std::errc::bad_file_descriptor
);

// report_error() calls dlsym, do not use it here!
Expand Down
Loading

0 comments on commit c66dfab

Please sign in to comment.