Sujet : Re: Help requested with P99-based macro to create Wayland listeners
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.lang.cDate : 11. Apr 2024, 03:56:36
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <864jc81uaj.fsf@linuxsc.com>
References : 1
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Blue-Maned_Hawk <
bluemanedhawk@invalid.invalid> writes:
This macro doesn't work and i don't know why. I've tried multiple
different things to try to delay the expansion of _0MAKE_LISTENER
and none of them have worked. I don't know if there's a more
idiomatic way to do this. I've tried looking at the output from
putting it through ?cpp? and that didn't work because it errored
out before i could see what it expanded to.
>
#define MAKE_LISTENER(object_type, ...) static const struct\
object_type##_listener object_type##_listener = { P99_SEQ(\
_0MAKE_LISTENER, P99_SEQ((object_type), __VA_ARGS__)) }
#define _0MAKE_LISTENER(x) _1MAKE_LISTENER (x, _2MAKE_LISTENER
#define _1MAKE_LISTENER _3MAKE_LISTENER
#define _2MAKE_LISTENER(y) y)
#define _3MAKE_LISTENER _4MAKE_LISTENER
#define _4MAKE_LISTENER(object_type, event_name) .event_name =\
callback_##object_type##_##event_name
After a lot of trial and error, I came up with the code below.
Probably it could be simplified, but I was happy just to get
it to work.
#include "p99.h"
#define static_const_LISTENER( kind, ... ) \
static const PLAIN_LISTENER( kind, __VA_ARGS__ )
#define PLAIN_LISTENER( kind, ... ) \
struct LISTENER_TAG_NAME( kind ) LISTENER_TAG_NAME( kind ) = { \
LISTENER_FIELDS( kind, __VA_ARGS__ ) \
}
#define LISTENER_TAG_NAME( kind ) JOIN2( kind, _listener )
#define LISTENER_FIELDS( kind, ... ) \
P99_SEQ( AS_IS, \
P99_SEQ( ONE_LISTENER_FIELD_2 AS_IS, \
P99_SEQ( ONE_LISTENER_FIELD, P99_SEQ( (kind), __VA_ARGS__ ) ) \
) \
)
#define ONE_LISTENER_FIELD( a ) ONE_LISTENER_FIELDx( a )
#define ONE_LISTENER_FIELDx( a ) SINGLES_INTO_PAIR( a )
#define SINGLES_INTO_PAIR( a_b ) ( ONE_COMMA_TWO a_b )
#define ONE_COMMA_TWO( a ) a, AS_IS
#define AS_IS( a ) a
#define ONE_LISTENER_FIELD_2( a, b ) ONE_LISTENER_FIELD_2x( a, b )
#define ONE_LISTENER_FIELD_2x( kind, field ) \
.field = callback_ ## kind ## _ ## field
#define JOIN2( a, b ) JOIN2x( a, b )
#define JOIN2x( a, b ) a ## b
static_const_LISTENER( wl_registry, global, global_remove );
static_const_LISTENER(
xdg_wm_base,
configure, close, configure_bounds, wm_capabilities
);