diff options
-rw-r--r-- | cmd/sbc_harness/fs_harness_flash_bin.c | 2 | ||||
-rw-r--r-- | cmd/sbc_harness/fs_harness_flash_bin.h | 1 | ||||
-rw-r--r-- | cmd/sbc_harness/fs_harness_uptime_txt.c | 2 | ||||
-rw-r--r-- | cmd/sbc_harness/fs_harness_uptime_txt.h | 1 | ||||
-rw-r--r-- | cmd/sbc_harness/main.c | 16 | ||||
-rw-r--r-- | lib9p/core_include/lib9p/core.h | 26 | ||||
-rw-r--r-- | lib9p/tests/test_server/fs_flush.c | 2 | ||||
-rw-r--r-- | lib9p/tests/test_server/fs_flush.h | 1 | ||||
-rw-r--r-- | lib9p/tests/test_server/fs_shutdown.c | 4 | ||||
-rw-r--r-- | lib9p/tests/test_server/fs_shutdown.h | 1 | ||||
-rw-r--r-- | lib9p/tests/test_server/fs_whoami.c | 2 | ||||
-rw-r--r-- | lib9p/tests/test_server/fs_whoami.h | 1 | ||||
-rw-r--r-- | lib9p/tests/test_server/main.c | 14 | ||||
-rw-r--r-- | lib9p_util/include/util9p/static.h | 24 | ||||
-rw-r--r-- | lib9p_util/static.c | 4 | ||||
-rw-r--r-- | libdhcp/tests/test_client.c | 6 | ||||
-rw-r--r-- | libhw_cr/host_net.c | 2 | ||||
-rw-r--r-- | libhw_cr/rp2040_hwtimer.c | 2 | ||||
-rw-r--r-- | libhw_cr/w5500.c | 8 | ||||
-rw-r--r-- | libmisc/include/libmisc/fmt.h | 52 | ||||
-rw-r--r-- | libmisc/include/libmisc/obj.h | 72 | ||||
-rw-r--r-- | libmisc/tests/test_obj.c | 2 | ||||
-rw-r--r-- | libmisc/tests/test_obj_nest.c | 2 |
23 files changed, 122 insertions, 125 deletions
diff --git a/cmd/sbc_harness/fs_harness_flash_bin.c b/cmd/sbc_harness/fs_harness_flash_bin.c index 5e6f49c..8bd144e 100644 --- a/cmd/sbc_harness/fs_harness_flash_bin.c +++ b/cmd/sbc_harness/fs_harness_flash_bin.c @@ -201,7 +201,7 @@ static lo_interface lib9p_srv_fio flash_file_fopen(struct flash_file *self, stru self->wbuf.ok = false; } - return lo_box_flash_file_as_lib9p_srv_fio(self); + return LO_BOX(lib9p_srv_fio, self); } /* srv_fio ********************************************************************/ diff --git a/cmd/sbc_harness/fs_harness_flash_bin.h b/cmd/sbc_harness/fs_harness_flash_bin.h index 36382be..148a446 100644 --- a/cmd/sbc_harness/fs_harness_flash_bin.h +++ b/cmd/sbc_harness/fs_harness_flash_bin.h @@ -25,6 +25,5 @@ struct flash_file { END_PRIVATE(FS_HARNESS_FLASH_BIN); }; LO_IMPLEMENTATION_H(lib9p_srv_file, struct flash_file, flash_file); -#define lo_box_flash_file_as_lib9p_srv_file(obj) util9p_box(flash_file, obj) #endif /* _SBC_HARNESS_FS_HARNESS_FLASH_BIN_H_ */ diff --git a/cmd/sbc_harness/fs_harness_uptime_txt.c b/cmd/sbc_harness/fs_harness_uptime_txt.c index 339b410..23ac9d4 100644 --- a/cmd/sbc_harness/fs_harness_uptime_txt.c +++ b/cmd/sbc_harness/fs_harness_uptime_txt.c @@ -89,7 +89,7 @@ static lo_interface lib9p_srv_fio uptime_file_fopen(struct uptime_file *self, st ret->parent = self; ret->buf_len = 0; - return lo_box_uptime_fio_as_lib9p_srv_fio(ret); + return LO_BOX(lib9p_srv_fio, ret); } /* srv_fio ********************************************************************/ diff --git a/cmd/sbc_harness/fs_harness_uptime_txt.h b/cmd/sbc_harness/fs_harness_uptime_txt.h index 7bf2945..c575580 100644 --- a/cmd/sbc_harness/fs_harness_uptime_txt.h +++ b/cmd/sbc_harness/fs_harness_uptime_txt.h @@ -14,6 +14,5 @@ struct uptime_file { uint64_t pathnum; }; LO_IMPLEMENTATION_H(lib9p_srv_file, struct uptime_file, uptime_file); -#define lo_box_uptime_file_as_lib9p_srv_file(obj) util9p_box(uptime_file, obj) #endif /* _SBC_HARNESS_FS_HARNESS_UPTIME_TXT_H_ */ diff --git a/cmd/sbc_harness/main.c b/cmd/sbc_harness/main.c index 71c39b2..d32b775 100644 --- a/cmd/sbc_harness/main.c +++ b/cmd/sbc_harness/main.c @@ -54,11 +54,11 @@ enum { PATH_BASE = __COUNTER__ }; #define STATIC_FILE(STRNAME, ...) UTIL9P_STATIC_FILE(PATH_COUNTER, STRNAME, __VA_ARGS__) #define STATIC_DIR(STRNAME, ...) UTIL9P_STATIC_DIR(PATH_COUNTER, STRNAME, __VA_ARGS__) -#define API_FILE(STRNAME, SYMNAME, ...) \ - lo_box_##SYMNAME##_file_as_lib9p_srv_file(&((struct SYMNAME##_file){ \ - .name = STRNAME, \ - .pathnum = PATH_COUNTER \ - __VA_OPT__(,) __VA_ARGS__ \ +#define API_FILE(STRNAME, SYMNAME, ...) \ + LO_BOX(lib9p_srv_file, &((struct SYMNAME##_file){ \ + .name = STRNAME, \ + .pathnum = PATH_COUNTER \ + __VA_OPT__(,) __VA_ARGS__ \ })) static struct lib9p_srv_file root = @@ -149,7 +149,7 @@ struct { static COROUTINE dhcp_cr(void *) { cr_begin(); - dhcp_client_main(lo_box_w5500_if_as_net_iface(&globals.dev_w5500), "harness"); + dhcp_client_main(LO_BOX(net_iface, &globals.dev_w5500), "harness"); cr_end(); } @@ -157,7 +157,7 @@ static COROUTINE dhcp_cr(void *) { static COROUTINE read9p_cr(void *) { cr_begin(); - lo_interface net_iface iface = lo_box_w5500_if_as_net_iface(&globals.dev_w5500); + lo_interface net_iface iface = LO_BOX(net_iface, &globals.dev_w5500); lo_interface net_stream_listener listener = LO_CALL(iface, tcp_listen, LIB9P_DEFAULT_PORT_9FS); lib9p_srv_accept_and_read_loop(&globals.srv, listener); @@ -204,7 +204,7 @@ COROUTINE init_cr(void *) { 17, /* PIN_CS */ 0, 1, 2, 3); /* DMA channels */ w5500_init(&globals.dev_w5500, "W5500", - lo_box_rp2040_hwspi_as_spi(&globals.dev_spi), + LO_BOX(spi, &globals.dev_spi), 21, /* PIN_INTR */ 20, /* PIN_RESET */ ((struct net_eth_addr){{ diff --git a/lib9p/core_include/lib9p/core.h b/lib9p/core_include/lib9p/core.h index afefa2b..1fb71f1 100644 --- a/lib9p/core_include/lib9p/core.h +++ b/lib9p/core_include/lib9p/core.h @@ -66,19 +66,19 @@ bool lib9p_ctx_has_error(struct lib9p_ctx *ctx); #endif /** Write a <libmisc/fmt.h>-style error into ctx, return -1. */ -#define lib9p_error(ctx, linux_errno, ...) ({ \ - if (!lib9p_ctx_has_error(ctx)) { \ - _lib9p_set_err_num(ctx, linux_errno); \ - struct fmt_buf _w = { \ - .dat = (ctx)->err_msg, \ - .cap = sizeof((ctx)->err_msg), \ - }; \ - lo_interface fmt_dest w = lo_box_fmt_buf_as_fmt_dest(&_w); \ - fmt_print(w, __VA_ARGS__); \ - if (_w.len < _w.cap) \ - memset(_w.dat + _w.len, 0, _w.cap - _w.len); \ - } \ - -1; \ +#define lib9p_error(ctx, linux_errno, ...) ({ \ + if (!lib9p_ctx_has_error(ctx)) { \ + _lib9p_set_err_num(ctx, linux_errno); \ + struct fmt_buf _w = { \ + .dat = (ctx)->err_msg, \ + .cap = sizeof((ctx)->err_msg), \ + }; \ + lo_interface fmt_dest w = LO_BOX(fmt_dest, &_w); \ + fmt_print(w, __VA_ARGS__); \ + if (_w.len < _w.cap) \ + memset(_w.dat + _w.len, 0, _w.cap - _w.len); \ + } \ + -1; \ }) /* misc utilities *************************************************************/ diff --git a/lib9p/tests/test_server/fs_flush.c b/lib9p/tests/test_server/fs_flush.c index e6408d7..fade0a1 100644 --- a/lib9p/tests/test_server/fs_flush.c +++ b/lib9p/tests/test_server/fs_flush.c @@ -67,7 +67,7 @@ static lo_interface lib9p_srv_fio flush_file_fopen(struct flush_file *self, stru struct flush_fio *ret = heap_alloc(1, struct flush_fio); ret->parent = self; - return lo_box_flush_fio_as_lib9p_srv_fio(ret); + return LO_BOX(lib9p_srv_fio, ret); } /* srv_fio ********************************************************************/ diff --git a/lib9p/tests/test_server/fs_flush.h b/lib9p/tests/test_server/fs_flush.h index 2b08850..023434b 100644 --- a/lib9p/tests/test_server/fs_flush.h +++ b/lib9p/tests/test_server/fs_flush.h @@ -22,6 +22,5 @@ struct flush_file { } flush_behavior; }; LO_IMPLEMENTATION_H(lib9p_srv_file, struct flush_file, flush_file); -#define lo_box_flush_file_as_lib9p_srv_file(obj) util9p_box(flush_file, obj) #endif /* _LIB9P_TESTS_TEST_SERVER_FS_FLUSH_H_ */ diff --git a/lib9p/tests/test_server/fs_shutdown.c b/lib9p/tests/test_server/fs_shutdown.c index d4ae67e..0dd473d 100644 --- a/lib9p/tests/test_server/fs_shutdown.c +++ b/lib9p/tests/test_server/fs_shutdown.c @@ -66,7 +66,7 @@ static lo_interface lib9p_srv_fio shutdown_file_fopen(struct shutdown_file *self struct shutdown_fio *ret = heap_alloc(1, struct shutdown_fio); ret->parent = self; - return lo_box_shutdown_fio_as_lib9p_srv_fio(ret); + return LO_BOX(lib9p_srv_fio, ret); } /* srv_fio ********************************************************************/ @@ -94,7 +94,7 @@ static uint32_t shutdown_fio_pwrite(struct shutdown_fio *self, struct lib9p_srv_ if (byte_count == 0) return 0; for (size_t i = 0; i < self->parent->nlisteners; i++) - LO_CALL(lo_box_hostnet_tcplist_as_net_stream_listener(&self->parent->listeners[i]), close); + LO_CALL(LO_BOX(net_stream_listener, &self->parent->listeners[i]), close); return byte_count; } static void shutdown_fio_pread(struct shutdown_fio *LM_UNUSED(self), struct lib9p_srv_ctx *LM_UNUSED(ctx), diff --git a/lib9p/tests/test_server/fs_shutdown.h b/lib9p/tests/test_server/fs_shutdown.h index 6b8683c..7b8d327 100644 --- a/lib9p/tests/test_server/fs_shutdown.h +++ b/lib9p/tests/test_server/fs_shutdown.h @@ -18,6 +18,5 @@ struct shutdown_file { size_t nlisteners; }; LO_IMPLEMENTATION_H(lib9p_srv_file, struct shutdown_file, shutdown_file); -#define lo_box_shutdown_file_as_lib9p_srv_file(obj) util9p_box(shutdown_file, obj) #endif /* _LIB9P_TESTS_TEST_SERVER_FS_SHUTDOWN_H_ */ diff --git a/lib9p/tests/test_server/fs_whoami.c b/lib9p/tests/test_server/fs_whoami.c index 7e1d635..6cc46ac 100644 --- a/lib9p/tests/test_server/fs_whoami.c +++ b/lib9p/tests/test_server/fs_whoami.c @@ -92,7 +92,7 @@ static lo_interface lib9p_srv_fio whoami_file_fopen(struct whoami_file *self, st ret->buf_len = 0; ret->buf = NULL; - return lo_box_whoami_fio_as_lib9p_srv_fio(ret); + return LO_BOX(lib9p_srv_fio, ret); } /* srv_fio ********************************************************************/ diff --git a/lib9p/tests/test_server/fs_whoami.h b/lib9p/tests/test_server/fs_whoami.h index 5e1aee9..518e11d 100644 --- a/lib9p/tests/test_server/fs_whoami.h +++ b/lib9p/tests/test_server/fs_whoami.h @@ -15,6 +15,5 @@ struct whoami_file { uint64_t pathnum; }; LO_IMPLEMENTATION_H(lib9p_srv_file, struct whoami_file, whoami_file); -#define lo_box_whoami_file_as_lib9p_srv_file(obj) util9p_box(whoami_file, obj) #endif /* _LIB9P_TESTS_TEST_SERVER_FS_WHOAMI_H_ */ diff --git a/lib9p/tests/test_server/main.c b/lib9p/tests/test_server/main.c index f2a73bf..052d180 100644 --- a/lib9p/tests/test_server/main.c +++ b/lib9p/tests/test_server/main.c @@ -60,11 +60,11 @@ static struct { #define STATIC_DIR(N, STRNAME, ...) \ UTIL9P_STATIC_DIR(N, STRNAME, __VA_ARGS__) -#define API_FILE(N, STRNAME, SYMNAME, ...) \ - lo_box_##SYMNAME##_file_as_lib9p_srv_file(&((struct SYMNAME##_file){ \ - .name = STRNAME, \ - .pathnum = N \ - __VA_OPT__(,) __VA_ARGS__ \ +#define API_FILE(N, STRNAME, SYMNAME, ...) \ + LO_BOX(lib9p_srv_file, &((struct SYMNAME##_file){ \ + .name = STRNAME, \ + .pathnum = N \ + __VA_OPT__(,) __VA_ARGS__ \ })) static struct lib9p_srv_file root = @@ -96,7 +96,7 @@ static COROUTINE read_cr(void *_i) { hostnet_tcp_listener_init(&globals.listeners[i], globals.port); - lib9p_srv_accept_and_read_loop(&globals.srv, lo_box_hostnet_tcplist_as_net_stream_listener(&globals.listeners[i])); + lib9p_srv_accept_and_read_loop(&globals.srv, LO_BOX(net_stream_listener, &globals.listeners[i])); cr_end(); } @@ -164,7 +164,7 @@ int main(int argc, char *argv[]) { struct hostclock clock_monotonic = { .clock_id = CLOCK_MONOTONIC, }; - bootclock = lo_box_hostclock_as_alarmclock(&clock_monotonic); + bootclock = LO_BOX(alarmclock, &clock_monotonic); coroutine_add("init", init_cr, NULL); coroutine_main(); fclose(globals.logstream); diff --git a/lib9p_util/include/util9p/static.h b/lib9p_util/include/util9p/static.h index 5454c24..c204607 100644 --- a/lib9p_util/include/util9p/static.h +++ b/lib9p_util/include/util9p/static.h @@ -9,12 +9,6 @@ #include <lib9p/srv.h> -#define util9p_box(nam, obj) \ - ((struct lib9p_srv_file){ \ - .self = obj, \ - .vtable = (void*)&_lo_##nam##_lib9p_srv_file_vtable, \ - }) - #define UTIL9P_ATIME 1728337905 #define UTIL9P_MTIME 1728337904 @@ -56,12 +50,11 @@ struct util9p_static_dir { lo_interface lib9p_srv_file members[]; }; LO_IMPLEMENTATION_H(lib9p_srv_file, struct util9p_static_dir, util9p_static_dir); -#define lo_box_util9p_static_dir_as_lib9p_srv_file(obj) util9p_box(util9p_static_dir, obj) -#define UTIL9P_STATIC_DIR(PATH, STRNAME, ...) \ - lo_box_util9p_static_dir_as_lib9p_srv_file(&((struct util9p_static_dir){ \ - .c = UTIL9P_STATIC_COMMON(PATH, STRNAME, 0555), \ - .members = { __VA_ARGS__ LO_NULL(lib9p_srv_file) }, \ +#define UTIL9P_STATIC_DIR(PATH, STRNAME, ...) \ + LO_BOX(lib9p_srv_file, &((struct util9p_static_dir){ \ + .c = UTIL9P_STATIC_COMMON(PATH, STRNAME, 0555), \ + .members = { __VA_ARGS__ LO_NULL(lib9p_srv_file) }, \ })) /* File ***********************************************************************/ @@ -74,12 +67,11 @@ struct util9p_static_file { size_t data_size; /* only used if .data_end==NULL */ }; LO_IMPLEMENTATION_H(lib9p_srv_file, struct util9p_static_file, util9p_static_file); -#define lo_box_util9p_static_file_as_lib9p_srv_file(obj) util9p_box(util9p_static_file, obj) -#define UTIL9P_STATIC_FILE(PATH, STRNAME, ...) \ - lo_box_util9p_static_file_as_lib9p_srv_file(&((struct util9p_static_file){ \ - .c = UTIL9P_STATIC_COMMON(PATH, STRNAME, 0444), \ - __VA_ARGS__ \ +#define UTIL9P_STATIC_FILE(PATH, STRNAME, ...) \ + LO_BOX(lib9p_srv_file, &((struct util9p_static_file){ \ + .c = UTIL9P_STATIC_COMMON(PATH, STRNAME, 0444), \ + __VA_ARGS__ \ })) #endif /* _UTIL9P_STATIC_H_ */ diff --git a/lib9p_util/static.c b/lib9p_util/static.c index c35d28c..40810b5 100644 --- a/lib9p_util/static.c +++ b/lib9p_util/static.c @@ -100,7 +100,7 @@ LIB9P_SRV_NOTFILE(struct util9p_static_dir, util9p_static_dir); static lo_interface lib9p_srv_dio util9p_static_dir_dopen(struct util9p_static_dir *self, struct lib9p_srv_ctx *ctx) { assert(self); assert(ctx); - return lo_box_util9p_static_dir_as_lib9p_srv_dio(self); + return LO_BOX(lib9p_srv_dio, self); } static void util9p_static_dir_iofree(struct util9p_static_dir *self) { assert(self); @@ -191,7 +191,7 @@ static lo_interface lib9p_srv_fio util9p_static_file_fopen(struct util9p_static_ assert(rd); assert(!wr); assert(!trunc); - return lo_box_util9p_static_file_as_lib9p_srv_fio(self); + return LO_BOX(lib9p_srv_fio, self); } static void util9p_static_file_iofree(struct util9p_static_file *self) { assert(self); diff --git a/libdhcp/tests/test_client.c b/libdhcp/tests/test_client.c index 24b3af6..09557e5 100644 --- a/libdhcp/tests/test_client.c +++ b/libdhcp/tests/test_client.c @@ -99,7 +99,7 @@ static lo_interface net_packet_conn test_udp_conn(struct test_iface *self, uint1 test_assert(local_port == 68); test_assert(!once); once = true; - return lo_box_test_udp_as_net_packet_conn(&self->conn); + return LO_BOX(net_packet_conn, &self->conn); } /******************************************************************************/ @@ -107,7 +107,7 @@ static lo_interface net_packet_conn test_udp_conn(struct test_iface *self, uint1 COROUTINE dhcp_cr(void *) { cr_begin(); struct test_iface iface = {}; - dhcp_client_main(lo_box_test_as_net_iface(&iface), "test-client"); + dhcp_client_main(LO_BOX(net_iface, &iface), "test-client"); cr_end(); } @@ -115,7 +115,7 @@ int main() { struct hostclock clock_monotonic = { .clock_id = CLOCK_MONOTONIC, }; - bootclock = lo_box_hostclock_as_alarmclock(&clock_monotonic); + bootclock = LO_BOX(alarmclock, &clock_monotonic); coroutine_add("dhcp", dhcp_cr, NULL); coroutine_main(); diff --git a/libhw_cr/host_net.c b/libhw_cr/host_net.c index 8016787..c8d4472 100644 --- a/libhw_cr/host_net.c +++ b/libhw_cr/host_net.c @@ -187,7 +187,7 @@ static lo_interface net_stream_conn hostnet_tcplist_accept(struct hostnet_tcp_li listener->active_conn.fd = ret_connfd; listener->active_conn.read_deadline_ns = 0; - return lo_box_hostnet_tcp_as_net_stream_conn(&listener->active_conn); + return LO_BOX(net_stream_conn, &listener->active_conn); } /* TCP listener close() *******************************************************/ diff --git a/libhw_cr/rp2040_hwtimer.c b/libhw_cr/rp2040_hwtimer.c index 8227abb..d9f0a24 100644 --- a/libhw_cr/rp2040_hwtimer.c +++ b/libhw_cr/rp2040_hwtimer.c @@ -44,7 +44,7 @@ static_assert(sizeof(hwtimers)/sizeof(hwtimers[0]) == _RP2040_HWALARM_NUM); lo_interface alarmclock rp2040_hwtimer(enum rp2040_hwalarm_instance alarm_num) { assert(alarm_num < _RP2040_HWALARM_NUM); - return lo_box_rp2040_hwtimer_as_alarmclock(&hwtimers[alarm_num]); + return LO_BOX(alarmclock, &hwtimers[alarm_num]); } diff --git a/libhw_cr/w5500.c b/libhw_cr/w5500.c index 76b8dec..58715c9 100644 --- a/libhw_cr/w5500.c +++ b/libhw_cr/w5500.c @@ -459,7 +459,7 @@ static lo_interface net_stream_listener w5500_if_tcp_listen(struct w5500 *chip, sock->read_deadline_ns = 0; sock->list_open = true; - return lo_box_w5500_tcplist_as_net_stream_listener(sock); + return LO_BOX(net_stream_listener, sock); } static lo_interface net_stream_conn w5500_if_tcp_dial(struct w5500 *chip, @@ -509,7 +509,7 @@ static lo_interface net_stream_conn w5500_if_tcp_dial(struct w5500 *chip, cr_yield(); break; case STATE_TCP_ESTABLISHED: - return lo_box_w5500_tcp_as_net_stream_conn(socket); + return LO_BOX(net_stream_conn, socket); default: goto restart; } @@ -545,7 +545,7 @@ static lo_interface net_packet_conn w5500_if_udp_conn(struct w5500 *chip, uint16 cr_yield(); cr_mutex_unlock(&chip->mu); - return lo_box_w5500_udp_as_net_packet_conn(socket); + return LO_BOX(net_packet_conn, socket); } static bool w5500_if_arp_ping(struct w5500 *chip, struct net_ip4_addr addr) { @@ -601,7 +601,7 @@ static lo_interface net_stream_conn w5500_tcplist_accept(struct _w5500_socket *s [[fallthrough]]; case STATE_TCP_CLOSE_WAIT: socket->write_open = true; - return lo_box_w5500_tcp_as_net_stream_conn(socket); + return LO_BOX(net_stream_conn, socket); default: goto restart; } diff --git a/libmisc/include/libmisc/fmt.h b/libmisc/include/libmisc/fmt.h index 6c04d99..135e48b 100644 --- a/libmisc/include/libmisc/fmt.h +++ b/libmisc/include/libmisc/fmt.h @@ -113,27 +113,27 @@ struct fmt_buf { }; LO_IMPLEMENTATION_H(fmt_dest, struct fmt_buf, fmt_buf); -#define fmt_snprint(buf, n, ...) ({ \ - struct fmt_buf _w = { .dat = buf, .cap = n }; \ - lo_interface fmt_dest w = lo_box_fmt_buf_as_fmt_dest(&_w); \ - fmt_print(w, __VA_ARGS__); \ - if (_w.len < _w.cap) \ - ((char *)_w.dat)[_w.len] = '\0'; \ - _w.len; \ +#define fmt_snprint(buf, n, ...) ({ \ + struct fmt_buf _w = { .dat = buf, .cap = n }; \ + lo_interface fmt_dest w = LO_BOX(fmt_dest, &_w); \ + fmt_print(w, __VA_ARGS__); \ + if (_w.len < _w.cap) \ + ((char *)_w.dat)[_w.len] = '\0'; \ + _w.len; \ }) -#define fmt_asprint(...) ({ \ - struct fmt_buf _w = {}; \ - lo_interface fmt_dest w = lo_box_fmt_buf_as_fmt_dest(&_w); \ - fmt_print(w, __VA_ARGS__); \ - while (_w.cap <= _w.len) { \ - _w.cap = _w.len + 1; \ - _w.len = 0; \ - _w.dat = realloc(_w.dat, _w.cap); \ - fmt_print(w, __VA_ARGS__); \ - } \ - ((char *)_w.dat)[_w.len] = '\0'; \ - _w.dat; \ +#define fmt_asprint(...) ({ \ + struct fmt_buf _w = {}; \ + lo_interface fmt_dest w = LO_BOX(fmt_dest, &_w); \ + fmt_print(w, __VA_ARGS__); \ + while (_w.cap <= _w.len) { \ + _w.cap = _w.len + 1; \ + _w.len = 0; \ + _w.dat = realloc(_w.dat, _w.cap); \ + fmt_print(w, __VA_ARGS__); \ + } \ + ((char *)_w.dat)[_w.len] = '\0'; \ + _w.dat; \ }) /* justify ********************************************************************/ @@ -145,13 +145,13 @@ LO_IMPLEMENTATION_H(fmt_dest, struct fmt_buf, fmt_buf); fmt_print_byte(w, fillchar); \ } while (0) -#define fmt_print_rjust(w, width, fillchar, ...) do { \ - struct fmt_buf _discard = {}; \ - lo_interface fmt_dest discard = lo_box_fmt_buf_as_fmt_dest(&_discard); \ - fmt_print2(discard, __VA_ARGS__); \ - while (_discard.len++ < width) \ - fmt_print_byte(w, fillchar); \ - fmt_print2(w, __VA_ARGS__); \ +#define fmt_print_rjust(w, width, fillchar, ...) do { \ + struct fmt_buf _discard = {}; \ + lo_interface fmt_dest discard = LO_BOX2(fmt_dest, &_discard); \ + fmt_print2(discard, __VA_ARGS__); \ + while (_discard.len++ < width) \ + fmt_print_byte(w, fillchar); \ + fmt_print2(w, __VA_ARGS__); \ } while (0) void fmt_print_base16_u8_(lo_interface fmt_dest w, uint8_t x); diff --git a/libmisc/include/libmisc/obj.h b/libmisc/include/libmisc/obj.h index 48695af..6afa391 100644 --- a/libmisc/include/libmisc/obj.h +++ b/libmisc/include/libmisc/obj.h @@ -30,10 +30,6 @@ * * Use `lo_interface {iface_name}` as the type of this interface; it * should not be a pointer type. - * - * If there are any LO_NEST interfaces, this will define a - * `lo_box_{iface_name}_as_{wrapped_iface_name}(obj)` function for - * each. */ #define LO_NEST(_ARG_child_iface_name) \ (lo_nest, _ARG_child_iface_name) @@ -52,26 +48,46 @@ _LO_IFACE_PROTO, _ARG_iface_name) \ LM_FORCE_SEMICOLON -#define _LO_IFACE_VTABLE(_tuple_typ, ...) _LO_IFACE_VTABLE_##_tuple_typ(__VA_ARGS__) -#define _LO_IFACE_VTABLE_lo_nest(_ARG_child_iface_name) const struct _lo_##_ARG_child_iface_name##_vtable *_lo_##_ARG_child_iface_name##_vtable; LM_FOREACH_TUPLE2(_ARG_child_iface_name##_LO_IFACE, _LO_IFACE_VTABLE2) -#define _LO_IFACE_VTABLE_lo_func(_ARG_ret_type, _ARG_func_name, ...) _ARG_ret_type (*_ARG_func_name)(void * __VA_OPT__(,) __VA_ARGS__); +#define _LO_IFACE_VTABLE(_tuple_typ, ...) _LO_IFACE_VTABLE_##_tuple_typ(__VA_ARGS__) +#define _LO_IFACE_VTABLE_lo_nest(_ARG_child_iface_name) \ + const struct _lo_##_ARG_child_iface_name##_vtable *_lo_##_ARG_child_iface_name##_vtable; \ + LM_FOREACH_TUPLE2(_ARG_child_iface_name##_LO_IFACE, _LO_IFACE_VTABLE2) +#define _LO_IFACE_VTABLE_lo_func(_ARG_ret_type, _ARG_func_name, ...) \ + _ARG_ret_type (*_ARG_func_name)(void * __VA_OPT__(,) __VA_ARGS__); #define _LO_IFACE_VTABLE_indirect() _LO_IFACE_VTABLE #define _LO_IFACE_VTABLE2(...) _LM_DEFER2(_LO_IFACE_VTABLE_indirect)()(__VA_ARGS__) -#define _LO_IFACE_PROTO(_ARG_iface_name, _tuple_typ, ...) _LO_IFACE_PROTO_##_tuple_typ(_ARG_iface_name, __VA_ARGS__) -#define _LO_IFACE_PROTO_lo_nest(_ARG_iface_name, _ARG_child_iface_name) \ - LM_ALWAYS_INLINE static lo_interface _ARG_child_iface_name \ - box_##_ARG_iface_name##_as_##_ARG_child_iface_name(lo_interface _ARG_iface_name obj) { \ - return (lo_interface _ARG_child_iface_name){ \ - .self = obj.self, \ - .vtable = obj.vtable->_lo_##_ARG_child_iface_name##_vtable, \ - }; \ - } +#define _LO_IFACE_PROTO(_ARG_iface_name, _tuple_typ, ...) _LO_IFACE_PROTO_##_tuple_typ(_ARG_iface_name, __VA_ARGS__) +#define _LO_IFACE_PROTO_lo_nest(_ARG_iface_name, _ARG_child_iface_name) \ + LM_DEFAPPEND(_LO_REGISTRY_##_ARG_child_iface_name, \ + (lo_interface _ARG_iface_name, _LO_BOX_##_ARG_iface_name##_as_##_ARG_child_iface_name)); \ + LM_DEFAPPEND(_LO_BOX_##_ARG_iface_name##_as_##_ARG_child_iface_name(obj), \ + { .self = obj.self, .vtable = obj.vtable->_lo_##_ARG_child_iface_name##_vtable }); #define _LO_IFACE_PROTO_lo_func(_ARG_iface_name, _ARG_ret_type, _ARG_func_name, ...) \ /* empty */ /** + * `LO_BOX(iface_name, obj)` boxes `obj` as a `lo_interface + * iface_name`. `obj` must be one of: + * + * - A pointer to a value that implements `lo_interface iface_name` + * - An already-boxed instance of `lo_interface iface_name` + * - An already-boxed instance of another interface that + * `iface_name` inherits from. + */ +#define LO_BOX(_ARG_iface_name, obj) _Generic((obj), \ + lo_interface _ARG_iface_name: obj \ + LM_FOREACH_TUPLE(_LO_REGISTRY_##_ARG_iface_name, \ + _LO_BOX, _ARG_iface_name, obj)) +#define LO_BOX2(_ARG_iface_name, obj) _Generic((obj), \ + lo_interface _ARG_iface_name: obj \ + LM_FOREACH_TUPLE2(_LO_REGISTRY_##_ARG_iface_name, \ + _LO_BOX, _ARG_iface_name, obj)) +#define _LO_BOX(_ARG_iface_name, obj, typ, boxfn) \ + , typ: (lo_interface _ARG_iface_name)boxfn(obj) + +/** * `LO_NULL(iface_name)` is the null/nil/zero value for `lo_interface {iface_name}`. */ #define LO_NULL(_ARG_iface_name) ((lo_interface _ARG_iface_name){}) @@ -99,23 +115,17 @@ * file to declare that `{impl_type}` implements the `{iface_name}` * interface with functions named `{impl_name}_{method_name}`. * - * This will also define a `lo_box_{impl_name}_as_{iface_name}(obj)` - * function. - * * You must also call the LO_IMPLEMENTATION_C in a single .c file. */ -#define LO_IMPLEMENTATION_H(_ARG_iface_name, _ARG_impl_type, _ARG_impl_name) \ - /* Vtable. */ \ - extern const struct _lo_##_ARG_iface_name##_vtable \ - _lo_##_ARG_impl_name##_##_ARG_iface_name##_vtable; \ - /* Boxing. */ \ - LM_ALWAYS_INLINE static lo_interface _ARG_iface_name \ - lo_box_##_ARG_impl_name##_as_##_ARG_iface_name(_ARG_impl_type *self) { \ - return (lo_interface _ARG_iface_name){ \ - .self = self, \ - .vtable = &_lo_##_ARG_impl_name##_##_ARG_iface_name##_vtable, \ - }; \ - } \ +#define LO_IMPLEMENTATION_H(_ARG_iface_name, _ARG_impl_type, _ARG_impl_name) \ + /* Vtable. */ \ + extern const struct _lo_##_ARG_iface_name##_vtable \ + _lo_##_ARG_impl_name##_##_ARG_iface_name##_vtable; \ + /* Boxing. */ \ + LM_DEFAPPEND(_LO_REGISTRY_##_ARG_iface_name, \ + (_ARG_impl_type *, _LO_BOX_##_ARG_impl_name##_as_##_ARG_iface_name)); \ + LM_DEFAPPEND(_LO_BOX_##_ARG_impl_name##_as_##_ARG_iface_name(obj), \ + { .self = obj, .vtable = &_lo_##_ARG_impl_name##_##_ARG_iface_name##_vtable }); \ LM_FORCE_SEMICOLON /** diff --git a/libmisc/tests/test_obj.c b/libmisc/tests/test_obj.c index 687ad4e..a13b8c9 100644 --- a/libmisc/tests/test_obj.c +++ b/libmisc/tests/test_obj.c @@ -53,7 +53,7 @@ int main() { struct myclass obj = { .a = MAGIC1, }; - lo_interface frobber iface = lo_box_myclass_as_frobber(&obj); + lo_interface frobber iface = LO_BOX(frobber, &obj); test_assert(LO_CALL(iface, frob) == MAGIC1); test_assert(LO_CALL(iface, frob1, MAGIC2) == MAGIC2); LO_CALL(iface, frob0); diff --git a/libmisc/tests/test_obj_nest.c b/libmisc/tests/test_obj_nest.c index d5e563e..ba5ac37 100644 --- a/libmisc/tests/test_obj_nest.c +++ b/libmisc/tests/test_obj_nest.c @@ -64,7 +64,7 @@ static ssize_t myclass_write(struct myclass *self, void *buf, size_t count) { int main() { struct myclass _obj = {}; - lo_interface read_writer obj = lo_box_myclass_as_read_writer(&_obj); + lo_interface read_writer obj = LO_BOX(read_writer, &_obj); test_assert(LO_CALL(obj, write, "Hello", 6) == 6); char buf[6] = {}; test_assert(LO_CALL(obj, read, buf, 3) == 3); |