type_to_string: make type printing dynamic.

The union still contains all the types, but we can only print
the ones which are linked in.

This makes it much easier to use type_to_string in different binaries
without pulling in the world.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-01-04 14:06:15 +10:30
parent f8eb454139
commit 5bd8063ddb
2 changed files with 51 additions and 4 deletions

View File

@ -11,6 +11,8 @@
#include <ccan/tal/str/str.h>
#include <inttypes.h>
REGISTER_TYPE_TO_STRING(netaddr, netaddr_name);
char *type_to_string_(const tal_t *ctx, const char *typename,
union printable_types u)
{
@ -78,9 +80,26 @@ char *type_to_string_(const tal_t *ctx, const char *typename,
&u.cstate->side[LOCAL]),
type_to_string(ctx, struct channel_oneside,
&u.cstate->side[REMOTE]));
} else if (streq(typename, "struct netaddr")) {
s = netaddr_name(ctx, u.netaddr);
}
} else {
size_t i;
static size_t num_p;
static struct type_to_string **t = NULL;
if (!t)
t = autodata_get(type_to_string, &num_p);
/* Typenames in registrations don't include "struct " */
if (strstarts(typename, "struct "))
typename += strlen("struct ");
for (i = 0; i < num_p; i++) {
if (streq(t[i]->typename, typename)) {
s = t[i]->fmt(ctx, u);
break;
}
}
if (!s)
s = tal_fmt(ctx, "UNKNOWN TYPE %s", typename);
}
return s;
}

View File

@ -1,7 +1,8 @@
#ifndef LIGHTNING_TYPE_TO_STRING_H
#define LIGHTNING_TYPE_TO_STRING_H
#include "config.h"
#include <ccan/tal/tal.h>
#include "utils.h"
#include <ccan/autodata/autodata.h>
/* This must match the type_to_string_ cases. */
union printable_types {
@ -27,4 +28,31 @@ union printable_types {
char *type_to_string_(const tal_t *ctx, const char *typename,
union printable_types u);
#define REGISTER_TYPE_TO_STRING(typename, fmtfn) \
static char *fmt_##typename##_(const tal_t *ctx, \
union printable_types u) \
{ \
return fmtfn(ctx, u.typename); \
} \
static struct type_to_string ttos_##typename = { \
#typename, fmt_##typename##_ \
}; \
AUTODATA(type_to_string, &ttos_##typename)
#define REGISTER_TYPE_TO_HEXSTR(typename) \
static char *fmt_##typename##_(const tal_t *ctx, \
union printable_types u) \
{ \
return tal_hexstr(ctx, u.typename, sizeof(*u.typename)); \
} \
static struct type_to_string ttos_##typename = { \
#typename, fmt_##typename##_ \
}; \
AUTODATA(type_to_string, &ttos_##typename)
struct type_to_string {
const char *typename;
char *(*fmt)(const tal_t *ctx, union printable_types u);
};
AUTODATA_TYPE(type_to_string, struct type_to_string);
#endif /* LIGHTNING_UTILS_H */