libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_opaque.h
Go to the documentation of this file.
1/*! \file
2 * \brief GblOpaqueClass and API for managing Opaque types
3 * \ingroup meta
4 *
5 * This file contains the API for registering and managing the lifetime of "opaque"
6 * user data types. This is an intermediate libGimbal type, which inherits from
7 * GBL_POINTER_TYPE, adding additional functionality. Rather than simple pointer
8 * assignments for copy semantics with no lifetime management, a type which derives
9 * from GBL_OPAQUE_TYPE adds the bare minimum virtual functions required to implement
10 * lifetime management.
11 *
12 * When assigning the value of a GBL_OPAQUE_TYPE to another, the "copy" virtual method
13 * is called, which may implement value-based copying via something like memcpy(), or
14 * reference-based copying via something like incrementing a reference counter.
15 *
16 * When the GBL_OPAQUE_TYPE is destroyed, the "free" virtual method is called, which
17 * may opt to actually deallocate the object for value-based copied objects OR it may
18 * simply decrement a reference counter for reference-based semantics.
19 *
20 * \author 2023, 2025 Falco Girgis
21 * \copyright MIT License
22 */
23#ifndef GIMBAL_OPAQUE_H
24#define GIMBAL_OPAQUE_H
25
27
28/*! \name Type System
29 * \brief Type UUID and cast macros
30 * @{
31 */
32#define GBL_OPAQUE_TYPE (GBL_TYPEID(GblOpaque)) //!< Type UUID for GblOpaque
33#define GBL_OPAQUE_CLASS(klass) (GBL_CLASS_CAST(GblOpaque, klass)) //!< Function-style GblOpaqueClass cast from GblClass
34//! @}
35
37
38//! Function signature for for an opaque copy operation, see \ref GblOpaqueVTable::pFnCopy
39typedef GBL_RESULT (*GblOpaqueCopyFn)(void* pOpaque, void** ppNewOpaque);
40//! Function signature for an opaque free operation, see \ref GblOpaqueVTable::pFnFree
41typedef GBL_RESULT (*GblOpaqueFreeFn)(void* pOpaque);
42
43//! Virtual table structure for a GblOpaqueClass
44typedef struct GblOpaqueVTable {
45 GblOpaqueCopyFn pFnCopy; //!< Copy method
46 GblOpaqueFreeFn pFnFree; //!< Free method
47} GblOpaqueVTable;
48
49/*! \struct GblOpaqueClass
50 * \extends GblPrimitiveClass
51 * \brief GblClass structure for opaque types
52 *
53 * Simply contains a virtual table pointer to the implementation details.
54 */
55GBL_CLASS_DERIVE(GblOpaque, GblPrimitive)
56 const GblOpaqueVTable* pVTable; //!< Pointer to the virtual table structure
58
59/*! \name Static Methods
60 * \brief Miscellaenous and Utility methods
61 * @{
62 */
63//! Returns the GblType UUID for GblOpaque.
65//! Registers a new opaque subtype with the virtual table given by \p pVTable.
67 const GblOpaqueVTable* pVTable) GBL_NOEXCEPT;
68//! Convenience function which registers a new type meta type which is used with GblRef to manage its lifetime.
70//! @}
71
72/*! \name Instance Methods
73 * \brief Methods for dealing with opaque instances (usually handled internally)
74 * @{
75 */
76//! Copies an opaque structure with the given type into \p ppNewOpaque, using its vtable
77GBL_EXPORT GBL_RESULT GblOpaque_copy (void* pOpaque,
78 GblType type,
79 void** ppNewOpaque) GBL_NOEXCEPT;
80//! Frees an opaque structure with the given type using its vtable
81GBL_EXPORT GBL_RESULT GblOpaque_free (void* pOpaque,
83//! @}
84
86
87#endif // GIMBAL_Opaque_H
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_TYPEID(instanceStruct)
#define GBL_CLASS_DERIVE(...)
#define GBL_EXPORT
#define GBL_CLASS_END
GblType GblOpaque_registerRef(const char *pName)
Convenience function which registers a new type meta type which is used with GblRef to manage its lif...
GBL_RESULT GblOpaque_free(void *pOpaque, GblType type)
Frees an opaque structure with the given type using its vtable.
GBL_RESULT GblOpaque_copy(void *pOpaque, GblType type, void **ppNewOpaque)
Copies an opaque structure with the given type into ppNewOpaque, using its vtable.
GblType GblOpaque_register(const char *pName, const GblOpaqueVTable *pVTable)
Registers a new opaque subtype with the virtual table given by pVTable.
GblType GblOpaque_type(void)
Returns the GblType UUID for GblOpaque.
uintptr_t GblType
Meta Type UUID.
Definition gimbal_type.h:52
const GblOpaqueVTable * pVTable
Pointer to the virtual table structure.
Virtual table structure for a GblOpaqueClass.
GblOpaqueCopyFn pFnCopy
Copy method.
GblOpaqueFreeFn pFnFree
Free method.