SDL_error.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * # CategoryError
  20. *
  21. * Simple error message routines for SDL.
  22. */
  23. #ifndef SDL_error_h_
  24. #define SDL_error_h_
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_begin_code.h>
  27. /* Set up for C function definitions, even when using C++ */
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /* Public functions */
  32. /**
  33. * Set the SDL error message for the current thread.
  34. *
  35. * Calling this function will replace any previous error message that was set.
  36. *
  37. * This function always returns false, since SDL frequently uses false
  38. * to signify a failing result, leading to this idiom:
  39. *
  40. * ```c
  41. * if (error_code) {
  42. * return SDL_SetError("This operation has failed: %d", error_code);
  43. * }
  44. * ```
  45. *
  46. * \param fmt a printf()-style message format string.
  47. * \param ... additional parameters matching % tokens in the `fmt` string, if
  48. * any.
  49. * \returns false.
  50. *
  51. * \since This function is available since SDL 3.0.0.
  52. *
  53. * \sa SDL_ClearError
  54. * \sa SDL_GetError
  55. */
  56. extern SDL_DECLSPEC bool SDLCALL SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(1);
  57. /**
  58. * Set an error indicating that memory allocation failed.
  59. *
  60. * This function does not do any memory allocation.
  61. *
  62. * \returns false.
  63. *
  64. * \since This function is available since SDL 3.0.0.
  65. */
  66. extern SDL_DECLSPEC bool SDLCALL SDL_OutOfMemory(void);
  67. /**
  68. * Retrieve a message about the last error that occurred on the current
  69. * thread.
  70. *
  71. * It is possible for multiple errors to occur before calling SDL_GetError().
  72. * Only the last error is returned.
  73. *
  74. * The message is only applicable when an SDL function has signaled an error.
  75. * You must check the return values of SDL function calls to determine when to
  76. * appropriately call SDL_GetError(). You should *not* use the results of
  77. * SDL_GetError() to decide if an error has occurred! Sometimes SDL will set
  78. * an error string even when reporting success.
  79. *
  80. * SDL will *not* clear the error string for successful API calls. You *must*
  81. * check return values for failure cases before you can assume the error
  82. * string applies.
  83. *
  84. * Error strings are set per-thread, so an error set in a different thread
  85. * will not interfere with the current thread's operation.
  86. *
  87. * The returned value is a thread-local string which will remain valid until
  88. * the current thread's error string is changed. The caller should make a copy
  89. * if the value is needed after the next SDL API call.
  90. *
  91. * \returns a message with information about the specific error that occurred,
  92. * or an empty string if there hasn't been an error message set since
  93. * the last call to SDL_ClearError().
  94. *
  95. * \since This function is available since SDL 3.0.0.
  96. *
  97. * \sa SDL_ClearError
  98. * \sa SDL_SetError
  99. */
  100. extern SDL_DECLSPEC const char * SDLCALL SDL_GetError(void);
  101. /**
  102. * Clear any previous error message for this thread.
  103. *
  104. * \returns true.
  105. *
  106. * \since This function is available since SDL 3.0.0.
  107. *
  108. * \sa SDL_GetError
  109. * \sa SDL_SetError
  110. */
  111. extern SDL_DECLSPEC bool SDLCALL SDL_ClearError(void);
  112. /**
  113. * \name Internal error functions
  114. *
  115. * \internal
  116. * Private error reporting function - used internally.
  117. */
  118. /* @{ */
  119. #define SDL_Unsupported() SDL_SetError("That operation is not supported")
  120. #define SDL_InvalidParamError(param) SDL_SetError("Parameter '%s' is invalid", (param))
  121. /* @} *//* Internal error functions */
  122. /* Ends C function definitions when using C++ */
  123. #ifdef __cplusplus
  124. }
  125. #endif
  126. #include <SDL3/SDL_close_code.h>
  127. #endif /* SDL_error_h_ */