fsautoproc
Basic file transformation automation management utility
Loading...
Searching...
No Matches
set.h
Go to the documentation of this file.
1#ifndef FSAUTOPROC_SET_H
2#define FSAUTOPROC_SET_H
3
8#define DEFINE_SET_TYPE(typ, inner) \
9 typedef inner typ##_inner_item; \
10 typedef struct { \
11 int count; /* number of elements in the set */ \
12 typ##_inner_item* items; /* array of elements or NULL */ \
13 } typ;
14
18#define DEFINE_SET_FREE_STATIC(typ) \
19 static void typ##_free(typ* set) { \
20 if (!set) return; \
21 je_free(set->items); \
22 je_free(set); \
23 }
24
32#define DEFINE_SET_ALLOC_STATIC(typ) \
33 static typ* typ##_alloc(int count) { \
34 void* items = NULL; \
35 if (count > 0) { \
36 items = je_calloc(count, sizeof(typ##_inner_item)); \
37 if (!items) return NULL; \
38 } \
39 typ* set = je_malloc(sizeof(typ)); \
40 if (!set) { \
41 je_free(items); \
42 return NULL; \
43 } \
44 set->count = count; \
45 set->items = items; \
46 return set; \
47 }
48
53#define DEFINE_SET_FOR_EACH_STATIC(typ) \
54 static void typ##_for_each(typ* set, void (*func)(typ##_inner_item*)) { \
55 for (int i = 0; set && set->items && i < set->count; i++) \
56 func(&set->items[i]); \
57 }
58
65#define SET_AT(name, index) \
66 (name != NULL && name->items != NULL && index >= 0 && index < name->count \
67 ? &(name)->items[index] \
68 : NULL)
69
70#endif//FSAUTOPROC_SET_H