libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_error.h
Go to the documentation of this file.
1/*! \file
2 * \brief GblError thread-local generic error code
3 * \ingroup core
4 *
5 * This file contains the public API for GblError, which is a structure
6 * used to represent a single pending error status for a single thread.
7 *
8 * \author 2023 Falco Girgis
9 * \copyright MIT License
10 */
11#ifndef GIMBAL_ERROR_H
12#define GIMBAL_ERROR_H
13
14#include "gimbal_thd.h"
15#include "../strings/gimbal_quark.h"
16
17#define GBL_ERROR_DOMAIN gblErrorDomain() //!< Builtin standard error domain
18#define GBL_ERROR_MESSAGE_SIZE 128 //!< Size of the GblError::message string
19
20#define GBL_SELF_TYPE GblError
21
23
24/*! Defines a GblError category
25 *
26 * GblErrorDomain is a user-definable structure which represents a
27 * grouping of error codes. It provides both a domain name and a method
28 * for converting an error code to its string representation.
29 *
30 * \sa GblError
31 */
32typedef struct GblErrorDomain {
33 GblQuark name; //!< Name of the error domain or category
34 const char* (*pFnCodeString)(GblEnum code); //!< Returns a string describing an error code
35} GblErrorDomain;
36
37/*! Low-level error structure
38 *
39 * GblError is a low-level error structure which represents a particular
40 * error code, a message, a source location, and a "domain" for the error,
41 * which is what is used to distinguish different categories or enum codes.
42 *
43 * GblError uses thread-local storage so that every thread has its own unique
44 * pending error, which can be assigned and manipulated independently.
45 *
46 * \sa GblErrorDomain
47 */
48typedef struct GblError {
49 char message[GBL_ERROR_MESSAGE_SIZE]; //!< Custom detailed error string
50 GblSourceLocation srcLocation; //!< C/C++ source code location
51 const GblErrorDomain* pDomain; //!< Pointer to the code's domain
52 GblEnum code; //!< Actual raw error code returned
53} GblError;
54
55/*! \name Accessors
56 * \brief Methods for accessing errors and their state
57 * @{
58 */
59//! Returns a pointer to the current thread's pending error or NULL
61//! Returns GblQuark of the name of the current thread's pending error domain
63//! Returns the stringified version of the current thread's pending error code
65
66/*! \name Management
67 * \brief Methods for managing pending errors
68 * @{
69 */
70//! Clears the current thread's pending error, returning GBL_TRUE if there was a pending error
72//! Equivalent to GblError_clear(), except the pending error is copied into the destination buffer
74//! Raises an error on the current thread with the given domain, code, and message
75GBL_EXPORT const GblError* GblError_raise (const char* pFile/*=NULL*/,
76 const char* pFunction/*=NULL*/,
77 size_t line/*=0*/,
78 const GblErrorDomain* pDomain,
79 GblEnum code,
80 const char* pFmt/*=NULL*/,
81 ...) GBL_NOEXCEPT;
82//! Equivalent to GblError_raise(), except the additional args are passed via a va_list
83GBL_EXPORT const GblError* GblError_raisev (const char* pFile/*=NULL*/,
84 const char* pFunction/*=NULL*/,
85 size_t line/*=0*/,
86 const GblErrorDomain* pDomain,
87 GblEnum code,
88 const char* pFmt,
89 va_list varArgs) GBL_NOEXCEPT;
90//! Reraises the current thread's pending error, updating its source context
91GBL_EXPORT const GblError* GblError_reraise (const char* pFile/*=NULL*/,
92 const char* pFunction/*=NULL*/,
93 size_t line/*=0*/) GBL_NOEXCEPT;
94//! @}
95
96//! Returns the builtin error domain used by GBL_RESULT
97GBL_EXPORT const GblErrorDomain* gblErrorDomain(void) GBL_NOEXCEPT;
98
100
101//! \cond
102#define GblError_raise(...)
103 GblError_raiseDefault_(__VA_ARGS__)
104#define GblError_raiseDefault_(...)
105 GblError_raiseDefault__(__FILE__, __FUNCTION__, __LINE__, __VA_ARGS__, GBL_NULL)
106#define GblError_raiseDefault__(file, func, line, domain, code, ...)
107 ((GblError_raise)(file, func, line, domain, code, __VA_ARGS__))
108
109#define GblError_reraise()
110 ((GblError_reraise)(__FILE__, __FUNCTION__, __LINE__))
111//! \endcond
112
113#undef GBL_SELF_TYPE
114
115#endif // GIMBAL_ERROR_H
#define GBL_NULL
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_EXPORT
const GblErrorDomain * gblErrorDomain(void)
Returns the builtin error domain used by GBL_RESULT.
GblBool GblError_clear(void)
Clears the current thread's pending error, returning GBL_TRUE if there was a pending error.
#define GBL_ERROR_MESSAGE_SIZE
Size of the GblError::message string.
const GblError * GblError_raise(const char *pFile, const char *pFunction, size_t line, const GblErrorDomain *pDomain, GblEnum code, const char *pFmt,...)
Raises an error on the current thread with the given domain, code, and message.
const char * GblError_string(void)
Returns the stringified version of the current thread's pending error code.
GblBool GblError_lower(GblError *pSelf)
Equivalent to GblError_clear(), except the pending error is copied into the destination buffer.
const GblError * GblError_pending(void)
Returns a pointer to the current thread's pending error or NULL.
const GblError * GblError_reraise(const char *pFile, const char *pFunction, size_t line)
Reraises the current thread's pending error, updating its source context.
const GblError * GblError_raisev(const char *pFile, const char *pFunction, size_t line, const GblErrorDomain *pDomain, GblEnum code, const char *pFmt, va_list varArgs)
Equivalent to GblError_raise(), except the additional args are passed via a va_list.
GblQuark GblError_domain(void)
Returns GblQuark of the name of the current thread's pending error domain.
uint32_t GblEnum
Standard-sized enum type, 32-bits across platforms.
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)
uintptr_t GblQuark
Uniquely identifiable interned string type.
Defines a GblError category.
GblQuark name
Name of the error domain or category.
const char *(* pFnCodeString)(GblEnum code)
Returns a string describing an error code.
Low-level error structure.
GblEnum code
Actual raw error code returned.
char message[128]
Custom detailed error string.
GblSourceLocation srcLocation
C/C++ source code location.
const GblErrorDomain * pDomain
Pointer to the code's domain.