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
157inline const char* GetError() { return SDL_GetError(); }
158
163class Error : public std::exception
164{
165 std::string m_message;
166
167public:
170 : m_message(SDL_GetError())
171 {
172 }
173
175 Error(std::string message)
176 : m_message(std::move(message))
177 {
178 }
179
181 constexpr const char* what() const noexcept { return m_message.c_str(); }
182
186 constexpr const std::string& str() const noexcept { return m_message; }
187};
188
197constexpr void CheckError(bool result)
198{
199 if (!result) throw Error();
200}
201
210template<class T>
211constexpr T CheckError(T result)
212{
213 if (!result) throw Error();
214 return result;
215}
216
227template<class T>
228constexpr T CheckError(T result, T invalidValue)
229{
230 if (result == invalidValue) throw Error();
231 return result;
232}
233
243template<class T>
244constexpr void CheckErrorIfNot(T result, T validValue)
245{
246 if (result != validValue) throw Error();
247}
248
261inline bool ClearError() { return SDL_ClearError(); }
262
263#ifdef SDL3PP_DOC
264
275#define SDL_Unsupported() SDL_SetError("That operation is not supported")
276
299#define SDL_InvalidParamError(param) \
300 SDL_SetError("Parameter '%s' is invalid", (param))
301
302#endif // SDL3PP_DOC
303
305
306} // namespace SDL
307
308#endif /* SDL3PP_ERROR_H_ */
An exception that returns GetError()
Definition: SDL3pp_error.h:164
constexpr const char * what() const noexcept
Returns the explanatory string.
Definition: SDL3pp_error.h:181
Error()
Default ctor.
Definition: SDL3pp_error.h:169
Error(std::string message)
Default ctor.
Definition: SDL3pp_error.h:175
constexpr const std::string & str() const noexcept
Returns the explanatory string.
Definition: SDL3pp_error.h:186
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:157
bool ClearError()
Clear any previous error message for this thread.
Definition: SDL3pp_error.h:261
constexpr void CheckErrorIfNot(T result, T validValue)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:244
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
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.