Reintroduce C++ support and add usage tests for C++ (#4)

Reviewed-on: #4
Co-authored-by: Abdelrahman <said.abdelrahman89@gmail.com>
Co-committed-by: Abdelrahman <said.abdelrahman89@gmail.com>
This commit is contained in:
2025-08-10 22:33:40 +00:00
committed by Abdelrahman Said
parent 011083ab83
commit d3fccd61b5
42 changed files with 2499 additions and 104 deletions

View File

@@ -11,6 +11,373 @@
#include "../../common/aliases/aliases.h"
#include "../../common/platform/platform.h"
#ifdef WAPP_PLATFORM_CPP
BEGIN_C_LINKAGE
#endif // !WAPP_PLATFORM_CPP
#define wapp_str8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((Str8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(Str8)))
#define wapp_void_ptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((VoidPArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(void *)))
#define wapp_b32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((B32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(b32)))
#define wapp_char_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((CharArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(char)))
#define wapp_c8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c8)))
#define wapp_c16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c16)))
#define wapp_c32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c32)))
#define wapp_i8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i8)))
#define wapp_i16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i16)))
#define wapp_i32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i32)))
#define wapp_i64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i64)))
#define wapp_u8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u8)))
#define wapp_u16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u16)))
#define wapp_u32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u32)))
#define wapp_u64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u64)))
#define wapp_f32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f32)))
#define wapp_f64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f64)))
#define wapp_f128_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F128Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f128)))
#define wapp_iptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((IptrArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(iptr)))
#define wapp_uptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((UptrArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(uptr)))
#ifdef WAPP_PLATFORM_CPP
#define wapp_str8_array(...) ([&]() { \
persistent Str8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(Str8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return Str8Array{ \
buf, \
wapp_misc_utils_va_args_count(Str8, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(Str8, __VA_ARGS__) * 2), \
sizeof(Str8) \
}; \
}())
#define wapp_str8_array_with_capacity(CAPACITY) ([&]() { \
persistent Str8 buf[CAPACITY] = {}; \
return Str8Array{buf, 0, CAPACITY, sizeof(Str8)}; \
}())
#define wapp_str8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_str8_array_pop(ARRAY_PTR) : \
Str8{} \
)
#define wapp_void_ptr_array(...) ([&]() { \
persistent void * buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(void *, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return VoidPArray{ \
buf, \
wapp_misc_utils_va_args_count(void *, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(void *, __VA_ARGS__) * 2), \
sizeof(void *) \
}; \
}())
#define wapp_void_ptr_array_with_capacity(CAPACITY) ([&]() { \
persistent void * buf[CAPACITY] = {}; \
return VoidPArray{buf, 0, CAPACITY, sizeof(void *)}; \
}())
#define wapp_void_ptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_void_ptr_array_pop(ARRAY_PTR) : \
void *{} \
)
#define wapp_b32_array(...) ([&]() { \
persistent b32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(b32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return B32Array{ \
buf, \
wapp_misc_utils_va_args_count(b32, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(b32, __VA_ARGS__) * 2), \
sizeof(b32) \
}; \
}())
#define wapp_b32_array_with_capacity(CAPACITY) ([&]() { \
persistent b32 buf[CAPACITY] = {}; \
return B32Array{buf, 0, CAPACITY, sizeof(b32)}; \
}())
#define wapp_b32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_b32_array_pop(ARRAY_PTR) : \
b32{} \
)
#define wapp_char_array(...) ([&]() { \
persistent char buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(char, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return CharArray{ \
buf, \
wapp_misc_utils_va_args_count(char, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(char, __VA_ARGS__) * 2), \
sizeof(char) \
}; \
}())
#define wapp_char_array_with_capacity(CAPACITY) ([&]() { \
persistent char buf[CAPACITY] = {}; \
return CharArray{buf, 0, CAPACITY, sizeof(char)}; \
}())
#define wapp_char_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_char_array_pop(ARRAY_PTR) : \
char{} \
)
#define wapp_c8_array(...) ([&]() { \
persistent c8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return C8Array{ \
buf, \
wapp_misc_utils_va_args_count(c8, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c8, __VA_ARGS__) * 2), \
sizeof(c8) \
}; \
}())
#define wapp_c8_array_with_capacity(CAPACITY) ([&]() { \
persistent c8 buf[CAPACITY] = {}; \
return C8Array{buf, 0, CAPACITY, sizeof(c8)}; \
}())
#define wapp_c8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_c8_array_pop(ARRAY_PTR) : \
c8{} \
)
#define wapp_c16_array(...) ([&]() { \
persistent c16 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c16, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return C16Array{ \
buf, \
wapp_misc_utils_va_args_count(c16, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c16, __VA_ARGS__) * 2), \
sizeof(c16) \
}; \
}())
#define wapp_c16_array_with_capacity(CAPACITY) ([&]() { \
persistent c16 buf[CAPACITY] = {}; \
return C16Array{buf, 0, CAPACITY, sizeof(c16)}; \
}())
#define wapp_c16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_c16_array_pop(ARRAY_PTR) : \
c16{} \
)
#define wapp_c32_array(...) ([&]() { \
persistent c32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return C32Array{ \
buf, \
wapp_misc_utils_va_args_count(c32, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c32, __VA_ARGS__) * 2), \
sizeof(c32) \
}; \
}())
#define wapp_c32_array_with_capacity(CAPACITY) ([&]() { \
persistent c32 buf[CAPACITY] = {}; \
return C32Array{buf, 0, CAPACITY, sizeof(c32)}; \
}())
#define wapp_c32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_c32_array_pop(ARRAY_PTR) : \
c32{} \
)
#define wapp_i8_array(...) ([&]() { \
persistent i8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return I8Array{ \
buf, \
wapp_misc_utils_va_args_count(i8, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i8, __VA_ARGS__) * 2), \
sizeof(i8) \
}; \
}())
#define wapp_i8_array_with_capacity(CAPACITY) ([&]() { \
persistent i8 buf[CAPACITY] = {}; \
return I8Array{buf, 0, CAPACITY, sizeof(i8)}; \
}())
#define wapp_i8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_i8_array_pop(ARRAY_PTR) : \
i8{} \
)
#define wapp_i16_array(...) ([&]() { \
persistent i16 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i16, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return I16Array{ \
buf, \
wapp_misc_utils_va_args_count(i16, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i16, __VA_ARGS__) * 2), \
sizeof(i16) \
}; \
}())
#define wapp_i16_array_with_capacity(CAPACITY) ([&]() { \
persistent i16 buf[CAPACITY] = {}; \
return I16Array{buf, 0, CAPACITY, sizeof(i16)}; \
}())
#define wapp_i16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_i16_array_pop(ARRAY_PTR) : \
i16{} \
)
#define wapp_i32_array(...) ([&]() { \
persistent 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_i32_array_with_capacity(CAPACITY) ([&]() { \
persistent i32 buf[CAPACITY] = {}; \
return I32Array{buf, 0, CAPACITY, sizeof(i32)}; \
}())
#define wapp_i32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_i32_array_pop(ARRAY_PTR) : \
i32{} \
)
#define wapp_i64_array(...) ([&]() { \
persistent i64 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i64, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return I64Array{ \
buf, \
wapp_misc_utils_va_args_count(i64, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i64, __VA_ARGS__) * 2), \
sizeof(i64) \
}; \
}())
#define wapp_i64_array_with_capacity(CAPACITY) ([&]() { \
persistent i64 buf[CAPACITY] = {}; \
return I64Array{buf, 0, CAPACITY, sizeof(i64)}; \
}())
#define wapp_i64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_i64_array_pop(ARRAY_PTR) : \
i64{} \
)
#define wapp_u8_array(...) ([&]() { \
persistent u8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return U8Array{ \
buf, \
wapp_misc_utils_va_args_count(u8, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u8, __VA_ARGS__) * 2), \
sizeof(u8) \
}; \
}())
#define wapp_u8_array_with_capacity(CAPACITY) ([&]() { \
persistent u8 buf[CAPACITY] = {}; \
return U8Array{buf, 0, CAPACITY, sizeof(u8)}; \
}())
#define wapp_u8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_u8_array_pop(ARRAY_PTR) : \
u8{} \
)
#define wapp_u16_array(...) ([&]() { \
persistent u16 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u16, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return U16Array{ \
buf, \
wapp_misc_utils_va_args_count(u16, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u16, __VA_ARGS__) * 2), \
sizeof(u16) \
}; \
}())
#define wapp_u16_array_with_capacity(CAPACITY) ([&]() { \
persistent u16 buf[CAPACITY] = {}; \
return U16Array{buf, 0, CAPACITY, sizeof(u16)}; \
}())
#define wapp_u16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_u16_array_pop(ARRAY_PTR) : \
u16{} \
)
#define wapp_u32_array(...) ([&]() { \
persistent u32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return U32Array{ \
buf, \
wapp_misc_utils_va_args_count(u32, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u32, __VA_ARGS__) * 2), \
sizeof(u32) \
}; \
}())
#define wapp_u32_array_with_capacity(CAPACITY) ([&]() { \
persistent u32 buf[CAPACITY] = {}; \
return U32Array{buf, 0, CAPACITY, sizeof(u32)}; \
}())
#define wapp_u32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_u32_array_pop(ARRAY_PTR) : \
u32{} \
)
#define wapp_u64_array(...) ([&]() { \
persistent u64 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u64, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return U64Array{ \
buf, \
wapp_misc_utils_va_args_count(u64, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u64, __VA_ARGS__) * 2), \
sizeof(u64) \
}; \
}())
#define wapp_u64_array_with_capacity(CAPACITY) ([&]() { \
persistent u64 buf[CAPACITY] = {}; \
return U64Array{buf, 0, CAPACITY, sizeof(u64)}; \
}())
#define wapp_u64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_u64_array_pop(ARRAY_PTR) : \
u64{} \
)
#define wapp_f32_array(...) ([&]() { \
persistent f32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return F32Array{ \
buf, \
wapp_misc_utils_va_args_count(f32, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f32, __VA_ARGS__) * 2), \
sizeof(f32) \
}; \
}())
#define wapp_f32_array_with_capacity(CAPACITY) ([&]() { \
persistent f32 buf[CAPACITY] = {}; \
return F32Array{buf, 0, CAPACITY, sizeof(f32)}; \
}())
#define wapp_f32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_f32_array_pop(ARRAY_PTR) : \
f32{} \
)
#define wapp_f64_array(...) ([&]() { \
persistent f64 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f64, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return F64Array{ \
buf, \
wapp_misc_utils_va_args_count(f64, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f64, __VA_ARGS__) * 2), \
sizeof(f64) \
}; \
}())
#define wapp_f64_array_with_capacity(CAPACITY) ([&]() { \
persistent f64 buf[CAPACITY] = {}; \
return F64Array{buf, 0, CAPACITY, sizeof(f64)}; \
}())
#define wapp_f64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_f64_array_pop(ARRAY_PTR) : \
f64{} \
)
#define wapp_f128_array(...) ([&]() { \
persistent f128 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f128, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return F128Array{ \
buf, \
wapp_misc_utils_va_args_count(f128, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f128, __VA_ARGS__) * 2), \
sizeof(f128) \
}; \
}())
#define wapp_f128_array_with_capacity(CAPACITY) ([&]() { \
persistent f128 buf[CAPACITY] = {}; \
return F128Array{buf, 0, CAPACITY, sizeof(f128)}; \
}())
#define wapp_f128_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_f128_array_pop(ARRAY_PTR) : \
f128{} \
)
#define wapp_iptr_array(...) ([&]() { \
persistent iptr buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(iptr, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return IptrArray{ \
buf, \
wapp_misc_utils_va_args_count(iptr, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(iptr, __VA_ARGS__) * 2), \
sizeof(iptr) \
}; \
}())
#define wapp_iptr_array_with_capacity(CAPACITY) ([&]() { \
persistent iptr buf[CAPACITY] = {}; \
return IptrArray{buf, 0, CAPACITY, sizeof(iptr)}; \
}())
#define wapp_iptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_iptr_array_pop(ARRAY_PTR) : \
iptr{} \
)
#define wapp_uptr_array(...) ([&]() { \
persistent uptr buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(uptr, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \
return UptrArray{ \
buf, \
wapp_misc_utils_va_args_count(uptr, __VA_ARGS__), \
wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(uptr, __VA_ARGS__) * 2), \
sizeof(uptr) \
}; \
}())
#define wapp_uptr_array_with_capacity(CAPACITY) ([&]() { \
persistent uptr buf[CAPACITY] = {}; \
return UptrArray{buf, 0, CAPACITY, sizeof(uptr)}; \
}())
#define wapp_uptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_uptr_array_pop(ARRAY_PTR) : \
uptr{} \
)
#else
#define wapp_str8_array(...) ((Str8Array){ \
.items = (Str8[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(Str8, __VA_ARGS__) * 2)]){__VA_ARGS__}, \
.count = wapp_misc_utils_va_args_count(Str8, __VA_ARGS__), \
@@ -18,7 +385,6 @@
.item_size = sizeof(Str8) \
})
#define wapp_str8_array_with_capacity(CAPACITY) ((Str8Array){.items = (Str8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(Str8)})
#define wapp_str8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((Str8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(Str8)))
#define wapp_str8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_str8_array_pop(ARRAY_PTR) : \
(Str8){0} \
@@ -30,7 +396,6 @@
.item_size = sizeof(void *) \
})
#define wapp_void_ptr_array_with_capacity(CAPACITY) ((VoidPArray){.items = (void *[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(void *)})
#define wapp_void_ptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((VoidPArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(void *)))
#define wapp_void_ptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_void_ptr_array_pop(ARRAY_PTR) : \
(void *){0} \
@@ -42,7 +407,6 @@
.item_size = sizeof(b32) \
})
#define wapp_b32_array_with_capacity(CAPACITY) ((B32Array){.items = (b32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(b32)})
#define wapp_b32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((B32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(b32)))
#define wapp_b32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_b32_array_pop(ARRAY_PTR) : \
(b32){0} \
@@ -54,7 +418,6 @@
.item_size = sizeof(char) \
})
#define wapp_char_array_with_capacity(CAPACITY) ((CharArray){.items = (char[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(char)})
#define wapp_char_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((CharArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(char)))
#define wapp_char_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_char_array_pop(ARRAY_PTR) : \
(char){0} \
@@ -66,7 +429,6 @@
.item_size = sizeof(c8) \
})
#define wapp_c8_array_with_capacity(CAPACITY) ((C8Array){.items = (c8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(c8)})
#define wapp_c8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c8)))
#define wapp_c8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_c8_array_pop(ARRAY_PTR) : \
(c8){0} \
@@ -78,7 +440,6 @@
.item_size = sizeof(c16) \
})
#define wapp_c16_array_with_capacity(CAPACITY) ((C16Array){.items = (c16[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(c16)})
#define wapp_c16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c16)))
#define wapp_c16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_c16_array_pop(ARRAY_PTR) : \
(c16){0} \
@@ -90,7 +451,6 @@
.item_size = sizeof(c32) \
})
#define wapp_c32_array_with_capacity(CAPACITY) ((C32Array){.items = (c32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(c32)})
#define wapp_c32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c32)))
#define wapp_c32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_c32_array_pop(ARRAY_PTR) : \
(c32){0} \
@@ -102,7 +462,6 @@
.item_size = sizeof(i8) \
})
#define wapp_i8_array_with_capacity(CAPACITY) ((I8Array){.items = (i8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(i8)})
#define wapp_i8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i8)))
#define wapp_i8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_i8_array_pop(ARRAY_PTR) : \
(i8){0} \
@@ -114,7 +473,6 @@
.item_size = sizeof(i16) \
})
#define wapp_i16_array_with_capacity(CAPACITY) ((I16Array){.items = (i16[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(i16)})
#define wapp_i16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i16)))
#define wapp_i16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_i16_array_pop(ARRAY_PTR) : \
(i16){0} \
@@ -126,7 +484,6 @@
.item_size = sizeof(i32) \
})
#define wapp_i32_array_with_capacity(CAPACITY) ((I32Array){.items = (i32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(i32)})
#define wapp_i32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i32)))
#define wapp_i32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_i32_array_pop(ARRAY_PTR) : \
(i32){0} \
@@ -138,7 +495,6 @@
.item_size = sizeof(i64) \
})
#define wapp_i64_array_with_capacity(CAPACITY) ((I64Array){.items = (i64[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(i64)})
#define wapp_i64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i64)))
#define wapp_i64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_i64_array_pop(ARRAY_PTR) : \
(i64){0} \
@@ -150,7 +506,6 @@
.item_size = sizeof(u8) \
})
#define wapp_u8_array_with_capacity(CAPACITY) ((U8Array){.items = (u8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(u8)})
#define wapp_u8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u8)))
#define wapp_u8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_u8_array_pop(ARRAY_PTR) : \
(u8){0} \
@@ -162,7 +517,6 @@
.item_size = sizeof(u16) \
})
#define wapp_u16_array_with_capacity(CAPACITY) ((U16Array){.items = (u16[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(u16)})
#define wapp_u16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u16)))
#define wapp_u16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_u16_array_pop(ARRAY_PTR) : \
(u16){0} \
@@ -174,7 +528,6 @@
.item_size = sizeof(u32) \
})
#define wapp_u32_array_with_capacity(CAPACITY) ((U32Array){.items = (u32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(u32)})
#define wapp_u32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u32)))
#define wapp_u32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_u32_array_pop(ARRAY_PTR) : \
(u32){0} \
@@ -186,7 +539,6 @@
.item_size = sizeof(u64) \
})
#define wapp_u64_array_with_capacity(CAPACITY) ((U64Array){.items = (u64[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(u64)})
#define wapp_u64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u64)))
#define wapp_u64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_u64_array_pop(ARRAY_PTR) : \
(u64){0} \
@@ -198,7 +550,6 @@
.item_size = sizeof(f32) \
})
#define wapp_f32_array_with_capacity(CAPACITY) ((F32Array){.items = (f32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(f32)})
#define wapp_f32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f32)))
#define wapp_f32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_f32_array_pop(ARRAY_PTR) : \
(f32){0} \
@@ -210,7 +561,6 @@
.item_size = sizeof(f64) \
})
#define wapp_f64_array_with_capacity(CAPACITY) ((F64Array){.items = (f64[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(f64)})
#define wapp_f64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f64)))
#define wapp_f64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_f64_array_pop(ARRAY_PTR) : \
(f64){0} \
@@ -222,7 +572,6 @@
.item_size = sizeof(f128) \
})
#define wapp_f128_array_with_capacity(CAPACITY) ((F128Array){.items = (f128[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(f128)})
#define wapp_f128_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F128Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f128)))
#define wapp_f128_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_f128_array_pop(ARRAY_PTR) : \
(f128){0} \
@@ -234,7 +583,6 @@
.item_size = sizeof(iptr) \
})
#define wapp_iptr_array_with_capacity(CAPACITY) ((IptrArray){.items = (iptr[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(iptr)})
#define wapp_iptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((IptrArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(iptr)))
#define wapp_iptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_iptr_array_pop(ARRAY_PTR) : \
(iptr){0} \
@@ -246,11 +594,11 @@
.item_size = sizeof(uptr) \
})
#define wapp_uptr_array_with_capacity(CAPACITY) ((UptrArray){.items = (uptr[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(uptr)})
#define wapp_uptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((UptrArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(uptr)))
#define wapp_uptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
*_uptr_array_pop(ARRAY_PTR) : \
(uptr){0} \
)
#endif // !WAPP_PLATFORM_CPP
typedef struct str8 Str8;
@@ -616,4 +964,8 @@ UptrArray *wapp_uptr_array_copy_alloc(const Allocator *allocator, const UptrArra
uptr *_uptr_array_pop(UptrArray *array);
VoidPArray *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size);
#ifdef WAPP_PLATFORM_CPP
END_C_LINKAGE
#endif // !WAPP_PLATFORM_CPP
#endif // !ARRAY_H

