OpenTTD Source: src/library_loader.h Source File

OpenTTD Source 15.0-RC2

library_loader.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef LIBRARY_LOADER_H
11#define LIBRARY_LOADER_H
12
13 class LibraryLoader {
14public:
20 class Function {
21 public:
22 explicit Function(void *p) : p(p) {}
23
24 template <typename T, typename = std::enable_if_t<std::is_function_v<T>>>
25 operator T *() const
26 {
27 return reinterpret_cast<T *>(this->p);
28 }
29
30 private:
31 void *p;
32 };
33
37 explicit LibraryLoader(const std::string &filename)
38 {
39 this->handle = this->OpenLibrary(filename);
40 }
41
45 ~LibraryLoader()
46 {
47 if (this->handle != nullptr) {
48 this->CloseLibrary();
49 }
50 }
51
57 bool HasError()
58 {
59 return this->error.has_value();
60 }
61
67 std::string GetLastError()
68 {
69 return this->error.value_or("No error");
70 }
71
78 Function GetFunction(const std::string &symbol_name)
79 {
80 if (this->error.has_value()) return Function(nullptr);
81 return Function(this->GetSymbol(symbol_name));
82 }
83
84private:
92 void *OpenLibrary(const std::string &filename);
93
97 void CloseLibrary();
98
106 void *GetSymbol(const std::string &symbol_name);
107
108 std::optional<std::string> error = {};
109 void *handle = nullptr;
110};
111
112#endif /* LIBRARY_LOADER_H */
LibraryLoader::Function
A function loaded from a library.
Definition library_loader.h:20
LibraryLoader
Definition library_loader.h:13
LibraryLoader::GetLastError
std::string GetLastError()
Get the last error that occurred while loading the library or a function.
Definition library_loader.h:67
LibraryLoader::HasError
bool HasError()
Check whether an error occurred while loading the library or a function.
Definition library_loader.h:57
LibraryLoader::error
std::optional< std::string > error
The last error that occurred, if set.
Definition library_loader.h:108
LibraryLoader::GetFunction
Function GetFunction(const std::string &symbol_name)
Get a function from a loaded library.
Definition library_loader.h:78
LibraryLoader::OpenLibrary
void * OpenLibrary(const std::string &filename)
Open the library with the given filename.
Definition library_loader_unix.cpp:39
LibraryLoader::handle
void * handle
Handle to the library.
Definition library_loader.h:109
LibraryLoader::~LibraryLoader
~LibraryLoader()
Close the library.
Definition library_loader.h:45
LibraryLoader::LibraryLoader
LibraryLoader(const std::string &filename)
Load a library with the given filename.
Definition library_loader.h:37
LibraryLoader::GetSymbol
void * GetSymbol(const std::string &symbol_name)
Get a symbol from the library.
Definition library_loader_unix.cpp:54
LibraryLoader::CloseLibrary
void CloseLibrary()
Close the library.
Definition library_loader_unix.cpp:49