160 lines
7.4 KiB
C
160 lines
7.4 KiB
C
#ifndef FX_VECTOR_H_
|
|
#define FX_VECTOR_H_
|
|
|
|
#include <fx/misc.h>
|
|
#include <fx/status.h>
|
|
#include <stddef.h>
|
|
|
|
typedef struct fx_vector_ops {
|
|
fx_status (*v_copy)(void *, const void *, size_t);
|
|
fx_status (*v_move)(void *, void *, size_t);
|
|
fx_status (*v_destroy)(void *);
|
|
} fx_vector_ops;
|
|
|
|
#define FX_VECTOR_DEFINE(type, name) \
|
|
struct { \
|
|
size_t count; \
|
|
size_t max; \
|
|
type *items; \
|
|
} name = {0}
|
|
|
|
#define FX_VECTOR_DECLARE(type, name) \
|
|
struct { \
|
|
size_t count; \
|
|
size_t max; \
|
|
type *items; \
|
|
} name
|
|
|
|
#define FX_VECTOR_ITEM(name, index) name.items[index]
|
|
#define FX_VECTOR_ITEM_PTR(name, index) &name.items[index]
|
|
#define FX_VECTOR_COUNT(name) name.count
|
|
#define FX_VECTOR_MAX(name) name.max
|
|
|
|
/* use this macros in your function prototype, to allow the function to accept
|
|
* a reference to a vector as a parameter */
|
|
#define FX_VECTOR_REF_PARAM(type, name) \
|
|
type **name, size_t *name##_count, size_t *name##_max
|
|
/* use this macro to pass a reference to a vector as a parameter to a function
|
|
* whose prototype uses FX_VECTOR_REF_PARAM */
|
|
#define FX_VECTOR_REF(name) &(name.items), &(name.count), &(name.max)
|
|
/* use this macro to forward your reference to a vector (which you got via
|
|
* FX_VECTOR_REF_PARAM in your function prototype), to another function whose
|
|
* prototype also uses FX_VECTOR_REF_PARAM */
|
|
#define FX_VECTOR_REF2(name) name, name##_count, name##_max
|
|
|
|
/* use these functions if you're accessing a vector directly. */
|
|
#define fx_vector_push_back(vector, ptr, ops) \
|
|
__fx_vector_push_back( \
|
|
(void **)&(vector.items), \
|
|
ptr, \
|
|
sizeof *ptr, \
|
|
&(vector.count), \
|
|
&(vector.max), \
|
|
ops)
|
|
#define fx_vector_pop_back(vector, ops) \
|
|
__fx_vector_pop_back( \
|
|
(void **)&(vector.items), \
|
|
sizeof *vector.items, \
|
|
&(vector.count), \
|
|
&(vector.max), \
|
|
ops)
|
|
#define fx_vector_emplace_back(vector, ops) \
|
|
__fx_vector_emplace_back( \
|
|
(void **)&(vector.items), \
|
|
sizeof *vector.items, \
|
|
&(vector.count), \
|
|
&(vector.max), \
|
|
ops)
|
|
#define fx_vector_trim(vector, ops) \
|
|
__fx_vector_trim( \
|
|
(void **)&(vector.items), \
|
|
sizeof *vector.items, \
|
|
&(vector.count), \
|
|
&(vector.max), \
|
|
ops)
|
|
#define fx_vector_destroy(vector, ops) \
|
|
__fx_vector_destroy( \
|
|
(void **)&(vector.items), \
|
|
sizeof *vector.items, \
|
|
&(vector.count), \
|
|
&(vector.max), \
|
|
ops)
|
|
|
|
/* use these functions if you're accessing a vector as a reference
|
|
* via FX_VECTOR_REF_PARAM. */
|
|
#define fx_vector_ref_push_back(vector, ptr, ops) \
|
|
__fx_vector_push_back( \
|
|
(void **)(vector), \
|
|
ptr, \
|
|
sizeof *ptr, \
|
|
(vector##_count), \
|
|
(vector##_max), \
|
|
ops)
|
|
#define fx_vector_ref_pop_back(vector, ops) \
|
|
__fx_vector_pop_back( \
|
|
(void **)(vector), \
|
|
sizeof **vector, \
|
|
(vector##_count), \
|
|
(vector##_max), \
|
|
ops)
|
|
#define fx_vector_ref_emplace_back(vector, ops) \
|
|
__fx_vector_emplace_back( \
|
|
(void **)(vector), \
|
|
sizeof **vector, \
|
|
(vector##_count), \
|
|
(vector##_max), \
|
|
ops)
|
|
#define fx_vector_ref_trim(vector, ops) \
|
|
__fx_vector_trim( \
|
|
(void **)(vector), \
|
|
sizeof **vector, \
|
|
(vector##_count), \
|
|
(vector##_max), \
|
|
ops)
|
|
#define fx_vector_ref_destroy(vector, ops) \
|
|
__fx_vector_destroy( \
|
|
(void **)(vector), \
|
|
sizeof **vector, \
|
|
(vector##_count), \
|
|
(vector##_max), \
|
|
ops)
|
|
#define fx_vector_ref_get_item(vector, index) (*vector)[index]
|
|
#define fx_vector_ref_get_item_ptr(vector, index) (&(*vector)[index])
|
|
#define fx_vector_ref_get_count(vector) *(vector##_count)
|
|
#define fx_vector_ref_get_max(vector) *(vector##_count)
|
|
|
|
/* don't use these functions */
|
|
FX_API int __fx_vector_push_back(
|
|
void **vector,
|
|
const void *item,
|
|
size_t item_size,
|
|
size_t *count,
|
|
size_t *max,
|
|
const struct fx_vector_ops *ops);
|
|
FX_API void __fx_vector_pop_back(
|
|
void **vector,
|
|
size_t item_size,
|
|
size_t *count,
|
|
size_t *max,
|
|
const struct fx_vector_ops *ops);
|
|
FX_API void *__fx_vector_emplace_back(
|
|
void **vector,
|
|
size_t item_size,
|
|
size_t *count,
|
|
size_t *max,
|
|
const struct fx_vector_ops *ops);
|
|
FX_API void __fx_vector_trim(
|
|
void **vector,
|
|
size_t item_size,
|
|
size_t *count,
|
|
size_t *max,
|
|
const struct fx_vector_ops *ops);
|
|
FX_API void __fx_vector_destroy(
|
|
void **vector,
|
|
size_t item_size,
|
|
size_t *count,
|
|
size_t *max,
|
|
const struct fx_vector_ops *ops);
|
|
|
|
#endif
|