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#define GBL_SELF_TYPE GblClosure
31
33
34/*! \struct GblClosureClass
35 * \brief GblClass VTable for GblClosure
36 * \extends GblBoxClass
37 *
38 * Provides an overridable virtual method for marshalling
39 * generic arguments to an actual function and marshalling
40 * its return value into a generic value.
41 *
42 * \sa GblBox, GblMarshalFn
43 */
44GBL_CLASS_DERIVE(GblClosure, GblBox)
45 //! Primary entry point when invoking a closure
46 GblMarshalFn pFnMetaMarshal;
48
49/*! \struct GblClosure
50 * \extends GblBox
51 * \ingroup signals
52 * \brief Base instance for all closure types
53 *
54 * A GblClosure represents an abstract generic
55 * callback object. It is analogous to a stateful C++
56 * function object, or functor, in that it can
57 * contain both a callback method as well as stateful
58 * data to operate upon.
59 *
60 * All closures have a single entry-point for
61 * having their callback logic called, which is
62 * GblClosure_invoke(). This calls the closure's
63 * "marshal" function, which is responsible for
64 * - validating arguments and return type
65 * - converting arguments into expected format for callback
66 * - calling actual callback or logic
67 * - converting return value back into a GblVariant
68 *
69 * As the abstract base closure, there is no underlying
70 * language-specific callback state within this class.
71 * For calling back into C function pointers, see
72 * GblCClosure.
73 *
74 * \note As GblClosure inherits GblBox, it can
75 * contain arbitrary language-specific or
76 * binding-specific userdata and has reference
77 * count semantics.
78 *
79 * \sa GblCClosure, GblMarshalFn, gimbal_signal.h
80 */
83 GblMarshalFn pFnMarshal; //!< Per-instance marshal function, private
86
87//! Returns the GblType UUID for GblClosure
89
90/*! \name Current Closure
91 * \brief Methods for querying the active GblClosure
92 * @{
93 */
94//! Returns a pointer to the inner-most currently executing GblClosure instance
96//! Returns a pointer to the userdatea of the inner-most currently executing GblClosure instance
98//! @}
99
100//! Creates a GblClosure-derived instance with the given attributes, returning a pointer to it
101GBL_EXPORT GblClosure* GblClosure_create (GblType derivedType,
102 size_t size /*=DEFAULT*/,
103 void* pUserdata/*=NULL*/,
104 GblArrayMapDtorFn pFnDtor /*=NULL*/) GBL_NOEXCEPT;
105//! Returns a new reference to the given GblClosure instance, increasing its reference count.
107//! Removes a reference to the given GblClosure, destroying it upon reaching zero.
109
110/*! \name Accessor Methods
111 * \brief Methods for reading/writing GblClosure fields
112 * \relatesalso GblClosure
113 * @{
114 */
115//! Sets the closure's marshal to the function pointed to by \p pFnMarshal
117 GblMarshalFn pFnMarshal) GBL_NOEXCEPT;
118//! Sets the closure's class's meta marshal to the function pointed to by \p pFnMeta, swizzling its class
120 GblMarshalFn pFnMeta) GBL_NOEXCEPT;
121//! Returns whether the GblClosure has an instance marshal or not
123//! Returns whether the GblClosure's class has a meta marshal or not
125//! @}
126
127/*! \name Invocation Method(s)
128 * \brief Methods used to invoke the closure
129 * \relatesalso GblClosure
130 */
131//! Invokes the given GblClosure, marshalling args in and a value out as variants
133 GblVariant* pRetValue,
134 size_t argCount,
135 GblVariant* pArgValues) GBL_NOEXCEPT;
136//! @}
137
139
140#define GblClosure_create(...)
141 GblClosure_createDefault_(__VA_ARGS__)
142#define GblClosure_createDefault_(...)
143 GblClosure_createDefault__(__VA_ARGS__, 0, NULL, NULL)
144#define GblClosure_createDefault__(type, size, ud, dtor, ...)
145 (GblClosure_create)(type, size, ud, dtor)
146
147#undef GBL_SELF_TYPE
148
149#endif // GIMBAL_CLOSURE_H
#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.
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:52
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.