|
| 1 | +{ |
| 2 | + This file is part of: |
| 3 | + |
| 4 | + SDL3 for Pascal |
| 5 | + (https://github.com/PascalGameDevelopment/SDL3-for-Pascal) |
| 6 | + SPDX-License-Identifier: Zlib |
| 7 | +} |
| 8 | + |
| 9 | +{* |
| 10 | + * # CategoryError |
| 11 | + * |
| 12 | + * Simple error message routines for SDL. |
| 13 | + * |
| 14 | + * Most apps will interface with these APIs in exactly one function: when |
| 15 | + * almost any SDL function call reports failure, you can get a human-readable |
| 16 | + * string of the problem from SDL_GetError(). |
| 17 | + * |
| 18 | + * These strings are maintained per-thread, and apps are welcome to set their |
| 19 | + * own errors, which is popular when building libraries on top of SDL for |
| 20 | + * other apps to consume. These strings are set by calling SDL_SetError(). |
| 21 | + * |
| 22 | + * A common usage pattern is to have a function that returns true for success |
| 23 | + * and false for failure, and do this when something fails: |
| 24 | + * |
| 25 | + * ```c |
| 26 | + * if (something_went_wrong) |
| 27 | + * return SDL_SetError("The thing broke in this specific way: %d", errcode); |
| 28 | + * |
| 29 | + * ``` |
| 30 | + * |
| 31 | + * It's also common to just return `false` in this case if the failing thing |
| 32 | + * is known to call SDL_SetError(), so errors simply propagate through. |
| 33 | + } |
| 34 | +
|
| 35 | +{* |
| 36 | + * Set the SDL error message for the current thread. |
| 37 | + * |
| 38 | + * Calling this function will replace any previous error message that was set. |
| 39 | + * |
| 40 | + * This function always returns false, since SDL frequently uses false to |
| 41 | + * signify a failing result, leading to this idiom: |
| 42 | + * |
| 43 | + * ```c |
| 44 | + * if (error_code) |
| 45 | + * return SDL_SetError("This operation has failed: %d", error_code); |
| 46 | + * |
| 47 | + * ``` |
| 48 | + * |
| 49 | + * \param fmt a printf()-style message format string. |
| 50 | + * \param ... additional parameters matching % tokens in the `fmt` string, if |
| 51 | + * any. |
| 52 | + * \returns false. |
| 53 | + * |
| 54 | + * \threadsafety It is safe to call this function from any thread. |
| 55 | + * |
| 56 | + * \since This function is available since SDL 3.1.3. |
| 57 | + * |
| 58 | + * \sa SDL_ClearError |
| 59 | + * \sa SDL_GetError |
| 60 | + * \sa SDL_SetErrorV |
| 61 | + } |
| 62 | +function SDL_SetError(fmt: PAnsiChar; Args: array of const): cbool; cdecl; overload; |
| 63 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SetError' {$ENDIF} {$ENDIF}; |
| 64 | +function SDL_SetError(fmt: PAnsiChar): cbool; cdecl; overload; |
| 65 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SetError' {$ENDIF} {$ENDIF}; |
| 66 | +
|
| 67 | +{* |
| 68 | + * Set the SDL error message for the current thread. |
| 69 | + * |
| 70 | + * Calling this function will replace any previous error message that was set. |
| 71 | + * |
| 72 | + * \param fmt a printf()-style message format string. |
| 73 | + * \param ap a variable argument list. |
| 74 | + * \returns false. |
| 75 | + * |
| 76 | + * \threadsafety It is safe to call this function from any thread. |
| 77 | + * |
| 78 | + * \since This function is available since SDL 3.2.0. |
| 79 | + * |
| 80 | + * \sa SDL_ClearError |
| 81 | + * \sa SDL_GetError |
| 82 | + * \sa SDL_SetError |
| 83 | + } |
| 84 | +{ #todo : SDL3-for-Pascal: How to handle va_list? } |
| 85 | +//function SDL_SetErrorV(fmt: PAnsiChar; ap: va_list): cbool; cdecl; |
| 86 | +// external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SetErrorV' {$ENDIF} {$ENDIF}; |
| 87 | +
|
| 88 | +{* |
| 89 | + * Set an error indicating that memory allocation failed. |
| 90 | + * |
| 91 | + * This function does not do any memory allocation. |
| 92 | + * |
| 93 | + * \returns false. |
| 94 | + * |
| 95 | + * \threadsafety It is safe to call this function from any thread. |
| 96 | + * |
| 97 | + * \since This function is available since SDL 3.1.3. |
| 98 | + } |
| 99 | +function SDL_OutOfMemory: cbool; cdecl; |
| 100 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_OutOfMemory' {$ENDIF} {$ENDIF}; |
| 101 | +
|
| 102 | +{* |
| 103 | + * Retrieve a message about the last error that occurred on the current |
| 104 | + * thread. |
| 105 | + * |
| 106 | + * It is possible for multiple errors to occur before calling SDL_GetError(). |
| 107 | + * Only the last error is returned. |
| 108 | + * |
| 109 | + * The message is only applicable when an SDL function has signaled an error. |
| 110 | + * You must check the return values of SDL function calls to determine when to |
| 111 | + * appropriately call SDL_GetError(). You should *not* use the results of |
| 112 | + * SDL_GetError() to decide if an error has occurred! Sometimes SDL will set |
| 113 | + * an error string even when reporting success. |
| 114 | + * |
| 115 | + * SDL will *not* clear the error string for successful API calls. You *must* |
| 116 | + * check return values for failure cases before you can assume the error |
| 117 | + * string applies. |
| 118 | + * |
| 119 | + * Error strings are set per-thread, so an error set in a different thread |
| 120 | + * will not interfere with the current thread's operation. |
| 121 | + * |
| 122 | + * The returned value is a thread-local string which will remain valid until |
| 123 | + * the current thread's error string is changed. The caller should make a copy |
| 124 | + * if the value is needed after the next SDL API call. |
| 125 | + * |
| 126 | + * \returns a message with information about the specific error that occurred, |
| 127 | + * or an empty string if there hasn't been an error message set since |
| 128 | + * the last call to SDL_ClearError(). |
| 129 | + * |
| 130 | + * \threadsafety It is safe to call this function from any thread. |
| 131 | + * |
| 132 | + * \since This function is available since SDL 3.1.3. |
| 133 | + * |
| 134 | + * \sa SDL_ClearError |
| 135 | + * \sa SDL_SetError |
| 136 | + } |
| 137 | +function SDL_GetError: PAnsiChar; cdecl; |
| 138 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetError' {$ENDIF} {$ENDIF}; |
| 139 | + |
| 140 | +{* |
| 141 | + * Clear any previous error message for this thread. |
| 142 | + * |
| 143 | + * \returns true. |
| 144 | + * |
| 145 | + * \threadsafety It is safe to call this function from any thread. |
| 146 | + * |
| 147 | + * \since This function is available since SDL 3.1.3. |
| 148 | + * |
| 149 | + * \sa SDL_GetError |
| 150 | + * \sa SDL_SetError |
| 151 | + } |
| 152 | +function SDL_ClearError: cbool; cdecl; |
| 153 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_ClearError' {$ENDIF} {$ENDIF}; |
| 154 | + |
0 commit comments