Pyrogenesis  trunk
DllLoader.h
Go to the documentation of this file.
1 /* Copyright (C) 2014 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 
25 ERROR_TYPE(DllLoader, DllNotLoaded);
26 ERROR_TYPE(DllLoader, SymbolNotFound);
27 
28 class DllLoader
29 {
30 public:
31  /**
32  * Prepare the DLL loader. Does no actual work.
33  *
34  * @param name base name of the library (from which we'll derive
35  * "name.dll", "libname_dbg.so", etc). Pointer must remain valid for
36  * this object's lifetime (which is fine if you just use a string literal).
37  * @param loadErrorLogMethod Allows to set the CLogger log level that is
38  * used when the DllLoader reports loading errors.
39  */
40  DllLoader(const char* name, CLogger::ELogMethod loadErrorLogMethod = CLogger::Error);
41 
42  ~DllLoader();
43 
44  /**
45  * Attempt to load and initialise the library, if not already. Can be harmlessly
46  * called multiple times. Returns false if unsuccessful.
47  */
48  bool LoadDLL();
49 
50  /**
51  * Check whether the library has been loaded successfully. Returns false
52  * before {@link #LoadDLL} has been called; otherwise returns the same as
53  * LoadDLL did.
54  */
55  bool IsLoaded() const;
56 
57  /**
58  * Unload the library, if it has been loaded already. (Usually not needed,
59  * since the destructor will unload it.)
60  */
61  void Unload();
62 
63  /**
64  * Attempt to load a named symbol from the library. If {@link #IsLoaded} is
65  * false, throws PSERROR_DllLoader_DllNotLoaded. If it cannot load the
66  * symbol, throws PSERROR_DllLoader_SymbolNotFound. In both cases, sets fptr
67  * to NULL. Otherwise, fptr is set to point to the loaded function.
68  *
69  * @throws PSERROR_DllLoader
70  */
71  template <typename T>
72  void LoadSymbol(const char* name, T& fptr) const;
73 
74  /**
75  * Override the build-time setting of the directory to search for libraries.
76  */
77  static void OverrideLibdir(const char* libdir);
78 
79 private:
80  // Typeless version - the public LoadSymbol hides the slightly ugly
81  // casting from users.
82  void LoadSymbolInternal(const char* name, void** fptr) const;
83 
84  void LogLoadError(const char* errors);
85 
86  const char* m_Name;
87  void* m_Handle;
89 };
90 
91 template <typename T>
92 void DllLoader::LoadSymbol(const char* name, T& fptr) const
93 {
94  LoadSymbolInternal(name, (void**)&fptr);
95 }
96 
97 #endif // INCLUDED_DLLLOADER
DllLoader(const char *name, CLogger::ELogMethod loadErrorLogMethod=CLogger::Error)
Prepare the DLL loader.
Definition: DllLoader.cpp:135
void Unload()
Unload the library, if it has been loaded already.
Definition: DllLoader.cpp:171
ELogMethod
Definition: CLogger.h:50
const char * m_Name
Definition: DllLoader.h:86
ERROR_GROUP(DllLoader)
ERROR_TYPE(DllLoader, DllNotLoaded)
bool IsLoaded() const
Check whether the library has been loaded successfully.
Definition: DllLoader.cpp:146
Definition: CLogger.h:53
void * m_Handle
Definition: DllLoader.h:87
~DllLoader()
Definition: DllLoader.cpp:140
#define T(string_literal)
Definition: secure_crt.cpp:76
Definition: DllLoader.h:28
CLogger::ELogMethod m_LoadErrorLogMethod
Definition: DllLoader.h:88
bool LoadDLL()
Attempt to load and initialise the library, if not already.
Definition: DllLoader.cpp:151
void LoadSymbol(const char *name, T &fptr) const
Attempt to load a named symbol from the library.
Definition: DllLoader.h:92
static void OverrideLibdir(const char *libdir)
Override the build-time setting of the directory to search for libraries.
Definition: DllLoader.cpp:210
void LoadSymbolInternal(const char *name, void **fptr) const
Definition: DllLoader.cpp:180
void LogLoadError(const char *errors)
Definition: DllLoader.cpp:194