Skip to content

Commit

Permalink
Initial commit!
Browse files Browse the repository at this point in the history
  • Loading branch information
maddouri committed Feb 17, 2018
0 parents commit a855d71
Show file tree
Hide file tree
Showing 14 changed files with 536 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
_build*/
build*/
cmake-build-*/
.idea/
.vs/

HunterGate.cmake
79 changes: 79 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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 )
#set(CMAKE_CXX_STANDARD_REQUIRED ON )
#set(CMAKE_CXX_EXTENSIONS OFF)

add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(${PROJECT_NAME} INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<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()



install(
DIRECTORY "include/${PROJECT_NAME}"
DESTINATION "include"
)

install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}Targets)

include (CMakePackageConfigHelpers)
write_basic_package_version_file (
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}ConfigVersion.cmake"
VERSION 0.9.0
COMPATIBILITY AnyNewerVersion
)
export(
EXPORT ${PROJECT_NAME}Targets
FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}Targets.cmake"
#NAMESPACE ${PROJECT_NAME}::
)
configure_file(
"cmake/Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}Config.cmake"
#COPYONLY
@ONLY
)
set(ConfigPackageLocation cmake/${PROJECT_NAME})
install(
EXPORT ${PROJECT_NAME}Targets
FILE ${PROJECT_NAME}Targets.cmake
#NAMESPACE ${PROJECT_NAME}::
DESTINATION ${ConfigPackageLocation}
)
install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION ${ConfigPackageLocation}
)

add_subdirectory(test)
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 Yassine MADDOURI

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions cmake/Config.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
9 changes: 9 additions & 0 deletions include/dynalo/detail/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#if defined(__linux__) || defined(__linux) || defined(linux) || defined(_LINUX)
#define DYNALO_HAS_LINUX
#elif defined(_WIN32) || defined(_WIN64)
#define DYNALO_HAS_WINDOWS
#else
#error "dynalo/detail/config.hpp OS Not Supported"
#endif
70 changes: 70 additions & 0 deletions include/dynalo/detail/linux/dynalo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#pragma once

#include <string>
#include <stdexcept>

#include <dlfcn.h>

namespace dynalo { namespace detail
{

inline
std::string last_error()
{
return std::string(::dlerror());
}

namespace native
{

using handle = void*;

inline handle invalid_handle() { return nullptr; }

namespace name
{

inline std::string prefix() { return std::string("lib"); }
inline std::string suffix() { return std::string(); }
inline std::string extension() { return std::string("so"); }

}

}

inline
native::handle open(const std::string& dyn_lib_path)
{
native::handle lib_handle = ::dlopen(dyn_lib_path.c_str(), RTLD_LAZY);
if (lib_handle == nullptr)
{
throw std::runtime_error(std::string("Failed to open [dyn_lib_path:") + dyn_lib_path + "]: " + last_error());
}

return lib_handle;
}

inline
void close(native::handle lib_handle)
{
const int rc = ::dlclose(lib_handle);
if (rc != 0)
{
throw std::runtime_error(std::string("Failed to close the dynamic library: ") + last_error());
}
}

template <typename FunctionSignature>
inline
FunctionSignature* get_function(native::handle lib_handle, const std::string& func_name)
{
void* func_ptr = ::dlsym(lib_handle, func_name.c_str());
if (func_ptr == nullptr)
{
throw std::runtime_error(std::string("Failed to get [func_name:") + func_name + "]: " + last_error());
}

return reinterpret_cast<FunctionSignature*>(func_ptr);
}

}}
98 changes: 98 additions & 0 deletions include/dynalo/detail/windows/dynalo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#pragma once

#include <string>
#include <stdexcept>

#include <Windows.h>
#include <strsafe.h>

namespace dynalo { namespace detail
{

std::string last_error()
{
// https://msdn.microsoft.com/en-us/library/ms680582%28VS.85%29.aspx
LPVOID lpMsgBuf;
DWORD dw = ::GetLastError();

::FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );

LPVOID lpDisplayBuf = (LPVOID)::LocalAlloc(LMEM_ZEROINIT,
(::lstrlen((LPCTSTR)lpMsgBuf) + 40) * sizeof(TCHAR));

::StringCchPrintf((LPTSTR)lpDisplayBuf,
::LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("Failed with error %d: %s"),
dw, lpMsgBuf);

std::string err_str((LPCTSTR)lpDisplayBuf);

::LocalFree(lpMsgBuf);
::LocalFree(lpDisplayBuf);

return err_str;
}


namespace native
{

using handle = HMODULE;

inline handle invalid_handle() { return nullptr; }

namespace name
{

inline std::string prefix() { return std::string(); }
inline std::string suffix() { return std::string(); }
inline std::string extension() { return std::string("dll"); }

}

}

inline
native::handle open(const std::string& dyn_lib_path)
{
native::handle lib_handle = ::LoadLibrary(dyn_lib_path.c_str());
if (lib_handle == nullptr)
{
throw std::runtime_error(std::string("Failed to open [dyn_lib_path:") + dyn_lib_path + "]: " + last_error());
}

return lib_handle;
}

inline
void close(native::handle lib_handle)
{
const BOOL rc = ::FreeLibrary(lib_handle);
if (rc == 0) // https://msdn.microsoft.com/en-us/library/windows/desktop/ms683152(v=vs.85).aspx
{
throw std::runtime_error(std::string("Failed to close the dynamic library: ") + last_error());
}
}

template <typename FunctionSignature>
inline
FunctionSignature* get_function(native::handle lib_handle, const std::string& func_name)
{
FARPROC func_ptr = ::GetProcAddress(lib_handle, func_name.c_str());
if (func_ptr == nullptr)
{
throw std::runtime_error(std::string("Failed to get [func_name:") + func_name + "]: " + last_error());
}

return reinterpret_cast<FunctionSignature*>(func_ptr);
}

}}
Loading

0 comments on commit a855d71

Please sign in to comment.