From f9de41b6c0b0567fd42bd18c1454fad73be1f049 Mon Sep 17 00:00:00 2001 From: Doyle Thai Date: Fri, 23 Jun 2017 21:17:26 +1000 Subject: [PATCH] Add cpp members to DqnArray --- dqn.h | 84 ++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/dqn.h b/dqn.h index aa6e3f0..bc81f95 100644 --- a/dqn.h +++ b/dqn.h @@ -429,36 +429,24 @@ struct DqnArray u64 count; u64 capacity; T *data; + + void Init (const size_t capacity, DqnMemAPI memAPI = DqnMemAPI_DefaultUseCalloc()); + bool Free (); + bool Grow (); + T *Push (const T item); + void Pop (); + T *Get (u64 index); + bool Clear (); + bool Remove (u64 index); + bool RemoveStable(u64 index); }; FILE_SCOPE const char *const DQN_MEM_API_CALLBACK_RESULT_TYPE_INCORRECT = "DqnMemAPICallbackResult type is incorrect"; -// Implementation taken from Milton, developed by Serge at -// https://github.com/serge-rgb/milton#license template -bool DqnArray_Free(DqnArray *array) -{ - if (array && array->data) - { - // TODO(doyle): Right now we assume free always works, and it probably should? - size_t sizeToFree = (size_t)array->capacity * sizeof(T); - DqnMemAPICallbackInfo info = DqnMemAPIInternal_CallbackInfoAskFree( - array->memAPI, array->data, sizeToFree); - array->memAPI.callback(info, NULL); - array->data = NULL; - - array->count = 0; - array->capacity = 0; - return true; - } - - return false; -} - -template -bool DqnArray_Init(DqnArray *array, size_t capacity, - DqnMemAPI memAPI = DqnMemAPI_DefaultUseCalloc()) +bool DqnArray_Init(DqnArray *const array, const size_t capacity, + const DqnMemAPI memAPI = DqnMemAPI_DefaultUseCalloc()) { if (!array) return false; if (array->data) DqnArray_Free(array); @@ -482,8 +470,30 @@ bool DqnArray_Init(DqnArray *array, size_t capacity, return true; } +// Implementation taken from Milton, developed by Serge at +// https://github.com/serge-rgb/milton#license template -bool DqnArray_Grow(DqnArray *array) +bool DqnArray_Free(DqnArray *const array) +{ + if (array && array->data) + { + // TODO(doyle): Right now we assume free always works, and it probably should? + size_t sizeToFree = (size_t)array->capacity * sizeof(T); + DqnMemAPICallbackInfo info = DqnMemAPIInternal_CallbackInfoAskFree( + array->memAPI, array->data, sizeToFree); + array->memAPI.callback(info, NULL); + array->data = NULL; + + array->count = 0; + array->capacity = 0; + return true; + } + + return false; +} + +template +bool DqnArray_Grow(DqnArray *const array) { if (!array || !array->data) return false; @@ -518,7 +528,7 @@ bool DqnArray_Grow(DqnArray *array) } template -T *DqnArray_Push(DqnArray *array, T item) +T *DqnArray_Push(DqnArray *const array, const T item) { if (!array) return NULL; @@ -544,7 +554,7 @@ void DqnArray_Pop(DqnArray *array) } template -T *DqnArray_Get(DqnArray *array, u64 index) +T *DqnArray_Get(DqnArray *const array, const u64 index) { T *result = NULL; if (index >= 0 && index <= array->count) result = &array->data[index]; @@ -552,7 +562,7 @@ T *DqnArray_Get(DqnArray *array, u64 index) } template -bool DqnArray_Clear(DqnArray *array) +bool DqnArray_Clear(DqnArray *const array) { if (array) { @@ -564,7 +574,7 @@ bool DqnArray_Clear(DqnArray *array) } template -bool DqnArray_Remove(DqnArray *array, u64 index) +bool DqnArray_Remove(DqnArray *const array, const u64 index) { if (!array) return false; if (index >= array->count) return false; @@ -583,7 +593,7 @@ bool DqnArray_Remove(DqnArray *array, u64 index) } template -bool DqnArray_RemoveStable(DqnArray *array, u64 index) +bool DqnArray_RemoveStable(DqnArray *const array, const u64 index) { if (!array) return false; if (index >= array->count) return false; @@ -609,6 +619,17 @@ bool DqnArray_RemoveStable(DqnArray *array, u64 index) array->count--; return true; } + +template void DqnArray::Init (const size_t capacity, DqnMemAPI memAPI) { DqnArray_Init(this, capacity, memAPI); } +template bool DqnArray::Free () { return DqnArray_Free(this); } +template bool DqnArray::Grow () { return DqnArray_Grow(this);} +template T* DqnArray::Push (const T item) { return DqnArray_Push(this, item); } +template void DqnArray::Pop () { DqnArray_Pop(this); } +template T* DqnArray::Get (const u64 index) { return DqnArray_Get(this, index); } +template bool DqnArray::Clear() { return DqnArray_Clear (this); } +template bool DqnArray::Remove (const u64 index) { return DqnArray_Remove(this, index); } +template bool DqnArray::RemoveStable(const u64 index) { return DqnArray_RemoveStable(this, u64 index); } + #endif // DQN_CPP_MODE //////////////////////////////////////////////////////////////////////////////// @@ -954,10 +975,9 @@ enum DqnFileAction typedef struct DqnFile { - u32 permissionFlags; + u32 permissionFlags; void *handle; size_t size; - } DqnFile; // NOTE: W(ide) versions of functions only work on Win32, since Unix is UTF-8 compatible.