14#define X(type) TYPE_##type,
23 typedef struct type##_data type##_data_t;
32 void type##_create(void *const element, void *const param); \
33 int type##_make_sound(const void *const element, void *const param); \
38typedef struct object_header
51typedef struct cat_data
53 const char *favorite_fish;
57typedef struct dog_data
59 const char *owners_name;
66#define X(type) type##_data_t type;
72typedef int (*make_sound_t) (
const void *
const element,
void *
const param);
74int make_sound(
const void *
const element,
void *
const param)
77 static make_sound_t table[TYPE_LAST] = {
78#define X(type) type##_make_sound,
83 const object_header_t* header = element;
84 return table[header->type](element, param);
88void dog_create(
void *
const element,
void *
const param)
90 object_header_t *
object = element;
91 object->type = TYPE_dog;
92 dog_data_t *data = (dog_data_t*)&object->data;
93 *data = *(dog_data_t*)param;
97void cat_create(
void *
const element,
void *
const param)
99 object_header_t *
object = element;
100 object->type = TYPE_cat;
101 cat_data_t *data = (cat_data_t*)&object->data;
102 *data = *(cat_data_t*)param;
106int cat_make_sound(
const void *
const element,
void *
const param)
115int dog_make_sound(
const void *
const element,
void *
const param)
126 const size_t size = 10;
127 vector_t *v =
vector_create(.element_size = (
sizeof(data_t) +
sizeof(object_header_t)), .initial_cap = size);
128 for (
size_t i = 0; i < size; ++i)
133 dog_create(
vector_get(v, i), &(dog_data_t){.owners_name =
"John"});
137 cat_create(
vector_get(v, i), &(cat_data_t){.favorite_fish =
"Salmon"});
void * vector_get(const vector_t *const vector, const size_t index)
Returns pointer for the element at index.
int vector_foreach(const vector_t *const vector, const size_t limit, const foreach_t func, void *const param)
Perform immutable action on each element of the vector.
#define vector_create(...)
Vector constructor.
void vector_destroy(vector_t *const vector)
Deallocates vector.
Vector control structure type.
Public interface of the vector.