From 9f8a72db9c604f8f13448ab241e7ece4d9f84a22 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Mon, 9 May 2016 03:58:54 +0300 Subject: [PATCH 1/2] Derive Exception from std::runtime_error instead of std::exception Apart from placing Exception into right place in exception class hierarchy this change allows more memory usage optimizations. Fixes #68 --- SDL2pp/Exception.cc | 19 +++++++++++-------- SDL2pp/Exception.hh | 16 +++++----------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/SDL2pp/Exception.cc b/SDL2pp/Exception.cc index f41e420..8386974 100644 --- a/SDL2pp/Exception.cc +++ b/SDL2pp/Exception.cc @@ -1,6 +1,6 @@ /* libSDL2pp - C++11 bindings/wrapper for SDL2 - Copyright (C) 2013-2015 Dmitry Marakasov + Copyright (C) 2013-2016 Dmitry Marakasov This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -25,19 +25,22 @@ namespace SDL2pp { +std::string Exception::make_what(const char* function, const char* sdl_error) { + std::string tmp(function); + tmp += " failed: "; + tmp += sdl_error; + return tmp; +} + Exception::Exception(const char* function) - : sdl_function_(function), - sdl_error_(SDL_GetError()), - what_(sdl_function_ + " failed: " + sdl_error_) { + : std::runtime_error(make_what(function, SDL_GetError())), + sdl_function_(function), + sdl_error_(SDL_GetError()) { } Exception::~Exception() noexcept { } -const char* Exception::what() const noexcept { - return what_.c_str(); -} - std::string Exception::GetSDLFunction() const { return sdl_function_; } diff --git a/SDL2pp/Exception.hh b/SDL2pp/Exception.hh index 7fb8a25..c0f72ba 100644 --- a/SDL2pp/Exception.hh +++ b/SDL2pp/Exception.hh @@ -1,6 +1,6 @@ /* libSDL2pp - C++11 bindings/wrapper for SDL2 - Copyright (C) 2013-2015 Dmitry Marakasov + Copyright (C) 2013-2016 Dmitry Marakasov This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -66,11 +66,13 @@ namespace SDL2pp { /// \endcode /// //////////////////////////////////////////////////////////// -class Exception : public std::exception { +class Exception : public std::runtime_error { private: std::string sdl_function_; ///< SDL function which caused an error std::string sdl_error_; ///< SDL error string - std::string what_; ///< User-readable message + +private: + static std::string make_what(const char* sdl_function, const char* sdl_error); public: //////////////////////////////////////////////////////////// @@ -93,14 +95,6 @@ public: //////////////////////////////////////////////////////////// virtual ~Exception() noexcept; - //////////////////////////////////////////////////////////// - /// \brief Get explanatory string - /// - /// \returns Explanatory string stored in the exception object - /// - //////////////////////////////////////////////////////////// - const char* what() const noexcept; - //////////////////////////////////////////////////////////// /// \brief Get name of SDL function which caused an error /// From 12980ab29c6e2947114621f667553adba053fc69 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Mon, 9 May 2016 04:55:49 +0300 Subject: [PATCH 2/2] Fix include --- SDL2pp/Exception.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SDL2pp/Exception.hh b/SDL2pp/Exception.hh index c0f72ba..356e109 100644 --- a/SDL2pp/Exception.hh +++ b/SDL2pp/Exception.hh @@ -23,7 +23,7 @@ #define SDL2PP_EXCEPTION_HH #include -#include +#include namespace SDL2pp {