View File

@@ -9,6 +9,32 @@
#include "../../common/aliases/aliases.h"
#include "../../common/platform/platform.h"
#ifdef WAPP_PLATFORM_CPP
BEGIN_C_LINKAGE
#endif // !WAPP_PLATFORM_CPP
#ifdef WAPP_PLATFORM_CPP
#define wapp_str8_list_node(ITEM_PTR) Str8Node{ITEM_PTR, nullptr, nullptr}
#define wapp_void_ptr_list_node(ITEM_PTR) VoidPNode{ITEM_PTR, nullptr, nullptr}
#define wapp_b32_list_node(ITEM_PTR) B32Node{ITEM_PTR, nullptr, nullptr}
#define wapp_char_list_node(ITEM_PTR) CharNode{ITEM_PTR, nullptr, nullptr}
#define wapp_c8_list_node(ITEM_PTR) C8Node{ITEM_PTR, nullptr, nullptr}
#define wapp_c16_list_node(ITEM_PTR) C16Node{ITEM_PTR, nullptr, nullptr}
#define wapp_c32_list_node(ITEM_PTR) C32Node{ITEM_PTR, nullptr, nullptr}
#define wapp_i8_list_node(ITEM_PTR) I8Node{ITEM_PTR, nullptr, nullptr}
#define wapp_i16_list_node(ITEM_PTR) I16Node{ITEM_PTR, nullptr, nullptr}
#define wapp_i32_list_node(ITEM_PTR) I32Node{ITEM_PTR, nullptr, nullptr}
#define wapp_i64_list_node(ITEM_PTR) I64Node{ITEM_PTR, nullptr, nullptr}
#define wapp_u8_list_node(ITEM_PTR) U8Node{ITEM_PTR, nullptr, nullptr}
#define wapp_u16_list_node(ITEM_PTR) U16Node{ITEM_PTR, nullptr, nullptr}
#define wapp_u32_list_node(ITEM_PTR) U32Node{ITEM_PTR, nullptr, nullptr}
#define wapp_u64_list_node(ITEM_PTR) U64Node{ITEM_PTR, nullptr, nullptr}
#define wapp_f32_list_node(ITEM_PTR) F32Node{ITEM_PTR, nullptr, nullptr}
#define wapp_f64_list_node(ITEM_PTR) F64Node{ITEM_PTR, nullptr, nullptr}
#define wapp_f128_list_node(ITEM_PTR) F128Node{ITEM_PTR, nullptr, nullptr}
#define wapp_iptr_list_node(ITEM_PTR) IptrNode{ITEM_PTR, nullptr, nullptr}
#define wapp_uptr_list_node(ITEM_PTR) UptrNode{ITEM_PTR, nullptr, nullptr}
#else
#define wapp_str8_list_node(ITEM_PTR) ((Str8Node){.item = ITEM_PTR})
#define wapp_void_ptr_list_node(ITEM_PTR) ((VoidPNode){.item = ITEM_PTR})
#define wapp_b32_list_node(ITEM_PTR) ((B32Node){.item = ITEM_PTR})
@@ -29,6 +55,7 @@
#define wapp_f128_list_node(ITEM_PTR) ((F128Node){.item = ITEM_PTR})
#define wapp_iptr_list_node(ITEM_PTR) ((IptrNode){.item = ITEM_PTR})
#define wapp_uptr_list_node(ITEM_PTR) ((UptrNode){.item = ITEM_PTR})
#endif // !WAPP_PLATFORM_CPP
typedef struct str8 Str8;
@@ -473,4 +500,8 @@ UptrNode *wapp_uptr_list_pop_back(UptrList *list);
UptrNode *wapp_uptr_list_remove(UptrList *list, u64 index);
void wapp_uptr_list_empty(UptrList *list);
#ifdef WAPP_PLATFORM_CPP
END_C_LINKAGE
#endif // !WAPP_PLATFORM_CPP
#endif // !DBL_LIST_H

