Baselib_ErrorState.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #include "Baselib_ErrorCode.h"
  3. #include "Baselib_SourceLocation.h"
  4. #include <assert.h>
  5. #ifdef __cplusplus
  6. BASELIB_C_INTERFACE
  7. {
  8. #endif
  9. // Native error code type.
  10. typedef enum Baselib_ErrorState_NativeErrorCodeType_t
  11. {
  12. // Native error code is not present.
  13. Baselib_ErrorState_NativeErrorCodeType_None = 0,
  14. // All platform error codes types must be bigger or equal to this value.
  15. Baselib_ErrorState_NativeErrorCodeType_PlatformDefined,
  16. } Baselib_ErrorState_NativeErrorCodeType_t;
  17. typedef uint8_t Baselib_ErrorState_NativeErrorCodeType;
  18. // Extra information type.
  19. typedef enum Baselib_ErrorState_ExtraInformationType_t
  20. {
  21. // Extra information is not present.
  22. Baselib_ErrorState_ExtraInformationType_None = 0,
  23. // Extra information is a pointer of const char* type.
  24. // Pointer guaranteed to be valid for lifetime of the program (static strings, buffers, etc).
  25. Baselib_ErrorState_ExtraInformationType_StaticString,
  26. // Extra information is a generation counter to ErrorState internal static buffer.
  27. Baselib_ErrorState_ExtraInformationType_GenerationCounter,
  28. } Baselib_ErrorState_ExtraInformationType_t;
  29. typedef uint8_t Baselib_ErrorState_ExtraInformationType;
  30. // Baselib error information.
  31. //
  32. // All functions that expect a pointer to a error state object will *not* allow to pass a nullptr for it
  33. // If an error state with code other than Success is passed, the function is guaranteed to early out.
  34. // Note that even if an error state is expected, there might be no full argument validation. For details check documentation of individual functions.
  35. typedef struct Baselib_ErrorState
  36. {
  37. Baselib_SourceLocation sourceLocation;
  38. uint64_t nativeErrorCode;
  39. uint64_t extraInformation;
  40. Baselib_ErrorCode code;
  41. Baselib_ErrorState_NativeErrorCodeType nativeErrorCodeType;
  42. Baselib_ErrorState_ExtraInformationType extraInformationType;
  43. } Baselib_ErrorState;
  44. // Creates a new error state object that is initialized to Baselib_ErrorCode_Success.
  45. static inline Baselib_ErrorState Baselib_ErrorState_Create(void)
  46. {
  47. Baselib_ErrorState errorState = {
  48. { NULL, NULL, 0 },
  49. 0,
  50. 0,
  51. Baselib_ErrorCode_Success,
  52. Baselib_ErrorState_NativeErrorCodeType_None,
  53. Baselib_ErrorState_ExtraInformationType_None
  54. };
  55. return errorState;
  56. }
  57. // Resets an existing error state to success and passes it on. Passes nullptr directly on.
  58. static inline Baselib_ErrorState* Baselib_ErrorState_Reset(Baselib_ErrorState* errorState)
  59. {
  60. if (errorState)
  61. errorState->code = Baselib_ErrorCode_Success;
  62. return errorState;
  63. }
  64. static inline bool Baselib_ErrorState_ErrorRaised(const Baselib_ErrorState* errorState)
  65. {
  66. BaselibAssert(errorState);
  67. return errorState->code != Baselib_ErrorCode_Success;
  68. }
  69. static inline void Baselib_ErrorState_RaiseError(
  70. Baselib_ErrorState* errorState,
  71. Baselib_ErrorCode errorCode,
  72. Baselib_ErrorState_NativeErrorCodeType nativeErrorCodeType,
  73. uint64_t nativeErrorCode,
  74. Baselib_ErrorState_ExtraInformationType extraInformationType,
  75. uint64_t extraInformation,
  76. Baselib_SourceLocation sourceLocation
  77. )
  78. {
  79. if (!errorState)
  80. return;
  81. if (errorState->code != Baselib_ErrorCode_Success)
  82. return;
  83. errorState->sourceLocation = sourceLocation;
  84. errorState->nativeErrorCode = nativeErrorCode;
  85. errorState->extraInformation = extraInformation;
  86. errorState->code = errorCode;
  87. errorState->nativeErrorCodeType = nativeErrorCodeType;
  88. errorState->extraInformationType = extraInformationType;
  89. }
  90. typedef enum Baselib_ErrorState_ExplainVerbosity
  91. {
  92. // Include error type with platform specific value (if specified).
  93. Baselib_ErrorState_ExplainVerbosity_ErrorType = 0,
  94. // Include error type with platform specific value (if specified),
  95. // source location (subject to BASELIB_ENABLE_SOURCELOCATION define) and an error explanation if available.
  96. Baselib_ErrorState_ExplainVerbosity_ErrorType_SourceLocation_Explanation = 1,
  97. } Baselib_ErrorState_ExplainVerbosity;
  98. BASELIB_ENUM_ENSURE_ABI_COMPATIBILITY(Baselib_ErrorState_ExplainVerbosity);
  99. // Writes a null terminated string containing native error code value and explanation if possible.
  100. //
  101. // \param errorState Error state to explain. If null an empty string will be written into buffer.
  102. // \param buffer Buffer to write explanation into.
  103. // If nullptr is passed, nothing will be written but function will still return correct amount of bytes.
  104. // \param bufferLen Length of buffer in bytes.
  105. // If 0 is passed, behaviour is the same as passing nullptr as buffer.
  106. // \param verbosity Verbosity level of the explanation string.
  107. //
  108. // \returns the number of characters that would have been written if buffer had been sufficiently large, including the terminating null character.
  109. BASELIB_API uint32_t Baselib_ErrorState_Explain(
  110. const Baselib_ErrorState* errorState,
  111. char buffer[],
  112. uint32_t bufferLen,
  113. Baselib_ErrorState_ExplainVerbosity verbosity
  114. );
  115. #include <C/Baselib_ErrorState.inl.h>
  116. #ifdef __cplusplus
  117. } // BASELIB_C_INTERFACE
  118. #endif