Skip to content

Commit

Permalink
feat: DyLib 1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-olivier committed Jun 24, 2021
1 parent 34cb021 commit 26f3970
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
48 changes: 23 additions & 25 deletions DyLib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* \file DyLib.hpp
* \brief Cross-platform Dynamic Library Loader
* \author Martin Olivier
* \version 1.4.2
* \version 1.5.0
*
* MIT License
* Copyright (c) 2021 Martin Olivier
Expand Down Expand Up @@ -58,14 +58,12 @@ class DyLib
static constexpr auto extension = ".dll";
#elif defined(__APPLE__)
static constexpr auto extension = ".dylib";
#elif defined(__unix__)
static constexpr auto extension = ".so";
#else
#error "DyLib : Unknown OS"
static constexpr auto extension = ".so";
#endif

/**
* This exception is thrown when the DyLib library encountered an error.
* This exception is thrown when the DyLib class encountered an error.
*
* @return error message by calling what() member function
*/
Expand All @@ -74,8 +72,8 @@ class DyLib
protected:
const std::string m_error;
public:
explicit exception(std::string &&message) : m_error(std::move(message)) {};
const char *what() const noexcept override {return m_error.c_str();};
explicit exception(std::string &&message) : m_error(std::move(message)) {}
const char *what() const noexcept override {return m_error.c_str();}
};

/**
Expand All @@ -87,7 +85,7 @@ class DyLib
class handle_error : public exception
{
public:
explicit handle_error(std::string &&message) : exception(std::move(message)) {};
explicit handle_error(std::string &&message) : exception(std::move(message)) {}
};

/**
Expand All @@ -99,7 +97,7 @@ class DyLib
class symbol_error : public exception
{
public:
explicit symbol_error(std::string &&message) : exception(std::move(message)) {};
explicit symbol_error(std::string &&message) : exception(std::move(message)) {}
};

/**
Expand Down Expand Up @@ -209,14 +207,14 @@ class DyLib
/**
* Get a function from the dynamic library currently loaded in the object
*
* @param template_Type the template argument must be the function prototype
* @param T the template argument must be the function prototype.
* it must be the same pattern as the template of std::function
* @param name symbol name of the function to get from the dynamic library
*
* @returns std::function<Type> that contains the function
* @returns std::function<T> that contains the function
*/
template<typename Type>
std::function<Type> getFunction(const char *name) const
template<typename T>
std::function<T> getFunction(const char *name) const
{
if (!m_handle)
throw handle_error("Error : no dynamic library loaded");
Expand All @@ -225,25 +223,25 @@ class DyLib
auto sym = getSymbol(name);
if (!sym)
throw symbol_error("Error while loading function : " + std::string(name));
return reinterpret_cast<Type *>(sym);
return reinterpret_cast<T *>(sym);
}

template<typename Type>
std::function<Type> getFunction(const std::string &name) const
template<typename T>
std::function<T> getFunction(const std::string &name) const
{
return getFunction<Type>(name.c_str());
return getFunction<T>(name.c_str());
}

/**
* Get a global variable from the dynamic library currently loaded in the object
*
* @param template_Type type of the global variable
* @param T type of the global variable
* @param name name of the global variable to get from the dynamic library
*
* @returns global variable of type <Type>
* @returns global variable of type <T>
*/
template<typename Type>
Type getVariable(const char *name) const
template<typename T>
T getVariable(const char *name) const
{
if (!m_handle)
throw handle_error("Error : no dynamic library loaded");
Expand All @@ -252,13 +250,13 @@ class DyLib
auto sym = getSymbol(name);
if (!sym)
throw symbol_error("Error while loading global variable : " + std::string(name));
return *reinterpret_cast<Type *>(sym);
return *reinterpret_cast<T *>(sym);
}

template<typename Type>
Type getVariable(const std::string &name) const
template<typename T>
T getVariable(const std::string &name) const
{
return getVariable<Type>(name.c_str());
return getVariable<T>(name.c_str());
}

/**
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# DyLib - Dynamic Library Loader for C++
[![DyLib](https://img.shields.io/badge/DyLib-v1.4.2-blue.svg)](https://github.com/tocola/DyLib/releases/tag/v1.4.2)
[![DyLib](https://img.shields.io/badge/DyLib-v1.5.0-blue.svg)](https://github.com/tocola/DyLib/releases/tag/v1.5.0)
[![MIT license](https://img.shields.io/badge/License-MIT-orange.svg)](https://github.com/tocola/DyLib/blob/main/LICENSE)
[![CPP Version](https://img.shields.io/badge/C++-11/14/17/20-darkgreen.svg)](https://isocpp.org/)

Expand All @@ -10,7 +10,7 @@
[![workflow](https://github.com/tocola/DyLib/actions/workflows/unit_tests.yml/badge.svg)](https://github.com/tocola/DyLib/actions/workflows/unit_tests.yml)
[![codecov](https://codecov.io/gh/tocola/DyLib/branch/main/graph/badge.svg?token=4V6A9B7PII)](https://codecov.io/gh/tocola/DyLib)

[![GitHub download](https://img.shields.io/github/downloads/tocola/DyLib/total?style=for-the-badge)](https://github.com/tocola/DyLib/releases/download/v1.4.2/DyLib.hpp)
[![GitHub download](https://img.shields.io/github/downloads/tocola/DyLib/total?style=for-the-badge)](https://github.com/tocola/DyLib/releases/download/v1.5.0/DyLib.hpp)

The goal of this C++ Library is to load dynamic libraries (.so, .dll, .dylib) and access its functions and global variables at runtime.

Expand All @@ -19,7 +19,7 @@ Works on `Linux`, `Windows`, `MacOS`

# Installation

Click [HERE](https://github.com/tocola/DyLib/releases/download/v1.4.2/DyLib.hpp) to download the DyLib header file
Click [HERE](https://github.com/tocola/DyLib/releases/download/v1.5.0/DyLib.hpp) to download the DyLib header file
`Don't forget to leave a star 🌟`

# Documentation
Expand Down

0 comments on commit 26f3970

Please sign in to comment.