Update C++ API

This commit is contained in:
2025-12-14 23:46:28 +00:00
parent dcadacdf2b
commit a2f42c135f
4 changed files with 104 additions and 109 deletions

View File

@@ -30,43 +30,38 @@ BEGIN_C_LINKAGE
} NAME } NAME
#ifdef WAPP_PLATFORM_CPP #ifdef WAPP_PLATFORM_CPP
#define wapp_array(TYPE, ...) ([&]() { \ #define wapp_array(ELEM_TYPE, ARRAY_TYPE, ...) ([&]() { \
u64 capacity = _calc_array_capacity(TYPE, __VA_ARGS__); \ wapp_persist ELEM_TYPE items[sizeof(ELEM_TYPE) == sizeof(*((ARRAY_TYPE{}).items)) ? \
_calc_array_capacity(ELEM_TYPE, __VA_ARGS__) : -1 \
] = {__VA_ARGS__}; \
\ \
TYPE items[_calc_array_capacity(TYPE, __VA_ARGS__)] = {__VA_ARGS__}; \ return ARRAY_TYPE{ \
\ WAPP_ARRAY_MAGIC, \
wapp_persist u8 array[ \ _calc_array_count(ELEM_TYPE, __VA_ARGS__), \
sizeof(ArrayHeader) + _calc_array_capacity(TYPE, __VA_ARGS__) * sizeof(TYPE) \ _calc_array_capacity(ELEM_TYPE, __VA_ARGS__), \
] = {0}; \ sizeof(ELEM_TYPE), \
ArrayHeader *header = (ArrayHeader *)array; \ items, \
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_array_with_capacity(TYPE, CAPACITY) ([&]() { \ #define wapp_array_with_capacity(ELEM_TYPE, ARRAY_TYPE, CAPACITY) ([&]() { \
wapp_persist u8 array[ \ wapp_persist ELEM_TYPE items[sizeof(ELEM_TYPE) == sizeof(*((ARRAY_TYPE{}).items)) ? \
sizeof(ArrayHeader) + CAPACITY * sizeof(TYPE) \ CAPACITY : -1] = {}; \
] = {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); \ return ARRAY_TYPE{ \
WAPP_ARRAY_MAGIC, \
0, \
CAPACITY, \
sizeof(ELEM_TYPE), \
items, \
}; \
}()) }())
#define wapp_array_pop(TYPE, ARRAY_PTR) ([&]() { \ #define wapp_array_pop(ELEM_TYPE, ARRAY_PTR) ([&]() { \
if (ARRAY_PTR == NULL || _array_count((u8 *)ARRAY_PTR) == 0) { \ if (ARRAY_PTR == NULL || (ARRAY_PTR)->count == 0) { \
TYPE result{}; \ ELEM_TYPE result{}; \
return result; \ return result; \
} \ } \
\ \
return *((TYPE *)_array_pop((u8 *)ARRAY_PTR, sizeof(TYPE))); \ return *((ELEM_TYPE *)_array_pop((Array *)ARRAY_PTR, sizeof(ELEM_TYPE))); \
}()) }())
#else #else
#define wapp_array(ELEM_TYPE, ARRAY_TYPE, ...) \ #define wapp_array(ELEM_TYPE, ARRAY_TYPE, ...) \

View File

