Path: blob/master/thirdparty/sdl/include/SDL3/SDL_error.h
9912 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/2021/**22* # CategoryError23*24* Simple error message routines for SDL.25*26* Most apps will interface with these APIs in exactly one function: when27* almost any SDL function call reports failure, you can get a human-readable28* string of the problem from SDL_GetError().29*30* These strings are maintained per-thread, and apps are welcome to set their31* own errors, which is popular when building libraries on top of SDL for32* other apps to consume. These strings are set by calling SDL_SetError().33*34* A common usage pattern is to have a function that returns true for success35* and false for failure, and do this when something fails:36*37* ```c38* if (something_went_wrong) {39* return SDL_SetError("The thing broke in this specific way: %d", errcode);40* }41* ```42*43* It's also common to just return `false` in this case if the failing thing44* is known to call SDL_SetError(), so errors simply propagate through.45*/4647#ifndef SDL_error_h_48#define SDL_error_h_4950#include <SDL3/SDL_stdinc.h>5152#include <SDL3/SDL_begin_code.h>53/* Set up for C function definitions, even when using C++ */54#ifdef __cplusplus55extern "C" {56#endif5758/* Public functions */596061/**62* Set the SDL error message for the current thread.63*64* Calling this function will replace any previous error message that was set.65*66* This function always returns false, since SDL frequently uses false to67* signify a failing result, leading to this idiom:68*69* ```c70* if (error_code) {71* return SDL_SetError("This operation has failed: %d", error_code);72* }73* ```74*75* \param fmt a printf()-style message format string.76* \param ... additional parameters matching % tokens in the `fmt` string, if77* any.78* \returns false.79*80* \threadsafety It is safe to call this function from any thread.81*82* \since This function is available since SDL 3.2.0.83*84* \sa SDL_ClearError85* \sa SDL_GetError86* \sa SDL_SetErrorV87*/88extern SDL_DECLSPEC bool SDLCALL SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(1);8990/**91* Set the SDL error message for the current thread.92*93* Calling this function will replace any previous error message that was set.94*95* \param fmt a printf()-style message format string.96* \param ap a variable argument list.97* \returns false.98*99* \threadsafety It is safe to call this function from any thread.100*101* \since This function is available since SDL 3.2.0.102*103* \sa SDL_ClearError104* \sa SDL_GetError105* \sa SDL_SetError106*/107extern SDL_DECLSPEC bool SDLCALL SDL_SetErrorV(SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(1);108109/**110* Set an error indicating that memory allocation failed.111*112* This function does not do any memory allocation.113*114* \returns false.115*116* \threadsafety It is safe to call this function from any thread.117*118* \since This function is available since SDL 3.2.0.119*/120extern SDL_DECLSPEC bool SDLCALL SDL_OutOfMemory(void);121122/**123* Retrieve a message about the last error that occurred on the current124* thread.125*126* It is possible for multiple errors to occur before calling SDL_GetError().127* Only the last error is returned.128*129* The message is only applicable when an SDL function has signaled an error.130* You must check the return values of SDL function calls to determine when to131* appropriately call SDL_GetError(). You should *not* use the results of132* SDL_GetError() to decide if an error has occurred! Sometimes SDL will set133* an error string even when reporting success.134*135* SDL will *not* clear the error string for successful API calls. You *must*136* check return values for failure cases before you can assume the error137* string applies.138*139* Error strings are set per-thread, so an error set in a different thread140* will not interfere with the current thread's operation.141*142* The returned value is a thread-local string which will remain valid until143* the current thread's error string is changed. The caller should make a copy144* if the value is needed after the next SDL API call.145*146* \returns a message with information about the specific error that occurred,147* or an empty string if there hasn't been an error message set since148* the last call to SDL_ClearError().149*150* \threadsafety It is safe to call this function from any thread.151*152* \since This function is available since SDL 3.2.0.153*154* \sa SDL_ClearError155* \sa SDL_SetError156*/157extern SDL_DECLSPEC const char * SDLCALL SDL_GetError(void);158159/**160* Clear any previous error message for this thread.161*162* \returns true.163*164* \threadsafety It is safe to call this function from any thread.165*166* \since This function is available since SDL 3.2.0.167*168* \sa SDL_GetError169* \sa SDL_SetError170*/171extern SDL_DECLSPEC bool SDLCALL SDL_ClearError(void);172173/**174* \name Internal error functions175*176* \internal177* Private error reporting function - used internally.178*/179/* @{ */180181/**182* A macro to standardize error reporting on unsupported operations.183*184* This simply calls SDL_SetError() with a standardized error string, for185* convenience, consistency, and clarity.186*187* \threadsafety It is safe to call this macro from any thread.188*189* \since This macro is available since SDL 3.2.0.190*/191#define SDL_Unsupported() SDL_SetError("That operation is not supported")192193/**194* A macro to standardize error reporting on unsupported operations.195*196* This simply calls SDL_SetError() with a standardized error string, for197* convenience, consistency, and clarity.198*199* A common usage pattern inside SDL is this:200*201* ```c202* bool MyFunction(const char *str) {203* if (!str) {204* return SDL_InvalidParamError("str"); // returns false.205* }206* DoSomething(str);207* return true;208* }209* ```210*211* \threadsafety It is safe to call this macro from any thread.212*213* \since This macro is available since SDL 3.2.0.214*/215#define SDL_InvalidParamError(param) SDL_SetError("Parameter '%s' is invalid", (param))216217/* @} *//* Internal error functions */218219/* Ends C function definitions when using C++ */220#ifdef __cplusplus221}222#endif223#include <SDL3/SDL_close_code.h>224225#endif /* SDL_error_h_ */226227228