OGLplus  (0.59.0) a C++ wrapper for rendering APIs

dynamic_library.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_DYNAMIC_LIBRARY_HPP
10 #define EAGINE_DYNAMIC_LIBRARY_HPP
11 
12 #include "callable_ref.hpp"
13 #include "config/platform.hpp"
14 #include "nothing.hpp"
15 #include "string_span.hpp"
16 #include <dlfcn.h>
17 #include <string>
18 #include <type_traits>
19 
20 namespace eagine {
21 
27 public:
29  executable_module() noexcept = default;
30 
32  executable_module(const executable_module&) = delete;
33 
36 
38  auto operator=(const executable_module&) = delete;
39 
41  auto operator=(executable_module&&) = delete;
42 
44  using handle_type = void*;
45 
46  executable_module(string_view filename, int flags)
47  : _handle(::dlopen(c_str(filename), flags)) {
48  if(!_handle) {
49  _message.assign(::dlerror());
50  }
51  }
52 
53  executable_module(nothing_t, int flags)
54  : _handle(::dlopen(nullptr, flags)) {
55  if(!_handle) {
56  _message.assign(::dlerror());
57  }
58  }
59 
61  ~executable_module() noexcept {
62  if(_handle) {
63  ::dlclose(_handle);
64  }
65  }
66 
69  auto is_open() const noexcept -> bool {
70  return bool(_handle);
71  }
72 
77  ::dlerror();
78  void* result = ::dlsym(_handle, c_str(name));
79  if(auto error = ::dlerror()) {
80  _message.assign(error);
81  return {};
82  }
83  return {result, true};
84  }
85 
87  auto error_message() const noexcept -> string_view {
88  return {_message};
89  }
90 
91 private:
92  handle_type _handle{nullptr};
93  std::string _message{};
94 };
95 
99 public:
102  shared_executable_module() noexcept = default;
103 
107  : _module{_do_open(nothing)} {}
108 
112  : _module{_do_open(filename)} {}
113 
117  auto open_self() -> auto& {
118  _module = _do_open(nothing);
119  return *this;
120  }
121 
125  auto open(string_view filename) -> auto& {
126  _module = _do_open(filename);
127  return *this;
128  }
129 
133  auto is_open() const noexcept -> bool {
134  return bool(_module) && _module->is_open();
135  }
136 
139  explicit operator bool() const noexcept {
140  return is_open();
141  }
142 
144  auto error_message() const noexcept -> string_view {
145  return bool(_module) ? _module->error_message() : string_view();
146  }
147 
151  auto exports(string_view name) const noexcept -> bool {
152  if(is_open()) {
153  if(auto found{_module->find_symbol(name)}) {
154  return true;
155  }
156  }
157  return false;
158  }
159 
163  template <typename Signature>
164  auto find(string_view name) const noexcept -> std::enable_if_t<
165  std::is_function_v<std::remove_pointer_t<Signature>>,
167  if(is_open()) {
168  if(auto found{_module->find_symbol(name)}) {
169  return {
170  reinterpret_cast<std::remove_pointer_t<Signature>*>(
171  extract(found)),
172  true};
173  }
174  }
175  return {};
176  }
177 
178 private:
179  std::shared_ptr<executable_module> _module{};
180 
181  static auto _do_open(string_view filename)
182  -> std::shared_ptr<executable_module> {
183  return std::make_shared<executable_module>(filename, RTLD_LAZY);
184  }
185 
186  static auto _do_open(nothing_t) -> std::shared_ptr<executable_module> {
187  return std::make_shared<executable_module>(nothing, RTLD_LAZY);
188  }
189 };
190 
191 } // namespace eagine
192 
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

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).