View File

@@ -7,6 +7,10 @@
#include "../../common/platform/platform.h"
#include <string.h>
#ifdef WAPP_PLATFORM_CPP
BEGIN_C_LINKAGE
#endif // !WAPP_PLATFORM_CPP
typedef void *(MemAllocFunc)(u64 size, void *alloc_obj);
typedef void *(MemAllocAlignedFunc)(u64 size, u64 alignment, void *alloc_obj);
typedef void *(MemReallocFunc)(void *ptr, u64 old_size, u64 new_size, void *alloc_obj);
@@ -23,7 +27,14 @@ struct allocator {
MemFreeFunc *free;
};
#ifdef WAPP_PLATFORM_CPP
#define wapp_mem_allocator_invalid(ALLOCATOR) ([&]() { \
Allocator alloc{}; \
return memcmp(ALLOCATOR, &alloc, sizeof(Allocator)) == 0; \
}())
#else
#define wapp_mem_allocator_invalid(ALLOCATOR) (memcmp(ALLOCATOR, &((Allocator){0}), sizeof(Allocator)) == 0)
#endif // !WAPP_PLATFORM_CPP
void *wapp_mem_allocator_alloc(const Allocator *allocator, u64 size);
void *wapp_mem_allocator_alloc_aligned(const Allocator *allocator, u64 size, u64 alignment);
@@ -32,4 +43,8 @@ void *wapp_mem_allocator_realloc_aligned(const Allocator *allocator, void *ptr,
u64 new_size, u64 alignment);
void wapp_mem_allocator_free(const Allocator *allocator, void **ptr, u64 size);
#ifdef WAPP_PLATFORM_CPP
END_C_LINKAGE
#endif // !WAPP_PLATFORM_CPP
#endif // !MEM_ALLOCATOR_H

