1#ifndef GIMBAL_CONTEXT_HPP
2#define GIMBAL_CONTEXT_HPP
5#include "../core/gimbal_api_frame.hpp"
6#include "../types/gimbal_version.hpp"
7#include "../core/gimbal_call_stack.hpp"
8#include "../types/gimbal_exception.hpp"
9#include "../preprocessor/gimbal_memory_resource.hpp"
14#define GBL_RESULT_CATCH(code) catch
15 (const Exception& resultException) {
16 code = resultException.getResult(); \
17} catch
19 code = Result(Result::ErrorUnhandledException); \
20}
22#define GBL_CONTEXT_EXT_C_TO_CPP_BEGIN_NO_THROW(pFrame, UdType)
24 GBL_API_BEGIN(pFrame->pContext);
25 UdType* pUd = static_cast<UdType*>(pFrame->pContextUd);
26 GBL_API_VERIFY_POINTER(pUd)
28#define GBL_CONTEXT_EXT_C_TO_CPP_BEGIN(pFrame, UdType)
34#define GBL_CONTEXT_EXT_C_TO_CPP_END()
45 static Context* fromGblObj(GblContext* pContext) {
56 pClass->iAllocatorIFace.pFnAlloc = gblMemAlloc_;
57 pClass->iAllocatorIFace.pFnRealloc = gblMemRealloc_;
58 pClass->iAllocatorIFace.pFnFree = gblMemFree_;
59 pClass->iLoggerIFace.pFnWrite = gblLogWrite_;
60 pClass->iLoggerIFace.pFnPush = gblLogPush_;
61 pClass->iLoggerIFace.pFnPop = gblLogPop_;
63 Exception::checkThrow(GblInstance_constructWithClass(
GBL_INSTANCE(
static_cast<GblContext*>(
this)),
GBL_CLASS(pClass)));
65 GblObject_userdataSet(
GBL_OBJECT(
static_cast<GblContext*>(
this)),
this);
70 virtual ~Context(
void) {
71 GblInstance_destruct(
GBL_INSTANCE(
static_cast<GblContext*>(
this)));
76 static Version getVersion(
void) {
83 const CallRecord& getCallRecord(
void)
const {
84 return *
static_cast<
const CallRecord*>(&
this->lastIssue);
87 void clearCallRecord(
void) {
88 Exception::checkThrow(GblContext_lastIssueClear(
this));
94 virtual void logPush(
const StackFrame& frame) {
95 GBL_API_BEGIN(frame.pContext);
96 GBL_API_CALL(pOldClass_->iLoggerIFace.pFnPush((GblILogger*)
static_cast<GblContext*>(
this), &frame));
98 Exception::checkThrow(GBL_API_RESULT());
100 virtual void logPop(
const StackFrame& frame, uint32_t count) {
101 gimbal::Result result = pOldClass_->iLoggerIFace.pFnPop((GblILogger*)
static_cast<GblContext*>(
this), &frame, count);
102 Exception::checkThrow(result);
104 virtual void logWrite(
const StackFrame& frame, LogLevel level,
const char* pFmt, va_list varArgs) {
105 GBL_API_BEGIN(
static_cast<GblContext*>(
this));
106 GBL_API_CALL(pOldClass_->iLoggerIFace.pFnWrite((GblILogger*)
static_cast<GblContext*>(
this), &frame, level, pFmt, varArgs));
108 Exception::checkThrow(GBL_API_RESULT());
111 virtual void* memAlloc(
const StackFrame& frame, Size size, Size alignment,
const char* pDebugInfoStr) {
112 void* pPtr =
nullptr;
113 GBL_API_BEGIN(
static_cast<GblContext*>(
this));
114 GBL_API_CALL(pOldClass_->iAllocatorIFace.pFnAlloc((GblIAllocator*)
static_cast<GblContext*>(
this), &frame, size, alignment, pDebugInfoStr, &pPtr));
116 Exception::checkThrow(GBL_API_RESULT());
120 virtual void* memRealloc(
const StackFrame& frame,
void* pPtr, Size newSize, Size newAlign) {
121 void* pNewPtr =
nullptr;
122 GBL_API_BEGIN(
static_cast<GblContext*>(
this));
123 GBL_API_CALL(pOldClass_->iAllocatorIFace.pFnRealloc((GblIAllocator*)
static_cast<GblContext*>(
this), &frame, pPtr, newSize, newAlign, &pNewPtr));
125 Exception::checkThrow(GBL_API_RESULT());
129 virtual void memFree(
const StackFrame& frame,
void* pPtr) {
130 GBL_API_BEGIN(
static_cast<GblContext*>(
this));
131 GBL_API_CALL(pOldClass_->iAllocatorIFace.pFnFree((GblIAllocator*)
static_cast<GblContext*>(
this), &frame, pPtr));
133 Exception::checkThrow(GBL_API_RESULT());
137 virtual void* do_allocate(size_t bytes, size_t align)
override {
138 GBL_API_BEGIN(
static_cast<GblContext*>(
this));
139 void* pValue =
nullptr;
141 pValue = GBL_API_MALLOC(bytes, align,
"PMR SHIT!");
146 virtual void do_deallocate(
void* pPtr, size_t bytes, size_t align)
override {
148 GBL_API_BEGIN(
static_cast<GblContext*>(
this));
154 virtual bool do_is_equal(
const memory_resource& other)
const noexcept override {
155 return this == &other;
160 GblContextClass* pOldClass_ =
nullptr;
162 static GBL_RESULT gblLogWrite_(GblILogger* pILogger,
const GblStackFrame* pFrame, GBL_LOG_LEVEL level,
const char* pFmt, va_list varArgs) {
164 static_cast<
Context*>((GblContext*)pILogger)->logWrite(*
static_cast<
const StackFrame*>(pFrame),level, pFmt, varArgs);
167 static GBL_RESULT gblLogPush_(GblILogger* pILogger,
const GblStackFrame* pFrame) {
169 static_cast<
Context*>((GblContext*)pILogger)->logPush(*
static_cast<
const StackFrame*>(pFrame));
173 static GBL_RESULT gblLogPop_(GblILogger* pILogger,
const GblStackFrame* pFrame, uint32_t count) {
174 gimbal::Result result;
176 static_cast<
Context*>((GblContext*)pILogger)->logPop(*
static_cast<
const StackFrame*>(pFrame), count);
177 }
catch(
const Exception& resultException) {
178 result = resultException.getResult();
181 result = Result(Result::ErrorUnhandledException);
186 static GBL_RESULT gblMemAlloc_(GblIAllocator* pIAllocator,
const GblStackFrame* pFrame, GblSize size, GblSize align,
const char* pDebugInfoStr,
void** ppPtr) {
188 *ppPtr =
static_cast<
Context*>((GblContext*)pIAllocator)->memAlloc(*
static_cast<
const StackFrame*>(pFrame), size, align, pDebugInfoStr);
189 GBL_API_VERIFY(*ppPtr, GBL_RESULT_ERROR_MEM_ALLOC);
192 static GBL_RESULT gblMemRealloc_(GblIAllocator* pIAllocator,
const GblStackFrame* pFrame,
void* pPtr, GblSize newSize, GblSize newAlign,
void** ppNewPtr) {
194 *ppNewPtr =
static_cast<
Context*>((GblContext*)pIAllocator)->memRealloc(*
static_cast<
const StackFrame*>(pFrame), pPtr, newSize, newAlign);
195 GBL_API_VERIFY(*ppNewPtr, GBL_RESULT_ERROR_MEM_ALLOC);
198 static GBL_RESULT gblMemFree_(GblIAllocator* pIAllocator,
const GblStackFrame* pFrame,
void* pPtr) {
200 static_cast<
Context*>((GblContext*)pIAllocator)->memFree(*
static_cast<
const StackFrame*>(pFrame), pPtr);
GBL_RESULT GblClass_destroyFloating(GblClass *pSelf)
Destroys the standalone, "floating" class, releasing its memory allocation.
#define GBL_CONTEXT_CLASS(klass)
#define GBL_CONTEXT_EXT_C_TO_CPP_END()
#define GBL_RESULT_CATCH(code)
#define GBL_CONTEXT_EXT_C_TO_CPP_BEGIN_NO_THROW(pFrame, UdType)
#define GBL_CONTEXT_EXT_C_TO_CPP_BEGIN(pFrame, UdType)
#define GBL_INSTANCE(self)
Casts GblInstance-compatible to GblInstance.
#define GBL_OBJECT(self)
Casts a GblInstance to a GblObject.
#define GBL_RESULT_SUCCESS(value)