Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
P-p-H-d committed Nov 5, 2023
1 parent 4486196 commit e78963a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
41 changes: 37 additions & 4 deletions m-generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,39 @@ struct m_g3neric_dummys;
gentype: M_C(M_CALL_, op)(oplist(), x, M_AS_TYPE(gentype, y)), \
const gentype: M_C(M_CALL_, op)(oplist(), x, M_AS_TYPE(const gentype, y)),

// Call the OPERATOR call of the oplist registered to the variable 'x'
// which takes two argument (type, keytype)
#define M_G3N_CALL_2k(op, x, y) \
_Generic( ((void)0, (x)), \
M_MAP2(M_G3N_CALL_2k_func, (op, x, y) M_G3N_REGISTERED_ITEMS() ) \
struct m_g3neric_dummys *: /* cannot happen */ (void) 0)
#define M_G3N_CALL_2k_func(x, oplist) \
M_G3N_CALL_2k_func_test(M_GET_GENTYPE oplist(), \
M_IF_METHOD(GENTYPE, M_GET_KEY_OPLIST oplist())(M_GET_GENTYPE M_GET_KEY_OPLIST oplist(), M_GET_KEY_TYPE oplist()), \
M_TRIPLE_1 x, M_TRIPLE_2 x, M_TRIPLE_3 x, oplist)
#define M_G3N_CALL_2k_func_test(gentype, keytype, op, x, y, oplist) \
M_IF_METHOD(op, oplist())(M_G3N_CALL_2k_func_expand, M_EAT)(gentype, keytype, op, x, y, oplist)
#define M_G3N_CALL_2k_func_expand(gentype, keytype, op, x, y, oplist) \
gentype: M_C(M_CALL_, op)(oplist(), M_AS_TYPE(gentype, x), M_AS_LINKED_TYPE(gentype, x, keytype, y)), \
const gentype: M_C(M_CALL_, op)(oplist(), M_AS_LINKED_TYPE(const gentype, x, gentype, x), M_AS_LINKED_TYPE(const gentype, x, keytype, y)),

// Call the OPERATOR call of the oplist registered to the variable 'x'
// which takes two argument (type, keytype, valuetype)
#define M_G3N_CALL_3kv(op, x, y, z) \
_Generic( ((void)0, (x)), \
M_MAP2(M_G3N_CALL_3kv_func, (op, x, y, z) M_G3N_REGISTERED_ITEMS() ) \
struct m_g3neric_dummys *: /* cannot happen */ (void) 0)
#define M_G3N_CALL_3kv_func(x, oplist) \
M_G3N_CALL_3kv_func_test(M_GET_GENTYPE oplist(), \
M_IF_METHOD(GENTYPE, M_GET_KEY_OPLIST oplist())(M_GET_GENTYPE M_GET_KEY_OPLIST oplist(), M_GET_KEY_TYPE oplist()), \
M_IF_METHOD(GENTYPE, M_GET_VALUE_OPLIST oplist())(M_GET_GENTYPE M_GET_VALUE_OPLIST oplist(), M_GET_VALUE_TYPE oplist()), \
M_QUAD_1 x, M_QUAD_2 x, M_QUAD_3 x, M_QUAD_4 x, oplist)
#define M_G3N_CALL_3kv_func_test(gentype, keytype, valuetype, op, x, y, z, oplist) \
M_IF_METHOD(op, oplist())(M_G3N_CALL_3kv_func_expand, M_EAT)(gentype, keytype, valuetype, op, x, y, z, oplist)
#define M_G3N_CALL_3kv_func_expand(gentype, keytype, valuetype, op, x, y, z, oplist) \
gentype: M_C(M_CALL_, op)(oplist(), M_AS_TYPE(gentype, x), M_AS_LINKED_TYPE(gentype, x, keytype, y), M_AS_LINKED_TYPE(gentype, x, valuetype, z)), \
const gentype: M_C(M_CALL_, op)(oplist(), M_AS_LINKED_TYPE(const gentype, x, gentype, x), M_AS_LINKED_TYPE(const gentype, x, keytype, y), M_AS_LINKED_TYPE(const gentype, x, valuetype, z) ),

#define M_AS_LINKED_TYPE(typex, x, typey, y) _Generic(((void)0,(x)), typex: (y), default: (typey) {0})

// Define generic functions
Expand All @@ -164,10 +197,10 @@ struct m_g3neric_dummys;
#define mul(x, y) M_G3N_CALL_2(MUL, x, y)
#define div(x, y) M_G3N_CALL_2(DIV, x, y)
#define reset(x) M_G3N_CALL_1(RESET, x)
// get
// set_at
// safe_get
// erase
#define get(x, y) M_G3N_CALL_2k(GET_KEY, x, y)
#define set_at(x, y, z) M_G3N_CALL_3kv(SET_KEY, x, y, z)
#define safe_get(x, y) M_G3N_CALL_2k(SAFE_GET_KEY, x, y)
#define erase(x, y) M_G3N_CALL_2k(ERASE, x, y)
#define get_size(x) M_G3N_CALL_1(GET_SIZE, x)
#define push(x, y) M_G3N_CALL_2t(PUSH, x, y)
#define pop(x, y) M_G3N_CALL_t2(PUSH, x, y)
Expand Down
3 changes: 2 additions & 1 deletion tests/test-mgeneric.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const string_t gx;
FIXME: uses of C23 typeof + extensions ?
*/

#define STR1 M_OPEXTEND(STRING_OPLIST, GENTYPE(struct m_string_s *))
#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) )

Expand Down Expand Up @@ -104,6 +104,7 @@ static void test_string(string_t p)
init_set(p, s);
bool b = test_empty(p);
assert(!b);
push(p, 'c');
clear(s);
clear(d);

Expand Down

0 comments on commit e78963a

Please sign in to comment.