patternsvtable
Vtable Pattern (C-Specific) pattern for C development
When & Why to Use This Skill
This Claude skill facilitates the implementation of the Vtable pattern in C programming, enabling object-oriented features like runtime polymorphism and interface-based design. It provides a structured approach to using function pointers, allowing developers to build modular, extensible, and testable C applications by decoupling interfaces from their specific implementations. Ideal for systems programming and embedded development, it helps maintain clean code architecture in non-OOP environments.
Use Cases
- Hardware Abstraction Layers (HAL): Define a unified interface for various hardware peripherals, allowing the core application to interact with different drivers (e.g., I2C, SPI, UART) through a consistent set of function pointers.
- Pluggable Provider Integration: Implement a flexible system for switching between different service providers, such as LLM APIs or database backends, at runtime without modifying the calling logic.
- Unit Testing and Mocking: Simplify the creation of mock implementations for complex subsystems, enabling developers to isolate components and perform rigorous testing by swapping real vtables with test-specific ones.
- Middleware and Layered Systems: Design extensible software layers where each layer (e.g., a rendering engine or network stack) can provide its own specific implementation of a standard interface.
| name | patterns/vtable |
|---|---|
| description | Vtable Pattern (C-Specific) pattern for C development |
Vtable Pattern (C-Specific)
Struct of function pointers enabling polymorphism. Different implementations provide different function pointer sets. Callers invoke through vtable.
ikigai Application
LLM providers:
typedef struct {
res_t (*send)(void *impl, ...);
res_t (*stream)(void *impl, ...);
void (*cleanup)(void *impl);
} ik_provider_vtable_t;
typedef struct {
ik_provider_vtable_t *vt;
void *impl; // OpenAI ctx, Anthropic ctx, etc.
} ik_llm_client_t;
Layer system: Each layer type provides render/resize functions.
Benefits:
- Runtime polymorphism without inheritance
- Swap implementations transparently
- Mock implementations for testing
Convention: Vtable struct named *_vtable_t, instance holds vt pointer plus impl context.