libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_module.h
Go to the documentation of this file.
1/*! \file
2 * \brief GblModule loadable plugin instance and management API
3 * \ingroup core
4 * \todo
5 * - advanced unit testing
6 * - plan out type registration/management
7 * - rig up option group parsery
8 * - stop inheriting GblContext
9 *
10 * \author 2023 Falco Girgis
11 * \copyright MIT License
12 */
13#ifndef GIMBAL_MODULE_H
14#define GIMBAL_MODULE_H
15
16#include "../meta/instances/gimbal_context.h"
17#include "../meta/ifaces/gimbal_iplugin.h"
18#include "../strings/gimbal_string_ref.h"
19#include "../utils/gimbal_version.h"
20
21/*! \name Type System
22 * \brief Type UUID and cast operators
23 * @{
24 */
25#define GBL_MODULE_TYPE (GBL_TYPEID(GblModule)) //!< Type UUID for GblModule
26#define GBL_MODULE(self) (GBL_CAST(GblModule, self)) //!< Function-style GblInstance cast
27#define GBL_MODULE_CLASS(klass) (GBL_CLASS_CAST(GblModule, klass)) //!< Function-style GblClass cast
28#define GBL_MODULE_GET_CLASS(self) (GBL_CLASSOF(GblModule, self)) //!< Gets a GblModuleClass from GblInstance
29//! @}
30
31#define GBL_REQUIRE(...) GBL_REQUIRE_(__VA_ARGS__) //!< Returns the module for the given type
32
33#define GBL_SELF_TYPE GblModule
34
36
37GBL_FORWARD_DECLARE_STRUCT(GblOptionGroup);
39
40//! Function callback used with GblModule_foreach() for iterating over active modules
41typedef GblBool (*GblModuleIterFn)(GblModule* pIt, void* pClosure);
42
43/*! \struct GblModuleClass
44 * \extends GblContextClass
45 * \implements GblIPluginClass
46 * \brief GblClass structure for GblModule
47 *
48 * Virtual method table for GblModule
49 * - inherits from GblContext
50 * - has to implement GblIPlugin's VTable
51 * - adds pure virtaul methods load/unload
52 *
53 * \sa GblModule
54 */
55GBL_CLASS_DERIVE(GblModule, GblContext, GblIPlugin)
56 GBL_RESULT (*pFnLoad) (GBL_SELF); //!< Called when a GblModule is first loaded
57 GBL_RESULT (*pFnUnload)(GBL_SELF); //!< Called after the GblModule is done being referenced
59
60/*! \struct GblModule
61 * \ingroup core
62 * \extends GblContext
63 * \implements GblIPlugin
64 * \brief Dynamically loadable service and associated meta types
65 *
66 * A Module is a lazily-loaded object which is registered then later
67 * can be queried for by anywhere else in the application. This
68 * behavior lets you implement "services" as global singletons which
69 * can be accessed from anywhere. Modules also maintain their own inner
70 * registry of associated meta types, which is managed through the
71 * implemented GblIPlugin interface.
72 *
73 * \sa GbModuleClass
74 */
76 GblVersion version; //!< Version of a module
77 GblStringRef* pPrefix; //!< Namespace prefix of a module
78 GblStringRef* pAuthor; //!< Author(s) of a module
79 GblStringRef* pDescription; //!< Description of a module
80 GblOptionGroup* pOptionGroup; //!< Command-line option handler of a module
82
83//! \cond
84GBL_PROPERTIES(GblModule,
85 (prefix, GBL_GENERIC, (READ, WRITE, LOAD, SAVE), GBL_STRING_TYPE),
86 (version, GBL_GENERIC, (READ, WRITE, LOAD, SAVE), GBL_UINT32_TYPE),
87 (author, GBL_GENERIC, (READ, WRITE, LOAD, SAVE), GBL_STRING_TYPE),
88 (description, GBL_GENERIC, (READ, WRITE, LOAD, SAVE), GBL_STRING_TYPE),
89 (useCount, GBL_GENERIC, (READ), GBL_INT16_TYPE),
90 (typeCount, GBL_GENERIC, (READ), GBL_SIZE_TYPE)
91)
92//! \cond
93
94// ===== Static/Service API =====
95GBL_EXPORT GblType GblModule_type (void) GBL_NOEXCEPT;
96GBL_EXPORT GblModule* GblModule_find (const char* pName) GBL_NOEXCEPT;
97GBL_EXPORT GblModule* GblModule_findQuark (GblQuark name) GBL_NOEXCEPT;
98GBL_EXPORT GblModule* GblModule_at (size_t index) GBL_NOEXCEPT;
99GBL_EXPORT size_t GblModule_count (void) GBL_NOEXCEPT;
100
101GBL_EXPORT GblBool GblModule_foreach (GblModuleIterFn pFnIter,
102 void* pClosure) GBL_NOEXCEPT;
103
104GBL_EXPORT GblModule* GblModule_require (const char* pName,
105 const char* pVersion,
106 const char* pFile,
107 const char* pFunc,
108 size_t line) GBL_NOEXCEPT;
109
110GBL_EXPORT GblModule* GblModule_requireQuark (GblQuark quark,
111 const char* pVersion,
112 const char* pFile,
113 const char* pFunc,
114 size_t line) GBL_NOEXCEPT;
115
116// ===== Instance API =====
117
118GBL_EXPORT GblModule* GblModule_create (GblType derivedType,
119 const char* pName,
120 GblVersion version,
121 const char* pAuthor,
122 const char* pDescription,
123 const char* pPrefix) GBL_NOEXCEPT;
124
126
127GBL_EXPORT GBL_RESULT GblModule_register (GBL_SELF) GBL_NOEXCEPT;
128GBL_EXPORT GBL_RESULT GblModule_unregister (GBL_SELF) GBL_NOEXCEPT;
129
130GBL_EXPORT GBL_RESULT GblModule_use (GBL_SELF) GBL_NOEXCEPT;
131GBL_EXPORT GBL_RESULT GblModule_unuse (GBL_SELF) GBL_NOEXCEPT;
132GBL_EXPORT GblRefCount GblModule_useCount (GBL_CSELF) GBL_NOEXCEPT;
133
134GBL_EXPORT GblBool GblModule_isLoaded (GBL_CSELF) GBL_NOEXCEPT;
135
136
137// ===== SubType API ===== (TODO)
138
139GblType GblModule_registerType (GBL_SELF,
140 GblType parent,
141 const char* pName,
142 const GblTypeInfo* pInfo,
143 GblFlags flags) GBL_NOEXCEPT;
144
145GblType GblModule_typeFromName (GBL_CSELF, const char* pName) GBL_NOEXCEPT;
146GblType GblModule_typeFromIndex (GBL_CSELF, size_t index) GBL_NOEXCEPT;
147
148// Fix for Windows: "Redefinition with different linkage"
149// Added GBL_EXPORT here and removed from implementation in gimbal_module.c
150GBL_EXPORT size_t GblModule_typeCount (GBL_CSELF) GBL_NOEXCEPT;
151
153
154// ====== IMPLEMENTATION =====
155///\cond
156#define GBL_REQUIRE_(...)
157 GBL_VA_OVERLOAD_CALL(GBL_REQUIRE_, GBL_VA_OVERLOAD_SUFFIXER_ARGC, __VA_ARGS__)
158
159#define GBL_REQUIRE__1(type)
160 GBL_CAST(type, GblModule_require(GblType_name(GBL_TYPEID(type)),
161 GBL_NULL, __FILE__, __func__,
162 __LINE__))
163#define GBL_REQUIRE__2(type, name)
164 GBL_CAST(type, GblModule_require(name, GBL_NULL, __FILE__, __func__,
165 __LINE__))
166
167#define GBL_REQUIRE__3(type, name, version)
168 GBL_CAST(type, GblModule_require(name, version, __FILE__, __func__,
169 __LINE__))
170///\endcond
171
172#undef GBL_SELF_TYPE
173
174#endif // GIMBAL_MODULE_H
#define GBL_CLASS_CAST(cType, klass)
#define GBL_NULL
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_FORWARD_DECLARE_STRUCT(S)
#define GBL_TYPEID(instanceStruct)
#define GBL_INSTANCE_DERIVE(derivedInstance, baseInstance)
#define GBL_CLASS_DERIVE(...)
#define GBL_INSTANCE_END
#define GBL_EXPORT
#define GBL_CLASS_END
#define GBL_CLASSOF(cType, self)
#define GBL_CAST(cType, self)
#define GBL_VA_OVERLOAD_CALL(BASE, SUFFIXER,...)
#define GBL_VA_OVERLOAD_SUFFIXER_ARGC(...)
#define GBL_INT16_TYPE
Builtin ID for int16_t GblVariant type.
#define GBL_SIZE_TYPE
#define GBL_STRING_TYPE
Builtin ID for string GblVariant type.
#define GBL_UINT32_TYPE
Builtin ID for uint32_t GblVariant type.
#define GBL_PROPERTIES(object,...)
Declares a list of properties for the given object/instance structure.
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.
const char GblStringRef
Reference-counted, const char*-compatible string type.
uint32_t GblVersion
32-bit unsigned integer representing a packed version in the form (MAJOR.MINOR.PATCH)
Dynamically loadable service and associated meta types.
GblStringRef * pDescription
Description of a module.
GblStringRef * pAuthor
Author(s) of a module.
GblStringRef * pPrefix
Namespace prefix of a module.
GblOptionGroup * pOptionGroup
Command-line option handler of a module.
GblVersion version
Version of a module.