Skip to content

Commit

Permalink
[ADD] DyLib global exception && [ADD] const keyword when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-olivier committed Apr 5, 2021
1 parent d367cc8 commit 5308e86
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
30 changes: 20 additions & 10 deletions DyLib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,28 @@ class DyLib
void *m_handle{nullptr};
#endif
public:
class handle_error : public std::runtime_error
class exception : public std::exception
{
protected:
const std::string m_error;
public:
handle_error(const char* message) : std::runtime_error(message) {};
handle_error(const std::string &message) : std::runtime_error(message) {};
exception(const char* message) : m_error(message) {}
exception(const std::string &message) : m_error(message) {};
const char *what() const noexcept override {return m_error.c_str();};
};

class symbol_error : public std::runtime_error
class handle_error : public exception
{
public:
symbol_error(const char* message) : std::runtime_error(message) {};
symbol_error(const std::string &message) : std::runtime_error(message) {};
handle_error(const char* message) : exception(message) {};
handle_error(const std::string &message) : exception(message) {};
};

class symbol_error : public exception
{
public:
symbol_error(const char* message) : exception(message) {};
symbol_error(const std::string &message) : exception(message) {};
};

/** Creates a Dynamic Library object.
Expand Down Expand Up @@ -107,7 +117,7 @@ class DyLib
* @returns std::function<Type> that contains the function
*/
template<typename Type>
std::function<Type> getFunction(const char *name)
std::function<Type> getFunction(const char *name) const
{
#ifdef _WIN32
if (!m_handle)
Expand All @@ -126,7 +136,7 @@ class DyLib
}

template<typename Type>
std::function<Type> getFunction(const std::string &name)
std::function<Type> getFunction(const std::string &name) const
{
return getFunction<Type>(name.c_str());
}
Expand All @@ -140,7 +150,7 @@ class DyLib
* @returns global variable of type <Type>
*/
template<class Type>
Type getVariable(const char *name)
Type getVariable(const char *name) const
{
#ifdef _WIN32
if (!m_handle)
Expand All @@ -159,7 +169,7 @@ class DyLib
}

template<class Type>
Type getVariable(const std::string &name)
Type getVariable(const std::string &name) const
{
return getVariable<Type>(name.c_str());
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int main(int ac, char **av)
if (ptr == nullptr)
std::cout << "nullptr" << std::endl;
}
catch (const std::runtime_error &e) {
catch (const DyLib::exception &e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
Expand Down

0 comments on commit 5308e86

Please sign in to comment.