Skip to content

Commit

Permalink
Refactor and cleanup
Browse files Browse the repository at this point in the history
* Rename config.h to config.hpp
* Rename test/some to test/shared
* Clean up the CMake files
  • Loading branch information
maddouri committed Feb 18, 2018
1 parent a855d71 commit 125cb6c
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 38 deletions.
19 changes: 0 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
cmake_minimum_required(VERSION 3.5.0)

# # hunter gate https://github.com/hunter-packages/gate
# if (NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/HunterGate.cmake")
# file(DOWNLOAD
# "https://raw.githubusercontent.com/hunter-packages/gate/master/cmake/HunterGate.cmake"
# "${CMAKE_CURRENT_LIST_DIR}/HunterGate.cmake"
# )
# endif()
# include("${CMAKE_CURRENT_LIST_DIR}/HunterGate.cmake")
# # hunter https://github.com/ruslo/hunter/releases
# HunterGate(
# URL "https://github.com/ruslo/hunter/archive/v0.20.9.tar.gz"
# SHA1 "8f7cf6d719d05c49224a4ebeba90c6bb90b90f4a"
# )


project(dynalo)

#set(CMAKE_CXX_STANDARD 11 )
Expand All @@ -27,13 +12,10 @@ target_include_directories(${PROJECT_NAME} INTERFACE
"$<INSTALL_INTERFACE:include>" # <prefix>/include
)

#set_target_properties(${PROJECT_NAME} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "$<INSTALL_INTERFACE:include>")
if (UNIX)
target_link_libraries(${PROJECT_NAME} INTERFACE dl)
#set_target_properties(${PROJECT_NAME} PROPERTIES INTERFACE_LINK_LIBRARIES dl)
elseif(WIN32)
target_link_libraries(${PROJECT_NAME} INTERFACE kernel32)
#set_target_properties(${PROJECT_NAME} PROPERTIES INTERFACE_LINK_LIBRARIES kernel32)
endif()


