Skip to content

Commit

Permalink
added example with pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
aodinokov committed Aug 23, 2024
1 parent c25caa6 commit 7919acd
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions examples/c_print_args_alt/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,32 @@ int test_function1_with_args(int a, short b){
}
METAC_GSYM_LINK(test_function1_with_args);

int my_printf(const char * format, ...) {
va_list l;
va_start(l, format);
int res = vprintf(format, l);
va_end(l);
return res;
typedef struct list_s{
double x;
struct list_s * p_next;
}list_t;

double test_function3_with_args(list_t *p_list) {
double sum = 0.0;
while(p_list != NULL) {
sum += p_list->x;
p_list->x += 1;

p_list = p_list->p_next;
}
printf("!!!result is %f %Lf!!!\n", sum, (long double)sum);
return sum;
}
METAC_GSYM_LINK(my_printf);
METAC_GSYM_LINK(test_function3_with_args);

// int my_printf(const char * format, ...) {
// va_list l;
// va_start(l, format);
// int res = vprintf(format, l);
// va_end(l);
// return res;
// }
// METAC_GSYM_LINK(my_printf);


metac_tag_map_t * p_tagmap = NULL;
Expand All @@ -27,18 +45,18 @@ METAC_TAG_MAP_NEW(va_args_tag_map, NULL, {.mask =
METAC_TAG_MAP_ENTRY_CATEGORY_MASK(METAC_TEC_final),},)
/* start tags for all types */

METAC_TAG_MAP_ENTRY(METAC_GSYM_LINK_ENTRY(my_printf))
METAC_TAG_MAP_SET_TAG(0, METAC_TEO_entry, 0, METAC_TAG_MAP_ENTRY_PARAMETER({.n = "format"}),
METAC_ZERO_ENDED_STRING()
)
METAC_TAG_MAP_SET_TAG(0, METAC_TEO_entry, 0, METAC_TAG_MAP_ENTRY_PARAMETER({.i = 1}),
METAC_FORMAT_BASED_VA_ARG()
)
METAC_TAG_MAP_ENTRY_END
// METAC_TAG_MAP_ENTRY(METAC_GSYM_LINK_ENTRY(my_printf))
// METAC_TAG_MAP_SET_TAG(0, METAC_TEO_entry, 0, METAC_TAG_MAP_ENTRY_PARAMETER({.n = "format"}),
// METAC_ZERO_ENDED_STRING()
// )
// METAC_TAG_MAP_SET_TAG(0, METAC_TEO_entry, 0, METAC_TAG_MAP_ENTRY_PARAMETER({.i = 1}),
// METAC_FORMAT_BASED_VA_ARG()
// )
// METAC_TAG_MAP_ENTRY_END

METAC_TAG_MAP_END

#define _APPEND(x) do { \
#define _APPEND_PARAM(x) do { \
WITH_METAC_DECLLOC(decl, typeof(x) _x_val = x); \
metac_entry_t *p_entry = METAC_ENTRY_FROM_DECLLOC(decl, _x_val); \
metac_entry_t *p_param_entry = metac_entry_by_paremeter_id(p_val_entry, param_id); \
Expand All @@ -63,7 +81,7 @@ METAC_TAG_MAP_END
metac_num_t param_id = 0; \
metac_entry_t *p_val_entry = metac_value_entry(p_val); \
/* append params*/ \
MR_FOREACH(_APPEND, _args_) \
MR_FOREACH(_APPEND_PARAM, _args_) \
} \
if (p_val != NULL) { \
char * s = metac_value_string_ex(p_val, METAC_WMODE_deep, _tag_map_); \
Expand Down Expand Up @@ -105,6 +123,9 @@ int main() {

printf("fn returned: %i\n", METAC_WRAP_FN_RES(NULL, test_function1_with_args,10, 22));

list_t * p_list = (list_t[]){{.x = 42.42, .p_next = (list_t[]){{ .x = 45.4, .p_next = NULL}}}};
printf("fn returned: %f\n", METAC_WRAP_FN_RES(NULL, test_function3_with_args, p_list));

metac_tag_map_delete(p_tagmap);
return 0;
}

0 comments on commit 7919acd

Please sign in to comment.