Use a macro to reduce code duplication
[openal-soft.git] / Alc / vector.h
blob852303787b396d131780e82c5f8e415c8f2ceaf0
1 #ifndef AL_VECTOR_H
2 #define AL_VECTOR_H
4 #include <stdlib.h>
6 #include <AL/al.h>
8 /* "Base" vector type, designed to alias with the actual vector types. */
9 typedef struct vector__s {
10 size_t Capacity;
11 size_t Size;
12 } *vector_;
14 #define TYPEDEF_VECTOR(T, N) typedef struct { \
15 size_t Capacity; \
16 size_t Size; \
17 T Data[]; \
18 } _##N; \
19 typedef _##N* N; \
20 typedef const _##N* const_##N;
22 #define VECTOR(T) struct { \
23 size_t Capacity; \
24 size_t Size; \
25 T Data[]; \
28 #define VECTOR_INIT(_x) do { (_x) = NULL; } while(0)
29 #define VECTOR_INIT_STATIC() NULL
30 #define VECTOR_DEINIT(_x) do { free((_x)); (_x) = NULL; } while(0)
32 /* Helper to increase a vector's reserve. Do not call directly. */
33 ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_size, size_t obj_count, ALboolean exact);
34 #define VECTOR_RESERVE(_x, _c) (vector_reserve((char*)&(_x), sizeof(*(_x)), sizeof((_x)->Data[0]), (_c), AL_TRUE))
36 ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_size, size_t obj_count);
37 #define VECTOR_RESIZE(_x, _c) (vector_resize((char*)&(_x), sizeof(*(_x)), sizeof((_x)->Data[0]), (_c)))
39 #define VECTOR_CAPACITY(_x) ((_x) ? (_x)->Capacity : 0)
40 #define VECTOR_SIZE(_x) ((_x) ? (_x)->Size : 0)
42 #define VECTOR_ITER_BEGIN(_x) ((_x) ? (_x)->Data + 0 : NULL)
43 #define VECTOR_ITER_END(_x) ((_x) ? (_x)->Data + (_x)->Size : NULL)
45 ALboolean vector_insert(char *ptr, size_t base_size, size_t obj_size, void *ins_pos, const void *datstart, const void *datend);
46 #ifdef __GNUC__
47 #define TYPE_CHECK(T1, T2) __builtin_types_compatible_p(T1, T2)
48 #define VECTOR_INSERT(_x, _i, _s, _e) __extension__({ \
49 ALboolean _r; \
50 static_assert(TYPE_CHECK(__typeof((_x)->Data[0]), __typeof(*(_i))), "Incompatible insertion iterator"); \
51 static_assert(TYPE_CHECK(__typeof((_x)->Data[0]), __typeof(*(_s))), "Incompatible insertion source type"); \
52 static_assert(TYPE_CHECK(__typeof(*(_s)), __typeof(*(_e))), "Incompatible iterator sources"); \
53 _r = vector_insert((char*)&(_x), sizeof(*(_x)), sizeof((_x)->Data[0]), (_i), (_s), (_e)); \
54 _r; \
56 #else
57 #define VECTOR_INSERT(_x, _i, _s, _e) (vector_insert((char*)&(_x), sizeof(*(_x)), sizeof((_x)->Data[0]), (_i), (_s), (_e)))
58 #endif
60 #define VECTOR_PUSH_BACK(_x, _obj) (vector_reserve((char*)&(_x), sizeof(*(_x)), sizeof((_x)->Data[0]), VECTOR_SIZE(_x)+1, AL_FALSE) && \
61 (((_x)->Data[(_x)->Size++] = (_obj)),AL_TRUE))
62 #define VECTOR_POP_BACK(_x) ((void)((_x)->Size--))
64 #define VECTOR_BACK(_x) ((_x)->Data[(_x)->Size-1])
65 #define VECTOR_FRONT(_x) ((_x)->Data[0])
67 #define VECTOR_ELEM(_x, _o) ((_x)->Data[(_o)])
69 #define VECTOR_FOR_EACH(_t, _x, _f) do { \
70 _t *_iter = VECTOR_ITER_BEGIN((_x)); \
71 _t *_end = VECTOR_ITER_END((_x)); \
72 for(;_iter != _end;++_iter) \
73 _f(_iter); \
74 } while(0)
76 #define VECTOR_FIND_IF(_i, _t, _x, _f) do { \
77 _t *_iter = VECTOR_ITER_BEGIN((_x)); \
78 _t *_end = VECTOR_ITER_END((_x)); \
79 for(;_iter != _end;++_iter) \
80 { \
81 if(_f(_iter)) \
82 break; \
83 } \
84 (_i) = _iter; \
85 } while(0)
87 #endif /* AL_VECTOR_H */