This GitLab instance is undergoing maintenance and is operating in read-only mode.

You are on a read-only GitLab instance.
Commit 055f8221 authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

#8. Updating dynamic library interface to allow for lib name without prefix...

#8. Updating dynamic library interface to allow for lib name without prefix (platform specific) or suffix (platform specific).
parent 772cb29c
Pipeline #10165 failed with stage
in 48 seconds
......@@ -8,6 +8,7 @@ library.cc
SET(HEADERS
function.hh
function.i.hh
library.hh
)
......
#include "radixbug/bug.hh"
#include "radixdl/library.hh"
namespace radix {
#include <fstream>
DynamicLibrary::DynamicLibrary(const std::string& filename) {
radix_tagged_line("Starting DynamicLibrary(" << filename << ")");
mFileName = filename;
namespace radix
{
const std::vector<std::string> DynamicLibrary::m_lib_prefixes = { "", "lib"};
const std::vector<std::string> DynamicLibrary::m_lib_suffixes = { ".so", ".dll", ".dylib", ""};
DynamicLibrary::DynamicLibrary()
{
radix_tagged_line("Starting DynamicLibrary()");
mFileName = "";
mHandle = nullptr;
// add current directory to default directory search
mDirs.push_back(".");
}
DynamicLibrary::~DynamicLibrary() {
DynamicLibrary::~DynamicLibrary()
{
radix_tagged_line("Destructing/Closing DynamicLibrary('" << mFileName << "')");
close();
}
bool DynamicLibrary::is_open() const {
void DynamicLibrary::set_lib_name(const std::string& name)
{
close();
mFileName = name;
}
bool DynamicLibrary::is_open() const
{
return (mHandle != nullptr);
}
bool DynamicLibrary::symbol_exists(const std::string& sym) {
radix_tagged_line("DynamicLibrary symbol_exists(" << sym << ")");
if (mHandle == nullptr) {
bool DynamicLibrary::function_exists(const std::string& func)
{
radix_tagged_line("DynamicLibrary function_exists(" << func << ")");
if (mHandle == nullptr)
{
radix_tagged_line("\tno library handle.");
return false;
}
#ifdef _WIN32
return (GetProcAddress(mHandle, sym.c_str()) != nullptr);
return (GetProcAddress(mHandle, func.c_str()) != nullptr);
#else
return (dlsym(mHandle, sym.c_str()) != nullptr);
return (dlsym(mHandle, func.c_str()) != nullptr);
#endif
}
bool DynamicLibrary::open() {
bool DynamicLibrary::open()
{
radix_tagged_line("Opening DynamicLibrary('" << mFileName << "')");
if (mHandle == nullptr) {
if (mHandle != nullptr)
{
return is_open();
}
// search for the existance of library name given available prefixes/suffixes
bool foundLibrary = false;
std::string absolutePath;
for(auto dir : mDirs)
{
for(auto prefix : m_lib_prefixes)
{
for(auto suffix : m_lib_suffixes)
{
std::stringstream ss;
ss << dir << "/" << prefix << mFileName << suffix;
std::string fullPath = ss.str();
radix_tagged_line("Checking library path (" << fullPath << ")");
{
std::ifstream lib(fullPath.c_str());
foundLibrary = lib.good();
}
if(foundLibrary)
{
absolutePath = fullPath;
radix_tagged_line("Found library(" << absolutePath << ")");
break;
}
}
}
}
#ifdef _WIN32
mHandle = LoadLibrary(mFileName.c_str());
mHandle = LoadLibrary(absolutePath.c_str());
#else
mHandle = dlopen(mFileName.c_str(), RTLD_NOW);
mHandle = dlopen(absolutePath.c_str(), RTLD_NOW);
#endif
}
return is_open();
}
bool DynamicLibrary::close() {
if (mHandle != nullptr) {
bool DynamicLibrary::close()
{
if (mHandle != nullptr)
{
bool result = false;
#ifdef _WIN32
return FreeLibrary(mHandle);
result = FreeLibrary(mHandle);
#else
return (dlclose(mHandle) == 0);
result = (dlclose(mHandle) == 0);
#endif
if(result)
{
mHandle = nullptr;
}
return result;
}
return true;
}
......
......@@ -8,6 +8,7 @@
#endif
#include <string>
#include <vector>
#include "radixdl/function.hh"
......@@ -15,20 +16,25 @@ namespace radix {
class DynamicLibrary {
public:
DynamicLibrary(const std::string&);
DynamicLibrary();
virtual ~DynamicLibrary();
bool is_open() const;
bool symbol_exists(const std::string&);
bool function_exists(const std::string&);
bool open();
bool close();
template <typename return_type = void, typename ...ARGS>
DLFunction<return_type, ARGS...> symbol(const std::string&);
DLFunction<return_type, ARGS...> function(const std::string&);
void set_lib_name(const std::string&);
private:
std::vector<std::string> mDirs;
std::string mFileName;
static const std::vector<std::string> m_lib_prefixes;
static const std::vector<std::string> m_lib_suffixes;
#ifdef _WIN32
HINSTANCE mHandle;
......@@ -38,17 +44,17 @@ private:
}; // class DLibrary
template<typename return_type, typename ... ARGS>
DLFunction<return_type, ARGS...> DynamicLibrary::symbol(
const std::string& sym) {
DLFunction<return_type, ARGS...> DynamicLibrary::function(
const std::string& func) {
if (mHandle == NULL) {
return DLFunction<return_type, ARGS...>();
}
#ifdef _WIN32
return DLFunction<return_type, ARGS...>(
GetProcAddress(mHandle, sym.c_str()));
GetProcAddress(mHandle, func.c_str()));
#else
return DLFunction<return_type, ARGS...>(
dlsym(mHandle, sym.c_str()));
dlsym(mHandle, func.c_str()));
#endif
}
......
......@@ -3,10 +3,9 @@
namespace radix
{
extern "C" {
int RADIX_PUBLIC fake_global_function(int a)
extern "C" int RADIX_PUBLIC fake_global_function(int a)
{
return a * 8;
}
}
} // namespace
......@@ -8,14 +8,15 @@
using namespace radix;
int main(int argc, char** argv)
{
DynamicLibrary library("libfakelib.so");
DynamicLibrary library;
library.set_lib_name("fakelib");
bool openResult = library.open();
std::cout << "Open result: "<< std::boolalpha << openResult << std::endl;
bool funcExists = library.symbol_exists("fake_global_function");
bool funcExists = library.function_exists("fake_global_function");
std::cout << "Function('fake_global_function') exists: "
<< std::boolalpha << funcExists << std::endl;
DLFunction<int, int> func = library.symbol<int,int>("fake_global_function");
DLFunction<int, int> func = library.function<int,int>("fake_global_function");
int funcResult = func(2);
std::cout << "Function('fake_global_function') correct result: "
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment