mirror of
https://github.com/Ed94/gencpp.git
synced 2025-07-01 03:11:03 -07:00
Finished draft pass verifying containers.array.hpp is equivalent to container.hpp's array.
gen_generic_selection_function_macro now works generically Imprvoed _Generic function overloading examples
This commit is contained in:
@ -133,6 +133,7 @@ Array<Type> array_init(AllocatorInfo allocator) {
|
||||
template<class Type> inline
|
||||
Array<Type> array_init_reserve(AllocatorInfo allocator, ssize capacity)
|
||||
{
|
||||
GEN_ASSERT(capacity > 0);
|
||||
ArrayHeader* header = rcast(ArrayHeader*, alloc(allocator, sizeof(ArrayHeader) + sizeof(Type) * capacity));
|
||||
|
||||
if (header == nullptr)
|
||||
@ -157,6 +158,8 @@ bool array_append_array(Array<Type>* array, Array<Type> other) {
|
||||
template<class Type> inline
|
||||
bool array_append(Array<Type>* array, Type value)
|
||||
{
|
||||
GEN_ASSERT( array != nullptr);
|
||||
GEN_ASSERT(* array != nullptr);
|
||||
ArrayHeader* header = array_get_header(* array);
|
||||
|
||||
if (header->Num == header->Capacity)
|
||||
@ -175,6 +178,10 @@ bool array_append(Array<Type>* array, Type value)
|
||||
template<class Type> inline
|
||||
bool array_append_items(Array<Type>* array, Type* items, usize item_num)
|
||||
{
|
||||
GEN_ASSERT( array != nullptr);
|
||||
GEN_ASSERT(* array != nullptr);
|
||||
GEN_ASSERT(items != nullptr);
|
||||
GEN_ASSERT(item_num > 0);
|
||||
ArrayHeader* header = array_get_header(array);
|
||||
|
||||
if (header->Num + item_num > header->Capacity)
|
||||
@ -193,6 +200,8 @@ bool array_append_items(Array<Type>* array, Type* items, usize item_num)
|
||||
template<class Type> inline
|
||||
bool array_append_at(Array<Type>* array, Type item, usize idx)
|
||||
{
|
||||
GEN_ASSERT( array != nullptr);
|
||||
GEN_ASSERT(* array != nullptr);
|
||||
ArrayHeader* header = array_get_header(* array);
|
||||
|
||||
ssize slot = idx;
|
||||
@ -215,19 +224,19 @@ bool array_append_at(Array<Type>* array, Type item, usize idx)
|
||||
mem_move(target + 1, target, (header->Num - slot) * sizeof(Type));
|
||||
header->Num++;
|
||||
|
||||
header = array_get_header(* array);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<class Type> inline
|
||||
bool array_append_items_at(Array<Type>* array, Type* items, usize item_num, usize idx)
|
||||
{
|
||||
GEN_ASSERT( array != nullptr);
|
||||
GEN_ASSERT(* array != nullptr);
|
||||
ArrayHeader* header = get_header(array);
|
||||
|
||||
if (idx >= header->Num)
|
||||
{
|
||||
return append(array, items, item_num);
|
||||
return array_append_items(array, items, item_num);
|
||||
}
|
||||
|
||||
if (item_num > header->Capacity)
|
||||
@ -262,6 +271,7 @@ Type* array_back(Array<Type> array)
|
||||
|
||||
template<class Type> inline
|
||||
void array_clear(Array<Type> array) {
|
||||
GEN_ASSERT(array != nullptr);
|
||||
ArrayHeader* header = array_get_header(array);
|
||||
header->Num = 0;
|
||||
}
|
||||
@ -269,6 +279,8 @@ void array_clear(Array<Type> array) {
|
||||
template<class Type> inline
|
||||
bool array_fill(Array<Type> array, usize begin, usize end, Type value)
|
||||
{
|
||||
GEN_ASSERT(array != nullptr);
|
||||
GEN_ASSERT(begin <= end);
|
||||
ArrayHeader* header = array_get_header(array);
|
||||
|
||||
if (begin < 0 || end > header->Num)
|
||||
@ -282,9 +294,10 @@ bool array_fill(Array<Type> array, usize begin, usize end, Type value)
|
||||
return true;
|
||||
}
|
||||
|
||||
template<class Type> inline
|
||||
template<class Type> forceinline
|
||||
void array_free(Array<Type>* array) {
|
||||
GEN_ASSERT(array != nullptr);
|
||||
GEN_ASSERT( array != nullptr);
|
||||
GEN_ASSERT(* array != nullptr);
|
||||
ArrayHeader* header = array_get_header(* array);
|
||||
allocator_free(header->Allocator, header);
|
||||
Type** Data = (Type**)array;
|
||||
@ -293,14 +306,18 @@ void array_free(Array<Type>* array) {
|
||||
|
||||
template<class Type> forceinline
|
||||
ArrayHeader* array_get_header(Array<Type> array) {
|
||||
GEN_ASSERT(array != nullptr);
|
||||
Type* Data = array;
|
||||
|
||||
using NonConstType = TRemoveConst<Type>;
|
||||
return rcast(ArrayHeader*, const_cast<NonConstType*>(Data)) - 1;
|
||||
}
|
||||
template<class Type> inline
|
||||
template<class Type> forceinline
|
||||
bool array_grow(Array<Type>* array, usize min_capacity)
|
||||
{
|
||||
GEN_ASSERT( array != nullptr);
|
||||
GEN_ASSERT(* array != nullptr);
|
||||
GEN_ASSERT( min_capacity > 0 );
|
||||
ArrayHeader* header = array_get_header(* array);
|
||||
usize new_capacity = array_grow_formula(header->Capacity);
|
||||
|
||||
@ -310,13 +327,15 @@ bool array_grow(Array<Type>* array, usize min_capacity)
|
||||
return array_set_capacity(array, new_capacity);
|
||||
}
|
||||
|
||||
template<class Type> inline
|
||||
template<class Type> forceinline
|
||||
usize array_num(Array<Type> array) {
|
||||
GEN_ASSERT(array != nullptr);
|
||||
return array_get_header(array)->Num;
|
||||
}
|
||||
|
||||
template<class Type> inline
|
||||
template<class Type> forceinline
|
||||
void array_pop(Array<Type> array) {
|
||||
GEN_ASSERT(array != nullptr);
|
||||
ArrayHeader* header = array_get_header(array);
|
||||
GEN_ASSERT(header->Num > 0);
|
||||
header->Num--;
|
||||
@ -325,6 +344,7 @@ void array_pop(Array<Type> array) {
|
||||
template<class Type> inline
|
||||
void array_remove_at(Array<Type> array, usize idx)
|
||||
{
|
||||
GEN_ASSERT(array != nullptr);
|
||||
ArrayHeader* header = array_get_header(array);
|
||||
GEN_ASSERT(idx < header->Num);
|
||||
|
||||
@ -335,6 +355,9 @@ void array_remove_at(Array<Type> array, usize idx)
|
||||
template<class Type> inline
|
||||
bool array_reserve(Array<Type>* array, usize new_capacity)
|
||||
{
|
||||
GEN_ASSERT( array != nullptr);
|
||||
GEN_ASSERT(* array != nullptr);
|
||||
GEN_ASSERT(num > 0)
|
||||
ArrayHeader* header = array_get_header(array);
|
||||
|
||||
if (header->Capacity < new_capacity)
|
||||
@ -346,6 +369,8 @@ bool array_reserve(Array<Type>* array, usize new_capacity)
|
||||
template<class Type> inline
|
||||
bool array_resize(Array<Type>* array, usize num)
|
||||
{
|
||||
GEN_ASSERT( array != nullptr);
|
||||
GEN_ASSERT(* array != nullptr);
|
||||
ArrayHeader* header = array_get_header(* array);
|
||||
|
||||
if (header->Capacity < num) {
|
||||
@ -361,10 +386,12 @@ bool array_resize(Array<Type>* array, usize num)
|
||||
template<class Type> inline
|
||||
bool array_set_capacity(Array<Type>* array, usize new_capacity)
|
||||
{
|
||||
GEN_ASSERT( array != nullptr);
|
||||
GEN_ASSERT(* array != nullptr);
|
||||
ArrayHeader* header = array_get_header(* array);
|
||||
|
||||
if (new_capacity == header->Capacity)
|
||||
return true;
|
||||
return true;
|
||||
|
||||
if (new_capacity < header->Num)
|
||||
{
|
||||
|
@ -269,13 +269,16 @@
|
||||
#define struct_init(type, value) {value}
|
||||
#endif
|
||||
|
||||
// ------------------------ _Generic function overloading -----------------------------------------
|
||||
#if GEN_COMPILER_C
|
||||
// ------------------------ _Generic function overloading -----------------------------------------
|
||||
// This implemnents macros for utilizing "The Naive Extendible _Generic Macro" explained in:
|
||||
// https://github.com/JacksonAllan/CC/blob/main/articles/Better_C_Generics_Part_1_The_Extendible_Generic.md
|
||||
// Since gencpp is used to generate the c-library, it was choosen over the more novel implementations to keep the macros as easy to understand and unopaque as possible.
|
||||
// Extensive effort was put in below to make this as easy as possible to understand what is going on with this mess of a preoprocessor.
|
||||
|
||||
// Where the signature would be defined using:
|
||||
#define GEN_TYPE_TO_EXP(type) (type*)NULL
|
||||
|
||||
#define GEN_COMMA_OPERATOR , // The comma operator is used by preprocessor macros to delimit arguments, so we have to represent it via a macro to prevent parsing incorrectly.
|
||||
|
||||
// Helper macros for argument selection
|
||||
@ -299,19 +302,19 @@
|
||||
// It takes advantage of the fact that if the macro is defined, then the expanded text will contain a comma.
|
||||
// Expands to ',' if it can find (type): (function) <comma_operator: ',' >
|
||||
// Where GEN_GENERIC_SEL_ENTRY_COMMA_DELIMITER is specifically looking for that <comma> ,
|
||||
#define GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( slot_exp ) GEN_SELECT_ARG_3( slot_exp, GEN_SELECT_ARG_1( slot_exp, ): GEN_SELECT_ARG_2( slot_exp, ) GEN_COMMA_OPERATOR, , )
|
||||
// ^Selects the comma ^ is the type ^ is the function ^ comma delimiter to select ^ Insert a comma
|
||||
// The slot won't exist if that comma is not found.
|
||||
#define GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( slot_exp ) GEN_GENERIC_SEL_ENTRY_COMMA_DELIMITER( slot_exp, GEN_GENERIC_SEL_ENTRY_TYPE( slot_exp, ): GEN_GENERIC_SEL_ENTRY_FUNCTION( slot_exp, ) GEN_COMMA_OPERATOR, , )
|
||||
// ^ Selects the comma ^ is the type ^ is the function ^ Insert a comma
|
||||
// The slot won't exist if that comma is not found. |
|
||||
// |
|
||||
// This is the same as above but it does not insert a comma V no comma here.
|
||||
#define GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST( slot_exp ) GEN_GENERIC_SEL_ENTRY_COMMA_DELIMITER( slot_exp, GEN_GENERIC_SEL_ENTRY_TYPE( slot_exp, ): GEN_GENERIC_SEL_ENTRY_FUNCTION( slot_exp, ), , )
|
||||
// Needed for the last slot as they don't allow trailing commas.
|
||||
// ----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
// Below are generated on demand for a generated function
|
||||
// Below are generated on demand for an overlaod depdendent on a type:
|
||||
// ----------------------------------------------------------------------------------------------------------------------------------
|
||||
#define GEN_FUNCTION_GENERIC_EXAMPLE( function_arguments ) _Generic( \
|
||||
(function_arguments), /* Select Via Expression*/ \
|
||||
#define GEN_FUNCTION_GENERIC_EXAMPLE( selector_arg ) _Generic( \
|
||||
(selector_arg), /* Select Via Expression*/ \
|
||||
/* Extendibility slots: */ \
|
||||
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
|
||||
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
|
||||
@ -320,8 +323,8 @@
|
||||
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
|
||||
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
|
||||
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
|
||||
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST FunctionID__ARGS_SIG_1 ) \
|
||||
) GEN_RESOLVED_FUNCTION_CALL( function_arguments )
|
||||
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST(FunctionID__ARGS_SIG_1 ) \
|
||||
) GEN_RESOLVED_FUNCTION_CALL( selector_arg )
|
||||
// ----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
// Then each definiton of a function has an associated define:
|
||||
@ -332,16 +335,14 @@
|
||||
// <etc> <return_type> <function_id> ( <arguments> ) { <implementation> }
|
||||
|
||||
// Concrete example:
|
||||
#define ENABLE_GENERIC_EXAMPLE 0
|
||||
#if ENABLE_GENERIC_EXAMPLE
|
||||
|
||||
// To add support for long:
|
||||
#define HASH__ARGS_SIG_1 GEN_GENERIC_FUNCTION_ARG_SIGNATURE( hash__P_long, long long )
|
||||
size_t hash__P_long ( long val ) { return val * 2654435761ull; }
|
||||
#define GEN_EXAMPLE_HASH__ARGS_SIG_1 GEN_GENERIC_FUNCTION_ARG_SIGNATURE( hash__P_long, long long )
|
||||
size_t gen_example_hash__P_long( long val ) { return val * 2654435761ull; }
|
||||
|
||||
// To add support for long long:
|
||||
#define HASH__ARGS_SIG_2 GEN_GENERIC_FUNCTION_ARG_SIGNATURE( hash__P_long_long, long long )
|
||||
size_t hash__P_long_long( long long val ) { return val * 2654435761ull; }
|
||||
#define GEN_EXAMPLE_HASH__ARGS_SIG_2 GEN_GENERIC_FUNCTION_ARG_SIGNATURE( hash__P_long_long, long long )
|
||||
size_t gen_example_hash__P_long_long( long long val ) { return val * 2654435761ull; }
|
||||
|
||||
// If using an Editor with support for syntax hightlighting macros: HASH__ARGS_SIG_1 and HASH_ARGS_SIG_2 should show color highlighting indicating the slot is enabled,
|
||||
// or, "defined" for usage during the compilation pass that handles the _Generic instrinsic.
|
||||
@ -358,8 +359,28 @@ size_t hash__P_long_long( long long val ) { return val * 2654435761ull; }
|
||||
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST( HASH__ARGS_SIG_8 ) \
|
||||
) GEN_RESOLVED_FUNCTION_CALL( function_arguments )
|
||||
|
||||
#endif // ENABLE_GENERIC_EXAMPLE
|
||||
// Additional Variations:
|
||||
|
||||
// If the function takes more than one argument the following is used:
|
||||
#define GEN_FUNCTION_GENERIC_EXAMPLE_VARADIC( selector_arg, ... ) _Generic( \
|
||||
(selector_arg), \
|
||||
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
|
||||
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_2 ) \
|
||||
... \
|
||||
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST(FunctionID__ARGS_SIG_N ) \
|
||||
) GEN_RESOLVED_FUNCTION_CALL( selector_arg, __VA_ARG__ )
|
||||
|
||||
// If the function does not take the arugment as a parameter:
|
||||
#define GEN_FUNCTION_GENERIC_EXAMPLE_DIRECT_TYPE( selector_arg ) _Generic( \
|
||||
( GEN_TYPE_TO_EXP(selector_arg) ), \
|
||||
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
|
||||
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_2 ) \
|
||||
/* ... */ \
|
||||
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST(FunctionID__ARGS_SIG_N ) \
|
||||
) GEN_RESOLVED_FUNCTION_CALL()
|
||||
|
||||
// typedef void* GEN_GenericExampleType;
|
||||
// GEN_FUNCTION_GENERIC_EXAMPLE_DIRECT_TYPE( GEN_GenericExampleType );
|
||||
// END OF ------------------------ _Generic function overloading ----------------------------------------- END OF
|
||||
#endif
|
||||
|
||||
|
@ -123,7 +123,7 @@
|
||||
#endif
|
||||
|
||||
#if GEN_COMPILER_C
|
||||
#pragma message("Detected C")
|
||||
#pragma message("GENCPP: Detected C")
|
||||
#endif
|
||||
|
||||
#pragma endregion Platform Detection
|
||||
|
Reference in New Issue
Block a user