Fracanto Vtable Pattern

Fracanto Vtable Pattern #1 — Structure

Structure — three building blocks of the vtable pattern

The pattern consists of three building blocks that together form a polymorphic abstraction in pure C11.

The operations table is a struct of function pointers that defines the interface. In the hal.h interface, fracanto_hal_ops_t contains eight operations: init, deinit, start, stop, send, set_filter, set_rx_callback, and get_tick_ms. Each operation receives an opaque void *ctx pointer as its first parameter.

The instance consists of a struct with two fields: a pointer to the operations table (const ops_t *ops) and the opaque context pointer (void *ctx). The context carries platform-specific data — for the SocketCAN HAL a socket file descriptor, interface name, and receive callback among other state, for the STM32 FDCAN HAL an FDCAN handle along with state information and callback registration.

Inline wrappers route every function call through the vtable, checking for NULL in the process. Calling fracanto_hal_send(hal, &frame) requires no knowledge of whether SocketCAN or STM32 FDCAN lies behind it — the vtable dispatches the call to the correct implementation.