Go to the documentation of this file.
9 #ifndef EAGINE_DYNAMIC_LIBRARY_HPP
10 #define EAGINE_DYNAMIC_LIBRARY_HPP
18 #include <type_traits>
47 : _handle(::dlopen(
c_str(filename), flags)) {
49 _message.assign(::dlerror());
54 : _handle(::dlopen(nullptr, flags)) {
56 _message.assign(::dlerror());
78 void* result = ::dlsym(_handle,
c_str(name));
79 if(
auto error = ::dlerror()) {
80 _message.assign(
error);
83 return {result,
true};
93 std::string _message{};
107 : _module{_do_open(
nothing)} {}
112 : _module{_do_open(filename)} {}
126 _module = _do_open(filename);
134 return bool(_module) && _module->is_open();
139 explicit operator bool() const noexcept {
145 return bool(_module) ? _module->error_message() :
string_view();
153 if(
auto found{_module->find_symbol(name)}) {
163 template <
typename Signature>
165 std::is_function_v<std::remove_pointer_t<Signature>>,
168 if(
auto found{_module->find_symbol(name)}) {
170 reinterpret_cast<std::remove_pointer_t<Signature>*
>(
179 std::shared_ptr<executable_module> _module{};
182 -> std::shared_ptr<executable_module> {
183 return std::make_shared<executable_module>(filename, RTLD_LAZY);
186 static auto _do_open(nothing_t) -> std::shared_ptr<executable_module> {
187 return std::make_shared<executable_module>(
nothing, RTLD_LAZY);
193 #endif // EAGINE_DYNAMIC_LIBRARY_HPP
shared_executable_module(string_view filename)
Construction which tries to open executable module with specified filename.
Definition: dynamic_library.hpp:111
auto error_message() const noexcept -> string_view
Returns the user-readable error message from the last failed operation.
Definition: dynamic_library.hpp:87
Declaration of class template storing a reference to a callable object.
Definition: callable_ref.hpp:24
basic_string_span< const char > string_view
Alias for const string views.
Definition: string_span.hpp:116
auto is_open() const noexcept -> bool
Indicates if this executable module is open.
Definition: dynamic_library.hpp:133
static constexpr auto c_str(memory::basic_span< C, P, S > s) -> std::enable_if_t< std::is_convertible_v< memory::basic_span< C, P, S >, basic_string_span< C, P, S >>, basic_c_str< C, P, S >>
Functions that construct a basic_c_str from a basic_string_span.
Definition: string_span.hpp:226
auto error_message() const noexcept -> string_view
Returns a human-readable error message from the last failed operation.
Definition: dynamic_library.hpp:144
auto is_open() const noexcept -> bool
Checks if the module is open.
Definition: dynamic_library.hpp:69
Common code is placed in this namespace.
Definition: eagine.hpp:21
auto open(string_view filename) -> auto &
Opens the specified executable module. Closes the previous one, if any.
Definition: dynamic_library.hpp:125
static constexpr auto extract(api_result_value< Result, api_result_validity::never > &) noexcept -> Result &
Overload of extract for api_result_value.
Definition: c_api_wrap.hpp:270
Class representing "none" / "nothing" values.
Definition: nothing.hpp:17
Primary template for conditionally valid values.
Definition: decl.hpp:49
shared_executable_module(nothing_t)
Constructor which opens the current executable as a module.
Definition: dynamic_library.hpp:106
auto find(string_view name) const noexcept -> std::enable_if_t< std::is_function_v< std::remove_pointer_t< Signature >>, callable_ref< std::remove_pointer_t< Signature >>>
Finds and returns pointer to exported function with the specified name.
Definition: dynamic_library.hpp:164
@ error
Error log entries, indicating serious problems.
shared_executable_module() noexcept=default
Default constructor.
Wrapper for a handle to dynamically linkable executable module.
Definition: dynamic_library.hpp:26
auto operator=(const executable_module &)=delete
Not move assignable.
Reference counting handle to dynamically linkable executable module.
Definition: dynamic_library.hpp:98
auto open_self() -> auto &
Opens the current executable module. Closes the previous one, if any.
Definition: dynamic_library.hpp:117
auto exports(string_view name) const noexcept -> bool
Tests if this executable module exports the specified symbol.
Definition: dynamic_library.hpp:151
void * handle_type
Alias for the handle type.
Definition: dynamic_library.hpp:44
executable_module() noexcept=default
Default constructor.
auto find_symbol(string_view name) -> optionally_valid< void * >
Returns a pointer to the exported symbol with the specifed name.
Definition: dynamic_library.hpp:76
~executable_module() noexcept
Closes the handle.
Definition: dynamic_library.hpp:61
static constexpr nothing_t nothing
Constant of nothing_t type.
Definition: nothing.hpp:30