Skip to content

Commit

Permalink
WIP: support for const struct
Browse files Browse the repository at this point in the history
  • Loading branch information
P-p-H-d committed Nov 4, 2023
1 parent 27d9c70 commit 242dce2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
14 changes: 10 additions & 4 deletions m-generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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?
Expand All @@ -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 :
Expand Down
13 changes: 9 additions & 4 deletions tests/test-mgeneric.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 242dce2

Please sign in to comment.