SDL_error.h 4.8 KB

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