2022-10-27 15:16:54 -06:00
|
|
|
#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;
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-03 19:55:56 -06:00
|
|
|
void ARC_Handler_Create(ARC_Handler **handler, uint32_t dataSize){
|
2022-10-27 15:16:54 -06:00
|
|
|
*handler = (ARC_Handler *) malloc(sizeof(ARC_Handler));
|
2022-11-03 19:55:56 -06:00
|
|
|
ARC_Vector_Create(&((*handler)->data), dataSize);
|
|
|
|
|
ARC_Vector_Create(&((*handler)->trash), dataSize);
|
2022-10-27 15:16:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ARC_Handler_Destroy(ARC_Handler *handler, ARC_Handler_CleanDataFn cleanfn){
|
|
|
|
|
ARC_Handler_Clear(handler);
|
|
|
|
|
ARC_Handler_Clean(handler, cleanfn);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int8_t ARC_Handler_RemoveCompareFn(void *a, void *b){ return a == b; }
|
|
|
|
|
|
|
|
|
|
void ARC_Handler_Remove(ARC_Handler *handler, void *data){
|
|
|
|
|
ARC_Vector_Add(handler->trash, data);
|
2022-10-29 22:09:48 -06:00
|
|
|
ARC_Vector_Remove(handler->data, data, ARC_Handler_RemoveCompareFn);
|
2022-10-27 15:16:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ARC_Handler_RemoveIndex(ARC_Handler *handler, uint32_t *index){
|
|
|
|
|
void *data = ARC_Vector_Get(handler->data, index);
|
|
|
|
|
ARC_Vector_RemoveIndex(handler->data, index);
|
|
|
|
|
ARC_Vector_Add(handler->trash, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ARC_Handler_Iterate(ARC_Handler *handler, ARC_Handler_DataFn datafn){
|
|
|
|
|
for(uint32_t i = 0; i < *ARC_Vector_Size(handler->data); i++){
|
|
|
|
|
datafn(ARC_Vector_Get(handler->data, &i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ARC_Handler_Clear(ARC_Handler *handler){
|
|
|
|
|
while(*ARC_Vector_Size(handler->data)){
|
|
|
|
|
ARC_Handler_Remove(handler, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ARC_Handler_Clean(ARC_Handler *handler, ARC_Handler_CleanDataFn cleanfn){
|
|
|
|
|
uint32_t i = 0;
|
|
|
|
|
while(*ARC_Vector_Size(handler->trash)){
|
|
|
|
|
void *data = ARC_Vector_Get(handler->trash, &i);
|
|
|
|
|
cleanfn(data);
|
|
|
|
|
ARC_Vector_RemoveIndex(handler->trash, &i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t *ARC_Handler_Size(ARC_Handler *handler){
|
|
|
|
|
return ARC_Vector_Size(handler->data);
|
|
|
|
|
}
|