1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#pragma once
#include <string>
#include <vector>
#include <memory>
#if defined(WIN32)
const char* const MODULE_FILE_EXTENSION = ".dll";
#elif defined(POSIX)
const char* const MODULE_FILE_EXTENSION = ".so";
#endif
/** greebo: This file declares the classes encapsulating a dynamically linked library.
* Each DL class must define a FunctionPointer typedef and a findSymbol() method.
*
* The declaration is platform-specific, currently Win32 and POSIX are declared.
*/
/**
* =============================== WIN32 ======================================
*/
#if defined(WIN32)
#define NOMINMAX
#include <windows.h>
namespace module
{
/** greebo: WIN32 DynamicLibrary. Loads a DLL given in the constructor.
*/
class DynamicLibrary
{
// The full filename of this library
std::wstring _name;
// The library handle
HMODULE _library;
public:
// The win32 function pointer typedef for calling symbols
typedef FARPROC FunctionPointer;
// Constructor, pass the full filename to load this DLL
DynamicLibrary(const std::string& filename);
~DynamicLibrary();
// Returns TRUE if the library load failed. (TODO: greebo: Remove this in favour of exceptions?)
bool failed();
/** greebo: The lookup method for symbols. Returns a platform-specific function
* pointer that can be used to call the symbol.
*/
FunctionPointer findSymbol(const std::string& symbol);
// Returns the filename of this module
std::string getName() const;
};
} // namespace module
/**
* =============================== POSIX ======================================
*/
#elif defined(POSIX)
#include <dlfcn.h>
namespace module
{
class DynamicLibrary
{
// The full filename of this library
std::string _name;
// The handle for accessing the dynamic library
void* _dlHandle;
public:
// The posix function pointer typedef for calling symbols
typedef int (* FunctionPointer)();
// Constructor, pass the full filename to load this DLL
DynamicLibrary(const std::string& filename);
~DynamicLibrary();
// Returns TRUE if the library load failed. (TODO: greebo: Remove this in favour of exceptions?)
bool failed();
/** greebo: The lookup method for symbols. Returns a platform-specific function
* pointer that can be used to call the symbol.
*/
FunctionPointer findSymbol(const std::string& symbol);
// Returns the filename of this module
std::string getName() const;
};
} // namespace module
#else
#error "unsupported platform"
#endif
namespace module
{
// Shared ptr typedef
typedef std::shared_ptr<DynamicLibrary> DynamicLibraryPtr;
// A list of allocated libraries
typedef std::vector<DynamicLibraryPtr> DynamicLibraryList;
}
|