Pyrogenesis trunk
DllLoader.h
Go to the documentation of this file.
1/* Copyright (C) 2023 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_DLLLOADER
19#define INCLUDED_DLLLOADER
20
21#include "ps/Errors.h"
22#include "ps/CLogger.h"
23#include "ps/CStr.h"
24
26ERROR_TYPE(DllLoader, DllNotLoaded);
27ERROR_TYPE(DllLoader, SymbolNotFound);
28
30{
31public:
32 /**
33 * Prepare the DLL loader. Does no actual work.
34 *
35 * @param name base name of the library (from which we'll derive
36 * "name.dll", "libname_dbg.so", etc). Pointer must remain valid for
37 * this object's lifetime (which is fine if you just use a string literal).
38 * @param loadErrorLogMethod Allows to set the CLogger log level that is
39 * used when the DllLoader reports loading errors.
40 */
41 DllLoader(const char* name, CLogger::ELogMethod loadErrorLogMethod = CLogger::Error);
42
43 ~DllLoader();
44
45 /**
46 * Attempt to load and initialise the library, if not already. Can be harmlessly
47 * called multiple times. Returns false if unsuccessful.
48 */
49 bool LoadDLL();
50
51 /**
52 * Check whether the library has been loaded successfully. Returns false
53 * before {@link #LoadDLL} has been called; otherwise returns the same as
54 * LoadDLL did.
55 */
56 bool IsLoaded() const;
57
58 /**
59 * Unload the library, if it has been loaded already. (Usually not needed,
60 * since the destructor will unload it.)
61 */
62 void Unload();
63
64 /**
65 * Attempt to load a named symbol from the library. If {@link #IsLoaded} is
66 * false, throws PSERROR_DllLoader_DllNotLoaded. If it cannot load the
67 * symbol, throws PSERROR_DllLoader_SymbolNotFound. In both cases, sets fptr
68 * to NULL. Otherwise, fptr is set to point to the loaded function.
69 *
70 * @throws PSERROR_DllLoader
71 */
72 template <typename T>
73 void LoadSymbol(const char* name, T& fptr) const;
74
75 /**
76 * Override the build-time setting of the directory to search for libraries.
77 */
78 static void OverrideLibdir(const char* libdir);
79
80 static CStr GenerateFilename(const CStr& name, const CStr& suffix, const CStr& extension);
81
82private:
83 // Typeless version - the public LoadSymbol hides the slightly ugly
84 // casting from users.
85 void LoadSymbolInternal(const char* name, void** fptr) const;
86
87 void LogLoadError(const char* errors);
88
89 const char* m_Name;
90 void* m_Handle;
92};
93
94template <typename T>
95void DllLoader::LoadSymbol(const char* name, T& fptr) const
96{
97 LoadSymbolInternal(name, (void**)&fptr);
98}
99
100#endif // INCLUDED_DLLLOADER
ERROR_TYPE(DllLoader, DllNotLoaded)
ERROR_GROUP(DllLoader)
ELogMethod
Definition: CLogger.h:54
@ Error
Definition: CLogger.h:56
Definition: DllLoader.h:30
void LogLoadError(const char *errors)
Definition: DllLoader.cpp:193
void Unload()
Unload the library, if it has been loaded already.
Definition: DllLoader.cpp:170
void * m_Handle
Definition: DllLoader.h:90
const char * m_Name
Definition: DllLoader.h:89
DllLoader(const char *name, CLogger::ELogMethod loadErrorLogMethod=CLogger::Error)
Prepare the DLL loader.
Definition: DllLoader.cpp:134
bool LoadDLL()
Attempt to load and initialise the library, if not already.
Definition: DllLoader.cpp:150
void LoadSymbolInternal(const char *name, void **fptr) const
Definition: DllLoader.cpp:179
~DllLoader()
Definition: DllLoader.cpp:139
static CStr GenerateFilename(const CStr &name, const CStr &suffix, const CStr &extension)
Definition: DllLoader.cpp:71
bool IsLoaded() const
Check whether the library has been loaded successfully.
Definition: DllLoader.cpp:145
static void OverrideLibdir(const char *libdir)
Override the build-time setting of the directory to search for libraries.
Definition: DllLoader.cpp:209
void LoadSymbol(const char *name, T &fptr) const
Attempt to load a named symbol from the library.
Definition: DllLoader.h:95
CLogger::ELogMethod m_LoadErrorLogMethod
Definition: DllLoader.h:91
const char * extension
Definition: mongoose.cpp:1741
#define T(string_literal)
Definition: secure_crt.cpp:77