libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_exception.h
Go to the documentation of this file.
1/*! \file
2 * \brief GblException object-oriented error type
3 * \ingroup core
4 *
5 * This file contains the API for GblException, an extensible,
6 * object-oriented error type, similar to exceptions seen
7 * in languages such as C++, C#, and Java.
8 *
9 * Unlike GblError, which is minimalistic and lightweight,
10 * GblException provides more flexibility for error data
11 * at the price of being heavier. It's better suited for
12 * errors which need to carry additional data payloads
13 * beyond just an error code and a message.
14 *
15 * \todo
16 * - consider making it a GblEvent and using event
17 * propagation to implement error filtering?
18 * - consider supporting child errors which become
19 * the children of a rethrown parent error
20 *
21 * \author 2023 Falco Girgis
22 * \copyright MIT License
23 */
24#ifndef GIMBAL_EXCEPTION_H
25#define GIMBAL_EXCEPTION_H
26
27#include "../meta/instances/gimbal_object.h"
28#include <gimbal/core/gimbal_thread.h>
29
30/*! \name Type System
31 * \brief Type UUID and cast operators
32 * @{
33 */
34#define GBL_EXCEPTION_TYPE (GBL_TYPEID(GblException)) //!< Type UUID for GblException
35#define GBL_EXCEPTION(self) (GBL_CAST(GblException, self)) //!< Casts a GblInstance to GblException
36#define GBL_EXCEPTION_CLASS(klass) (GBL_CLASS_CAST(GblException, klass)) //!< Casts a GblClass to GblExceptionClass
37#define GBL_EXCEPTION_GET_CLASS(self) (GBL_CLASSOF(GblException, self)) //!< Retrieves a GblExceptionClass from a GblInstance
38//! @}
39
40/*! \name DSL Macros
41 * \brief Macros for managing exceptions and control flow
42 * \todo Implement
43 * @{
44 */
45#define GBL_TRY
46#define GBL_THROW(type, ...)
47#define GBL_CATCH
48#define GBL_CATCH_AS(type, name)
49//! @}
50
51#define GBL_SELF_TYPE GblException
52
54
55GBL_FORWARD_DECLARE_STRUCT(GblException);
56
57/*! \struct GblExceptionClass
58 * \extends GblObjectClass
59 * \brief GblClass VTable structure for GblException
60 *
61 * GblExceptionClass provides virtual methods for implementing
62 * a custom GblException type.
63 *
64 * \sa GblException
65 */
66GBL_CLASS_DERIVE(GblException, GblObject)
67 //! Virtual method returning the string representation of an error code
68 GBL_RESULT (*pFnResultString)(GBL_CSELF, GblEnum resultCode, const char** ppString);
69 //! A GblEnum type representing the exception's set of error codes
72
73/*! \struct GblException
74 * \extends GblObject
75 * \ingroup core
76 * \brief Object containing an application error and its context
77 *
78 * Instance structure encapsulating data for a single exception.
79 * It consists of a result code, an error message, source code
80 * context, and a pointer to the thread that generated it.
81 *
82 * \sa GblExceptionClass
83 */
85 GblType resultType; //!< Most derived type of the GblException::result code
86 GblEnum result; //!< Result status code representing the error
87 GblStringRef* pMessage; //!< Custom message associated with the error
88 GblThread* pThread; //!< Thread that generated the exception
89 const char* pFile; //!< Source file from which the exception was generated
90 const char* pFunction; //!< Function from which the exception was generated
91 size_t line; //!< Line of code form which the exception was generated
93
94//! \cond
95GBL_PROPERTIES(GblException,
96 (result, GBL_GENERIC, (READ, CONSTRUCT), GBL_ENUM_TYPE),
97 (thread, GBL_GENERIC, (READ, CONSTRUCT), GBL_THREAD_TYPE),
98 (message, GBL_GENERIC, (READ, WRITE ), GBL_STRING_TYPE),
99 (file, GBL_GENERIC, (READ, WRITE ), GBL_STRING_TYPE),
100 (function, GBL_GENERIC, (READ, WRITE ), GBL_STRING_TYPE),
101 (line, GBL_GENERIC, (READ, WRITE ), GBL_SIZE_TYPE)
102)
103//! \endcond
104
105//! Returns the GblType UUID corresponding to GblException
107
108/*! \name Static Methods
109 * \brief Static methods for managing a thread's active exception
110 * @{
111 */
112//! Returns a pointer to the current thread's pending GblException or NULL if there isn't one
114//! Clears the current thread's active GblException, returning GBL_TRUE if it had one
116//! Takes ownership of the current thread's active GblException if it matches \p type, otherwise returns NULL
118//! Sets the given GblException as the current thread's active exception, taking ownership of it
120//! @}
121
122/*! \name Instance Methods
123 * \brief Methods for creating and accessing GblExceptions
124 * \relatesalso GblException
125 * @{
126 */
127//! Creates a GblException of the \p derived type, with the given properties
129 const char* pFile,
130 const char* pFunc,
131 size_t line,
132 GblType resultType,
133 GblEnum result,
134 const char* pFmt,
135 ...) GBL_NOEXCEPT;
136//! Returns a new reference to an existing GblException, incrementing its refcount
138//! Decrements and returns the refcount of a GblException, potentially destroying it
140//! Returns the string representation of the GblException::result code
142//! Returns the enumeration type of the GblException::result code
144//! Returns GBL_TRUE if the given GblException has a source context associated with it
146//! Returns a non-NULL string containing the source file from which the GblException originated
148//! Returns a non-NULL string containing the function name from which the GblException originated
150//! Returns the source code line number from which the GblException originated
152//! @}
153
155
156//! \cond
157#define GblException_create(derived, result, /*resultType,*/ ...)
158 (GblException_create)(derived, GBL_SRC_FILE, GBL_SRC_FN, GBL_SRC_LN, result, __VA_ARGS__)
159//! \endcond
160
161#undef GBL_SELF_TYPE
162
163#endif // GIMBAL_EXCEPTION_H
#define GBL_CLASS_CAST(cType, klass)
#define GBL_NOEXCEPT
#define GBL_SRC_FN
Definition gimbal_ctx.h:34
#define GBL_SRC_LN
Definition gimbal_ctx.h:35
#define GBL_SRC_FILE
Definition gimbal_ctx.h:33
#define GBL_DECLS_BEGIN
#define GBL_FORWARD_DECLARE_STRUCT(S)
#define GBL_TYPEID(instanceStruct)
#define GBL_INSTANCE_DERIVE(derivedInstance, baseInstance)
#define GBL_CLASS_DERIVE(...)
#define GBL_INSTANCE_END
#define GBL_EXPORT
#define GBL_CLASS_END
#define GBL_ENUM_TYPE
Type UUID of GblEnumClass.
Definition gimbal_enum.h:22
GblException * GblException_current(void)
Returns a pointer to the current thread's pending GblException or NULL if there isn't one.
GblType GblException_type(void)
Returns the GblType UUID corresponding to GblException.
GblException * GblException_catch(GblType type)
Takes ownership of the current thread's active GblException if it matches type, otherwise returns NUL...
GblBool GblException_clear(void)
Clears the current thread's active GblException, returning GBL_TRUE if it had one.
void GblException_throw(GblException *pErr)
Sets the given GblException as the current thread's active exception, taking ownership of it.
#define GBL_CLASSOF(cType, self)
#define GBL_CAST(cType, self)
#define GBL_SIZE_TYPE
#define GBL_STRING_TYPE
Builtin ID for string GblVariant type.
#define GBL_PROPERTIES(object,...)
Declares a list of properties for the given object/instance structure.
#define GBL_THREAD_TYPE
Type UUID for GblThread.
uint32_t GblEnum
Standard-sized enum type, 32-bits across platforms.
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)
uint16_t GblRefCount
Type able to hold a reference counter across the codebase.
uintptr_t GblType
Meta Type UUID.
Definition gimbal_type.h:51
const char GblStringRef
Reference-counted, const char*-compatible string type.
GblType resultType
A GblEnum type representing the exception's set of error codes.
Object containing an application error and its context.
GblStringRef * pMessage
Custom message associated with the error.
size_t line
Line of code form which the exception was generated.
const char * pFunction
Function from which the exception was generated.
GblType GblException_resultType(const GblException *pSelf)
Returns the enumeration type of the GblException::result code.
GblType resultType
Most derived type of the GblException::result code.
GblException * GblException_create(GblType derived, const char *pFile, const char *pFunc, size_t line, GblType resultType, GblEnum result, const char *pFmt,...)
Creates a GblException of the derived type, with the given properties.
GblRefCount GblException_unref(GblException *pSelf)
Decrements and returns the refcount of a GblException, potentially destroying it.
GblThread * pThread
Thread that generated the exception.
const char * pFile
Source file from which the exception was generated.
const char * GblException_function(const GblException *pSelf)
Returns a non-NULL string containing the function name from which the GblException originated.
size_t GblException_line(const GblException *pSelf)
Returns the source code line number from which the GblException originated.
GblEnum result
Result status code representing the error.
const char * GblException_file(const GblException *pSelf)
Returns a non-NULL string containing the source file from which the GblException originated.
const char * GblException_resultString(const GblException *pSelf)
Returns the string representation of the GblException::result code.
GblBool GblException_hasSource(const GblException *pSelf)
Returns GBL_TRUE if the given GblException has a source context associated with it.
GblException * GblException_ref(const GblException *pSelf)
Returns a new reference to an existing GblException, incrementing its refcount.