Skip to content

Commit

Permalink
alternative approach, though still need a type to have copy of results
Browse files Browse the repository at this point in the history
  • Loading branch information
aodinokov committed Jun 10, 2024
1 parent 1d37fac commit 1f94fd2
Showing 1 changed file with 87 additions and 104 deletions.
191 changes: 87 additions & 104 deletions examples/c_print_args/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,51 @@

#include "metac/reflect.h"

void vprint_args(metac_tag_map_t * p_tag_map, metac_entry_t *p_entry, va_list args) {
static metac_flag_t handle_sz(va_list *p_args, metac_size_t sz, char * p_buf /*16*/) {
if (p_buf == NULL) {
return 0;
}
#define _handle_sz_(_sz_) \
do { \
if (sz == _sz_) { \
char *x = va_arg(*p_args, char[_sz_]); \
memcpy(p_buf, x, sz); \
return 1;\
} \
} while(0)
_handle_sz_(1);
_handle_sz_(2);
_handle_sz_(3);
_handle_sz_(4);
_handle_sz_(5);
_handle_sz_(6);
_handle_sz_(7);
_handle_sz_(8);
_handle_sz_(9);
_handle_sz_(10);
_handle_sz_(11);
_handle_sz_(12);
_handle_sz_(13);
_handle_sz_(14);
_handle_sz_(15);
_handle_sz_(16);
#undef _handle_sz_
return 0;
}

void vprint_args(metac_tag_map_t * p_tag_map, metac_flag_t has_res, metac_entry_t *p_entry, va_list args) {
if (p_entry == NULL || metac_entry_has_paremeter(p_entry) == 0) {
return;
}

if (has_res == 0) {
printf("calling ");
}

printf("%s(", metac_entry_name(p_entry));

char buf[16];

for (int i = 0; i < metac_entry_paremeter_count(p_entry); ++i) {
if (i > 0) {
printf(", ");
Expand Down Expand Up @@ -41,55 +79,12 @@ void vprint_args(metac_tag_map_t * p_tag_map, metac_entry_t *p_entry, va_list ar
break;
}

char buf[32];
int handled = 0;
#define _handle_sz_(_sz_) \
do { \
if (param_byte_sz == _sz_) { \
char *x = va_arg(args, char[_sz_]); \
memcpy(buf, x, _sz_); \
handled = 1; \
} \
} while(0)
_handle_sz_(1);
_handle_sz_(2);
_handle_sz_(3);
_handle_sz_(4);
_handle_sz_(5);
_handle_sz_(6);
_handle_sz_(7);
_handle_sz_(8);
_handle_sz_(9);
_handle_sz_(10);
_handle_sz_(11);
_handle_sz_(12);
_handle_sz_(13);
_handle_sz_(14);
_handle_sz_(15);
_handle_sz_(16);
_handle_sz_(17);
_handle_sz_(18);
_handle_sz_(19);
_handle_sz_(20);
_handle_sz_(21);
_handle_sz_(22);
_handle_sz_(23);
_handle_sz_(24);
_handle_sz_(25);
_handle_sz_(26);
_handle_sz_(27);
_handle_sz_(28);
_handle_sz_(29);
_handle_sz_(30);
_handle_sz_(31);
_handle_sz_(32);
#undef _handle_sz_

int handled = handle_sz(&args, param_byte_sz, &buf[0]);
if (handled == 0) {
break;
}

metac_value_t * p_val = metac_new_value(p_param_type_entry, &buf);
metac_value_t * p_val = metac_new_value(p_param_type_entry, &buf[0]);
if (p_val == NULL) {
break;
}
Expand All @@ -110,82 +105,70 @@ void vprint_args(metac_tag_map_t * p_tag_map, metac_entry_t *p_entry, va_list ar

free(arg_decl);
free(v);

metac_value_delete(p_val);

}
printf(")");
}

void print_args(metac_tag_map_t * p_tag_map, metac_entry_t *p_entry, ...) {
va_list args;
va_start(args, p_entry);
vprint_args(p_tag_map, p_entry, args);
va_end(args);
return;
}
do {
if (has_res != 0 && metac_entry_has_result(p_entry)){
metac_entry_t * p_result_type_entry = metac_entry_result_type(p_entry);
if (p_result_type_entry == NULL) {
break;
}

metac_size_t result_byte_sz = 0;
if (metac_entry_byte_size(p_result_type_entry, &result_byte_sz) != 0) {
break;
}

int handled = handle_sz(&args, result_byte_sz, &buf[0]);
if (handled == 0) {
break;
}

metac_value_t * p_val = metac_new_value(p_result_type_entry, &buf[0]);
if (p_val == NULL) {
break;
}
char * v = metac_value_string_ex(p_val, METAC_WMODE_deep, p_tag_map);
if (v == NULL) {
metac_value_delete(p_val);
break;
}
printf(" returned %s", v);

void print_res(metac_tag_map_t * p_tag_map, metac_entry_t * p_entry, metac_value_t * p_val) {
if (p_entry == NULL || metac_entry_has_result(p_entry) == 0 || p_val == NULL) {
return;
}
metac_entry_t * p_res_type = metac_entry_result_type(p_entry);
if (metac_entry_final_entry(p_res_type, NULL) != metac_entry_final_entry(metac_value_entry(p_val), NULL)) {
printf("provided type doesn't match with type info\n");
return;
}
char * v = metac_value_string_ex(p_val, METAC_WMODE_deep, p_tag_map);
if (v == NULL) {
return;
}
free(v);

printf("%s() returned %s\n", metac_entry_name(p_entry), v);
free(v);
metac_value_delete(p_val);
}
}while(0);

metac_value_delete(p_val);
printf("\n");
}

void print_args_and_res(metac_tag_map_t * p_tag_map, metac_entry_t * p_entry, metac_value_t * p_val, ...) {
if (p_entry == NULL || metac_entry_has_result(p_entry) == 0 || p_val == NULL) {
return;
}
metac_entry_t * p_res_type = metac_entry_result_type(p_entry);
if (metac_entry_final_entry(p_res_type, NULL) != metac_entry_final_entry(metac_value_entry(p_val), NULL)) {
printf("provided type doesn't match with type info\n");
return;
}
char * v = metac_value_string_ex(p_val, METAC_WMODE_deep, p_tag_map);
if (v == NULL) {
return;
}

void print_args(metac_tag_map_t * p_tag_map, metac_flag_t has_res, metac_entry_t *p_entry, ...) {
va_list args;
va_start(args, p_val);
vprint_args(p_tag_map, p_entry, args);
va_start(args, p_entry);
vprint_args(p_tag_map, has_res, p_entry, args);
va_end(args);
printf(" returned %s\n", v);
free(v);

metac_value_delete(p_val);
return;
}

#define METAC_WRAP_FN(_fn_, _args_...) ({ \
printf("calling "); \
print_args(NULL, METAC_GSYM_LINK_ENTRY(_fn_), _args_); \
printf("\n"); \
print_args(NULL, 0, METAC_GSYM_LINK_ENTRY(_fn_), _args_); \
_fn_(_args_); \
})

#define METAC_WRAP_FN_RES(_type_, _fn_, _args_...) ({ \
printf("calling "); \
print_args(NULL, METAC_GSYM_LINK_ENTRY(_fn_), _args_); \
printf("\n"); \
WITH_METAC_DECLLOC(loc, _type_ res = _fn_(_args_)); \
print_args_and_res(NULL, METAC_GSYM_LINK_ENTRY(_fn_), METAC_VALUE_FROM_DECLLOC(loc, res), _args_); \
print_args(NULL, 0, METAC_GSYM_LINK_ENTRY(_fn_), _args_); \
_type_ res = _fn_(_args_); \
print_args(NULL, 1, METAC_GSYM_LINK_ENTRY(_fn_), _args_, res); \
res; \
})



int test_function1_with_args(int a, short b){
return a + b + 6;
}
Expand Down Expand Up @@ -220,15 +203,15 @@ METAC_GSYM_LINK(test_function4_with_args);
int main() {
printf("fn returned: %i\n", METAC_WRAP_FN(test_function1_with_args, 10, 22));

int x = 689; /* could use (int[]){{689, }}, but used this to simplify reading */
printf("fn returned: %i\n", METAC_WRAP_FN(test_function2_with_args, &x, 22));
// int x = 689; /* could use (int[]){{689, }}, but used this to simplify reading */
// printf("fn returned: %i\n", METAC_WRAP_FN(test_function2_with_args, &x, 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(test_function3_with_args, p_list));
// printf("fn returned: %f\n", METAC_WRAP_FN(test_function3_with_args, p_list));

METAC_WRAP_FN_RES(int, test_function2_with_args, &x, 22);
// METAC_WRAP_FN_RES(int, test_function2_with_args, &x, 22);

METAC_WRAP_FN_RES(double, test_function4_with_args, p_list);
printf("fn returned: %f\n", METAC_WRAP_FN_RES(double, test_function4_with_args, p_list));

return 0;
}

0 comments on commit 1f94fd2

Please sign in to comment.