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
38
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(std::vformat(fmt, std::make_format_args(args...)));
107}
108
120inline bool OutOfMemory() { return SDL_OutOfMemory(); }
121
156inline const char* GetError() { return SDL_GetError(); }
157
162class Error : public std::exception
163{
164 std::string m_message;
165
166public:
169 : m_message(SDL_GetError())
170 {
171 }
172
174 Error(std::string message)
175 : m_message(std::move(message))
176 {
177 }
178
180 constexpr const char* what() const noexcept final
181 {
182 return m_message.c_str();
183 }
184
188 constexpr const std::string& str() const noexcept { return m_message; }
189};
190
199constexpr void CheckError(bool result)
200{
201 if (!result) throw Error();
202}
203
212template<class T>
213constexpr T CheckError(T result)
214{
215 if (!result) throw Error();
216 return result;
217}
218
229template<class T>
230constexpr T CheckError(T result, T invalidValue)
231{
232 if (result == invalidValue) throw Error();
233 return result;
234}
235
245template<class T>
246constexpr void CheckErrorIfNot(T result, T validValue)
247{
248 if (result != validValue) throw Error();
249}
250
263inline bool ClearError() { return SDL_ClearError(); }
264
265#ifdef SDL3PP_DOC
266
277#define SDL_Unsupported() SDL_SetError("That operation is not supported")
278
301#define SDL_InvalidParamError(param) \
302 SDL_SetError("Parameter '%s' is invalid", (param))
303
304#endif // SDL3PP_DOC
305
307
308} // namespace SDL
309
310#endif /* SDL3PP_ERROR_H_ */
An exception that returns GetError().
Definition SDL3pp_error.h:163
constexpr const char * what() const noexcept final
Returns the explanatory string.
Definition SDL3pp_error.h:180
Error()
Default ctor.
Definition SDL3pp_error.h:168
Error(std::string message)
Default ctor.
Definition SDL3pp_error.h:174
constexpr const std::string & str() const noexcept
Returns the explanatory string.
Definition SDL3pp_error.h:188
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:120
const char * GetError()
Retrieve a message about the last error that occurred on the current thread.
Definition SDL3pp_error.h:156
bool ClearError()
Clear any previous error message for this thread.
Definition SDL3pp_error.h:263
constexpr void CheckErrorIfNot(T result, T validValue)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:246
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
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.