4 ms·
This is an excellent pattern in C. The Dovecot mail server has many fine examples of the style as well e.g. struct dict dict_driver_ldap = { .name
by inopinatus 2y ago
This is an excellent pattern in C. The Dovecot mail server has many fine examples of the style as well e.g.
struct dict dict_driver_ldap = {
.name = "ldap",
.v = {
.init = ldap_dict_init,
.deinit = ldap_dict_deinit,
.wait = ldap_dict_wait,
.lookup = ldap_dict_lookup,
.lookup_async = ldap_dict_lookup_async,
.switch_ioloop = ldap_dict_switch_ioloop,
}
};
defines the virtual function table for the LDAP module, and any other subsystem that looks things up via the abstract dict interface can consequently be configured to use the ldap service without concrete knowledge of it.
(those interested in a deeper dive might start at https://github.com/dovecot/core/blob/main/src/lib-dict/dict-private.h https://github.com/dovecot/core/blob/main/src/lib-dict/dict-...)
- dividuum 2y agoSo does the good old Quake 2 rendering API. The game exported a bunch of functions to the renderer via refimport_t and the renderer in return provided functions via refexport_t. The only visible symbol in a rendering DLL is GetRefAPI_t: https://github.com/id-Software/Quake-2/blob/master/client/ref.h#L224 https://github.com/id-Software/Quake-2/blob/master/client/re... I remember being impressed by this approach, so I shamelessly copied it for my programming game: https://github.com/dividuum/infon/blob/master/renderer.h https://github.com/dividuum/infon/blob/master/renderer.h :)
- dfox 2y agoI somehow suspect that the reason why Quake2 does this lies in the legacy of Quake1 written in DJGPP. DJGPP supports dynamicaly loaded libraries (although the API is technically unsupported and internal-only), but does not have any kind of dynamic linker, thus passing around pair of such structs during library initialization is the only way to make that work.
- jamesfinlayson 2y agoPretty sure Half-Life does something pretty similar - all functionality between the game and engine is done via function pointer structs.
- MisterTea 2y agoThe original Half-Life engine, now called Goldsrc, is a heavily modified Quake engine with bits of Quake 2 in there.
- immibis 2y agoThis is cargo cult. You can just directly export and import the rendering functions.
- jrmg 2y agoReminds me of Apple’s CoreFoundation.