Added check to make sure the user passes at least one function pointer

when adding an object to the store. Otherwise, the object isn't added.
This commit is contained in:
abdelrahman committed 2023-01-02 20:44:16 +00:00
1 parent 681fa2a5dc
commit be547b00ed
1 file changed
+12 -12
+12 -12
View File
@@ -16,11 +16,11 @@
struct object_store {
u64 count;
u64 deleted_count;
ObjectID reusable_ids[MAX_OBJ_COUNT];
void *object_ptrs[MAX_OBJ_COUNT];
EventHandlerPtr event_handlers[MAX_OBJ_COUNT];
UpdateFuncPtr update_funcs[MAX_OBJ_COUNT];
RenderFuncPtr render_funcs[MAX_OBJ_COUNT];
ObjectID reusable_ids[MAX_OBJ_COUNT];
};
bool object_exists(ObjectStore *os, void *object);
@@ -35,18 +35,22 @@ ObjectStore *create_object_store() {
os->count = 0;
os->deleted_count = 0;
memset(os->reusable_ids, 0, sizeof(os->reusable_ids));
memset(os->object_ptrs, 0, sizeof(os->object_ptrs));
memset(os->event_handlers, 0, sizeof(os->event_handlers));
memset(os->update_funcs, 0, sizeof(os->update_funcs));
memset(os->render_funcs, 0, sizeof(os->render_funcs));
memset(os->reusable_ids, 0, sizeof(os->reusable_ids));
return os;
}
ObjectID add_object_to_store(ObjectStore *os, void *object, EventHandlerPtr eh,
UpdateFuncPtr uf, RenderFuncPtr rf) {
if (!os || !object || object_exists(os, object) ||
// If all function pointers are NULL, no point of adding the object to the
// store
bool no_funcs_passed = !eh && !uf && !rf;
if (!os || !object || object_exists(os, object) || no_funcs_passed ||
(os->count + 1 == MAX_OBJ_COUNT && os->deleted_count == 0)) {
#ifdef DEBUG_ENGINE
printf("Failed to add object\n");
@@ -136,15 +140,11 @@ void destroy_object_store(ObjectStore **os) {
return;
}
// TODO (Abdelrahman): Figure out if there is a better way to remove all of
// the existing objects without having to loop for the maximum number of
// objects allowed.
for (size_t i = 0; i < MAX_OBJ_COUNT; ++i) {
(*os)->object_ptrs[i] = NULL;
(*os)->event_handlers[i] = NULL;
(*os)->update_funcs[i] = NULL;
(*os)->render_funcs[i] = NULL;
}
memset((*os)->object_ptrs, 0, sizeof((*os)->object_ptrs));
memset((*os)->event_handlers, 0, sizeof((*os)->event_handlers));
memset((*os)->update_funcs, 0, sizeof((*os)->update_funcs));
memset((*os)->render_funcs, 0, sizeof((*os)->render_funcs));
memset((*os)->reusable_ids, 0, sizeof((*os)->reusable_ids));
(*os)->count = 0;
(*os)->deleted_count = 0;