libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_type.h
Go to the documentation of this file.
1/*! \file
2 * \brief GblType UUID, meta type registration, and API
3 * \ingroup meta
4 *
5 * This file provides the lowest-level entry-point
6 * and API into the meta type system, built around
7 * GblType, a UUID which is returned after registering
8 * a type.
9 *
10 * To register a type with the type system and get
11 * a UUID for it, simply use GblType_register().
12 *
13 * UUIDs are automatically made available for all
14 * builtin types.
15 *
16 * \todo
17 * - GblType_next does not iterate over builtin types
18 * - GblType_foreach() is probably better!
19 * - GBL_PROTCOL_TYPE solidification and testing
20 *
21 * \author 2023 Falco Girgis
22 * \copyright MIT License
23 */
24#ifndef GIMBAL_TYPE_H
25#define GIMBAL_TYPE_H
26
27#include "../../strings/gimbal_quark.h"
29
30//! GblType UUID of the invalid type
31#define GBL_INVALID_TYPE ((GblType)0)
32
33#define GBL_SELF_TYPE GblType
34
36
39GBL_FORWARD_DECLARE_STRUCT(GblInterface);
40
41/*! \brief Meta Type UUID
42 * \ingroup meta
43 *
44 * GblType is an integral-type representing a
45 * unique identifier for every type which has
46 * been registered with the type system, or
47 * GBL_INVALID_TYPE as an initial, invalid value.
48 *
49 * \sa GblType_register()
50 */
51typedef uintptr_t GblType;
52
53typedef GBL_RESULT (*GblClassInitFn) (GblClass*, const void*); //!< Function type used as a GblType's GblClass initializer
54typedef GBL_RESULT (*GblClassFinalFn) (GblClass*, const void*); //!< Function type used as a GblType's GblClass finalizer
55typedef GBL_RESULT (*GblInstanceInitFn)(GblInstance*); //!< Function type used as a GblType's GblInstance initializer
56
57//! Flags controlling behavior of GblTypes. These can be set at any level in a type heirarchy.
58typedef enum GblTypeFlags {
59 GBL_TYPE_FLAGS_NONE = 0, //!< Type which adds no additional flags beyond what's inherited
60 GBL_TYPE_FLAG_BUILTIN = (1 << 6), //!< Type was automatically registered as a builtin type (do not use on a new type)
61 GBL_TYPE_FLAG_TYPEINFO_STATIC = (1 << 7), //!< Type's GblTypeInfo specified upon registration is static, so no internal storage has to be allocated for storing a copy
62 GBL_TYPE_FLAG_CLASS_PINNED = (1 << 8), //!< Type's GblClass is never destroyed and remains persistent upon construction. The default behavior is to create/destroy as referenced.
63 GBL_TYPE_FLAG_CLASS_PREINIT = (1 << 9), //!< Type's GblClass should be constructed immediately, rather than lazily upon use by default. Also implies class pinning.
64 GBL_TYPE_FLAG_UNMAPPABLE = (1 << 10), //!< Cannot obtain Type's associated GblInterface from a GblClass it has been mapped to. Disambiguates casting with common interface base classes.
65 GBL_TYPE_FLAG_INCOMPLETE = (1 << 11), //!< Incomplete/partial type missing some type dependency
66 GBL_TYPE_FLAG_ABSTRACT = (1 << 12), //!< Type cannot be instantiated without being derived
67 GBL_TYPE_FLAG_FINAL = (1 << 13), //!< Type cannot be derived from
68 GBL_TYPE_FLAGS_MASK = 0xffffffc0 //!< Mask of all user type flags
69} GblTypeFlags;
70
71//! Provides implementation details of a GblInterface for a type
72typedef struct GblInterfaceImpl {
73 GblType interfaceType; //!< GblType of the implemented GblInterface
74 size_t classOffset; //!< offset of the GblInterface into GblClass (using offsetof())
75} GblInterfaceImpl;
76
77//! Provides type information when registering a new GblType
78typedef struct GblTypeInfo {
79 GblClassInitFn pFnClassInit; //!< Function used to initialize the values a GblType's associated GblClass
80 GblClassFinalFn pFnClassFinal; //!< Function used to finalize the values of a GblType's associated GblClass
81 size_t classSize; //!< Size of a GblType's associated GblClass structure
82 size_t classPrivateSize; //!< Size of extra private storage to be associated with a GblType's GblClass
83 const void* pClassData; //!< Size of extra private storage to be associated with a GblType's GblClass
84 uint8_t interfaceCount; //!< Number of GblInterface mappings in pInterfaceImpls array
85 const GblInterfaceImpl* pInterfaceImpls; //!< Array providing information for each interface implemented by the type
86 uint8_t dependencyCount; //!< Number of dependent GblTypes in pDependencies array
87 const GblType* pDependencies; //!< Array providing a list of type dependencies that must be implemented a GblType
88 GblInstanceInitFn pFnInstanceInit; //!< Function used to initialize the values a GblType's associated GblInstance
89 size_t instanceSize; //!< Size of a GblType's associated GblInstance structure
90 size_t instancePrivateSize; //!< Size of extra private storage to be associated with a GblType's GblInstance.
91} GblTypeInfo;
92
93/*! \name Registry
94 * \brief Methods for managing the type registry
95 * @{
96 */
97//! Registers a new type with the given information, returning a unique identifier for it
99 GblType baseType,
100 const GblTypeInfo* pInfo,
101 GblFlags flags) GBL_NOEXCEPT;
102//! Unregisters a type from the type system. Typically not done, better not be used.
104//! Returns the total number of types registered to the type system
106//! Iterates over the registry by passing a previously returned type or GBL_TYPE_INVALID initially
108//! @}
109
110/*! \name Querying
111 * \brief Methods for looking up or querying types
112 * @{
113 */
114//! Queries the internal type registry for a GblType by its type name
116//! Queries the registry for a GblType using a GblQuark of its type name (faster)
118//! @}
119
120/*! \name Attributes
121 * \brief Accessor methods for getting a type's information
122 * @{
123 */
124//! Returns the type name string associated with the given GblType
126//! Returns the type name quark associated with the given GblType
128//! Returns the given type's parent's GblType or GBL_INVALID_TYPE if it's a root type
130//! Returns the GblTypeInfo struct for the given type, containing its meta information
132//! Returns the combined flags for a given type, including flags it has inherited
134//! @}
135
136/*! \name Resource Tracking
137 * \brief Methods for querying a type's active resource counters
138 * @{
139 */
140//! Returns the reference counter for the given GblType's internally-managed default GblClass
142//! Returns the number of active instances of the given GblType
144//! @}
145
146/*! \name Hierarchy
147 * \brief Methods for querying a type's hierarchy
148 * @{
149 */
150//! Returns the root parent GblType of the given GblType
152//! Returns the base GblType of the given GblType, where a \p depth of 0 is its root
154//! Similar to GblType_base(), but in the reverse direction. \p level 0 is identity.
156//! Returns the depth of the given GblType, where a depth of 0 means it's a root type
158//! @}
159
160/*! \name Type Checking
161 * \brief Methods performing type system queries on a type
162 * @{
163 */
164//! Returns GBL_TRUE if the given type is a valid, registred type
166//! Returns GBL_TRUE if the given type can be safely cast to the \p other type
168//! Returns GBL_TRUE if the given type depends on the \p dependency type
170//! Returns GBL_TRUE if the given type is a subtype, inheriting from \p superType
172//! Returns GBL_TRUE if the given type has implemented \p ifaceType
174//! Returns the most-derived, deepest common-type between the given type and \p other
176//! Returns true if the given type's class contains an implementation of the \p iface interface
178//! Returns true if the given type meets all type dependencies for the \p dependent type
180//! @}
181
183
184#undef GBL_SELF_TYPE
185
186#endif // GIMBAL_TYPE_H
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_FORWARD_DECLARE_STRUCT(S)
#define GBL_EXPORT
#define GBL_VSELF
GblBool GblType_conforms(GblType self, GblType dependent)
Returns true if the given type meets all type dependencies for the dependent type.
GblType GblType_ancestor(GblType self, size_t level)
Similar to GblType_base(), but in the reverse direction. level 0 is identity.
GblType GblType_common(GblType self, GblType other)
Returns the most-derived, deepest common-type between the given type and other.
GblType GblType_findQuark(GblQuark quark)
Queries the registry for a GblType using a GblQuark of its type name (faster)
GblRefCount GblType_classRefCount(GblType self)
Returns the reference counter for the given GblType's internally-managed default GblClass.
GblBool GblType_maps(GblType self, GblType iface)
Returns true if the given type's class contains an implementation of the iface interface.
GblBool GblType_derives(GblType self, GblType superType)
Returns GBL_TRUE if the given type is a subtype, inheriting from superType.
GBL_RESULT GblType_unregister(GblType type)
Unregisters a type from the type system. Typically not done, better not be used.
GblType GblType_parent(GblType self)
Returns the given type's parent's GblType or GBL_INVALID_TYPE if it's a root type.
size_t GblType_count(void)
Returns the total number of types registered to the type system.
GblBool GblType_depends(GblType self, GblType dependency)
Returns GBL_TRUE if the given type depends on the dependency type.
GblBool GblType_implements(GblType self, GblType ifaceType)
Returns GBL_TRUE if the given type has implemented ifaceType.
GblType GblType_base(GblType self, size_t depth)
Returns the base GblType of the given GblType, where a depth of 0 is its root.
GblQuark GblType_nameQuark(GblType self)
Returns the type name quark associated with the given GblType.
GblType GblType_find(const char *pName)
Queries the internal type registry for a GblType by its type name.
GblBool GblType_check(GblType self, GblType other)
Returns GBL_TRUE if the given type can be safely cast to the other type.
GblFlags GblType_flags(GblType self)
Returns the combined flags for a given type, including flags it has inherited.
size_t GblType_depth(GblType self)
Returns the depth of the given GblType, where a depth of 0 means it's a root type.
GblTypeFlags
Flags controlling behavior of GblTypes. These can be set at any level in a type heirarchy.
Definition gimbal_type.h:58
@ GBL_TYPE_FLAG_TYPEINFO_STATIC
Type's GblTypeInfo specified upon registration is static, so no internal storage has to be allocated ...
Definition gimbal_type.h:61
@ GBL_TYPE_FLAGS_MASK
Mask of all user type flags.
Definition gimbal_type.h:68
@ GBL_TYPE_FLAG_CLASS_PREINIT
Type's GblClass should be constructed immediately, rather than lazily upon use by default.
Definition gimbal_type.h:63
@ GBL_TYPE_FLAG_FINAL
Type cannot be derived from.
Definition gimbal_type.h:67
@ GBL_TYPE_FLAGS_NONE
Type which adds no additional flags beyond what's inherited.
Definition gimbal_type.h:59
@ GBL_TYPE_FLAG_CLASS_PINNED
Type's GblClass is never destroyed and remains persistent upon construction.
Definition gimbal_type.h:62
@ GBL_TYPE_FLAG_ABSTRACT
Type cannot be instantiated without being derived.
Definition gimbal_type.h:66
@ GBL_TYPE_FLAG_BUILTIN
Type was automatically registered as a builtin type (do not use on a new type)
Definition gimbal_type.h:60
@ GBL_TYPE_FLAG_UNMAPPABLE
Cannot obtain Type's associated GblInterface from a GblClass it has been mapped to.
Definition gimbal_type.h:64
@ GBL_TYPE_FLAG_INCOMPLETE
Incomplete/partial type missing some type dependency.
Definition gimbal_type.h:65
const GblTypeInfo * GblType_info(GblType self)
Returns the GblTypeInfo struct for the given type, containing its meta information.
GblRefCount GblType_instanceCount(GblType self)
Returns the number of active instances of the given GblType.
const char * GblType_name(GblType self)
Returns the type name string associated with the given GblType.
GblType GblType_next(GblType previousType)
Iterates over the registry by passing a previously returned type or GBL_TYPE_INVALID initially.
GblBool GblType_verify(GblType self)
Returns GBL_TRUE if the given type is a valid, registred type.
GblType GblType_register(const char *pName, GblType baseType, const GblTypeInfo *pInfo, GblFlags flags)
Registers a new type with the given information, returning a unique identifier for it.
GblType GblType_root(GblType self)
Returns the root parent GblType of the given GblType.
uint32_t GblFlags
Standard-sized flags 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
uintptr_t GblQuark
Uniquely identifiable interned string type.
Provides implementation details of a GblInterface for a type.
Definition gimbal_type.h:72
size_t classOffset
offset of the GblInterface into GblClass (using offsetof())
Definition gimbal_type.h:74
GblType interfaceType
GblType of the implemented GblInterface.
Definition gimbal_type.h:73
Provides type information when registering a new GblType.
Definition gimbal_type.h:78
size_t classPrivateSize
Size of extra private storage to be associated with a GblType's GblClass.
Definition gimbal_type.h:82
uint8_t dependencyCount
Number of dependent GblTypes in pDependencies array.
Definition gimbal_type.h:86
const GblInterfaceImpl * pInterfaceImpls
Array providing information for each interface implemented by the type.
Definition gimbal_type.h:85
GblClassInitFn pFnClassInit
Function used to initialize the values a GblType's associated GblClass.
Definition gimbal_type.h:79
GblInstanceInitFn pFnInstanceInit
Function used to initialize the values a GblType's associated GblInstance.
Definition gimbal_type.h:88
const void * pClassData
Size of extra private storage to be associated with a GblType's GblClass.
Definition gimbal_type.h:83
GblClassFinalFn pFnClassFinal
Function used to finalize the values of a GblType's associated GblClass.
Definition gimbal_type.h:80
size_t classSize
Size of a GblType's associated GblClass structure.
Definition gimbal_type.h:81
uint8_t interfaceCount
Number of GblInterface mappings in pInterfaceImpls array.
Definition gimbal_type.h:84
size_t instanceSize
Size of a GblType's associated GblInstance structure.
Definition gimbal_type.h:89
size_t instancePrivateSize
Size of extra private storage to be associated with a GblType's GblInstance.
Definition gimbal_type.h:90
const GblType * pDependencies
Array providing a list of type dependencies that must be implemented a GblType.
Definition gimbal_type.h:87