diff --git a/m-generic.h b/m-generic.h index f5991896..ce61c9b4 100644 --- a/m-generic.h +++ b/m-generic.h @@ -59,7 +59,8 @@ struct m_g3neric_dummys; #define M_G3N_CALL_1_func(x, oplist) \ M_G3N_CALL_1_func_expand(M_GET_GENTYPE oplist(), M_PAIR_1 x, M_PAIR_2 x, oplist) #define M_G3N_CALL_1_func_expand(gentype, call, x, oplist) \ - gentype: call(oplist(), M_AS_TYPE(gentype, x)), + gentype: call(oplist(), M_AS_TYPE(gentype, x)), \ + const gentype: call(oplist(), M_AS_LINKED_TYPE(const gentype, x, gentype, x)), // Call the OPERATOR call of the oplist registered to the variable 'x' // which takes two argument (type, type) @@ -70,7 +71,8 @@ struct m_g3neric_dummys; #define M_G3N_CALL_2_func(x, oplist) \ M_G3N_CALL_2_func_expand(M_GET_GENTYPE oplist(), M_TRIPLE_1 x, M_TRIPLE_2 x, M_TRIPLE_3 x, oplist) #define M_G3N_CALL_2_func_expand(gentype, call, x, y, oplist) \ - gentype: call(oplist(), M_AS_TYPE(gentype, x), M_AS_TYPE(gentype, y)), + gentype: call(oplist(), M_AS_TYPE(gentype, x), M_AS_LINKED_TYPE(gentype, x, gentype, y)), \ + const gentype: call(oplist(), M_AS_TYPE(const gentype, x), M_AS_LINKED_TYPE(const gentype, x, gentype, y)), // Call the OPERATOR call of the oplist registered to the variable 'x' // which takes two argument (type, subtype) @@ -83,8 +85,12 @@ struct m_g3neric_dummys; M_IF_METHOD(GENTYPE, M_GET_OPLIST oplist())(M_GET_GENTYPE M_GET_OPLIST oplist(), M_GET_SUBTYPE oplist()), \ M_TRIPLE_1 x, M_TRIPLE_2 x, M_TRIPLE_3 x, oplist) #define M_G3N_CALL_2t_func_expand(gentype, basictype, call, x, y, oplist) \ - gentype: call(oplist(), M_AS_TYPE(gentype, x), M_AS_TYPE(basictype, y)), + gentype: call(oplist(), M_AS_TYPE(gentype, x), M_AS_LINKED_TYPE(gentype, x, basictype, y)), \ + const gentype: call(oplist(), M_AS_TYPE(const gentype, x), M_AS_LINKED_TYPE(const gentype, x, basictype, y)), +#define M_AS_LINKED_TYPE(typex, x, typey, y) _Generic(((void)0,(x)), typex: (y), default: (typey) {0}) + +//TODO: M_AS_TYPE is incorrect. We need M_AS_TYPE_???(gentype, x, basictype, y) ak if type(x) == gentype, then y else basictype // Define generic functions // TODO: m_ prefix? @@ -94,7 +100,7 @@ struct m_g3neric_dummys; #define clear(x) M_G3N_CALL_1(M_CALL_CLEAR, x) #define init_move(x, y) M_G3N_CALL_2(M_CALL_INIT_MOVE, x, y) #define push(x, y) M_G3N_CALL_2t(M_CALL_PUSH, x, y) - +#define empty_p(x) M_G3N_CALL_1(M_CALL_EMPTY_P, x) /* User code has to register oplists : diff --git a/tests/test-mgeneric.c b/tests/test-mgeneric.c index 2b82b0c0..6c5adff6 100644 --- a/tests/test-mgeneric.c +++ b/tests/test-mgeneric.c @@ -87,25 +87,30 @@ const string_t gx; // MSVC++: decltype // C23; typeof +static bool test_empty(const string_t p) +{ + return empty_p(p); +} + static void test_string(string_t p) { string_t s , d; - // ‘_Generic’ selector of type ‘const struct m_string_s *’ is not compatible with any association - //init(gx); init(s); h(s); init_set(d, s); h(d); - init_set(p, gx); + init_set(p, s); + bool b = test_empty(p); + assert(!b); clear(s); clear(d); float f; init(f); clear(f); + clear(p); -// clear(gx); } int main(void)