libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_box.h
Go to the documentation of this file.
1/*! \file
2 * \brief GblBox (reference-counted, opaque userdata), and related functions
3 * \ingroup metaBuiltinTypes
4 *
5 * GblBox represents the most minimal type within the type
6 * system that is still runtime extensible and still offers
7 * everything typically required for an object to have
8 * bindings to another language. A GblBox extends GblInstance with:
9 *
10 * * Reference counting/shared pointer semantics
11 * * Arbitrary userdata storage corresponding destructors
12 *
13 * \author 2023 Falco Girgis
14 * \copyright MIT License
15 */
16#ifndef GIMBAL_BOX_H
17#define GIMBAL_BOX_H
18
20#include "../ifaces/gimbal_ivariant.h"
21#include "../../containers/gimbal_array_map.h"
22
23/*! \name Type System
24 * \brief Type UUID and Cast operators
25 * @{
26 */
27#define GBL_BOX_TYPE (GBL_TYPEID(GblBox)) //!< GblType UUID of a GblBox
28#define GBL_BOX(self) (GBL_CAST(GblBox, self)) //!< Casts a GblInstance to GblBox
29#define GBL_BOX_CLASS(klass) (GBL_CLASS_CAST(GblBox, klass)) //!< Casts a GblClass to GblBoxClass
30#define GBL_BOX_GET_CLASS(self) (GBL_CLASSOF(GblBox, self)) //!< Gets a GblBoxClass from a GblInstance
31//! @}
32
33#define GBL_REF(box) (GblBox_ref(GBL_BOX(box))) //!< Auto-casting convenience macro around GblBox_ref()
34#define GBL_UNREF(box) (GblBox_unref(GBL_AS(GblBox, box))) //!< Auto-casting convenience macro around GblBox_unref()
35
36#define GBL_SELF_TYPE GblBox
37
39
41
42/*! \struct GblBoxClass
43 * \extends GblClass
44 * \implements GblIVariantClass
45 * \ingroup metaBuiltinTypes
46 * \brief GblClass VTable/static data structure for GblBox
47 *
48 * GblBoxClass extends GblClass and implements the GblIVariant
49 * interface for GblVariant container support. It also provides
50 * arbitrary userdata storage at the class-level.
51 *
52 * \sa GblBox
53 */
54GBL_CLASS_BASE(GblBox, GblIVariant)
56 GblArrayMap* pFields; //!< PRIVATE: Internal storage for userdata fields
58
59 //! Virtual method invoked when a GblBox is being destroyed
60 GBL_RESULT (*pFnDestructor)(GBL_SELF);
62
63/*! \struct GblBox
64 * \extends GblInstance
65 * \implements GblIVariant
66 * \ingroup metaBuiltinTypes
67 * \brief Minimally bindable GblInstance with reference semantics and opaque userdata
68 *
69 * A GblBox is a GblInstance-derived type which simply adds the bare minimal set of
70 * functionality that is typically required for language bindings and interop. This
71 * includes:
72 * - destructors
73 * - reference counting semantics
74 * - a dictionary for storing arbitrary associated userdata
75 * - interop with GblVariant types
76 * - arbitrary flag bits
77 *
78 * \note
79 * A GblBox is 12 or 20 bytes total (32 or 64 bit respectively).
80 *
81 * \sa GblBoxClass
82 */
83GBL_INSTANCE_BASE(GblBox) // Size (32/64 bit)
84 GBL_PRIVATE_BEGIN // 12/20 Bytes Total
85 GblArrayMap* pFields; //!< PRIVATE: Storage for extended userdata fields
86 volatile uint16_t refCounter; //!< PRIVATE: Atomic reference counter
87 uint16_t contextType : 1; //!< PRIVATE: GblContext type flag \deprecated
88 uint16_t constructedInPlace : 1; //!< PRIVATE: Flag for whether memory is deallocated upon destruction
89 uint16_t derivedFlags : 14; //!< PRIVATE: Extra flags for use in derived classes
92
93/*! \name Floating Classes
94 * \brief Methods for managing floating classes
95 * @{
96 */
97//! Creates an extended floating class, setting its properties to the given values. Has default values.
99 size_t size /*=DEFAULT*/,
100 void* pUserdata/*=NULL*/,
101 GblArrayMapDtorFn pFnUdDtor/*=NULL*/) GBL_NOEXCEPT;
102//! Constructs an extended floating class in-place, setting its properties to the given values. Has default values.
104 GblType derivedType,
105 void* pUserdata /*=NULL*/,
106 GblArrayMapDtorFn pFnUdDtor /*=NULL*/) GBL_NOEXCEPT;
107//! @}
108
109/*! \name Userdata
110 * \brief Methods for setting and destroying userdata
111 * \relatesalso GblBoxClass
112 * @{
113 */
114//! Returns the void* userdata pointer that has been associated with the given GblBoxClass
116//! Sets the void* userdata pointer, associating it with the given GblBoxClass
118//! Adds an extra destructor callback to the field list, passing the given class back as the destructed value
120 GblArrayMapDtorFn pFnUdDtor) GBL_NOEXCEPT;
121//! @}
122
123/*! \name Fields
124 * \brief Methods for managing extended userdata fields
125 * \relatesalso GblBoxClass
126 * @{
127 */
128//! Returns the generic userdata field value associated with the given key on the class, or 0 if there isn't one
130//! Extracts the generic userdata field value associated with the given key on the class, without destroying it
132//! Destroys the generic userdata field value associated with the given key on the class, returning GBL_TRUE upon success
134//! Returns GBL_TRUE if the given class has generic userdata field value associated with the given key
136//! Sets the generic userdata value and its optional destructor for the given key on the class, destroying the previous value if there was one
138 GblQuark key,
139 uintptr_t ud,
140 GblArrayMapDtorFn pFnDtor/*=NULL*/) GBL_NOEXCEPT;
141//! @}
142
143//! Returns the GblType UUID associated with GblBox
145
146/*! \name Constructors
147 * \brief Overloaded constructors for GblBox
148 * @{
149 */
150//! Creates a GblBox instance of the derived type and returns a pointer to it. Has default arguments.
152 size_t size /*=DEFAULT*/,
153 void* pUserdata/*=NULL*/,
154 GblArrayMapDtorFn pFnUdDtor/*=NULL*/,
155 GblBoxClass* pClass /*=NULL*/) GBL_NOEXCEPT;
156//! Constructs a GblBox instance of the derived type in-place, returning a result status code
158 GblType derived,
159 void* pUserdata/*=NULL*/,
160 GblArrayMapDtorFn pFnUdDtor/*=NULL*/,
161 GblBoxClass* pClass /*=NULL*/) GBL_NOEXCEPT;
162//! @}
163
164/*! \name Reference Counting
165 * \brief Methods for managing shared reference lifetime
166 * \relatesalso GblBox
167 * @{
168 */
169//! Increments the given GblBox's reference count by 1, returning a pointer to it
171//! Decrements the given GblBox's reference count by 1, destructing it when it hits 0
173//! Returns the number of active references held to the given GblBox
175//! @}
176
177/*! \name Userdata
178 * \brief Methods for managing and destroying userdata
179 * \relatesalso GblBox
180 * @{
181 */
182//! Returns the userdata pointer stored within the given GblBox
184//! Stores the untyped userdata pointer within the GblBox
186//! Sets an additional user destructor to be invoked with the GblBox passed back to it when its being destructed
188 GblArrayMapDtorFn pFnUdDtor) GBL_NOEXCEPT;
189//! @}
190
191/*! \name Fields
192 * \brief Methods for managing extended userdata fields
193 * \relatesalso GblBox
194 * @{
195 */
196//! Returns the generic userdata field value for the given GblBox associated with the key
198//! Extracts the generic userdata field value for the given key, without destroying it
200//! Clears the field value for the given key, if it exists, calling its destructor (if it has one) and returning GBL_TRUE
202//! Returns GBL_TRUE if there is a field value on the given GblBox corresponding to the key
204//! Inserts \p ud into the the GblBox with the given \p key and optional destructor, destroying any overwritten value
206 GblQuark key,
207 uintptr_t ud,
208 GblArrayMapDtorFn pFnDtor/*=NULL*/) GBL_NOEXCEPT;
209//! @}
210
211//! \cond
212#define GblBoxClass_createFloating(...)
213 GblBoxClass_createFloatingDefault_(__VA_ARGS__)
214#define GblBoxClass_createFloatingDefault_(...)
215 GblBoxClass_createFloatingDefault__(__VA_ARGS__, 0, GBL_NULL, GBL_NULL)
216#define GblBoxClass_createFloatingDefault__(type, size, ud, dtor, ...)
217 (GblBoxClass_createFloating)(type, size, ud, dtor)
218
219#define GblBoxClass_constructFloating(...)
220 GblBoxClass_constructFloatingDefault_(__VA_ARGS__)
221#define GblBoxClass_constructFloatingDefault_(...)
222 GblBoxClass_constructFloatingDefault__(__VA_ARGS__, GBL_NULL, GBL_NULL)
223#define GblBoxClass_constructFloatingDefault__(klass, type, ud, dtor, ...)
224 (GblBoxClass_constructFloating)(klass, type, ud, dtor)
225
226#define GblBoxClass_setField(...)
227 GblBoxClass_setFieldDefault_(__VA_ARGS__)
228#define GblBoxClass_setFieldDefault_(...)
229 GblBoxClass_setFieldDefault__(__VA_ARGS__, GBL_NULL)
230#define GblBoxClass_setFieldDefault__(klass, key, value, dtor, ...)
231 (GblBoxClass_setField)(klass, key, value, dtor)
232
233#define GblBox_create(...)
234 GblBox_createDefault_(__VA_ARGS__)
235#define GblBox_createDefault_(...)
236 GblBox_createDefault__(__VA_ARGS__, 0, GBL_NULL, GBL_NULL, GBL_NULL)
237#define GblBox_createDefault__(type, size, ud, dtor, klass, ...)
238 (GblBox_create)(type, size, ud, dtor, klass)
239
240#define GblBox_construct(...)
241 GblBox_constructDefault_(__VA_ARGS__)
242#define GblBox_constructDefault_(...)
243 GblBox_constructDefault__(__VA_ARGS__, GBL_NULL, GBL_NULL, GBL_NULL)
244#define GblBox_constructDefault__(self, type, ud, dtor, klass, ...)
245 (GblBox_construct)(self, type, ud, dtor, klass)
246
247#define GblBox_setField(...)
248 GblBox_setFieldDefault_(__VA_ARGS__)
249#define GblBox_setFieldDefault_(...)
250 GblBox_setFieldDefault__(__VA_ARGS__, GBL_NULL)
251#define GblBox_setFieldDefault__(self, key, value, dtor, ...)
252 (GblBox_setField)(self, key, value, dtor)
253//! \endcond
254
256
257#undef GBL_SELF_TYPE
258
259#endif // GIMBAL_BOX_H
GblBox * GblBox_create(GblType derived, size_t size, void *pUserdata, GblArrayMapDtorFn pFnUdDtor, GblBoxClass *pClass)
Creates a GblBox instance of the derived type and returns a pointer to it. Has default arguments.
GBL_RESULT GblBox_construct(GblBox *pSelf, GblType derived, void *pUserdata, GblArrayMapDtorFn pFnUdDtor, GblBoxClass *pClass)
Constructs a GblBox instance of the derived type in-place, returning a result status code.
#define GBL_BOX(self)
Casts a GblInstance to GblBox.
Definition gimbal_box.h:28
GBL_RESULT GblBoxClass_constructFloating(GblBoxClass *pSelf, GblType derivedType, void *pUserdata, GblArrayMapDtorFn pFnUdDtor)
Constructs an extended floating class in-place, setting its properties to the given values....
GblBoxClass * GblBoxClass_createFloating(GblType derivedType, size_t size, void *pUserdata, GblArrayMapDtorFn pFnUdDtor)
Creates an extended floating class, setting its properties to the given values. Has default values.
#define GBL_CLASS_CAST(cType, klass)
#define GBL_NULL
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_FORWARD_DECLARE_STRUCT(S)
#define GBL_INSTANCE_BASE(instance)
#define GBL_TYPEID(instanceStruct)
#define GBL_CLASS_BASE(...)
#define GBL_PRIVATE_BEGIN
#define GBL_INSTANCE_END
#define GBL_EXPORT
#define GBL_CLASS_END
#define GBL_PRIVATE_END
Private data structure.
#define GBL_CLASSOF(cType, self)
#define GBL_CAST(cType, self)
#define GBL_AS(cType, self)
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
uintptr_t GblQuark
Uniquely identifiable interned string type.
GblBool GblBoxClass_clearField(GblBoxClass *pSelf, GblQuark key)
Destroys the generic userdata field value associated with the given key on the class,...
GblArrayMap * pFields
PRIVATE: Internal storage for userdata fields.
Definition gimbal_box.h:56
uintptr_t GblBoxClass_field(const GblBoxClass *pSelf, GblQuark key)
Returns the generic userdata field value associated with the given key on the class,...
GblType GblBox_type(void)
Returns the GblType UUID associated with GblBox.
void * GblBoxClass_userdata(const GblBoxClass *pSelf)
Returns the void* userdata pointer that has been associated with the given GblBoxClass.
GBL_RESULT GblBoxClass_setUserDestructor(GblBoxClass *pSelf, GblArrayMapDtorFn pFnUdDtor)
Adds an extra destructor callback to the field list, passing the given class back as the destructed v...
GBL_RESULT GblBoxClass_setField(GblBoxClass *pSelf, GblQuark key, uintptr_t ud, GblArrayMapDtorFn pFnDtor)
Sets the generic userdata value and its optional destructor for the given key on the class,...
GblBool GblBoxClass_hasField(const GblBoxClass *pSelf, GblQuark key)
Returns GBL_TRUE if the given class has generic userdata field value associated with the given key.
GBL_RESULT GblBoxClass_setUserdata(GblBoxClass *pSelf, void *pUserdata)
Sets the void* userdata pointer, associating it with the given GblBoxClass.
uintptr_t GblBoxClass_takeField(GblBoxClass *pSelf, GblQuark key)
Extracts the generic userdata field value associated with the given key on the class,...
Minimally bindable GblInstance with reference semantics and opaque userdata.
Definition gimbal_box.h:83
uintptr_t GblBox_takeField(GblBox *pSelf, GblQuark key)
Extracts the generic userdata field value for the given key, without destroying it.
GBL_RESULT GblBox_setField(GblBox *pSelf, GblQuark key, uintptr_t ud, GblArrayMapDtorFn pFnDtor)
Inserts ud into the the GblBox with the given key and optional destructor, destroying any overwritten...
uintptr_t GblBox_field(const GblBox *pSelf, GblQuark key)
Returns the generic userdata field value for the given GblBox associated with the key.
uint16_t contextType
PRIVATE: GblContext type flag.
Definition gimbal_box.h:87
GblBool GblBox_hasField(const GblBox *pSelf, GblQuark key)
Returns GBL_TRUE if there is a field value on the given GblBox corresponding to the key.
GBL_RESULT GblBox_setUserdata(GblBox *pSelf, void *pUserdata)
Stores the untyped userdata pointer within the GblBox.
volatile uint16_t refCounter
PRIVATE: Atomic reference counter.
Definition gimbal_box.h:86
GblBool GblBox_clearField(GblBox *pSelf, GblQuark key)
Clears the field value for the given key, if it exists, calling its destructor (if it has one) and re...
uint16_t constructedInPlace
PRIVATE: Flag for whether memory is deallocated upon destruction.
Definition gimbal_box.h:88
GblRefCount GblBox_unref(GblBox *pSelf)
Decrements the given GblBox's reference count by 1, destructing it when it hits 0.
uint16_t derivedFlags
PRIVATE: Extra flags for use in derived classes.
Definition gimbal_box.h:89
void * GblBox_userdata(const GblBox *pSelf)
Returns the userdata pointer stored within the given GblBox.
GblRefCount GblBox_refCount(const GblBox *pSelf)
Returns the number of active references held to the given GblBox.
GblBox * GblBox_ref(GblBox *pSelf)
Increments the given GblBox's reference count by 1, returning a pointer to it.
GBL_RESULT GblBox_setUserDestructor(GblBox *pSelf, GblArrayMapDtorFn pFnUdDtor)
Sets an additional user destructor to be invoked with the GblBox passed back to it when its being des...
GblArrayMap * pFields
PRIVATE: Storage for extended userdata fields.
Definition gimbal_box.h:85