libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
Type System Manual

Table of Contents

How to use the type system and its features.

The language interop layer of libGimbal is primarily facilitated through its low-level, C-based meta type system. The type system has two primary goals, one of which is to provide the C (and C++) programming language(s) with higher-level object-oriented constructs, and the other is to use these constructs to create a common type system for cross-langauge interoperability.

Type System

Overview

At its core, the LibGimbal Type Framework utilizes a dynamic type system, provided by the GblType API (within gimbal_type.h). This system provides a runtime type registry maintaining all type information provided by builtin or user-registered types. This registry provides object-oriented concepts such as classes, objects, interfaces, poperties, signals, and an inheritance model to both the C programming language, and any langauge which can provide glue code to a C API.

Example

One of the things libGimabl's type system aims to do is endow C programmers with a powerful set of tools and constructs from higher-level object-oriented languages. Constructs such as inheritance, private member variables, virtual functions, interfaces, and even signals and properties become accessible to C programs. The following is an almost complete header from the ElysianVMU emulator, creating an object-oriented type for the piezoelectric Buzzer peripheral:

#ifndef EVMU_BUZZER_H
#define EVMU_BUZZER_H
#include <gimbal/meta.h>
// UUID associated with the given type
#define EVMU_BUZZER_TYPE (GBL_TYPEOF(EvmuBuzzer))
// Convenience function-style cast operators
#define EVMU_BUZZER(instance) (GBL_INSTANCE_CAST(instance, EvmuBuzzer))
#define EVMU_BUZZER_CLASS(klass) (GBL_CLASS_CAST(klass, EvmuBuzzer))
#define EVMU_BUZZER_GET_CLASS(instance) (GBL_INSTANCE_GET_CLASS(instance, EvmuBuzzer))
// Define the "self" or "this" type for its methods
#define GBL_SELF_TYPE EvmuBuzzer
// Declare everything extern "C"
GBL_DECLS_BEGIN
// Define a "type class" structure or vtable, containing overridable
// virtual functions, inheriting EvmuPeripheral's vtable.
GBL_CLASS_DERIVE(EvmuBuzzer, EvmuPeripheral)
EVMU_RESULT (*pFnPlayPcm) (GBL_SELF);
EVMU_RESULT (*pFnStopPcm) (GBL_SELF);
EVMU_RESULT (*pFnBufferPcm)(GBL_SELF);
GBL_CLASS_END
// Define a "type instance" structure containing public members,
// inheriting EvmuPeripheral's public instance members.
GBL_INSTANCE_DERIVE(EvmuBuzzer, EvmuPeripheral)
GblBool pcmChanged;
GblBool enableFreqResp;
GBL_INSTANCE_END
// Create a list of properties for the given type.
GBL_PROPERTIES(EvmuBuzzer,
(enabled, GBL_GENERIC, (READ, WRITE), GBL_BOOL_TYPE),
(configured, GBL_GENERIC, (READ), GBL_BOOL_TYPE),
(active, GBL_GENERIC, (READ, WRITE), GBL_BOOL_TYPE),
(period, GBL_GENERIC, (READ, WRITE), GBL_UINT16_TYPE),
(invPulseLength, GBL_GENERIC, (READ, WRITE), GBL_UINT8_TYPE),
(frequency, GBL_GENERIC, (READ), GBL_UINT32_TYPE),
(gain, GBL_GENERIC, (READ), GBL_FLOAT_TYPE)
)
// Define a list of signals on the type, which can be
// subscribed to with custom callback logic.
GBL_SIGNALS(EvmuBuzzer,
(toneStart, (GBL_INSTANCE_TYPE, pReceiver)),
(toneStop, (GBL_INSTANCE_TYPE, pReceiver)),
(toneUpdate, (GBL_INSTANCE_TYPE, pReceiver),
(GBL_UINT16_TYPE, period),
(GBL_UINT8_TYPE, invPulseLength))
)
// Declare all public methods for the EvmuBuzzer type
EVMU_EXPORT GblType EvmuBuzzer_type (void) GBL_NOEXCEPT;
EVMU_EXPORT GblBool EvmuBuzzer_isActive (GBL_CSELF) GBL_NOEXCEPT;
EVMU_EXPORT GblBool EvmuBuzzer_isConfigured (GBL_CSELF) GBL_NOEXCEPT;
EVMU_EXPORT GblBool EvmuBuzzer_isEnabled (GBL_CSELF) GBL_NOEXCEPT;
EVMU_EXPORT void EvmuBuzzer_setEnabled (GBL_SELF,
GblBool enabled) GBL_NOEXCEPT;
EVMU_EXPORT void EvmuBuzzer_tone (GBL_CSELF,
uint16_t* pPeriod,
uint8_t* pInvPulseLen) GBL_NOEXCEPT;
EVMU_EXPORT EVMU_RESULT EvmuBuzzer_setTone (GBL_SELF,
uint16_t period,
uint8_t invPulseLen) GBL_NOEXCEPT;
EVMU_EXPORT EVMU_RESULT EvmuBuzzer_playTone (GBL_SELF) GBL_NOEXCEPT;
EVMU_EXPORT EVMU_RESULT EvmuBuzzer_stopTone (GBL_SELF) GBL_NOEXCEPT;
EVMU_EXPORT const void* EvmuBuzzer_pcmBuffer (GBL_CSELF) GBL_NOEXCEPT;
EVMU_EXPORT GblSize EvmuBuzzer_pcmSamples (GBL_CSELF) GBL_NOEXCEPT;
EVMU_EXPORT GblSize EvmuBuzzer_pcmFrequency (GBL_CSELF) GBL_NOEXCEPT;
EVMU_EXPORT float EvmuBuzzer_pcmGain (GBL_CSELF) GBL_NOEXCEPT;
GBL_DECLS_END
#undef GBL_SELF_TYPE
#endif // EVMU_BUZZER_H
#define GBL_INSTANCE_TYPE
Type UUID for GblInstance.
#define GBL_UINT16_TYPE
Builtin ID for uint16_t GblVariant type.
#define GBL_FLOAT_TYPE
Builtin ID for float GblVariant type.
#define GBL_BOOL_TYPE
Builtin ID for boolean GblVariant type.
#define GBL_UINT8_TYPE
Builtin ID for uint8_t 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.
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)
uintptr_t GblType
Meta Type UUID.
Definition gimbal_type.h:51