Expand All @@ -59,7 +41,6 @@ export(
configure_file(
"cmake/Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}Config.cmake"
#COPYONLY
@ONLY
)
set(ConfigPackageLocation cmake/${PROJECT_NAME})
Expand Down
File renamed without changes.
51 changes: 50 additions & 1 deletion include/dynalo/dynalo.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
#pragma once

#include "detail/config.h"
#include "detail/config.hpp"

#if defined(DYNALO_HAS_LINUX)
#include "detail/linux/dynalo.hpp"
#elif defined(DYNALO_HAS_WINDOWS)
#include "detail/windows/dynalo.hpp"
#endif

/// DYNAmic LOading of shared libraries and access to their exported functions
namespace dynalo
{

namespace native
{

/// Native handle (usuall a pointer) on the loaded shared library
using handle = detail::native::handle;

/// @return An invalid library handle
inline
handle invalid_handle()
{
Expand All @@ -24,18 +28,21 @@ handle invalid_handle()
namespace name
{

/// @return The name prefix of a shared library on the current system
inline
std::string prefix()
{
return detail::native::name::prefix();
}

/// @return The name suffix of a shared library on the current system
inline
std::string suffix()
{
return detail::native::name::suffix();
}

/// @return The file extension of a shared library on the current system
inline
std::string extension()
{
Expand All @@ -46,6 +53,11 @@ std::string extension()

}

/// @param lib_name Name of the library without neither the lib prefix, suffix nor file extension
///
/// @return The native name of the shared library.
/// e.g. If @p lib_name is `awesome`,
/// then this function will return `libawesome.so` in Linux and `awesome.dll` in Windows
inline
std::string to_native_name(const std::string& lib_name)
{
Expand All @@ -60,25 +72,58 @@ std::string to_native_name(const std::string& lib_name)
}
}

/// Loads a shared library
///
/// @param dyn_lib_path Path to the shared library file to be loaded
///
/// @return The handle of the loaded shared library
///
/// @throw std::runtime_error If it fails to load the library
inline
native::handle open(const std::string& dyn_lib_path)
{
return detail::open(dyn_lib_path);
}

/// Unloads the shared library which handle is @p lib_handle
///
/// @param lib_handle The handle of the library to be unloaded
///
/// @throw std::runtime_error If it fails to unload the shared library
inline
void close(native::handle lib_handle)
{
detail::close(lib_handle);
}

/// Looks up a function in the shared library and returns pointer to it
///
/// @tparam FunctionSignature The signature of the function to be looked up.
/// i.e. `return_type(param_types...)`
/// e.g. `void(const char*)`, `bool(int, int)`
///
/// @param lib_handle The handle of the library that contains the function
/// @param func_name The name of the function to find
///
/// @return A pointer to the @p func_name function
///
/// @throw std::runtime_error If it fails to find the @p func_name function
template <typename FunctionSignature>
inline
FunctionSignature* get_function(native::handle lib_handle, const std::string& func_name)
{
return detail::get_function<FunctionSignature>(lib_handle, func_name);
}


/// A shared library
///
/// This class wraps the open/close/get_function functions of the dynalo namespace:
/// <ul>
/// <li>The shared library is loaded in the class' constructor</li>
/// <li>You can get pointer to a functio using library::get_function</li>
/// <li>The shared library is automatically unloaed in the destructor</li>
/// </ul>
class library
{
private:
Expand All @@ -102,6 +147,7 @@ class library
return *this;
}

/// Unloads the shared library using dynalo::close
~library()
{
if (m_handle != native::invalid_handle())
Expand All @@ -110,16 +156,19 @@ class library
}
}

/// Loads a shared library using dynalo::open
explicit library(const std::string& dyn_lib_path)
: m_handle(dynalo::open(dyn_lib_path))
{}

/// Returns a pointer to the @p func_name function using dynalo::get_function
template <typename FunctionSignature>
FunctionSignature* get_function(const std::string& func_name)
{
return dynalo::get_function<FunctionSignature>(m_handle, func_name);
}

/// Returns the native handle of the loaded shared library
native::handle get_native_handle()
{
return m_handle;
Expand Down
23 changes: 20 additions & 3 deletions include/dynalo/symbol_helper.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
// this header can be included multiple times

// This header can be included multiple times
// It should come in handy for switching betwee, exporting and importing symbols
//
// USAGE
//
// DYNALO_EXPORT <return type> DYNALO_CALL <function name>(<function argument types...>)
//
// EXAMPLE
//
// DYNALO_EXPORT int32_t DYNALO_CALL add_integers(const int32_t a, const int32_t b);
// DYNALO_EXPORT void DYNALO_CALL print_message(const char* message);
// EXPORTING FUNCTIONS
//
// #define DYNALO_EXPORT_SYMBOLS
// #include <dynalo/symbol_helper.hpp>
//
// DYNALO_EXPORT int32_t DYNALO_CALL add_integers(const int32_t a, const int32_t b);
// DYNALO_EXPORT void DYNALO_CALL print_message(const char* message);
//
// IMPORTING FUNCTIONS
//
// #define DYNALO_IMPORT_SYMBOLS
// #include <dynalo/symbol_helper.hpp>
//
// DYNALO_EXPORT int32_t DYNALO_CALL add_integers(const int32_t a, const int32_t b);
// DYNALO_EXPORT void DYNALO_CALL print_message(const char* message);
//

#if !defined(DYNALO_DEMANGLE)
Expand All @@ -19,6 +34,7 @@
#endif

#if defined(DYNALO_EXPORT_SYMBOLS)
#undef DYNALO_EXPORT_SYMBOLS

#if defined(DYNALO_EXPORT)
#undef DYNALO_EXPORT
Expand All @@ -31,6 +47,7 @@
#endif

#elif defined(DYNALO_IMPORT_SYMBOLS)
#undef DYNALO_IMPORT_SYMBOLS

#if defined(DYNALO_EXPORT)
#undef DYNALO_EXPORT
Expand Down
16 changes: 3 additions & 13 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
cmake_minimum_required(VERSION 3.5.0)

# linux
# cmake -DCMAKE_CXX_STANDARD=11 -DCMAKE_BUILD_TYPE=Debug -H. -Bbuild-linux && cmake --build build-linux --config Debug -- -j4

# windows
# cmake -G"Visual Studio 15 2017 Win64" -DCMAKE_CXX_STANDARD=11 -DCMAKE_BUILD_TYPE=Debug -H. -Bbuild-windows && cmake --build build-windows --config Debug -- /m:4

# test
# cd build-{linux,window}
# ctest -V

project(dynalo-test)

set(CMAKE_CXX_STANDARD 11 )
Expand All @@ -19,11 +9,11 @@ set(CMAKE_CXX_EXTENSIONS OFF)
add_executable(loader loader.cpp)
target_link_libraries(loader dynalo)

add_library(some SHARED some.cpp)
add_library(shared SHARED shared.cpp)

add_dependencies(loader some)
add_dependencies(loader shared)

enable_testing()
add_test(NAME all_tests COMMAND
"$<TARGET_FILE:loader>" "$<TARGET_FILE_DIR:some>" "some" # --log_level=all --color_output=true
"$<TARGET_FILE:loader>" "$<TARGET_FILE_DIR:shared>" "shared"
)
2 changes: 1 addition & 1 deletion test/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

int main(int argc, char* argv[])
{
dynalo::library lib(std::string(argv[1]) + "/" + dynalo::to_native_name("some"));
dynalo::library lib(std::string(argv[1]) + "/" + dynalo::to_native_name("awesome"));

auto add_integers = lib.get_function<int32_t(const int32_t, const int32_t)>("add_integers");
auto print_message = lib.get_function<void(const char*)>("print_message");
Expand Down
2 changes: 1 addition & 1 deletion test/some.cpp → test/shared.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "some.hpp"
#include "shared.hpp"

#include <iostream>

Expand Down
File renamed without changes.

0 comments on commit 125cb6c

Please sign in to comment.