Skip to content

Commit

Permalink
Add support for M_PRINT
Browse files Browse the repository at this point in the history
  • Loading branch information
P-p-H-d committed Dec 23, 2023
1 parent 88039e9 commit a7d4e42
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
40 changes: 39 additions & 1 deletion m-generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,45 @@ typedef const char **m_g3n_cstring_end;
it_next(l_it))

// TODO: init_with ? How to handle the different type of parameters ? emplace ?
// TODO: Integrate in M_PRINT ?

#define M_FPRINT_ARG_G3N(f, x) \
M_G3N_BEGIN_PROTECTED_CODE \
_Generic(((void)0,(x)), \
char: fprintf(f, "%c", M_AS_TYPE(char,x)), \
bool: fprintf(f, "%d", M_AS_TYPE(bool,x)), \
signed char: fprintf(f, "%hhd", M_AS_TYPE(signed char,x)), \
unsigned char: fprintf(f, "%hhu", M_AS_TYPE(unsigned char,x)), \
signed short: fprintf(f, "%hd", M_AS_TYPE(signed short,x)), \
unsigned short: fprintf(f, "%hu", M_AS_TYPE(unsigned short,x)), \
signed int: fprintf(f, "%d", M_AS_TYPE(signed int,x)), \
unsigned int: fprintf(f, "%u", M_AS_TYPE(unsigned int,x)), \
long int: fprintf(f, "%ld", M_AS_TYPE(long int,x)), \
unsigned long int: fprintf(f, "%lu", M_AS_TYPE(unsigned long int,x)), \
long long int: fprintf(f, "%lld", M_AS_TYPE(long long int,x)), \
unsigned long long int: fprintf(f, "%llu", M_AS_TYPE(unsigned long long int,x)), \
float: fprintf(f, "%f", M_AS_TYPE(float,x)), \
double: fprintf(f, "%f", M_AS_TYPE(double,x)), \
long double: fprintf(f, "%Lf", M_AS_TYPE(long double,x)), \
const char *: fprintf(f, "%s", M_AS_TYPE(const char*,x)), \
char *: fprintf(f, "%s", M_AS_TYPE(char*,x)), \
const void *: fprintf(f, "%p", M_AS_TYPE(const void *,x)), \
void *: fprintf(f, "%p", M_AS_TYPE(void *,x)), \
M_MAP2(M_G3N_CALL_2_func, (OUT_STR, x, m_g3n_file, f, TYPE, x) M_G3N_REGISTERED_ITEMS() ) \
struct m_g3neric_dummys *: /* cannot happen */ (void) 0) \
M_G3N_END_PROTECTED_CODE

#define M_G3N_FPRINT_ARG(f, x) \
M_IF(M_PARENTHESIS_P(x)) \
( M_FPRINT_ARG_OUT_STR(f, M_PAIR_2 x, M_PAIR_1 x), \
M_FPRINT_ARG_G3N(f, x) )

/* Ovverride M_PRINT */
#undef M_PRINT
#define M_PRINT(...) do { M_REDUCE2(M_G3N_FPRINT_ARG, M_SEPARATE_PER_SEMICOLON, stdout, __VA_ARGS__); } while (0)

#undef M_FPRINT
#define M_FPRINT(f,...) do { M_REDUCE2(M_G3N_FPRINT_ARG, M_SEPARATE_PER_SEMICOLON, f, __VA_ARGS__); } while (0)


/* User code has to register oplists :
Expand Down
14 changes: 14 additions & 0 deletions tests/test-mgeneric.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,25 @@ static void test_array(void)
}
}

static void test_print(void)
{
int x = 0;
M_PRINT("X= ", x, "\n");
M_LET( (s, "Hello"), string_t) {
M_PRINT("s=", s, "\n");
}
M_LET( (a, 3,4,5), array_int_t) {
M_PRINT("a=", a, "\n");
}
}

int main(void)
{
freopen("atmp-generic.dat", "w", stdout);
string_t p;
test_string(p);
test_array();
test_print();
exit(0);
}

Expand Down

0 comments on commit a7d4e42

Please sign in to comment.