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