cpp-sdl2
C++ header-only SDL2 wrapper
exception.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <exception>
4 #include <stdexcept>
5 #include <string>
6 #include <string_view>
7 
8 #include <SDL_error.h>
9 
11 #ifdef CPP_SDL2_DISABLE_EXCEPTIONS
12 #include <iostream> //Instead of throwing, we are going to dump the function and error into stderr via std::cerr
13 #endif
14 
15 namespace sdl
16 {
18 #ifndef CPP_SDL2_DISABLE_EXCEPTIONS
19 
21 class Exception : public std::exception
22 {
23 public:
26  Exception(std::string const& function) : function_{function}, error_{SDL_GetError()}
27  {
28  SDL_ClearError();
29  }
30 
32  std::string function() { return function_; }
34  std::string error() { return error_; }
35 
36  const char* what() const noexcept override
37  {
38  if (what_.empty())
39  {
40  what_ = "function: ";
41  what_ += function_;
42  what_ += " SDL error: ";
43  what_ += error_;
44  }
45 
46  return what_.c_str();
47  }
48 
49 private:
51  std::string function_;
53  std::string error_;
55  mutable std::string what_;
56 };
57 
58 #else // Do not use exceptions
59 
60 // Stub the 'throw' keyword
61 #define throw
62 
64 class Exception
65 {
66 public:
68  Exception(std::string const& function)
69  {
70  std::cerr << "Error in : " << function << "\n";
71  const auto error = SDL_GetError();
72  std::cerr << "Error : " << error << "\n";
73  abort();
74  }
75 };
76 
77 #endif
78 
79 } // namespace sdl
std::string error_
storage for SDL_GetError() return string
Definition: exception.hpp:53
std::string error()
Return the error message generated by SDL_GetError() at event object construction.
Definition: exception.hpp:34
const char * what() const noexcept override
Definition: exception.hpp:36
std::string function_
storage for function name string
Definition: exception.hpp:51
Exception(std::string const &function)
Construct an exception object.
Definition: exception.hpp:26
Define to deactivate exception support.
Definition: color.hpp:7
std::string what_
storage for the "what()" string. string is dynamically built when calling what() const ...
Definition: exception.hpp:55
Define to deactivate exception support.
Definition: exception.hpp:21