OpenTTD Source  20241108-master-g80f628063a
library_loader_unix.cpp
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 <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "../../stdafx.h"
11 
12 #include <dlfcn.h>
13 
14 #include "../../library_loader.h"
15 
16 #include "../../safeguards.h"
17 
18 /* Emscripten cannot dynamically load other files. */
19 #if defined(__EMSCRIPTEN__)
20 
21 void *LibraryLoader::OpenLibrary(const std::string &)
22 {
23  this->error = "Dynamic loading is not supported on this platform.";
24  return nullptr;
25 }
26 
28 {
29 }
30 
31 void *LibraryLoader::GetSymbol(const std::string &)
32 {
33  this->error = "Dynamic loading is not supported on this platform.";
34  return nullptr;
35 }
36 
37 #else
38 
39 void *LibraryLoader::OpenLibrary(const std::string &filename)
40 {
41  void *h = dlopen(filename.c_str(), RTLD_NOW | RTLD_LOCAL);
42  if (h == nullptr) {
43  this->error = dlerror();
44  }
45 
46  return h;
47 }
48 
50 {
51  dlclose(this->handle);
52 }
53 
54 void *LibraryLoader::GetSymbol(const std::string &symbol_name)
55 {
56  void *p = dlsym(this->handle, symbol_name.c_str());
57  if (p == nullptr) {
58  this->error = dlerror();
59  }
60 
61  return p;
62 }
63 
64 #endif /* __EMSCRIPTEN__ */
std::optional< std::string > error
The last error that occurred, if set.
void * OpenLibrary(const std::string &filename)
Open the library with the given filename.
void * handle
Handle to the library.
void * GetSymbol(const std::string &symbol_name)
Get a symbol from the library.
void CloseLibrary()
Close the library.