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 <string>
7#include <string_view>
8#include <SDL3/SDL_error.h>
9#include "SDL3pp_strings.h"
10
11namespace SDL {
12
66inline bool SetErrorUnformatted(StringParam message)
67{
68 return SDL_SetError("%s", static_cast<const char*>(message));
69}
70
105template<class... ARGS>
106inline bool SetError(std::string_view fmt, ARGS... args)
107{
108 return SetError(
109 std::vformat(fmt, std::make_format_args(std::forward<ARGS>(args)...)));
110}
111
123inline bool OutOfMemory() { return SDL_OutOfMemory(); }
124
160inline const char* GetError() { return SDL_GetError(); }
161
166class Error : public std::exception
167{
168 std::string m_message;
169
170public:
175 : m_message(SDL_GetError())
176 {
177 }
178
182 Error(std::string message)
183 : m_message(std::move(message))
184 {
185 }
186
190 constexpr const char* what() const noexcept { return m_message.c_str(); }
191
195 constexpr const std::string& str() const noexcept { return m_message; }
196};
197
206constexpr void CheckError(bool result)
207{
208 if (!result) throw Error();
209}
210
219template<class T>
220constexpr T CheckError(T result)
221{
222 if (!result) throw Error();
223 return result;
224}
225
236template<class T>
237constexpr T CheckError(T result, T invalidValue)
238{
239 if (result == invalidValue) throw Error();
240 return result;
241}
242
255inline bool ClearError() { return SDL_ClearError(); }
256
257#ifdef SDL3PP_DOC
258
269#define SDL_Unsupported() SDL_SetError("That operation is not supported")
270
293#define SDL_InvalidParamError(param) \
294 SDL_SetError("Parameter '%s' is invalid", (param))
295
296#endif // SDL3PP_DOC
297
300} // namespace SDL
301
302#endif /* SDL3PP_ERROR_H_ */
An exception that returns GetError()
Definition SDL3pp_error.h:167
constexpr const char * what() const noexcept
Returns the explanatory string.
Definition SDL3pp_error.h:190
Error()
Default ctor.
Definition SDL3pp_error.h:174
Error(std::string message)
Constructs from string.
Definition SDL3pp_error.h:182
constexpr const std::string & str() const noexcept
Returns the explanatory string.
Definition SDL3pp_error.h:195
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:66
bool OutOfMemory()
Set an error indicating that memory allocation failed.
Definition SDL3pp_error.h:123
const char * GetError()
Retrieve a message about the last error that occurred on the current thread.
Definition SDL3pp_error.h:160
bool ClearError()
Clear any previous error message for this thread.
Definition SDL3pp_error.h:255
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:206
bool SetError(std::string_view fmt, ARGS... args)
Set the SDL error message for the current thread.
Definition SDL3pp_error.h:106
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7