@@ -4,15 +4,15 @@
TestFuncResult test_i32_array(void) { TestFuncResult test_i32_array(void) {
b8 result; b8 result;
i32 *array = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7); I32Array array = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7);
result = wapp_array_count(array) == 7 && wapp_array_capacity(array) == 16; result = array.count == 7 && array.capacity == 16;
i32 *item; i32 *item;
u64 count = wapp_array_count(array); u64 count = array.count;
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
item = wapp_array_get(i32, array, index); item = wapp_array_get(i32, &array, index);
result = result && item && (*item == (i32)(index + 1)); result = result && item && (*item == (i32)(index + 1));
++index; ++index;
@@ -25,8 +25,8 @@ TestFuncResult test_i32_array(void) {
TestFuncResult test_i32_array_with_capacity(void) { TestFuncResult test_i32_array_with_capacity(void) {
b8 result; b8 result;
i32 *array = wapp_array_with_capacity(i32, 64); I32Array array = wapp_array_with_capacity(i32, I32Array, 64);
result = wapp_array_count(array) == 0 && wapp_array_capacity(array) == 64; result = array.count == 0 && array.capacity == 64;
return wapp_tester_result(result); return wapp_tester_result(result);
} }
@@ -34,14 +34,14 @@ TestFuncResult test_i32_array_with_capacity(void) {
TestFuncResult test_i32_array_get(void) { TestFuncResult test_i32_array_get(void) {
b8 result = true; b8 result = true;
i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8);
i32 *item; i32 *item;
u64 count = wapp_array_count(array); u64 count = array.count;
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
item = wapp_array_get(i32, array, index); item = wapp_array_get(i32, &array, index);
result = result && item && (*item == (i32)index); result = result && item && (*item == (i32)index);
++index; ++index;
@@ -54,16 +54,16 @@ TestFuncResult test_i32_array_get(void) {
TestFuncResult test_i32_array_set(void) { TestFuncResult test_i32_array_set(void) {
b8 result = true; b8 result = true;
i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8);
i32 *item; i32 *item;
u64 count = wapp_array_count(array); u64 count = array.count;
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
i32 num = (i32)(index * 2); i32 num = (i32)(index * 2);
wapp_array_set(i32, array, index, &num); wapp_array_set(i32, &array, index, &num);
item = wapp_array_get(i32, array, index); item = wapp_array_get(i32, &array, index);
result = result && item && (*item == (i32)(index * 2)); result = result && item && (*item == (i32)(index * 2));
++index; ++index;
@@ -76,19 +76,19 @@ TestFuncResult test_i32_array_set(void) {
TestFuncResult test_i32_array_append_capped(void) { TestFuncResult test_i32_array_append_capped(void) {
b8 result; b8 result;
i32 *array = wapp_array_with_capacity(i32, 64); I32Array array = wapp_array_with_capacity(i32, I32Array, 64);
i32 item1 = 10; i32 item1 = 10;
wapp_array_append_capped(i32, array, &item1); wapp_array_append_capped(i32, &array, &item1);
result = wapp_array_count(array) == 1; result = array.count == 1;
i32 *item = wapp_array_get(i32, array, 0); i32 *item = wapp_array_get(i32, &array, 0);
result = result && item && *item == 10; result = result && item && *item == 10;
array = wapp_array(i32, 1); array = wapp_array(i32, I32Array, 1);
i32 item2 = 10; i32 item2 = 10;
wapp_array_append_capped(i32, array, &item2); wapp_array_append_capped(i32, &array, &item2);
result = result && wapp_array_count(array) == 2; result = result && array.count == 2;
return wapp_tester_result(result); return wapp_tester_result(result);
} }
@@ -96,14 +96,14 @@ TestFuncResult test_i32_array_append_capped(void) {
TestFuncResult test_i32_array_extend_capped(void) { TestFuncResult test_i32_array_extend_capped(void) {
b8 result; b8 result;
i32 *array1 = wapp_array(i32, 1, 2, 3, 4); I32Array array1 = wapp_array(i32, I32Array, 1, 2, 3, 4);
i32 *array2 = wapp_array(i32, 10, 20); I32Array array2 = wapp_array(i32, I32Array, 10, 20);
result = wapp_array_count(array1) == 4 && wapp_array_count(array2) == 2; result = array1.count == 4 && array2.count == 2;
wapp_array_extend_capped(i32, array1, array2); wapp_array_extend_capped(i32, &array1, &array2);
result = result && wapp_array_count(array1) == 6; result = result && array1.count == 6;
return wapp_tester_result(result); return wapp_tester_result(result);
} }
@@ -111,31 +111,31 @@ TestFuncResult test_i32_array_extend_capped(void) {
TestFuncResult test_i32_array_copy_capped(void) { TestFuncResult test_i32_array_copy_capped(void) {
b8 result; b8 result;
i32 *src = wapp_array(i32, 1, 2, 3, 4, 5); I32Array src = wapp_array(i32, I32Array, 1, 2, 3, 4, 5);
i32 *dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6); I32Array dst1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6);
i32 *dst2 = wapp_array(i32, 1, 2); I32Array dst2 = wapp_array(i32, I32Array, 1, 2);
u64 expected_count = 5; u64 expected_count = 5;
wapp_array_copy_capped(i32, dst1, src); wapp_array_copy_capped(i32, &dst1, &src);
result = wapp_array_count(dst1) == expected_count; result = dst1.count == expected_count;
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
result = result && (*wapp_array_get(i32, src, index) == *wapp_array_get(i32, dst1, index)); result = result && (*wapp_array_get(i32, &src, index) == *wapp_array_get(i32, &dst1, index));
++index; ++index;
running = index < expected_count; running = index < expected_count;
} }
expected_count = 4; expected_count = 4;
wapp_array_copy_capped(i32, dst2, src); wapp_array_copy_capped(i32, &dst2, &src);
result = result && wapp_array_count(dst2) == expected_count; result = result && dst2.count == expected_count;
index = 0; index = 0;
running = true; running = true;
while (running) { while (running) {
result = result && (*wapp_array_get(i32, src, index) == *wapp_array_get(i32, dst2, index)); result = result && (*wapp_array_get(i32, &src, index) == *wapp_array_get(i32, &dst2, index));
++index; ++index;
running = index < expected_count; running = index < expected_count;
@@ -149,9 +149,9 @@ TestFuncResult test_i32_array_alloc_capacity(void) {
Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); Allocator allocator = wapp_mem_arena_allocator_init(MB(4));
u64 capacity = 32; u64 capacity = 32;
i32 *array = wapp_array_alloc_capacity(i32, &allocator, capacity); I32Array *array = wapp_array_alloc_capacity(i32, I32Array, &allocator, capacity);
result = array && wapp_array_capacity(array) == capacity; result = array && array->capacity == capacity;
wapp_mem_arena_allocator_destroy(&allocator); wapp_mem_arena_allocator_destroy(&allocator);
@@ -162,24 +162,24 @@ TestFuncResult test_i32_array_append_alloc(void) {
b8 result; b8 result;
Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); Allocator allocator = wapp_mem_arena_allocator_init(MB(4));
i32 *array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7, 8);
i32 *array2 = wapp_array(i32, 1, 2); I32Array array2 = wapp_array(i32, I32Array, 1, 2);
i32 num = 10; i32 num = 10;
i32 *arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &num); I32Array *arr_ptr = wapp_array_append_alloc(i32, I32Array, &allocator, &array1, &num);
result = arr_ptr == array1; result = arr_ptr == &array1;
u64 count = 4; u64 count = 4;
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
i32 num = (i32)index; i32 num = (i32)index;
arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num); arr_ptr = wapp_array_append_alloc(i32, I32Array, &allocator, &array2, &num);
++index; ++index;
running = index < count; running = index < count;
} }
result = result && arr_ptr != array2; result = result && arr_ptr != &array2;
wapp_mem_arena_allocator_destroy(&allocator); wapp_mem_arena_allocator_destroy(&allocator);
@@ -190,15 +190,15 @@ TestFuncResult test_i32_array_extend_alloc(void) {
b8 result; b8 result;
Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); Allocator allocator = wapp_mem_arena_allocator_init(MB(4));
i32 *array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7, 8);
i32 *array2 = wapp_array(i32, 1, 2); I32Array array2 = wapp_array(i32, I32Array, 1, 2);
i32 *array3 = wapp_array(i32, 1, 2, 3, 4); I32Array array3 = wapp_array(i32, I32Array, 1, 2, 3, 4);
i32 *arr_ptr = wapp_array_extend_alloc(i32, &allocator, array1, array3); I32Array *arr_ptr = wapp_array_extend_alloc(i32, I32Array, &allocator, &array1, &array3);
result = arr_ptr == array1; result = arr_ptr == &array1;
arr_ptr = wapp_array_extend_alloc(i32, &allocator, array2, array3); arr_ptr = wapp_array_extend_alloc(i32, I32Array, &allocator, &array2, &array3);
result = result && arr_ptr != array2; result = result && arr_ptr != &array2;
wapp_mem_arena_allocator_destroy(&allocator); wapp_mem_arena_allocator_destroy(&allocator);
@@ -209,32 +209,32 @@ TestFuncResult test_i32_array_copy_alloc(void) {
b8 result; b8 result;
Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); Allocator allocator = wapp_mem_arena_allocator_init(MB(4));
i32 *src = wapp_array(i32, 1, 2, 3, 4, 5); I32Array src = wapp_array(i32, I32Array, 1, 2, 3, 4, 5);
i32 *dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6); I32Array dst1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6);
i32 *dst2 = wapp_array(i32, 1, 2); I32Array dst2 = wapp_array(i32, I32Array, 1, 2);
i32 *array_ptr = nullptr; I32Array *array_ptr = nullptr;
u64 expected_count = 5; u64 expected_count = 5;
array_ptr = wapp_array_copy_alloc(i32, &allocator, dst1, src); array_ptr = wapp_array_copy_alloc(i32, I32Array, &allocator, &dst1, &src);
result = wapp_array_count(array_ptr) == expected_count && array_ptr == dst1; result = array_ptr->count == expected_count && array_ptr == &dst1;
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
result = result && (*wapp_array_get(i32, src, index) == *wapp_array_get(i32, array_ptr, index)); result = result && (*wapp_array_get(i32, &src, index) == *wapp_array_get(i32, array_ptr, index));
++index; ++index;
running = index < expected_count; running = index < expected_count;
} }
expected_count = 5; expected_count = 5;
array_ptr = wapp_array_copy_alloc(i32, &allocator, dst2, src); array_ptr = wapp_array_copy_alloc(i32, I32Array, &allocator, &dst2, &src);
result = result && wapp_array_count(array_ptr) == expected_count && array_ptr != dst2; result = result && array_ptr->count == expected_count && array_ptr != &dst2;
index = 0; index = 0;
running = true; running = true;
while (running) { while (running) {
result = result && (*wapp_array_get(i32, src, index) == *wapp_array_get(i32, array_ptr, index)); result = result && (*wapp_array_get(i32, &src, index) == *wapp_array_get(i32, array_ptr, index));
++index; ++index;
running = index < expected_count; running = index < expected_count;
@@ -248,12 +248,12 @@ TestFuncResult test_i32_array_copy_alloc(void) {
TestFuncResult test_i32_array_clear(void) { TestFuncResult test_i32_array_clear(void) {
b8 result; b8 result;
i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8);
result = wapp_array_count(array) == 9; result = array.count == 9;
wapp_array_clear(i32, array); wapp_array_clear(i32, &array);
result = result && wapp_array_count(array) == 0; result = result && array.count == 0;
return wapp_tester_result(result); return wapp_tester_result(result);
} }
@@ -261,11 +261,11 @@ TestFuncResult test_i32_array_clear(void) {
TestFuncResult test_i32_array_pop(void) { TestFuncResult test_i32_array_pop(void) {
b8 result; b8 result;
i32 *array1 = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array1 = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8);
i32 *array2 = wapp_array_with_capacity(i32, 32); I32Array array2 = wapp_array_with_capacity(i32, I32Array, 32);
i32 item1 = wapp_array_pop(i32, array1); i32 item1 = wapp_array_pop(i32, &array1);
i32 item2 = wapp_array_pop(i32, array2); i32 item2 = wapp_array_pop(i32, &array2);
result = item1 == 8 && item2 == 0; result = item1 == 8 && item2 == 0;

View File

@@ -10,16 +10,16 @@ TestFuncResult test_str8_array(void) {
Str8 str1 = wapp_str8_lit("Hello"); Str8 str1 = wapp_str8_lit("Hello");
Str8 str2 = wapp_str8_lit("Hi"); Str8 str2 = wapp_str8_lit("Hi");
Str8 str3 = wapp_str8_lit("Bye"); Str8 str3 = wapp_str8_lit("Bye");
Str8 *array = wapp_array(Str8, str1, str2, str3); Str8Array array = wapp_array(Str8, Str8Array, str1, str2, str3);
result = wapp_array_count(array) == 3 && wapp_array_capacity(array) == 8; result = array.count == 3 && array.capacity == 8;
Str8 *item; Str8 *item;
u64 count = wapp_array_count(array); u64 count = array.count;
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
item = wapp_array_get(Str8, array, index); item = wapp_array_get(Str8, &array, index);
result = result && item && (wapp_str8_equal(item, &expected[index])); result = result && item && (wapp_str8_equal(item, &expected[index]));
++index; ++index;

View File

@@ -617,10 +617,10 @@ TestFuncResult test_str8_from_bytes(void) {
Str8 str = wapp_str8_buf(1024); Str8 str = wapp_str8_buf(1024);
Str8 expected = wapp_str8_lit_ro("WAPP"); Str8 expected = wapp_str8_lit_ro("WAPP");
u8 *bytes = wapp_array(u8, 'W', 'A', 'P', 'P'); U8Array bytes = wapp_array(u8, U8Array, 'W', 'A', 'P', 'P');
wapp_str8_from_bytes(&str, bytes); wapp_str8_from_bytes(&str, &bytes);
result = str.size == wapp_array_count(bytes) * wapp_array_item_size(bytes); result = str.size == bytes.count * bytes.item_size;
result = result && wapp_str8_equal(&str, &expected); result = result && wapp_str8_equal(&str, &expected);
return wapp_tester_result(result); return wapp_tester_result(result);