Loading...
Searching...
No Matches
handler.c
Go to the documentation of this file.
#include "arc/std/handler.h"
#include "arc/std/errno.h"
#include "arc/std/vector.h"
#include <stdlib.h>
struct ARC_Handler {
ARC_Vector *data;
ARC_Vector *trash;
ARC_Handler_CleanDataFn cleanfn;
};
void ARC_Handler_Create(ARC_Handler **handler, ARC_Handler_CompareDataFn *compareFn, ARC_Handler_CleanDataFn cleanfn){
*handler = (ARC_Handler *) malloc(sizeof(ARC_Handler));
ARC_Vector_Create(&((*handler)->data), NULL, NULL);
ARC_Vector_Create(&((*handler)->trash), compareFn, NULL);
(*handler)->cleanfn = cleanfn;
}
void ARC_Handler_Destroy(ARC_Handler *handler){
ARC_Handler_Clear(handler);
ARC_Handler_Clean(handler);
ARC_Vector_Destroy(handler->data);
ARC_Vector_Destroy(handler->trash);
free(handler);
}
void ARC_Handler_Add(ARC_Handler *handler, void *data){
ARC_Vector_Add(handler->data, data);
}
void ARC_Handler_Remove(ARC_Handler *handler, void *data){
ARC_Vector_Add(handler->trash, data);
ARC_Vector_Remove(handler->data, data);
}
void ARC_Handler_RemoveIndex(ARC_Handler *handler, uint32_t index){
if(ARC_Vector_GetSize(handler->data) == 0){
return;
}
void *data = ARC_Vector_Get(handler->data, index);
ARC_Vector_Add(handler->trash, data);
ARC_Vector_RemoveIndex(handler->data, index);
}
void ARC_Handler_Iterate(ARC_Handler *handler, ARC_Handler_DataFn datafn){
for(uint32_t i = 0; i < ARC_Vector_GetSize(handler->data); i++){
datafn(ARC_Vector_Get(handler->data, i));
}
}
void ARC_Handler_Clear(ARC_Handler *handler){
uint32_t zeroIndex = 0;
while(ARC_Vector_GetSize(handler->data)){
ARC_Handler_RemoveIndex(handler, zeroIndex);
}
}
void ARC_Handler_Clean(ARC_Handler *handler){
uint32_t i = 0;
while(ARC_Vector_GetSize(handler->trash)){
void *data = ARC_Vector_Get(handler->trash, i);
if(handler->cleanfn){
handler->cleanfn(data);
}
ARC_Vector_RemoveIndex(handler->trash, i);
}
}
uint32_t ARC_Handler_GetSize(ARC_Handler *handler){
return ARC_Vector_GetSize(handler->data);
}
errno.h
ARC_Handler_Add
void ARC_Handler_Add(ARC_Handler *handler, void *data)
adds data to handler
Definition handler.c:31
ARC_Handler_Destroy
void ARC_Handler_Destroy(ARC_Handler *handler)
destroyes ARC_Handler type
Definition handler.c:21
ARC_Handler_Clear
void ARC_Handler_Clear(ARC_Handler *handler)
clears all data from handler and puts it in trash vector
Definition handler.c:56
ARC_Handler_GetSize
uint32_t ARC_Handler_GetSize(ARC_Handler *handler)
gets size of vector
Definition handler.c:76
ARC_Handler_Iterate
void ARC_Handler_Iterate(ARC_Handler *handler, ARC_Handler_DataFn datafn)
calls provided function on each element in handler
Definition handler.c:50
ARC_Handler_Create
void ARC_Handler_Create(ARC_Handler **handler, ARC_Handler_CompareDataFn *compareFn, ARC_Handler_CleanDataFn cleanfn)
creates ARC_Handler type
Definition handler.c:14
ARC_Handler_Clean
void ARC_Handler_Clean(ARC_Handler *handler)
clears trash from handler
Definition handler.c:63
ARC_Handler_Remove
void ARC_Handler_Remove(ARC_Handler *handler, void *data)
remove from handler
Definition handler.c:35
ARC_Handler_RemoveIndex
void ARC_Handler_RemoveIndex(ARC_Handler *handler, uint32_t index)
remove from handler
Definition handler.c:40
handler.h
ARC_Handler_CompareDataFn
ARC_Bool(* ARC_Handler_CompareDataFn)(void *a, void *b)
data comparison function ptr
Definition handler.h:26
ARC_Handler_CleanDataFn
void(* ARC_Handler_CleanDataFn)(void *data)
a function that will be used during destruction of trash vector
Definition handler.h:40
ARC_Handler_DataFn
void(* ARC_Handler_DataFn)(void *data)
a function that will take iterated data
Definition handler.h:33
ARC_Handler
Definition handler.c:7
ARC_Handler::data
ARC_Vector * data
Definition handler.c:8
ARC_Handler::trash
ARC_Vector * trash
Definition handler.c:9
ARC_Handler::cleanfn
ARC_Handler_CleanDataFn cleanfn
Definition handler.c:11
ARC_Vector
a private dynamic array type
Definition vector.c:12
vector.h
ARC_Vector_GetSize
uint32_t ARC_Vector_GetSize(ARC_Vector *vector)
gets the current size of an ARC_Vector as an unsigned 32 bit integer
Definition vector.c:149
ARC_Vector_Get
void * ARC_Vector_Get(ARC_Vector *vector, uint32_t index)
gets an item from an ARC_Vector at a position index
Definition vector.c:153
ARC_Vector_Add
void ARC_Vector_Add(ARC_Vector *vector, void *data)
adds an item to an ARC_Vector
Definition vector.c:70
ARC_Vector_Remove
void ARC_Vector_Remove(ARC_Vector *vector, void *data)
removes an item from a matching item in an ARC_Vector
Definition vector.c:97
ARC_Vector_Destroy
void ARC_Vector_Destroy(ARC_Vector *vector)
destroys an ARC_Vector
Definition vector.c:54
ARC_Vector_RemoveIndex
void ARC_Vector_RemoveIndex(ARC_Vector *vector, uint32_t index)
removes an item from an ARC_Vector at an index
Definition vector.c:110
ARC_Vector_Create
void ARC_Vector_Create(ARC_Vector **vector, ARC_Vector_CompareDataFn *compareDataFn, ARC_Vector_DestroyDataFn *destroyDataFn)
creates an ARC_Vector which is an "expandable" array
Definition vector.c:31