Skip to content

Commit

Permalink
since we already using extension - made type detection of result auto…
Browse files Browse the repository at this point in the history
…matic
  • Loading branch information
aodinokov committed Aug 23, 2024
1 parent 2cb622f commit c25caa6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/c_print_args_alt/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ This is an alternative approach to the c_print_args

The original approach requires some non-documented capabilities of va_arg to work with structures and
va_list (it's not easy to extract struct or va_list from va_list which we used as a storage).
The current approach in its turn requires __typeof compiler extension.
The current approach in its turn requires `typeof` compiler extension.
Also this apporach has a limitation of number of arguments: it's necessary to regenerate mr_pp.generated.h using mr_pp.sh and provide the maximum number of arguments. For now it's generated with maximum 1024 arguments
which is mentioned as a minimum supported number of arguments in C in some referenses.
9 changes: 5 additions & 4 deletions examples/c_print_args_alt/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdlib.h> /*free*/

int test_function1_with_args(int a, short b){
printf("called\n");
return a + b + 6;
}
METAC_GSYM_LINK(test_function1_with_args);
Expand Down Expand Up @@ -38,7 +39,7 @@ METAC_TAG_MAP_NEW(va_args_tag_map, NULL, {.mask =
METAC_TAG_MAP_END

#define _APPEND(x) do { \
WITH_METAC_DECLLOC(decl, __typeof(x) _x_val = x); \
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); \
/* TODO: compare types */ \
Expand All @@ -53,7 +54,7 @@ METAC_TAG_MAP_END
}while(0);

// alternative implementation
#define METAC_WRAP_FN_RES(_type_, _tag_map_, _fn_, _args_...) ({ \
#define METAC_WRAP_FN_RES(_tag_map_, _fn_, _args_...) ({ \
metac_value_t * p_val = NULL; \
metac_parameter_storage_t * p_param_storage = metac_new_parameter_storage(); \
if (p_param_storage != NULL) { \
Expand All @@ -71,7 +72,7 @@ METAC_TAG_MAP_END
free(s); \
} \
} \
WITH_METAC_DECLLOC(loc, _type_ res = _fn_(_args_)); \
WITH_METAC_DECLLOC(loc, typeof(_fn_(_args_)) res = _fn_(_args_)); \
if (p_val != NULL) { \
metac_value_t *p_res_val = METAC_VALUE_FROM_DECLLOC(loc, res); \
char * s = metac_value_string_ex(p_val, METAC_WMODE_deep, _tag_map_); \
Expand Down Expand Up @@ -102,7 +103,7 @@ METAC_TAG_MAP_END
int main() {
p_tagmap = va_args_tag_map();

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

metac_tag_map_delete(p_tagmap);
return 0;
Expand Down

0 comments on commit c25caa6

Please sign in to comment.