Add library loading helpers.
This commit is contained in:
parent
d01a5c1661
commit
aa37e7a868
@ -28,7 +28,9 @@ SET(CORE_SRCS
|
||||
Radix.cpp
|
||||
CpuInfo.h
|
||||
CpuInfo.cpp
|
||||
Algorithms.h)
|
||||
Algorithms.h
|
||||
Library.h
|
||||
Library.cpp)
|
||||
|
||||
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
|
41
src/nvcore/Library.cpp
Normal file
41
src/nvcore/Library.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
|
||||
#include "Library.h"
|
||||
#include "Debug.h"
|
||||
|
||||
#if NV_OS_WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define VC_EXTRALEAN
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <dlfcn.h>
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
void * nvLoadLibrary(const char * name)
|
||||
{
|
||||
#if NV_OS_WIN32
|
||||
return LoadLibraryExA( name, NULL, 0 );
|
||||
#else
|
||||
return dlopen(name, RTLD_LAZY);
|
||||
#endif
|
||||
}
|
||||
|
||||
void nvUnloadLibrary(void * handle)
|
||||
{
|
||||
nvDebugCheck(handle != NULL);
|
||||
#if NV_OS_WIN32
|
||||
FreeLibrary(handle);
|
||||
#else
|
||||
dlclose(handle);
|
||||
#endif
|
||||
}
|
||||
|
||||
void * nvBindSymbol(void * handle, const char * symbol)
|
||||
{
|
||||
#if NV_OS_WIN32
|
||||
return (void *)GetProcAddressA(handle, symbol);
|
||||
#else
|
||||
return (void *)dlsym(handle, symbol);
|
||||
#endif
|
||||
}
|
50
src/nvcore/Library.h
Normal file
50
src/nvcore/Library.h
Normal file
@ -0,0 +1,50 @@
|
||||
// This code is in the public domain -- castano@gmail.com
|
||||
|
||||
#ifndef NV_CORE_LIBRARY_H
|
||||
#define NV_CORE_LIBRARY_H
|
||||
|
||||
#include <nvcore/nvcore.h>
|
||||
|
||||
#if NV_OS_WIN32
|
||||
#define LIBRARY_NAME(name) #name ".dll"
|
||||
#elif NV_OS_DARWIN
|
||||
#define NV_LIBRARY_NAME(name) "lib" #name ".dylib"
|
||||
#else
|
||||
#define NV_LIBRARY_NAME(name) "lib" #name ".so"
|
||||
#endif
|
||||
|
||||
NVCORE_API void * nvLoadLibrary(const char * name);
|
||||
NVCORE_API void nvUnloadLibrary(void * lib);
|
||||
NVCORE_API void * nvBindSymbol(void * lib, const char * symbol);
|
||||
|
||||
class NVCORE_CLASS Library
|
||||
{
|
||||
public:
|
||||
Library(const char * name)
|
||||
{
|
||||
handle = nvLoadLibrary(name);
|
||||
}
|
||||
~Library()
|
||||
{
|
||||
if (isValid())
|
||||
{
|
||||
nvUnloadLibrary(handle);
|
||||
}
|
||||
}
|
||||
|
||||
bool isValid() const
|
||||
{
|
||||
return handle != NULL;
|
||||
}
|
||||
|
||||
void * bindSymbol(const char * symbol)
|
||||
{
|
||||
return nvBindSymbol(handle, symbol);
|
||||
}
|
||||
|
||||
private:
|
||||
void * handle;
|
||||
};
|
||||
|
||||
|
||||
#endif // NV_CORE_LIBRARY_H
|
Loading…
Reference in New Issue
Block a user