├── .codecov.yml ├── .travis.yml ├── CMakeLists.txt ├── LICENSE ├── README.md ├── demo-library ├── CMakeLists.txt ├── post_build_steps.cmake └── src │ ├── demo-library.cpp │ └── demo-library.hpp ├── example ├── CMakeLists.txt └── src │ └── main.cpp ├── include └── function_loader │ ├── detail │ ├── cpp_utils │ │ └── primitives │ │ │ └── idisable_copy.hpp │ ├── function_loader_base.hpp │ ├── function_loader_platform_specific.hpp │ ├── function_loader_platform_specific_unix.hpp │ ├── function_loader_platform_specific_windows.hpp │ ├── library_loader.hpp │ ├── library_loader_base.hpp │ ├── library_loader_platform_specific.hpp │ ├── library_loader_platform_specific_unix.hpp │ └── library_loader_platform_specific_windows.hpp │ ├── exceptions.hpp │ └── function_loader.hpp └── tests └── unit ├── CMakeLists.txt ├── include └── helpers.hpp └── src ├── exceptions_test.cpp ├── function_loader_test.cpp └── library_loader_test.cpp /.codecov.yml: -------------------------------------------------------------------------------- 1 | ignore: 2 | - "tests" 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | git: 2 | depth: 1 3 | 4 | matrix: 5 | include: 6 | - name: "function-loader, example, tests -- linux, debug, cppcheck, coverage, g++, 64-bit" 7 | os: linux 8 | dist: xenial 9 | language: cpp 10 | env: BUILD_TYPE="Debug" 11 | compiler: g++ 12 | sudo: required 13 | addons: 14 | apt: 15 | sources: 16 | - ubuntu-toolchain-r-test 17 | packages: 18 | - g++-6 19 | - cppcheck 20 | before_install: 21 | - pip install --user cpp-coveralls 22 | install: export CXX="g++-6" 23 | script: 24 | - set -e 25 | 26 | - cppcheck --enable=all 27 | -I include 28 | --language=c++ 29 | --platform=unix64 30 | --std=c++11 31 | --check-config 32 | --suppress=missingIncludeSystem 33 | -v ./tests 34 | 35 | - cmake -Bbuild -H. -DCMAKE_BUILD_TYPE:STRING=$BUILD_TYPE 36 | 37 | - cmake -Bbuild/example -Hexample -Dfunction-loader_DIR:PATH=$(pwd)/build -DCMAKE_BUILD_TYPE:STRING=$BUILD_TYPE 38 | - cmake --build build/example --config $BUILD_TYPE 39 | - pushd build/example 40 | - ./example 41 | - popd 42 | 43 | - cmake -Bbuild/submodules/test-utils -Hsubmodules/test-utils -DCMAKE_BUILD_TYPE:STRING=$BUILD_TYPE 44 | - cmake -Bbuild/tests/unit -Htests/unit 45 | -Dfunction-loader_DIR:PATH=$(pwd)/build 46 | -Dtest-utils_DIR:PATH=$(pwd)/build/submodules/test-utils 47 | -DCMAKE_BUILD_TYPE:STRING=$BUILD_TYPE 48 | -DCOVERAGE:BOOL=ON 49 | - cmake --build build/tests/unit --target run-all-tests-verbose --config $BUILD_TYPE -- -j $(nproc) 50 | 51 | - bash <(curl -s https://codecov.io/bash) 52 | 53 | - set +e 54 | 55 | - name: "function-loader, example, tests -- osx, release with debug info, valgrind, clang++, 64-bit" 56 | os: osx 57 | osx_image: xcode10.1 58 | language: cpp 59 | env: BUILD_TYPE="RelWithDebInfo" 60 | compiler: clang 61 | before_install: 62 | - brew install valgrind 63 | script: 64 | - set -e 65 | 66 | - cmake -Bbuild -H. -DCMAKE_BUILD_TYPE:STRING=$BUILD_TYPE 67 | 68 | - cmake -Bbuild/example -Hexample -Dfunction-loader_DIR:PATH=$(pwd)/build -DCMAKE_BUILD_TYPE:STRING=$BUILD_TYPE 69 | - cmake --build build/example --config $BUILD_TYPE 70 | # RVM overrides these shell built-ins for Ruby, but we need to use these 71 | - unset -f pushd && unset -f popd 72 | - pushd build/example 73 | - valgrind --error-exitcode=255 -v ./example 74 | - popd 75 | 76 | - cmake -Bbuild/submodules/test-utils -Hsubmodules/test-utils -DCMAKE_BUILD_TYPE:STRING=$BUILD_TYPE 77 | - cmake -Bbuild/tests/unit -Htests/unit 78 | -Dfunction-loader_DIR:PATH=$(pwd)/build 79 | -Dtest-utils_DIR:PATH=$(pwd)/build/submodules/test-utils 80 | -DCMAKE_BUILD_TYPE:STRING=$BUILD_TYPE 81 | -DCOVERAGE:BOOL=ON 82 | - cmake --build build/tests/unit --target run-all-tests-verbose --config $BUILD_TYPE -- -j $(sysctl -n hw.ncpu) 83 | 84 | - set +e 85 | 86 | - name: "function-loader, example, tests -- windows, release, msvc, 32-bit" 87 | os: windows 88 | language: cpp 89 | env: BUILD_TYPE="Release" 90 | script: 91 | - set -e 92 | 93 | - cmake -Bbuild -H. -DCMAKE_BUILD_TYPE:STRING=$BUILD_TYPE 94 | 95 | - cmake -Bbuild/example -Hexample -Dfunction-loader_DIR:PATH=$(pwd)/build -DCMAKE_BUILD_TYPE:STRING=$BUILD_TYPE 96 | - cmake --build build/example --config $BUILD_TYPE 97 | - pushd build/example/$BUILD_TYPE 98 | - start example.exe 99 | - popd 100 | 101 | - cmake -Bbuild/submodules/test-utils -Hsubmodules/test-utils -DCMAKE_BUILD_TYPE:STRING=$BUILD_TYPE 102 | - cmake -Bbuild/tests/unit -Htests/unit 103 | -Dfunction-loader_DIR:PATH=$(pwd)/build 104 | -Dtest-utils_DIR:PATH=$(pwd)/build/submodules/test-utils 105 | -DCMAKE_BUILD_TYPE:STRING=$BUILD_TYPE 106 | - cmake --build build/tests/unit --target run-all-tests-verbose --config $BUILD_TYPE 107 | 108 | - set +e 109 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.8 FATAL_ERROR) 2 | 3 | project(function-loader VERSION 1.2.6 LANGUAGES CXX) 4 | 5 | add_library(${PROJECT_NAME} INTERFACE) 6 | 7 | target_sources(${PROJECT_NAME} 8 | INTERFACE 9 | ${CMAKE_CURRENT_LIST_DIR}/include/function_loader/function_loader.hpp 10 | ${CMAKE_CURRENT_LIST_DIR}/include/function_loader/exceptions.hpp 11 | 12 | ${CMAKE_CURRENT_LIST_DIR}/include/function_loader/detail/function_loader_base.hpp 13 | ${CMAKE_CURRENT_LIST_DIR}/include/function_loader/detail/function_loader_platform_specific.hpp 14 | $<$: 15 | ${CMAKE_CURRENT_LIST_DIR}/include/function_loader/detail/function_loader_platform_specific_windows.hpp> 16 | $<$: 17 | ${CMAKE_CURRENT_LIST_DIR}/include/function_loader/detail/function_loader_platform_specific_unix.hpp> 18 | 19 | ${CMAKE_CURRENT_LIST_DIR}/include/function_loader/detail/library_loader.hpp 20 | ${CMAKE_CURRENT_LIST_DIR}/include/function_loader/detail/library_loader_base.hpp 21 | ${CMAKE_CURRENT_LIST_DIR}/include/function_loader/detail/library_loader_platform_specific.hpp 22 | $<$: 23 | ${CMAKE_CURRENT_LIST_DIR}/include/function_loader/detail/library_loader_platform_specific_windows.hpp> 24 | $<$: 25 | ${CMAKE_CURRENT_LIST_DIR}/include/function_loader/detail/library_loader_platform_specific_unix.hpp>) 26 | 27 | target_include_directories(${PROJECT_NAME} 28 | INTERFACE 29 | ${CMAKE_CURRENT_LIST_DIR}/include) 30 | 31 | if (CMAKE_DL_LIBS) 32 | message(STATUS "Will add ${CMAKE_DL_LIBS}") 33 | set_target_properties(${PROJECT_NAME} 34 | PROPERTIES 35 | INTERFACE_LINK_LIBRARIES "${CMAKE_DL_LIBS}") 36 | endif() 37 | 38 | target_compile_features(${PROJECT_NAME} 39 | INTERFACE 40 | cxx_std_11) 41 | 42 | install(TARGETS ${PROJECT_NAME} 43 | EXPORT _targets) 44 | 45 | export(EXPORT _targets 46 | NAMESPACE burda:: 47 | FILE "${PROJECT_NAME}-config.cmake") 48 | 49 | include(CMakePackageConfigHelpers) 50 | write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake 51 | COMPATIBILITY ExactVersion) 52 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Karel Burda 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Version](https://img.shields.io/badge/version-1.2.6-blue.svg) 2 | [![License](https://img.shields.io/badge/license-MIT_License-blue.svg?style=flat)](LICENSE) 3 | [![Build Status](https://travis-ci.org/karel-burda/function-loader.svg?branch=master)](https://travis-ci.org/karel-burda/function-loader) 4 | [![Codecov Status](https://codecov.io/gh/karel-burda/function-loader/branch/develop/graph/badge.svg)](https://codecov.io/gh/karel-burda/function-loader/branch/master) 5 | 6 | ## Important 7 | This project contains git sub-modules that are needed for building example and tests. 8 | 9 | If you just want to use the implementation, you can clone without sub-modules. In case you want to build the example or tests, be sure to clone the repository 10 | with `--recurse-submodules` or `--recursive` on older versions of git. Alternatively, you can clone without sub-modules and initialize these later. 11 | 12 | ## Introduction 13 | `function_loader` is a header-only library that can find free functions in a shared library and provides `std::function` wrapper around the found function. 14 | 15 | Essentially represents a wrapper around the calls `LoadLibrary`, `GetProcedure` and `FreeLibrary` system calls on Windows and `dlopen`, `dlsym` and `dlclose` on POSIXes. 16 | 17 | `function_loader` class supports move semantics and disables copy operations; implementation is in C++11. 18 | 19 | These exceptions might be thrown: 20 | * `library_load_failed` 21 | * `library_handle_invalid` 22 | * `function_does_not_exist` 23 | 24 | Exceptions provide additional information about the reason using `what()`, see [exceptions.hpp](include/function_loader/exceptions.hpp) for more info. 25 | 26 | ## Usage 27 | In order to use the `function_loader`, it's the `include` directory that matters. Just make sure that the header search path is pointing to the [include](include) directory located in the root directory. 28 | 29 | Implementation resides in the `burda::function_loader` namespace, so it might be useful to do something like `namespace fe = burda::function_loader;` in your project. 30 | 31 | There are basically these options when it comes to build system integration: 32 | 33 | ### 1. CMake Way 34 | Recommended option. 35 | 36 | There are essentially these ways of how to use this package depending on your preferences our build architecture: 37 | 38 | #### A) Generate directly 39 | 40 | Call `add_subdirectory(...)` directly in your CMakeLists.txt: 41 | 42 | ```cmake 43 | add_executable(my-project main.cpp) 44 | 45 | add_subdirectory() 46 | # example: add_subdirectory(function-loader ${CMAKE_BINARY_DIR}/function-loader) 47 | 48 | # query of package version 49 | message(STATUS "Current version of function-loader is: ${function-loader_VERSION}") 50 | 51 | add_library(burda::function-loader ALIAS function-loader) 52 | 53 | # this will import search paths, compile definitions and other dependencies of the function-loader as well 54 | target_link_libraries(my-project function-loader) 55 | # or with private visibility: target_link_libraries(my-project PRIVATE function-loader) 56 | ``` 57 | 58 | #### B) Generate separately 59 | 60 | Generation phase on the function-loader is run separately, that means that you run: 61 | ```cmake 62 | cmake 63 | # example: cmake -Bbuild/function-loader -Hfunction-loader in the root of your project 64 | ``` 65 | 66 | This will create automatically generated package configuration file `function-loader-config.cmake` that contains exported target and all important information. 67 | 68 | Then you can do this in your CMakeLists.txt: 69 | 70 | ```cmake 71 | add_executable(my-project main.cpp) 72 | 73 | find_package(function-loader CONFIG PATHS ) 74 | # alternatively assuming that the "function-loader_DIR" variable is set: find_package(function-loader CONFIG) 75 | 76 | # you can also query (or force specific version during the previous "find_package()" call) 77 | message(STATUS "Found version of function-loader is: ${function-loader_VERSION}") 78 | 79 | # this will import search paths, compile definitions and other dependencies of the function-loader as well 80 | target_link_libraries(my-project burda::function-loader) 81 | # or with public visibility: target_link_libraries(my-project PUBLIC burda::function-loader) 82 | ``` 83 | 84 | ### 2. Manual Way 85 | Not recommended. 86 | 87 | Make sure that the `include` directory is in the search paths. 88 | 89 | You also have to set C++ 11 standard and potentially other settings as well (e.g. linking `libdl` on POSIXes). 90 | 91 | ## Examples 92 | For full use cases, see [main.cpp](example/src/main.cpp) or implementation of unit tests at [tests/unit](tests/unit). 93 | 94 | ```cpp 95 | #include 96 | 97 | #include 98 | #include 99 | 100 | namespace function_loader = burda::function_loader; 101 | 102 | try 103 | { 104 | // or load DLL on Windows, or .dylib on OS X 105 | function_loader::function_loader loader{ "./shared-library.so" }; 106 | // function_loader supports move semantics, so we can safely do e.g. "const auto other = std::move(loader)" 107 | 108 | // get procedures at runtime from the shared library 109 | // see "demo-library.hpp" and "demo-library.cpp" in the "demo-library" directory 110 | const auto func_simple = loader.get_function("foo"); 111 | const auto func_more_complex = loader.get_function("bar"); 112 | 113 | // don't have to check for call-ability, otherwise the "function_does_not_exist" would be thrown 114 | func_simple(); 115 | std::clog << "func_more_complex returned " << func_more_complex(99.0, "foo"); 116 | 117 | // if the "loader" object went out of scope in here, it would free all resources and unload 118 | // the library handle, but for demo purposes, we'll move the instance 119 | const auto another_loader = std::move(loader); 120 | // do whatever actions you like with the "another_loader" 121 | 122 | // the "another_loader" goes out of scope 123 | } 124 | catch (const function_loader::exceptions::library_load_failed & error) 125 | { 126 | // handle exception, error.what() contains information about the error code from the OS 127 | // library load failed upon construction of the function_loader 128 | } 129 | catch (const function_loader::exceptions::library_handle_invalid & error) 130 | { 131 | // happens when "get_function" called on the function_loader with invalid library handle 132 | // (may happen after the object was moved) 133 | } 134 | catch (const function_loader::exceptions::function_does_not_exist & error) 135 | { 136 | // given function not found in the library, might be caused by incorrect signature, 137 | // or function is not exported (visible) from outside 138 | } 139 | ``` 140 | 141 | Where this is the header of the `shared-library.(so|dll|dylib)`: 142 | ```cpp 143 | extern "C" 144 | { 145 | /// LIBRARY_EXPORT is defined elsewhere, but we just need the symbols to be visible from outside 146 | /// the shared libary (e.g. using "__declspec(dllexport)" or "__attribute__((visibility("default")))" on the GCC). 147 | /// When using function_loader, we don't need to import any symbols (e.g. "__declspec(dllimport)"), 148 | /// because there's no static linking. 149 | 150 | LIBRARY_EXPORT void foo(); 151 | LIBRARY_EXPORT int bar(float number, const char * str); 152 | } 153 | ``` 154 | 155 | ## Unit Tests 156 | Tests require sub-modules [cmake-helpers](https://github.com/karel-burda/cmake-helpers) and [test-utils](https://github.com/karel-burda/test-utils). 157 | 158 | For building tests, run CMake in the source directory [tests/unit](tests/unit): 159 | 160 | ```cmake 161 | cmake -Bbuild -H. 162 | 163 | cmake -Bbuild/submodules/test-utils -Hsubmodules/test-utils 164 | # you can also add coverage by appending "-DCOVERAGE:BOOL=ON" 165 | cmake -Bbuild/tests/unit -Htests/unit 166 | -Dfunction-loader_DIR:PATH=$(pwd)/build 167 | -Dtest-utils_DIR:PATH=$(pwd)/build/submodules/test-utils 168 | cmake --build build/tests/unit 169 | 170 | # this runs target "run-all-tests-verbose" that will also run the tests with timeout, etc.: 171 | cmake --build build/tests/unit --target run-all-tests-verbose 172 | ``` 173 | 174 | This is the example of running tests in the debug mode. 175 | 176 | For more info, see [.travis.yml](.travis.yml). 177 | 178 | ## Continuous Integration 179 | Continuous Integration is now being run Linux, OS X and Windows on Travis: https://travis-ci.org/karel-burda/function-loader. 180 | 181 | Compilers are set-up to treat warnings as errors and with pedantic warning level. 182 | Targets are built in one stage with debug symbols with code coverage measure and in release mode with debug symbols in the second one. 183 | 184 | Valgrind is being run on the example as well. 185 | 186 | The project is using these jobs: 187 | * `function-loader, example, tests -- linux, debug, cppcheck, coverage, g++, 64-bit` 188 | * `function-loader, example, tests -- osx, release with debug info, valgrind, clang++, 64-bit` 189 | * `function-loader, example, tests -- windows, release, msvc, 32-bit` 190 | 191 | Project uses [codecov.io](https://codecov.io/gh/karel-burda/function-loader) for code coverage summary. 192 | -------------------------------------------------------------------------------- /demo-library/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.8 FATAL_ERROR) 2 | 3 | project(demo-library LANGUAGES CXX) 4 | 5 | include(${CMAKE_CURRENT_LIST_DIR}/../submodules/cmake-helpers/cmake-helpers/cpp_warnings.cmake) 6 | 7 | add_library(${PROJECT_NAME} SHARED) 8 | 9 | include(GenerateExportHeader) 10 | set(CMAKE_CXX_VISIBILITY_PRESET default) 11 | generate_export_header(${PROJECT_NAME} EXPORT_FILE_NAME "export.hpp") 12 | FILE(COPY ${CMAKE_CURRENT_BINARY_DIR}/export.hpp 13 | DESTINATION ${CMAKE_CURRENT_LIST_DIR}/src) 14 | 15 | target_sources(${PROJECT_NAME} 16 | PUBLIC 17 | ${CMAKE_CURRENT_LIST_DIR}/src/demo-library.hpp 18 | ${CMAKE_CURRENT_LIST_DIR}/src/demo-library.cpp 19 | ${CMAKE_CURRENT_LIST_DIR}/src/export.hpp) 20 | 21 | # Unfortunately, we cannot use SUFFIX as empty string property on MSVC, because the LoadLibrary(...) method from the WinAPI 22 | # checks whether the file name has some suffix at all; for simplicity, use the same suffix also on POSIXEs 23 | set_target_properties(${PROJECT_NAME} 24 | PROPERTIES 25 | PREFIX "" 26 | SUFFIX ".dll") 27 | 28 | burda_cmake_helpers_cpp_warnings_add_pedantic_level_cxx(${PROJECT_NAME} PRIVATE) 29 | -------------------------------------------------------------------------------- /demo-library/post_build_steps.cmake: -------------------------------------------------------------------------------- 1 | macro(_burda_function_loader_demo_library_add_post_build_steps _target) 2 | get_target_property(_demo-library_prefix demo-library PREFIX) 3 | get_target_property(_demo-library_suffix demo-library SUFFIX) 4 | set(_demo-library_location ${CMAKE_CURRENT_BINARY_DIR}/demo-library) 5 | set(_demo-library_filename ${_demo-library_prefix}demo-library${_demo-library_suffix}) 6 | if (MSVC) 7 | string(APPEND _demo-library_location /${CMAKE_BUILD_TYPE}) 8 | endif() 9 | 10 | add_custom_command(TARGET ${_target} 11 | POST_BUILD 12 | COMMAND 13 | ${CMAKE_COMMAND} -E copy 14 | ${_demo-library_location}/${_demo-library_filename} 15 | ${CMAKE_CURRENT_BINARY_DIR}/${_demo-library_filename} 16 | 17 | # We deliberately copy the testing library into a subdirectories in order to test whether loading of library from nested dirs work 18 | COMMAND 19 | ${CMAKE_COMMAND} -E copy 20 | ${_demo-library_location}/${_demo-library_filename} 21 | ${CMAKE_CURRENT_BINARY_DIR}/subdirectory/another/${_demo-library_filename}) 22 | endmacro() 23 | -------------------------------------------------------------------------------- /demo-library/src/demo-library.cpp: -------------------------------------------------------------------------------- 1 | #include "demo-library.hpp" 2 | 3 | #include 4 | #include 5 | 6 | namespace burda 7 | { 8 | namespace function_loader 9 | { 10 | namespace example 11 | { 12 | void function_with_no_params() 13 | { 14 | std::clog << "Executing 'function_with_no_params'" << std::endl; 15 | } 16 | 17 | int function_with_return_value_and_params(float number, const char * str) 18 | { 19 | std::clog << "Executing 'function_with_return_value_and_params', parameters: number=" 20 | << std::to_string(number) << ", str=" << (str ? str : "(nullptr)") << std::endl; 21 | 22 | return 999; 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /demo-library/src/demo-library.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "export.hpp" 4 | 5 | namespace burda 6 | { 7 | namespace function_loader 8 | { 9 | namespace example 10 | { 11 | extern "C" 12 | { 13 | DEMO_LIBRARY_EXPORT void function_with_no_params(); 14 | DEMO_LIBRARY_EXPORT int function_with_return_value_and_params(float number, const char * str); 15 | } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /example/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.8 FATAL_ERROR) 2 | 3 | project(example LANGUAGES CXX) 4 | 5 | include(${CMAKE_CURRENT_LIST_DIR}/../submodules/cmake-helpers/cmake-helpers/cpp_warnings.cmake) 6 | 7 | add_executable(${PROJECT_NAME} "") 8 | 9 | target_sources(${PROJECT_NAME} 10 | PRIVATE 11 | ${CMAKE_CURRENT_LIST_DIR}/src/main.cpp) 12 | 13 | find_package(function-loader CONFIG REQUIRED) 14 | message(STATUS "Found version of function-loader is: ${function-loader_VERSION}") 15 | target_link_libraries(${PROJECT_NAME} PRIVATE burda::function-loader) 16 | 17 | add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../demo-library ${CMAKE_CURRENT_BINARY_DIR}/demo-library) 18 | add_dependencies(${PROJECT_NAME} demo-library) 19 | 20 | include(${CMAKE_CURRENT_LIST_DIR}/../demo-library/post_build_steps.cmake) 21 | _burda_function_loader_demo_library_add_post_build_steps(${PROJECT_NAME}) 22 | 23 | burda_cmake_helpers_cpp_warnings_add_pedantic_level_cxx(${PROJECT_NAME} PRIVATE) 24 | -------------------------------------------------------------------------------- /example/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | 7 | static int print_error_and_exit(const std::exception & error) 8 | { 9 | std::cerr << error.what() << std::endl; 10 | 11 | return EXIT_FAILURE; 12 | } 13 | 14 | static int show_usage() 15 | { 16 | namespace function_loader = burda::function_loader; 17 | 18 | try 19 | { 20 | function_loader::function_loader loader{ "./demo-library.dll" }; 21 | // function_loader supports move semantics, so we can safely do e.g. "const auto other = std::move(loader)" 22 | 23 | // get procedures at runtime from the shared library 24 | // see "demo-library.hpp" and "demo-library.cpp" in the "demo-library" directory 25 | const auto func_void_no_params = loader.get_function("function_with_no_params"); 26 | const auto func_with_return_value_and_params = loader.get_function("function_with_return_value_and_params"); 27 | 28 | // don't have to check for call-ability, otherwise the "function_does_not_exist" would be thrown 29 | func_void_no_params(); 30 | std::clog << "func_with_return_value_and_params returned " << func_with_return_value_and_params(99.0, "foo"); 31 | 32 | // if the "loader" object went out of scope in here, it would free all resources and unload the library handle, 33 | // but for demo purposes, we'll move the instance 34 | const auto another_loader = std::move(loader); 35 | // do whatever actions you like with the "another_loader" 36 | 37 | // the "another_loader" goes out of scope 38 | } 39 | catch (const function_loader::exceptions::library_load_failed & error) 40 | { 41 | // library load failed upon construction of the function_loader 42 | return print_error_and_exit(error); 43 | } 44 | catch (const function_loader::exceptions::library_handle_invalid & error) 45 | { 46 | // happens when "get_function" called on the function_loader with invalid library handle 47 | // (may happen after the object was moved) 48 | return print_error_and_exit(error); 49 | } 50 | catch (const function_loader::exceptions::function_does_not_exist & error) 51 | { 52 | // given function not found in the library, might be caused by incorrect signature, 53 | // or function is not exported (visible) from outside 54 | return print_error_and_exit(error); 55 | } 56 | 57 | return EXIT_SUCCESS; 58 | } 59 | 60 | int main(int /*argc*/, char ** /*argv*/) 61 | { 62 | return show_usage(); 63 | } 64 | -------------------------------------------------------------------------------- /include/function_loader/detail/cpp_utils/primitives/idisable_copy.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | /// Origin: https://github.com/karel-burda/cpp-utils 4 | /// Version: 1.1.0 5 | 6 | namespace burda 7 | { 8 | namespace cpp_utils 9 | { 10 | namespace primitives 11 | { 12 | /// Helper class that enables default construction and disables copy operations 13 | /// Recommended way of inheriting from this base class is private 14 | struct idisable_copy 15 | { 16 | idisable_copy() = default; 17 | ~idisable_copy() = default; 18 | 19 | idisable_copy(const idisable_copy &) = delete; 20 | idisable_copy & operator=(const idisable_copy &) = delete; 21 | 22 | idisable_copy(idisable_copy &&) = default; 23 | idisable_copy & operator=(idisable_copy &&) = default; 24 | }; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /include/function_loader/detail/function_loader_base.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "function_loader/detail/library_loader.hpp" 4 | 5 | namespace burda 6 | { 7 | namespace function_loader 8 | { 9 | namespace detail 10 | { 11 | class function_loader_base 12 | { 13 | public: 14 | explicit function_loader_base(const std::string & library_path) 15 | : library{library_path} 16 | { 17 | } 18 | 19 | protected: 20 | library_loader library; 21 | }; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /include/function_loader/detail/function_loader_platform_specific.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifdef _WIN32 4 | #include "function_loader/detail/function_loader_platform_specific_windows.hpp" 5 | #elif defined(__unix__) || defined(__APPLE__) 6 | #include "function_loader/detail/function_loader_platform_specific_unix.hpp" 7 | #else 8 | #error Platform not implemented 9 | #endif 10 | -------------------------------------------------------------------------------- /include/function_loader/detail/function_loader_platform_specific_unix.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "function_loader/detail/function_loader_base.hpp" 6 | 7 | namespace burda 8 | { 9 | namespace function_loader 10 | { 11 | namespace detail 12 | { 13 | class function_loader_platform_specific : public function_loader_base 14 | { 15 | public: 16 | using function_loader_base::function_loader_base; 17 | 18 | protected: 19 | void * get_function_address(const std::string & function_name) const 20 | { 21 | library.throw_if_handle_is_invalid(); 22 | 23 | return dlsym(library.get_handle(), function_name.c_str()); 24 | } 25 | }; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /include/function_loader/detail/function_loader_platform_specific_windows.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | 7 | #include "function_loader/detail/function_loader_base.hpp" 8 | 9 | namespace burda 10 | { 11 | namespace function_loader 12 | { 13 | namespace detail 14 | { 15 | class function_loader_platform_specific : public function_loader_base 16 | { 17 | public: 18 | using function_loader_base::function_loader_base; 19 | 20 | protected: 21 | void * get_function_address(const std::string & function_name) const 22 | { 23 | library.throw_if_handle_is_invalid(); 24 | 25 | return GetProcAddress(library.get_handle(), function_name.c_str()); 26 | } 27 | }; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /include/function_loader/detail/library_loader.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "function_loader/exceptions.hpp" 6 | #include "function_loader/detail/library_loader_platform_specific.hpp" 7 | 8 | namespace burda 9 | { 10 | namespace function_loader 11 | { 12 | namespace detail { 13 | class library_loader : public detail::library_loader_platform_specific 14 | { 15 | public: 16 | explicit library_loader(const std::string & path) 17 | { 18 | load_library(path); 19 | 20 | if (get_handle() == nullptr) 21 | { 22 | throw exceptions::library_load_failed{ path + ", reason: " + get_last_error() }; 23 | } 24 | } 25 | 26 | ~library_loader() 27 | { 28 | unload_library(); 29 | } 30 | 31 | library_loader(library_loader && source) 32 | { 33 | handle = source.handle; 34 | source.handle = nullptr; 35 | } 36 | 37 | library_loader & operator=(library_loader && source) 38 | { 39 | if (this != &source) 40 | { 41 | unload_library(); 42 | std::swap(handle, source.handle); 43 | } 44 | 45 | return *this; 46 | } 47 | }; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /include/function_loader/detail/library_loader_base.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "function_loader/exceptions.hpp" 6 | #include "function_loader/detail/cpp_utils/primitives/idisable_copy.hpp" 7 | 8 | namespace burda 9 | { 10 | namespace function_loader 11 | { 12 | namespace detail 13 | { 14 | template 15 | class library_loader_base : private burda::cpp_utils::primitives::idisable_copy 16 | { 17 | public: 18 | handle_type get_handle() const 19 | { 20 | return handle; 21 | } 22 | 23 | void throw_if_handle_is_invalid() const 24 | { 25 | if (handle == nullptr) 26 | { 27 | throw exceptions::library_handle_invalid{}; 28 | } 29 | } 30 | 31 | protected: 32 | handle_type handle = nullptr; 33 | }; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /include/function_loader/detail/library_loader_platform_specific.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifdef _WIN32 4 | #include "function_loader/detail/library_loader_platform_specific_windows.hpp" 5 | #elif defined(__unix__) || defined(__APPLE__) 6 | #include "function_loader/detail/library_loader_platform_specific_unix.hpp" 7 | #else 8 | #error Platform not implemented 9 | #endif 10 | -------------------------------------------------------------------------------- /include/function_loader/detail/library_loader_platform_specific_unix.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "function_loader/detail/library_loader_base.hpp" 6 | 7 | namespace burda 8 | { 9 | namespace function_loader 10 | { 11 | namespace detail 12 | { 13 | class library_loader_platform_specific : public library_loader_base 14 | { 15 | protected: 16 | void load_library(const std::string & path) 17 | { 18 | handle = dlopen(path.c_str(), RTLD_NOW); 19 | } 20 | 21 | void unload_library() 22 | { 23 | if (handle) 24 | { 25 | dlclose(handle); 26 | 27 | handle = nullptr; 28 | } 29 | } 30 | 31 | std::string get_last_error() const 32 | { 33 | const auto error_from_os = dlerror(); 34 | 35 | return error_from_os != nullptr ? error_from_os : ""; 36 | } 37 | }; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /include/function_loader/detail/library_loader_platform_specific_windows.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "function_loader/detail/library_loader_base.hpp" 6 | 7 | namespace burda 8 | { 9 | namespace function_loader 10 | { 11 | namespace detail 12 | { 13 | class library_loader_platform_specific : public library_loader_base 14 | { 15 | protected: 16 | void load_library(const std::string & path) 17 | { 18 | handle = LoadLibraryExA(path.c_str(), nullptr, 0); 19 | } 20 | 21 | void unload_library() 22 | { 23 | if (handle != nullptr) 24 | { 25 | FreeLibrary(handle); 26 | 27 | handle = nullptr; 28 | } 29 | } 30 | 31 | std::string get_last_error() const 32 | { 33 | const auto error_code_from_os = GetLastError(); 34 | 35 | return error_code_from_os != 0 ? std::to_string(error_code_from_os) : ""; 36 | } 37 | }; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /include/function_loader/exceptions.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | namespace burda 6 | { 7 | namespace function_loader 8 | { 9 | namespace exceptions 10 | { 11 | struct library_load_failed : public std::runtime_error 12 | { 13 | explicit library_load_failed(const std::string & path) 14 | : std::runtime_error{"Could not load library (or its dependencies) at path " + path } 15 | { 16 | } 17 | }; 18 | 19 | struct library_handle_invalid : public std::runtime_error 20 | { 21 | explicit library_handle_invalid() 22 | : std::runtime_error{ "Library handle is invalid (might be caused by using instance after is has been moved)" } 23 | { 24 | } 25 | }; 26 | 27 | struct function_does_not_exist : public std::logic_error 28 | { 29 | explicit function_does_not_exist(const std::string & name) 30 | : std::logic_error{ "Could not load function " + name } 31 | { 32 | } 33 | }; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /include/function_loader/function_loader.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "function_loader/detail/function_loader_platform_specific.hpp" 6 | 7 | namespace burda 8 | { 9 | namespace function_loader 10 | { 11 | /// RAI wrapper that searches shared library for functions at run-time and binds these to the std::function type 12 | /// Loads shared library handle in the construction and frees upon destruction 13 | class function_loader : public detail::function_loader_platform_specific 14 | { 15 | public: 16 | /// Loads library handle as part of the construction 17 | /// Usage:library_loader{"./foo/bar/library.(so|dll|dylib)"} 18 | /// @param library_path might be either absolute or relative to the working directory 19 | /// @see "LoadLibrary" from the Windows API or "dlopen" on POSIXes for explanation of the path argument 20 | /// @see "library_lodader_base" 21 | /// @throws library_load_failed 22 | using function_loader_platform_specific::function_loader_platform_specific; 23 | 24 | /// Constructs std::function that binds to the shared library function 25 | /// @tparam is the type specifier of the std::function, e.g. "void(int, float)" for the "std::function" 26 | /// @returns std::function that binds to the found function in a shared library 27 | /// @throws function_does_not_exist 28 | template 29 | std::function get_function(const std::string & procedure_name) const 30 | { 31 | void * procedure_address = get_function_address(procedure_name); 32 | 33 | if (procedure_address == nullptr) 34 | { 35 | throw exceptions::function_does_not_exist{ procedure_name }; 36 | } 37 | 38 | return reinterpret_cast(procedure_address); 39 | } 40 | }; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /tests/unit/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.8 FATAL_ERROR) 2 | 3 | project(tests LANGUAGES CXX) 4 | 5 | include(${CMAKE_CURRENT_LIST_DIR}/../../submodules/cmake-helpers/cmake-helpers/cpp_coverage.cmake) 6 | include(${CMAKE_CURRENT_LIST_DIR}/../../submodules/cmake-helpers/cmake-helpers/cpp_gtest.cmake) 7 | include(${CMAKE_CURRENT_LIST_DIR}/../../submodules/cmake-helpers/cmake-helpers/cpp_warnings.cmake) 8 | 9 | add_executable(${PROJECT_NAME} "") 10 | 11 | target_sources(${PROJECT_NAME} 12 | PRIVATE 13 | ${CMAKE_CURRENT_LIST_DIR}/include/helpers.hpp 14 | 15 | ${CMAKE_CURRENT_LIST_DIR}/src/exceptions_test.cpp 16 | ${CMAKE_CURRENT_LIST_DIR}/src/function_loader_test.cpp 17 | ${CMAKE_CURRENT_LIST_DIR}/src/library_loader_test.cpp) 18 | 19 | target_include_directories(${PROJECT_NAME} 20 | PRIVATE 21 | ${CMAKE_CURRENT_LIST_DIR}/include) 22 | 23 | find_package(test-utils CONFIG REQUIRED) 24 | message(STATUS "Found version of test-utils is: ${test-utils_VERSION}") 25 | target_link_libraries(${PROJECT_NAME} PRIVATE burda::test-utils) 26 | 27 | find_package(function-loader CONFIG REQUIRED) 28 | message(STATUS "Found version of function-loader is: ${function-loader_VERSION}") 29 | target_link_libraries(${PROJECT_NAME} PRIVATE burda::function-loader) 30 | 31 | target_compile_options(${PROJECT_NAME} 32 | PRIVATE 33 | $<$:/MP>) 34 | 35 | burda_cmake_helpers_cpp_gtest_bootstrap_and_link(${PROJECT_NAME} "release-1.8.1" "Release" PRIVATE) 36 | 37 | if (COVERAGE) 38 | message(STATUS "Building unit tests with code coverage") 39 | burda_cmake_helpers_cpp_coverage_add_build_options_cxx(${PROJECT_NAME} PRIVATE) 40 | endif() 41 | 42 | add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../../demo-library ${CMAKE_CURRENT_BINARY_DIR}/demo-library) 43 | add_dependencies(${PROJECT_NAME} demo-library) 44 | 45 | include(${CMAKE_CURRENT_LIST_DIR}/../../demo-library/post_build_steps.cmake) 46 | _burda_function_loader_demo_library_add_post_build_steps(${PROJECT_NAME}) 47 | 48 | # Create invalid (empty) file that will be used in the tests to verify, whether the implementation correctly refuses to load this file 49 | file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/empty.dll "") 50 | 51 | enable_testing() 52 | 53 | add_test(NAME all-unit-tests 54 | COMMAND $ --gtest_color=yes --gtest_shuffle) 55 | 56 | add_custom_target(run-all-tests-verbose 57 | COMMAND ${CMAKE_CTEST_COMMAND} -V -C ${CMAKE_BUILD_TYPE} --build-run-dir ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} --timeout 300 58 | DEPENDS ${PROJECT_NAME}) 59 | 60 | burda_cmake_helpers_cpp_warnings_add_pedantic_level_cxx(${PROJECT_NAME} PRIVATE) 61 | burda_cmake_helpers_cpp_warnings_suppress_cxx(${PROJECT_NAME} "keyword-macro" PRIVATE) 62 | -------------------------------------------------------------------------------- /tests/unit/include/helpers.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace burda 4 | { 5 | namespace function_loader 6 | { 7 | namespace testing 8 | { 9 | constexpr const char * get_demo_library_file_path() 10 | { 11 | return "./demo-library.dll"; 12 | } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/unit/src/exceptions_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | namespace 7 | { 8 | namespace exceptions = burda::function_loader::exceptions; 9 | namespace test_utils = burda::test_utils; 10 | 11 | TEST(exceptions, static_assertions) 12 | { 13 | test_utils::statics::assert_default_constructibility(); 14 | test_utils::statics::assert_default_constructibility(); 15 | test_utils::statics::assert_default_constructibility(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/unit/src/function_loader_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // deliberately in this place ahead of following includes 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "helpers.hpp" 12 | 13 | namespace 14 | { 15 | namespace exceptions = burda::function_loader::exceptions; 16 | namespace test_utils = burda::test_utils; 17 | namespace testing = burda::function_loader::testing; 18 | 19 | using function_loader = burda::function_loader::function_loader; 20 | 21 | TEST(function_loader, static_assertions) 22 | { 23 | test_utils::statics::assert_default_constructibility(); 24 | test_utils::statics::assert_copy_constructibility(); 25 | test_utils::statics::assert_move_constructibility(); 26 | } 27 | 28 | TEST(function_loader, construction_destruction) 29 | { 30 | test_utils::lifetime::assert_construction_and_destruction(testing::get_demo_library_file_path()); 31 | EXPECT_THROW(function_loader{ "foo" }, exceptions::library_load_failed); 32 | EXPECT_NO_THROW(function_loader{ "./subdirectory/another/demo-library.dll" }); 33 | } 34 | 35 | TEST(function_loader, move_operations) 36 | { 37 | function_loader first_loader{ testing::get_demo_library_file_path() }; 38 | 39 | ASSERT_NE(first_loader.library.get_handle(), nullptr); 40 | 41 | const auto second_loader = std::move(first_loader); 42 | 43 | ASSERT_EQ(first_loader.library.get_handle(), nullptr); 44 | ASSERT_NE(second_loader.library.get_handle(), nullptr); 45 | 46 | EXPECT_THROW(first_loader.get_function("function_with_no_params")(), exceptions::library_handle_invalid); 47 | EXPECT_NO_THROW(second_loader.get_function("function_with_no_params")()); 48 | } 49 | 50 | TEST(function_loader, default_values) 51 | { 52 | function_loader loader{ testing::get_demo_library_file_path() }; 53 | EXPECT_NE(&loader.library, nullptr); 54 | } 55 | 56 | TEST(function_loader, get_function) 57 | { 58 | function_loader loader{ testing::get_demo_library_file_path() }; 59 | 60 | { 61 | const auto func_void_no_params = loader.get_function("function_with_no_params"); 62 | 63 | EXPECT_NO_THROW(func_void_no_params()); 64 | } 65 | 66 | { 67 | const auto func_with_return_value_and_params = loader.get_function("function_with_return_value_and_params"); 68 | int returnedValue = 0; 69 | EXPECT_NO_THROW(returnedValue = func_with_return_value_and_params(2.3f, "foo-bar-baz")); 70 | EXPECT_EQ(returnedValue, 999); 71 | } 72 | 73 | { 74 | EXPECT_THROW(loader.get_function("function_that_does_not_exists"), exceptions::function_does_not_exist); 75 | } 76 | } 77 | 78 | TEST(function_loader, exceptions) 79 | { 80 | // what() called on the exception should not be empty 81 | try 82 | { 83 | function_loader loader{ testing::get_demo_library_file_path() }; 84 | loader.get_function("foo-bar-baz"); 85 | } 86 | catch (const exceptions::function_does_not_exist & error) 87 | { 88 | EXPECT_NE(error.what(), ""); 89 | } 90 | catch (...) 91 | { 92 | FAIL() << "Correct exception not thrown"; 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /tests/unit/src/library_loader_test.cpp: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | #include 3 | #elif __linux__ 4 | #include 5 | #endif 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "helpers.hpp" 16 | 17 | namespace 18 | { 19 | namespace exceptions = burda::function_loader::exceptions; 20 | namespace function_loader = burda::function_loader; 21 | namespace test_utils = burda::test_utils; 22 | namespace testing = function_loader::testing; 23 | 24 | using library_loader = function_loader::detail::library_loader; 25 | 26 | TEST(library_loader, static_assertions) 27 | { 28 | test_utils::statics::assert_default_constructibility(); 29 | test_utils::statics::assert_copy_constructibility(); 30 | test_utils::statics::assert_move_constructibility(); 31 | } 32 | 33 | TEST(library_loader, construction_destruction) 34 | { 35 | test_utils::lifetime::assert_construction_and_destruction(testing::get_demo_library_file_path()); 36 | EXPECT_THROW(library_loader{ "foo" }, exceptions::library_load_failed); 37 | EXPECT_NO_THROW(library_loader{ testing::get_demo_library_file_path() }); 38 | EXPECT_NO_THROW(library_loader{ "./subdirectory/another/demo-library.dll" }); 39 | EXPECT_THROW(library_loader{ "./subdirectory/another/a/b/c/d/demo-library.dll" }, exceptions::library_load_failed); 40 | } 41 | 42 | TEST(library_loader, move_operations) 43 | { 44 | library_loader first_loader{ testing::get_demo_library_file_path() }; 45 | library_loader second_loader{ "./subdirectory/another/demo-library.dll" }; 46 | 47 | ASSERT_NE(first_loader.get_handle(), nullptr); 48 | ASSERT_NE(second_loader.get_handle(), nullptr); 49 | 50 | second_loader = std::move(first_loader); 51 | EXPECT_EQ(first_loader.get_handle(), nullptr); 52 | EXPECT_NE(second_loader.get_handle(), nullptr); 53 | 54 | const auto third_loader{ std::move(second_loader) }; 55 | EXPECT_EQ(second_loader.get_handle(), nullptr); 56 | EXPECT_NE(third_loader.get_handle(), nullptr); 57 | } 58 | 59 | TEST(library_loader, default_values) 60 | { 61 | library_loader shared_library{ testing::get_demo_library_file_path() }; 62 | EXPECT_NE(shared_library.get_handle(), nullptr); 63 | } 64 | 65 | 66 | TEST(library_loader, resource_deallocation) 67 | { 68 | library_loader shared_library{ testing::get_demo_library_file_path() }; 69 | 70 | const auto handle = shared_library.get_handle(); 71 | 72 | shared_library.~library_loader(); 73 | 74 | // error is expected, because the handle is freed 75 | #ifdef _WIN32 76 | EXPECT_EQ(FreeLibrary(handle), 0); 77 | #elif defined(__unix__) || defined(__APPLE__) 78 | EXPECT_NE(dlclose(handle), 0); 79 | #endif 80 | } 81 | 82 | TEST(library_loader, exceptions) 83 | { 84 | // what() called on the exception should not be empty 85 | try 86 | { 87 | library_loader loader{ "./foo" }; 88 | } 89 | catch (const exceptions::library_load_failed & error) 90 | { 91 | EXPECT_NE(error.what(), ""); 92 | } 93 | catch (...) 94 | { 95 | FAIL() << "Correct exception not thrown"; 96 | } 97 | } 98 | 99 | TEST(library_loader, empty_library) 100 | { 101 | EXPECT_THROW(library_loader{ "./empty.dll" }, exceptions::library_load_failed); 102 | } 103 | } 104 | --------------------------------------------------------------------------------