Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging attempt 2 #8

Merged
merged 6 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 104 additions & 112 deletions examples/c_print_args/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,52 @@

#include "metac/reflect.h"

void vprint_args(metac_tag_map_t * p_tag_map, metac_entry_t *p_entry, va_list args) {
if (p_entry == NULL || metac_entry_has_paremeter(p_entry) == 0) {
static metac_flag_t handle_sz(va_list *p_args, metac_size_t sz, char * p_buf /*max 16b */) {
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 calling, metac_entry_t *p_entry, metac_value_t * p_res, va_list args) {
if (p_entry == NULL || metac_entry_has_paremeters(p_entry) == 0) {
return;
}

if (calling == 1) {
printf("calling ");
}

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

for (int i = 0; i < metac_entry_paremeter_count(p_entry); ++i) {
char buf[16];

for (int i = 0; i < metac_entry_paremeters_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,91 +105,67 @@ 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;
}
if (calling == 0) {
printf(" returned");
if (p_res != NULL) {
char * v = metac_value_string_ex(p_res, METAC_WMODE_deep, p_tag_map);
if (v == NULL) {
return;
}
printf(" %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_res);
}
}

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 calling, metac_entry_t *p_entry, metac_value_t * p_res, ...) {
va_list args;
va_start(args, p_val);
vprint_args(p_tag_map, p_entry, args);
va_start(args, p_res);
vprint_args(p_tag_map, calling, p_entry, p_res, 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"); \
#define METAC_WRAP_FN_NORES(_fn_, _args_...) { \
print_args(NULL, 1, METAC_GSYM_LINK_ENTRY(_fn_), NULL, _args_); \
_fn_(_args_); \
})

print_args(NULL, 0, METAC_GSYM_LINK_ENTRY(_fn_), NULL, _args_); \
}
#define METAC_WRAP_FN_RES(_type_, _fn_, _args_...) ({ \
printf("calling "); \
print_args(NULL, METAC_GSYM_LINK_ENTRY(_fn_), _args_); \
printf("\n"); \
print_args(NULL, 1, METAC_GSYM_LINK_ENTRY(_fn_), NULL, _args_); \
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_), METAC_VALUE_FROM_DECLLOC(loc, res), _args_); \
res; \
})


#define DEBUG

int test_function1_with_args(int a, short b){
return a + b + 6;
}
METAC_GSYM_LINK(test_function1_with_args);
#ifdef DEBUG
#define test_function1_with_args(args...) METAC_WRAP_FN_RES(int, test_function1_with_args, args)
#endif

int test_function2_with_args(int *a, short b){
return *a + b + 999;
}
METAC_GSYM_LINK(test_function2_with_args);
#ifdef DEBUG
#define test_function2_with_args(args...) METAC_WRAP_FN_RES(int, test_function2_with_args, args)
#endif

typedef struct list_s{
double x;
Expand All @@ -205,30 +176,51 @@ 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;
}
return sum;
}
METAC_GSYM_LINK(test_function3_with_args);
}
METAC_GSYM_LINK(test_function3_with_args);
#ifdef DEBUG
#define test_function3_with_args(args...) METAC_WRAP_FN_RES(double, test_function3_with_args, args)
#endif

double test_function4_with_args(list_t *p_list) {
return METAC_WRAP_FN_RES(double, test_function3_with_args, p_list) - 1000;
return test_function3_with_args(p_list) - 1000;
}
METAC_GSYM_LINK(test_function4_with_args);

#ifdef DEBUG
#define test_function4_with_args(args...) METAC_WRAP_FN_RES(double, test_function4_with_args, args)
#endif

void test_function5_no_res(list_t *p_list) {
while(p_list != NULL) {
p_list->x -= 1;

p_list = p_list->p_next;
}
}
METAC_GSYM_LINK(test_function5_no_res);
#ifdef DEBUG
#define test_function5_no_res(args...) METAC_WRAP_FN_NORES(test_function5_no_res, args);
#endif

int main() {
printf("fn returned: %i\n", METAC_WRAP_FN(test_function1_with_args, 10, 22));
printf("fn returned: %i\n", 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));
printf("fn returned: %i\n", 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", test_function3_with_args(p_list));

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

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

test_function5_no_res(p_list);

return 0;
}
6 changes: 3 additions & 3 deletions include/metac/reflect/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@ metac_entry_t * metac_entry_by_member_ids(metac_entry_t * p_entry, metac_flag_t
/**
* @brief check if final entry is function (METAC_KND_subroutine)
*/
metac_flag_t metac_entry_has_paremeter(metac_entry_t * p_entry);
metac_flag_t metac_entry_has_paremeters(metac_entry_t * p_entry);
/**
* @brief return number of parameters. Negative value is errno
*/
metac_num_t metac_entry_paremeter_count(metac_entry_t *p_entry);
metac_num_t metac_entry_paremeters_count(metac_entry_t *p_entry);
/**
* @brief returns pointer to the parameter entry (kind==METAC_KND_member) by param_id which is in range [0; metac_entry_paremeter_count -1]
* @brief returns pointer to the parameter entry (kind==METAC_KND_member) by param_id which is in range [0; metac_entry_paremeters_count -1]
*/
metac_entry_t * metac_entry_by_paremeter_id(metac_entry_t *p_entry, metac_num_t param_id);
/**
Expand Down
6 changes: 3 additions & 3 deletions src/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,11 @@ static metac_entry_t * _entry_with_paremeter_info(metac_entry_t *p_entry) {
return p_final_entry;
}

metac_flag_t metac_entry_has_paremeter(metac_entry_t * p_entry) {
metac_flag_t metac_entry_has_paremeters(metac_entry_t * p_entry) {
return _entry_with_paremeter_info(p_entry) != NULL;
}

metac_num_t metac_entry_paremeter_count(metac_entry_t *p_entry) {
metac_num_t metac_entry_paremeters_count(metac_entry_t *p_entry) {
metac_entry_t * p_final_entry = _entry_with_paremeter_info(p_entry);
_check_(p_final_entry == NULL, -(EINVAL));
return p_final_entry->subprogram_info.parameters_count;
Expand All @@ -489,7 +489,7 @@ metac_num_t metac_entry_paremeter_count(metac_entry_t *p_entry) {
metac_num_t metac_entry_paremeter_name_to_id(metac_entry_t *p_entry, metac_name_t name) {
metac_entry_t * p_final_entry = _entry_with_paremeter_info(p_entry);
_check_(p_final_entry == NULL, -(EINVAL));
metac_num_t par_count = metac_entry_paremeter_count(p_entry);
metac_num_t par_count = metac_entry_paremeters_count(p_entry);
_check_(par_count < 0, -(EFAULT));
for (metac_num_t i = 0; i < par_count; ++i) {
if (p_final_entry->subprogram_info.parameters[i].name != NULL &&
Expand Down
Loading