libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_closure.h
Go to the documentation of this file.
1/*! \file
2 * \ingroup signals
3 * \brief GblClosure, generic callable instance, and API
4 *
5 * This file contains the type declaration of GblClosure
6 * and its associated API. A GblClosure repersents the
7 * base type for all callable objects, providing:
8 * - A uniform, generic invocation method (GblMarshal)
9 * - Any state data that the closure captures
10 *
11 * \author 2023 Falco Girgis
12 * \copyright MIT License
13 */
14#ifndef GIMBAL_CLOSURE_H
15#define GIMBAL_CLOSURE_H
16
17#include "../instances/gimbal_box.h"
18#include "gimbal_marshal.h"
19
20/*! \name Type System
21 * \brief Type UUID and cast operators
22 * @{
23 */
24#define GBL_CLOSURE_TYPE (GBL_TYPEID(GblClosure)) //!< Type UUID for GblClosure
25#define GBL_CLOSURE(self) (GBL_CAST(GblClosure, self)) //!< Casts a GblInstance to GblClosure
26#define GBL_CLOSURE_CLASS(klass) (GBL_CLASS_CAST(GblClosure, klass)) //!< Casts a GblClass to GblClosureClass
27#define GBL_CLOSURE_GET_CLASS(self) (GBL_CLASSOF(GblClosure, self)) //!< Gets a GblClosureClass from GblInstance
28//! @}
29
30//! Casts a C function pointer to a generic callback type
31#ifndef __cplusplus
32# define GBL_CALLBACK(fn) ((GblFnPtr)fn)
33#else
34# define GBL_CALLBACK(fn) ((GblFnPtr)+fn)
35#endif
36
37#define GBL_SELF_TYPE GblClosure
38
40
41/*! \struct GblClosureClass
42 * \brief GblClass VTable for GblClosure
43 * \extends GblBoxClass
44 *
45 * Provides an overridable virtual method for marshalling
46 * generic arguments to an actual function and marshalling
47 * its return value into a generic value.
48 *
49 * \sa GblBox, GblMarshalFn
50 */
51GBL_CLASS_DERIVE(GblClosure, GblBox)
52 //! Primary entry point when invoking a closure
53 GblMarshalFn pFnMetaMarshal;
55
56/*! \struct GblClosure
57 * \extends GblBox
58 * \ingroup signals
59 * \brief Base instance for all closure types
60 *
61 * A GblClosure represents an abstract generic
62 * callback object. It is analogous to a stateful C++
63 * function object, or functor, in that it can
64 * contain both a callback method as well as stateful
65 * data to operate upon.
66 *
67 * All closures have a single entry-point for
68 * having their callback logic called, which is
69 * GblClosure_invoke(). This calls the closure's
70 * "marshal" function, which is responsible for
71 * - validating arguments and return type
72 * - converting arguments into expected format for callback
73 * - calling actual callback or logic
74 * - converting return value back into a GblVariant
75 *
76 * As the abstract base closure, there is no underlying
77 * language-specific callback state within this class.
78 * For calling back into C function pointers, see
79 * GblCClosure.
80 *
81 * \note As GblClosure inherits GblBox, it can
82 * contain arbitrary language-specific or
83 * binding-specific userdata and has reference
84 * count semantics.
85 *
86 * \sa GblCClosure, GblMarshalFn, gimbal_signal.h
87 */
90 GblMarshalFn pFnMarshal; //!< Per-instance marshal function, private
93
94//! Returns the GblType UUID for GblClosure
96
97/*! \name Current Closure
98 * \brief Methods for querying the active GblClosure
99 * @{
100 */
101//! Returns a pointer to the inner-most currently executing GblClosure instance
103//! Returns a pointer to the userdatea of the inner-most currently executing GblClosure instance
105//! @}
106
107//! Creates a GblClosure-derived instance with the given attributes, returning a pointer to it
108GBL_EXPORT GblClosure* GblClosure_create (GblType derivedType,
109 size_t size /*=DEFAULT*/,
110 void* pUserdata/*=NULL*/,
111 GblArrayMapDtorFn pFnDtor /*=NULL*/) GBL_NOEXCEPT;
112//! Returns a new reference to the given GblClosure instance, increasing its reference count.
114//! Removes a reference to the given GblClosure, destroying it upon reaching zero.
116
117/*! \name Accessor Methods
118 * \brief Methods for reading/writing GblClosure fields
119 * \relatesalso GblClosure
120 * @{
121 */
122//! Sets the closure's marshal to the function pointed to by \p pFnMarshal
124 GblMarshalFn pFnMarshal) GBL_NOEXCEPT;
125//! Sets the closure's class's meta marshal to the function pointed to by \p pFnMeta, swizzling its class
127 GblMarshalFn pFnMeta) GBL_NOEXCEPT;
128//! Returns whether the GblClosure has an instance marshal or not
130//! Returns whether the GblClosure's class has a meta marshal or not
132//! @}
133
134/*! \name Invocation Method(s)
135 * \brief Methods used to invoke the closure
136 * \relatesalso GblClosure
137 */
138//! Invokes the given GblClosure, marshalling args in and a value out as variants
140 GblVariant* pRetValue,
141 size_t argCount,
142 GblVariant* pArgValues) GBL_NOEXCEPT;
143//! @}
144
146
147#define GblClosure_create(...)
148 GblClosure_createDefault_(__VA_ARGS__)
149#define GblClosure_createDefault_(...)
150 GblClosure_createDefault__(__VA_ARGS__, 0, NULL, NULL)
151#define GblClosure_createDefault__(type, size, ud, dtor, ...)
152 (GblClosure_create)(type, size, ud, dtor)
153
154#undef GBL_SELF_TYPE
155
156#endif // GIMBAL_CLOSURE_H
#define GBL_CLASS_CAST(cType, klass)
#define GblClosure_create(...)
#define GblClosure_createDefault__(type, size, ud, dtor,...)
GblType GblClosure_type(void)
Returns the GblType UUID for GblClosure.
GblClosure * GblClosure_current(void)
Returns a pointer to the inner-most currently executing GblClosure instance.
#define GblClosure_createDefault_(...)
void * GblClosure_currentUserdata(void)
Returns a pointer to the userdatea of the inner-most currently executing GblClosure instance.
GblClosure * GblClosure_ref(GblClosure *pSelf)
Returns a new reference to the given GblClosure instance, increasing its reference count.
GblClosure * GblClosure_create(GblType derivedType, size_t size, void *pUserdata, GblArrayMapDtorFn pFnDtor)
Creates a GblClosure-derived instance with the given attributes, returning a pointer to it.
GblRefCount GblClosure_unref(GblClosure *pSelf)
Removes a reference to the given GblClosure, destroying it upon reaching zero.
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_TYPEID(instanceStruct)
#define GBL_INSTANCE_DERIVE(derivedInstance, baseInstance)
#define GBL_PRIVATE_BEGIN
#define GBL_CLASS_DERIVE(...)
#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)
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
GblMarshalFn pFnMetaMarshal
Primary entry point when invoking a closure.
Base instance for all closure types.
GBL_RESULT GblClosure_setMetaMarshal(GblClosure *pSelf, GblMarshalFn pFnMeta)
Sets the closure's class's meta marshal to the function pointed to by pFnMeta, swizzling its class.
void GblClosure_setMarshal(GblClosure *pSelf, GblMarshalFn pFnMarshal)
Sets the closure's marshal to the function pointed to by pFnMarshal.
GblMarshalFn pFnMarshal
Per-instance marshal function, private.
GblBool GblClosure_hasMarshal(const GblClosure *pSelf)
Returns whether the GblClosure has an instance marshal or not.
GblBool GblClosure_hasMetaMarshal(const GblClosure *pSelf)
Returns whether the GblClosure's class has a meta marshal or not.
GBL_RESULT GblClosure_invoke(GblClosure *pSelf, GblVariant *pRetValue, size_t argCount, GblVariant *pArgValues)
Invokes the given GblClosure, marshalling args in and a value out as variants.