SDL_error.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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. * \file SDL_error.h
  20. *
  21. * Simple error message routines for SDL.
  22. */
  23. #ifndef SDL_error_h_
  24. #define SDL_error_h_
  25. #include "SDL_stdinc.h"
  26. #include "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 -1, since SDL frequently uses -1 to signify an
  38. * 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 always -1.
  50. *
  51. * \sa SDL_ClearError
  52. * \sa SDL_GetError
  53. */
  54. extern DECLSPEC int SDLCALL SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(1);
  55. /**
  56. * Retrieve a message about the last error that occurred on the current
  57. * thread.
  58. *
  59. * It is possible for multiple errors to occur before calling SDL_GetError().
  60. * Only the last error is returned.
  61. *
  62. * The message is only applicable when an SDL function has signaled an error.
  63. * You must check the return values of SDL function calls to determine when to
  64. * appropriately call SDL_GetError(). You should _not_ use the results of
  65. * SDL_GetError() to decide if an error has occurred! Sometimes SDL will set
  66. * an error string even when reporting success.
  67. *
  68. * SDL will _not_ clear the error string for successful API calls. You _must_
  69. * check return values for failure cases before you can assume the error
  70. * string applies.
  71. *
  72. * Error strings are set per-thread, so an error set in a different thread
  73. * will not interfere with the current thread's operation.
  74. *
  75. * The returned string is internally allocated and must not be freed by the
  76. * application.
  77. *
  78. * \returns a message with information about the specific error that occurred,
  79. * or an empty string if there hasn't been an error message set since
  80. * the last call to SDL_ClearError(). The message is only applicable
  81. * when an SDL function has signaled an error. You must check the
  82. * return values of SDL function calls to determine when to
  83. * appropriately call SDL_GetError().
  84. *
  85. * \sa SDL_ClearError
  86. * \sa SDL_SetError
  87. */
  88. extern DECLSPEC const char *SDLCALL SDL_GetError(void);
  89. /**
  90. * Get the last error message that was set for the current thread.
  91. *
  92. * This allows the caller to copy the error string into a provided buffer, but
  93. * otherwise operates exactly the same as SDL_GetError().
  94. *
  95. * \param errstr A buffer to fill with the last error message that was set for
  96. * the current thread
  97. * \param maxlen The size of the buffer pointed to by the errstr parameter
  98. * \returns the pointer passed in as the `errstr` parameter.
  99. *
  100. * \sa SDL_GetError
  101. */
  102. extern DECLSPEC char * SDLCALL SDL_GetErrorMsg(char *errstr, int maxlen);
  103. /**
  104. * Clear any previous error message for this thread.
  105. *
  106. * \sa SDL_GetError
  107. * \sa SDL_SetError
  108. */
  109. extern DECLSPEC void SDLCALL SDL_ClearError(void);
  110. /**
  111. * \name Internal error functions
  112. *
  113. * \internal
  114. * Private error reporting function - used internally.
  115. */
  116. /* @{ */
  117. #define SDL_OutOfMemory() SDL_Error(SDL_ENOMEM)
  118. #define SDL_Unsupported() SDL_Error(SDL_UNSUPPORTED)
  119. #define SDL_InvalidParamError(param) SDL_SetError("Parameter '%s' is invalid", (param))
  120. typedef enum
  121. {
  122. SDL_ENOMEM,
  123. SDL_EFREAD,
  124. SDL_EFWRITE,
  125. SDL_EFSEEK,
  126. SDL_UNSUPPORTED,
  127. SDL_LASTERROR
  128. } SDL_errorcode;
  129. /* SDL_Error() unconditionally returns -1. */
  130. extern DECLSPEC int SDLCALL SDL_Error(SDL_errorcode code);
  131. /* @} *//* Internal error functions */
  132. /* Ends C function definitions when using C++ */
  133. #ifdef __cplusplus
  134. }
  135. #endif
  136. #include "close_code.h"
  137. #endif /* SDL_error_h_ */
  138. /* vi: set ts=4 sw=4 expandtab: */