SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_error.h
1#ifndef SDL3PP_ERROR_H_
2#define SDL3PP_ERROR_H_
3
4#include <exception>
5#include <format>
6#include <SDL3/SDL_error.h>
7#include "SDL3pp_strings.h"
8
9namespace SDL {
10
64inline bool SetErrorUnformatted(StringParam message)
65{
66 return SDL_SetError("%s", static_cast<const char*>(message));
67}
68
103template<class... ARGS>
104inline bool SetError(std::string_view fmt, ARGS... args)
105{
106 return SetError(
107 std::vformat(fmt, std::make_format_args(std::forward<ARGS>(args)...)));
108}
109
121inline bool OutOfMemory() { return SDL_OutOfMemory(); }
122
158inline const char* GetError() { return SDL_GetError(); }
159
164class Error : public std::exception
165{
166 std::string m_message;
167
168public:
171 : m_message(SDL_GetError())
172 {
173 }
174
176 Error(std::string message)
177 : m_message(std::move(message))
178 {
179 }
180
182 constexpr const char* what() const noexcept { return m_message.c_str(); }
183
187 constexpr const std::string& str() const noexcept { return m_message; }
188};
189
198constexpr void CheckError(bool result)
199{
200 if (!result) throw Error();
201}
202
211template<class T>
212constexpr T CheckError(T result)
213{
214 if (!result) throw Error();
215 return result;
216}
217
228template<class T>
229constexpr T CheckError(T result, T invalidValue)
230{
231 if (result == invalidValue) throw Error();
232 return result;
233}
234
244template<class T>
245constexpr void CheckErrorIfNot(T result, T validValue)
246{
247 if (result != validValue) throw Error();
248}
249
262inline bool ClearError() { return SDL_ClearError(); }
263
264#ifdef SDL3PP_DOC
265
276#define SDL_Unsupported() SDL_SetError("That operation is not supported")
277
300#define SDL_InvalidParamError(param) \
301 SDL_SetError("Parameter '%s' is invalid", (param))
302
303#endif // SDL3PP_DOC
304
306
307} // namespace SDL
308
309#endif /* SDL3PP_ERROR_H_ */
An exception that returns GetError()
Definition: SDL3pp_error.h:165
constexpr const char * what() const noexcept
Returns the explanatory string.
Definition: SDL3pp_error.h:182
Error()
Default ctor.
Definition: SDL3pp_error.h:170
Error(std::string message)
Default ctor.
Definition: SDL3pp_error.h:176
constexpr const std::string & str() const noexcept
Returns the explanatory string.
Definition: SDL3pp_error.h:187
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
bool SetErrorUnformatted(StringParam message)
Set the SDL error message for the current thread.
Definition: SDL3pp_error.h:64
bool OutOfMemory()
Set an error indicating that memory allocation failed.
Definition: SDL3pp_error.h:121
const char * GetError()
Retrieve a message about the last error that occurred on the current thread.
Definition: SDL3pp_error.h:158
bool ClearError()
Clear any previous error message for this thread.
Definition: SDL3pp_error.h:262
constexpr void CheckErrorIfNot(T result, T validValue)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:245
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:198
bool SetError(std::string_view fmt, ARGS... args)
Set the SDL error message for the current thread.
Definition: SDL3pp_error.h:104
Main include header for the SDL3pp library.