Skip to content

Instantly share code, notes, and snippets.

@edenhill
Last active September 15, 2016 20:25
Show Gist options
  • Save edenhill/25c7e6e2e869186ad6c94c69259c20cc to your computer and use it in GitHub Desktop.
Save edenhill/25c7e6e2e869186ad6c94c69259c20cc to your computer and use it in GitHub Desktop.
str reps
add_str_rep (const char *typestr, const char *rep (void *obj)) {
LIST_INSERT..(&str_reps, {typestr, rep});
}
const char *to_str (const char *typestr, void *obj) {
str_rep = LIST_FIND(&str_reps, trim_stuff(typestr); /* remove "const", "*", etc.. */));
if (!str_rep)
return rsprintf("(%s)%p", typestr, obj);
else
return str_rep.callback(obj);
}
#define STR(OBJ) to_str(# typeof(OBJ), OBJ)
int main (void) {
struct iovec iov;
const my_type_t hehe[19];
int foo;
add_str_rep("struct iovec", func (obj) { struct iovec *p = obj; return tsprintf("iov_base=%p, iov_len=%d", p->iov_base, p->iov_len) });
...
printf("%s: %s, %s\n", STR(&foo), STR(&iov), STR(&hehe[2]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment