Fix unintented newline in rP20809.
[0ad.git] / source / ps / DllLoader.h
blob2f360f102a8641c1602da3a9659ba70d9fc77e36
1 /* Copyright (C) 2014 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef INCLUDED_DLLLOADER
19 #define INCLUDED_DLLLOADER
21 #include "ps/Errors.h"
22 #include "ps/CLogger.h"
24 ERROR_GROUP(DllLoader);
25 ERROR_TYPE(DllLoader, DllNotLoaded);
26 ERROR_TYPE(DllLoader, SymbolNotFound);
28 class DllLoader
30 public:
31 /**
32 * Prepare the DLL loader. Does no actual work.
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.
40 DllLoader(const char* name, CLogger::ELogMethod loadErrorLogMethod = CLogger::Error);
42 ~DllLoader();
44 /**
45 * Attempt to load and initialise the library, if not already. Can be harmlessly
46 * called multiple times. Returns false if unsuccessful.
48 bool LoadDLL();
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.
55 bool IsLoaded() const;
57 /**
58 * Unload the library, if it has been loaded already. (Usually not needed,
59 * since the destructor will unload it.)
61 void Unload();
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.
69 * @throws PSERROR_DllLoader
71 template <typename T>
72 void LoadSymbol(const char* name, T& fptr) const;
74 /**
75 * Override the build-time setting of the directory to search for libraries.
77 static void OverrideLibdir(const char* libdir);
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;
84 void LogLoadError(const char* errors);
86 const char* m_Name;
87 void* m_Handle;
88 CLogger::ELogMethod m_LoadErrorLogMethod;
91 template <typename T>
92 void DllLoader::LoadSymbol(const char* name, T& fptr) const
94 LoadSymbolInternal(name, (void**)&fptr);
97 #endif // INCLUDED_DLLLOADER