Skip to content

Commit

Permalink
Add platform code for testing C API
Browse files Browse the repository at this point in the history
Adds platform code to load DLL/so/dylib in the C API test.
  • Loading branch information
sleweke committed Feb 17, 2023
1 parent 00d49c4 commit 5f20306
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 27 deletions.
7 changes: 2 additions & 5 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,8 @@ endforeach()

# ---------------------------------------------------


if(WIN32)
add_executable(testCAPIv1 "${CMAKE_CURRENT_BINARY_DIR}/Paths_$<CONFIG>.cpp" testCAPIv1.cpp)
target_include_directories(testCAPIv1 PRIVATE ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/ThirdParty/json)
endif()
add_executable(testCAPIv1 "${CMAKE_CURRENT_BINARY_DIR}/Paths_$<CONFIG>.cpp" testCAPIv1.cpp)
target_include_directories(testCAPIv1 PRIVATE ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/ThirdParty/json)

# ---------------------------------------------------

Expand Down
96 changes: 74 additions & 22 deletions test/testCAPIv1.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@

#ifdef _WIN32
// Windows (x64 and x86)

#define NOMINMAX
#define _WINDOWS
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#elif __unix__ || __linux__ || __APPLE__
// Linux and MacOS

#include <dlfcn.h>
#endif

#include <stdexcept>
#include <stdint.h>
#include <windows.h>
#include <iostream>
#include <memory>
#include <functional>
Expand All @@ -25,36 +40,73 @@ void logHandler(const char* file, const char* func, const unsigned int line, int
std::cout << lvlStr << " [" << func << ":" << line << "] " << message << std::endl;
}

class LibLoader
{
public:
LibLoader(char const* path) : _hdLib(0)
{
_hdLib = LoadLibrary(path);
if (_hdLib)
std::cout << "Loaded library " << path << std::endl;
}
#ifdef _WIN32

~LibLoader()
class LibLoader
{
if (_hdLib)
public:
LibLoader(char const* path) : _hdLib(0)
{
FreeLibrary(_hdLib);
std::cout << "Deleted library" << std::endl;
_hdLib = LoadLibrary(path);
if (_hdLib)
std::cout << "Loaded library " << path << std::endl;
}

~LibLoader()
{
if (_hdLib)
{
FreeLibrary(_hdLib);
std::cout << "Deleted library" << std::endl;
}
}

bool isValid() { return _hdLib; }

template <typename T> T load(char const* func)
{
return reinterpret_cast<T>(GetProcAddress(_hdLib, func));
}
}

bool isValid() { return _hdLib; }
private:
HINSTANCE _hdLib;
};

#elif __unix__ || __linux__ || __APPLE__

template <typename T> T load(char const* func)
class LibLoader
{
return reinterpret_cast<T>(GetProcAddress(_hdLib, func));
}
public:
LibLoader(char const* path) : _hdLib(0)
{
_hdLib = dlopen(path, RTLD_LAZY);
if (_hdLib)
std::cout << "Loaded library " << path << std::endl;
}

private:
HINSTANCE _hdLib;
};
~LibLoader()
{
if (_hdLib)
{
if (dlclose(_hdLib) != 0)
std::cout << "Failed to delete library" << std::endl;
else
std::cout << "Deleted library" << std::endl;
}
}

bool isValid() { return _hdLib; }

template <typename T> T load(char const* func)
{
return reinterpret_cast<T>(dlsym(_hdLib, func));
}

private:
void* _hdLib;
};

#endif

json createColumnWithSMAJson(const std::string& uoType)
{
Expand Down

0 comments on commit 5f20306

Please sign in to comment.