View File

@@ -28,6 +28,15 @@ RETURN_STR8:
return str;
}
Str8 *wapp_str8_alloc_and_fill_buf(const Allocator *allocator, u64 capacity) {
Str8 *out = wapp_str8_alloc_buf(allocator, capacity);
if (out) {
memset(out->buf, 0, capacity);
out->size = capacity;
}
return out;
}
Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str) {
wapp_debug_assert(allocator != NULL && str != NULL, "`allocator` and `str` should not be NULL");

View File

@@ -4,12 +4,17 @@
#define STR8_H
#include "../../../common/aliases/aliases.h"
#include "../../../common/assert/assert.h"
#include "../../../common/platform/platform.h"
#include "../../../primitives/dbl_list/dbl_list.h"
#include "../../mem_allocator/mem_allocator.h"
#include <string.h>
#include <stdbool.h>
#ifdef WAPP_PLATFORM_CPP
BEGIN_C_LINKAGE
#endif // !WAPP_PLATFORM_CPP
typedef struct str8 Str8;
struct str8 {
u64 capacity;
@@ -28,6 +33,25 @@ typedef const Str8 Str8RO;
/**
* Str8 stack buffers
*/
#ifdef WAPP_PLATFORM_CPP
// Uses a lambda to achieve the same behaviour achieved by the C macro
#define wapp_str8_buf(CAPACITY) ([&](){ \
persistent c8 buf[CAPACITY] = {}; \
memset(buf, 0, CAPACITY); \
return Str8{CAPACITY, 0, buf}; \
}())
// Uses a lambda to achieve the same behaviour achieved by the C macro
#define wapp_str8_lit(STRING) ([&]() { \
persistent c8 buf[sizeof(STRING) * 2] = {}; \
memcpy(buf, STRING, sizeof(STRING)); \
return Str8{(sizeof(STRING) - 1) * 2, sizeof(STRING) - 1, buf}; \
}())
#define wapp_str8_lit_ro(STRING) Str8RO{sizeof(STRING) - 1, sizeof(STRING) - 1, (c8 *)STRING}
#define wapp_str8_lit_ro_initialiser_list(STRING) {sizeof(STRING) - 1, sizeof(STRING) - 1, (c8 *)STRING}
#else
#define wapp_str8_buf(CAPACITY) ((Str8){.capacity = CAPACITY, .size = 0, .buf = (c8[CAPACITY]){0}})
// Utilises the fact that memcpy returns pointer to dest buffer and that getting
@@ -44,11 +68,13 @@ typedef const Str8 Str8RO;
#define wapp_str8_lit_ro_initialiser_list(STRING) {.capacity = sizeof(STRING) - 1, \
.size = sizeof(STRING) - 1, \
.buf = (c8 *)STRING}
#endif // !WAPP_PLATFORM_CPP
/**
* Str8 allocated buffers
*/
Str8 *wapp_str8_alloc_buf(const Allocator *allocator, u64 capacity);
Str8 *wapp_str8_alloc_and_fill_buf(const Allocator *allocator, u64 capacity);
Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str);
Str8 *wapp_str8_alloc_str8(const Allocator *allocator, Str8RO *str);
Str8 *wapp_str8_alloc_substr(const Allocator *allocator, Str8RO *str, u64 start, u64 end);
@@ -90,8 +116,23 @@ Str8 *wapp_str8_join(const Allocator *allocator, const Str8List *list, Str8R
/**
* Str8 list utilities
*/
#ifdef WAPP_PLATFORM_CPP
#define wapp_str8_node_from_cstr(STRING) wapp_str8_list_node([&]() { \
persistent Str8 str = wapp_str8_lit(STRING); \
return &str; \
}())
#define wapp_str8_node_from_str8(STRING) wapp_str8_list_node([&]() { \
persistent Str8 str = STRING; \
return &str; \
}())
#else
#define wapp_str8_node_from_cstr(STRING) wapp_str8_list_node(&wapp_str8_lit(STRING))
#define wapp_str8_node_from_str8(STRING) wapp_str8_list_node(&(STRING))
#endif // !WAPP_PLATFORM_CPP
u64 wapp_str8_list_total_size(const Str8List *list);
#ifdef WAPP_PLATFORM_CPP
END_C_LINKAGE
#endif // !WAPP_PLATFORM_CPP
#endif // !STR8_H