From 73e3d1d1bd31cb61dba097f7ea5c72b1381deff5 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Thu, 28 Dec 2023 20:05:43 +0100 Subject: [PATCH] Register string_t globally --- m-generic.h | 19 +++++++++++++------ m-string.h | 11 +++++++---- tests/test-mgeneric.c | 10 +++++----- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/m-generic.h b/m-generic.h index 7c82da49..36f93e05 100644 --- a/m-generic.h +++ b/m-generic.h @@ -110,7 +110,7 @@ #endif /* Warnings disabled for CLANG in C mode: - Due to the genericty of the _Generic generation, + Due to the genericity of the _Generic generation, we cannot avoid generating both T and const T in the generic association. */ #if defined(__clang__) && __clang_major__ >= 15 #define M_G3N_BEGIN_PROTECTED_CODE \ @@ -135,13 +135,13 @@ /* Generate a sequence of the oplist that have been registered. - 3 levels of indirection are neeeded: + 3 levels of indirection are needed: * One level is the organisation. * One level is the component of the organisation. * One level is the oplist within the component of the organisation - Return the list of unevaled oplist that are registered accorss all organisations, all components. - MLIB Organisation is the default organisation and is always registered - Usage Domain: Nb organizations * Nb components * Nb oplists < M_MAX_NB_ARGUMENT + Return the list of unevaluated oplist that are registered across all organizations, all components. + M*LIB Organisation (MLIB) is the default organisation and is always registered + Usage Domain: Nb organizations * Nb components * Nb oplist <= M_MAX_NB_ARGUMENT */ #define M_G3N_REGISTERED_ITEMS() \ M_CROSS_MAP( M_G3N_IS_PRESENT3, ( comp M_CROSS_MAP(M_G3N_IS_PRESENT2, (MLIB M_MAP(M_G3N_IS_PRESENT, M_G3N_SEQ_INT)), ( M_G3N_SEQ_INT ) ) ), ( M_G3N_SEQ_INT ) ) @@ -155,10 +155,11 @@ #define M_G3N_IS_PRESENT3(el1, num) \ M_IF(M_OPLIST_P(M_C4(M_GENERIC_ORG_, el1, _OPLIST_, num)()))(M_DEFERRED_COMMA M_C4(M_GENERIC_ORG_, el1, _OPLIST_, num), ) + // Dummy unused structure. Just to fill in default case of the _Generic struct m_g3neric_dummys; -// Synonymous for some basic pointers doesn't work well with prepropressing code +// Synonymous for some basic pointers doesn't work well with preprocessing code typedef FILE *m_g3n_file; typedef const char *m_g3n_cstring; typedef const char **m_g3n_cstring_end; @@ -278,6 +279,7 @@ typedef const char **m_g3n_cstring_end; gentype: (M_G3N_TYPE(op, oplist)){0}, \ const gentype: (M_G3N_TYPE(op, oplist)){0}, + // for each item in the container // Same as M_EACH except it uses the generic macros #define M_G3N_EACHI(item, container, l_it, l_pass) \ @@ -289,6 +291,7 @@ typedef const char **m_g3n_cstring_end; && (item = it_ref(l_it), true) ; \ it_next(l_it)) + // TODO: init_with ? How to handle the different type of parameters ? emplace ? // Overwrite M_FPRINT & M_PRINT to support global oplists registration @@ -330,4 +333,8 @@ typedef const char **m_g3n_cstring_end; #undef M_FPRINT #define M_FPRINT(f,...) do { M_REDUCE2(M_G3N_FPRINT_ARG, M_SEPARATE_PER_SEMICOLON, f, __VA_ARGS__); } while (0) +// Register CORE component of M*LIB +#define M_GENERIC_ORG_MLIB_COMP_1() (CORE) + + #endif diff --git a/m-string.h b/m-string.h index 501ac1bb..e10dc4b3 100644 --- a/m-string.h +++ b/m-string.h @@ -2023,14 +2023,14 @@ m_string_utf8_p(m_string_t str) /* Use of Compound Literals to init a constant string. NOTE: The use of the additional structure layer is to ensure that the pointer to char is properly aligned to an int (this - is a needed asumption by m_string_hash). + is a needed assumption by m_string_hash). Otherwise it could have been : #define M_STRING_CTE(s) \ ((const m_string_t){{.size = sizeof(s)-1, .alloc = sizeof(s), \ .ptr = s}}) which produces faster code. Note: This code doesn't work with C++ (use of c99 feature - of recursive struct definition and compound literral). + of recursive struct definition and compound literal). As such, there is a separate C++ definition. */ #ifndef __cplusplus @@ -2101,7 +2101,7 @@ namespace m_lib { SWAP(m_string_swap), RESET(m_string_reset), \ EMPTY_P(m_string_empty_p), \ CLEAR(m_string_clear), HASH(m_string_hash), EQUAL(m_string_equal_p), \ - CMP(m_string_cmp), TYPE(m_string_t), \ + CMP(m_string_cmp), TYPE(m_string_t), GENTYPE(struct m_string_s *), \ PARSE_STR(m_string_parse_str), GET_STR(m_string_get_str), \ OUT_STR(m_string_out_str), IN_STR(m_string_in_str), \ OUT_SERIAL(m_string_out_serial), IN_SERIAL(m_string_in_serial), \ @@ -2115,13 +2115,16 @@ namespace m_lib { ,IT_END_P(m_string_end_p) \ ,IT_EQUAL_P(m_string_it_equal_p) \ ,IT_NEXT(m_string_next) \ - ,IT_CREF(m_string_cref) \ + ,IT_CREF(m_string_cref), PUSH(m_string_push_u) \ ,EMPLACE_TYPE(const char*) \ ) /* Register the OPLIST as a global one */ #define M_OPL_m_string_t() M_STRING_OPLIST +/* Register the string_t oplist as a generic type */ +#define M_GENERIC_ORG_MLIB_COMP_CORE_OPLIST_1() M_STRING_OPLIST + /***********************************************************************/ /* */ diff --git a/tests/test-mgeneric.c b/tests/test-mgeneric.c index 01c149fc..d4678891 100644 --- a/tests/test-mgeneric.c +++ b/tests/test-mgeneric.c @@ -46,22 +46,22 @@ static void h(string_t x) string_set_str(x, "TEST STRING"); } +// Global variable to test genericity on a global variable const string_t gx; -#define STR1 M_OPEXTEND(STRING_OPLIST, GENTYPE(struct m_string_s *), PUSH(m_string_push_u)) #define FLT1 (GENTYPE(float), TYPE(float), INIT(M_INIT_BASIC), INIT_SET(M_SET_BASIC), SET(M_SET_BASIC), \ CLEAR(M_NOTHING_DEFAULT) ) +#define INT1 (GENTYPE(int), TYPE(int), INIT(M_INIT_BASIC), INIT_SET(M_SET_BASIC), SET(M_SET_BASIC), \ + CLEAR(M_NOTHING_DEFAULT) ) #define ARRAY1 M_OPEXTEND(M_OPL_array_int_t(), GENTYPE(struct array_int_s *)) #define LIST1 M_OPEXTEND(M_OPL_list_int_t(), GENTYPE(struct list_int_s **)) -//#define M_GENERIC_ORG_MLIB_COMP_1() (CORE) -//#define M_GENERIC_ORG_MLIB_COMP_CORE_OPLIST_1() STR1 -//#define M_GENERIC_ORG_MLIB_COMP_CORE_OPLIST_5() FLT1 +//NOTE: string_t is registered by the CORE component of the MLIB organization #define M_GENERIC_ORG_2() (USER) #define M_GENERIC_ORG_USER_COMP_1() (CORE) #define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_6() FLT1 -#define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_7() STR1 +#define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_1() INT1 #define M_GENERIC_ORG_3() (ARRAY) #define M_GENERIC_ORG_ARRAY_COMP_5() (AINT)