Migrate C++ array API
This commit is contained in:
@@ -20,23 +20,41 @@ BEGIN_C_LINKAGE
|
||||
#define wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY) ((TYPE *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(TYPE)))
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
#define wapp_i32_array(...) ([&]() { \
|
||||
wapp_persist i32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
|
||||
return I32Array{ \
|
||||
buf, \
|
||||
wapp_misc_utils_va_args_count(i32, __VA_ARGS__), \
|
||||
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i32, __VA_ARGS__) * 2), \
|
||||
sizeof(i32) \
|
||||
}; \
|
||||
#define wapp_array(TYPE, ...) ([&]() { \
|
||||
u64 capacity = _calc_array_capacity(TYPE, __VA_ARGS__); \
|
||||
\
|
||||
TYPE items[_calc_array_capacity(TYPE, __VA_ARGS__)] = {__VA_ARGS__}; \
|
||||
\
|
||||
wapp_persist u8 array[ \
|
||||
sizeof(ArrayHeader) + _calc_array_capacity(TYPE, __VA_ARGS__) * sizeof(TYPE) \
|
||||
] = {0}; \
|
||||
ArrayHeader *header = (ArrayHeader *)array; \
|
||||
header->magic = WAPP_ARRAY_MAGIC; \
|
||||
header->count = _calc_array_count(TYPE, __VA_ARGS__); \
|
||||
header->capacity = _calc_array_capacity(TYPE, __VA_ARGS__); \
|
||||
header->item_size = sizeof(TYPE); \
|
||||
\
|
||||
u8 *buf = (u8 *)(header + 1); \
|
||||
memcpy(buf, items, capacity * sizeof(TYPE)); \
|
||||
return (TYPE *)buf; \
|
||||
}())
|
||||
#define wapp_i32_array_with_capacity(CAPACITY) ([&]() { \
|
||||
wapp_persist i32 buf[CAPACITY] = {}; \
|
||||
return I32Array{buf, 0, CAPACITY, sizeof(i32)}; \
|
||||
#define wapp_array_with_capacity(TYPE, CAPACITY) ([&]() { \
|
||||
wapp_persist u8 array[ \
|
||||
sizeof(ArrayHeader) + CAPACITY * sizeof(TYPE) \
|
||||
] = {0}; \
|
||||
ArrayHeader *header = (ArrayHeader *)array; \
|
||||
header->magic = WAPP_ARRAY_MAGIC; \
|
||||
header->count = 0; \
|
||||
header->capacity = CAPACITY; \
|
||||
header->item_size = sizeof(TYPE); \
|
||||
\
|
||||
return (TYPE *)(header + 1); \
|
||||
}())
|
||||
#define wapp_i32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
|
||||
*_i32_array_pop(ARRAY_PTR) : \
|
||||
i32{} \
|
||||
)
|
||||
#define wapp_array_pop(TYPE, ARRAY_PTR) \
|
||||
(ARRAY_PTR != NULL && _array_count((u8 *)ARRAY_PTR, sizeof(TYPE)) > 0 ? \
|
||||
*((TYPE *)_array_pop((u8 *)ARRAY_PTR, sizeof(TYPE))) : \
|
||||
TYPE{} \
|
||||
)
|
||||
#else
|
||||
#define _stack_array(TYPE, SIZE) struct { ArrayHeader header; TYPE items[SIZE]; }
|
||||
#define wapp_array(TYPE, ...) \
|
||||
@@ -63,6 +81,13 @@ BEGIN_C_LINKAGE
|
||||
.items = {0}, \
|
||||
}.items \
|
||||
)
|
||||
#define wapp_array_pop(TYPE, ARRAY_PTR) \
|
||||
(ARRAY_PTR != NULL && _array_count((u8 *)ARRAY_PTR, sizeof(TYPE)) > 0 ? \
|
||||
*((TYPE *)_array_pop((u8 *)ARRAY_PTR, sizeof(TYPE))) : \
|
||||
(TYPE){0} \
|
||||
)
|
||||
#endif // !WAPP_PLATFORM_CPP
|
||||
|
||||
#define wapp_array_count(TYPE, ARRAY_PTR) \
|
||||
_array_count((u8 *)ARRAY_PTR, sizeof(TYPE))
|
||||
#define wapp_array_capacity(TYPE, ARRAY_PTR) \
|
||||
@@ -85,14 +110,8 @@ BEGIN_C_LINKAGE
|
||||
(TYPE *)_array_extend_alloc(ALLOCATOR_PTR, (u8 *)DST_ARRAY_PTR, (u8 *)SRC_ARRAY_PTR, sizeof(TYPE))
|
||||
#define wapp_array_copy_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY_PTR, SRC_ARRAY_PTR) \
|
||||
(TYPE *)_array_copy_alloc(ALLOCATOR_PTR, (u8 *)DST_ARRAY_PTR, (u8 *)SRC_ARRAY_PTR, sizeof(TYPE))
|
||||
#define wapp_array_pop(TYPE, ARRAY_PTR) \
|
||||
(ARRAY_PTR != NULL && _array_count((u8 *)ARRAY_PTR, sizeof(TYPE)) > 0 ? \
|
||||
*((TYPE *)_array_pop((u8 *)ARRAY_PTR, sizeof(TYPE))) : \
|
||||
(TYPE){0} \
|
||||
)
|
||||
#define wapp_array_clear(TYPE, ARRAY_PTR) \
|
||||
_array_clear((u8 *)ARRAY_PTR, sizeof(TYPE))
|
||||
#endif // !WAPP_PLATFORM_CPP
|
||||
|
||||
typedef struct header ArrayHeader;
|
||||
struct header {
|
||||
@@ -102,32 +121,26 @@ struct header {
|
||||
u64 item_size;
|
||||
};
|
||||
|
||||
typedef struct array Array;
|
||||
struct array {
|
||||
ArrayHeader header;
|
||||
u8 *items;
|
||||
};
|
||||
|
||||
typedef struct GenericArray GenericArray;
|
||||
struct GenericArray {
|
||||
ArrayHeader header;
|
||||
void *items;
|
||||
};
|
||||
|
||||
u64 _array_count(u8 *array, u64 item_size);
|
||||
u64 _array_capacity(u8 *array, u64 item_size);
|
||||
u64 _array_item_size(u8 *array, u64 item_size);
|
||||
u8 *_array_get(u8 *array, u64 index, u64 item_size);
|
||||
u64 _array_count(u8 *array, u64 item_size);
|
||||
u64 _array_capacity(u8 *array, u64 item_size);
|
||||
u64 _array_item_size(u8 *array, u64 item_size);
|
||||
u8 *_array_get(u8 *array, u64 index, u64 item_size);
|
||||
void _array_set(u8 *array, u64 index, u8 *value, u64 item_size);
|
||||
void _array_append_capped(u8 *array, u8 *value, u64 item_size);
|
||||
void _array_extend_capped(u8 *dst_array, const u8 *src_array, u64 item_size);
|
||||
void _array_copy_capped(u8 *dst_array, const u8 *src_array, u64 item_size);
|
||||
u8 *_array_append_alloc(const Allocator *allocator, u8 *array, u8 *value, u64 item_size);
|
||||
u8 *_array_extend_alloc(const Allocator *allocator, u8 *dst_array, const u8 *src_array, u64 item_size);
|
||||
u8 *_array_copy_alloc(const Allocator *allocator, u8 *dst_array, const u8 *src_array, u64 item_size);
|
||||
u8 *_array_pop(u8 *array, u64 item_size);
|
||||
u8 *_array_append_alloc(const Allocator *allocator, u8 *array, u8 *value, u64 item_size);
|
||||
u8 *_array_extend_alloc(const Allocator *allocator, u8 *dst_array, const u8 *src_array, u64 item_size);
|
||||
u8 *_array_copy_alloc(const Allocator *allocator, u8 *dst_array, const u8 *src_array, u64 item_size);
|
||||
u8 *_array_pop(u8 *array, u64 item_size);
|
||||
void _array_clear(u8 *array, u64 item_size);
|
||||
u8 *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size);
|
||||
u8 *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size);
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
END_C_LINKAGE
|
||||
|
||||
@@ -99,7 +99,7 @@ void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity);
|
||||
void wapp_str8_format(Str8 *dst, const char *format, ...);
|
||||
void wapp_str8_to_lower(Str8 *dst, Str8RO *src);
|
||||
void wapp_str8_to_upper(Str8 *dst, Str8RO *src);
|
||||
void wapp_str8_from_bytes(Str8 *dst, const u8 *src);
|
||||
void wapp_str8_from_bytes(Str8 *dst, const u8 *src_byte_array);
|
||||
|
||||
/**
|
||||
* Str8 find functions
|
||||
|
||||
Reference in New Issue
Block a user