id-objmodel — simple dynamic dispatch for C

id-objmodel is a tiny prototype-based object model that adds messaging (dynamic dispatch) to C data structures.

Like C++, it uses the type of an object and the name of a method to select (and invoke) a function pointer from a virtual table. Unlike C++, the virtual tables are first-class and dynamic. Along with new object types, virtual tables can be created, populated, inspected and modified freely at runtime, and method names can be constructed at run time from normal C strings. This can all be surprisingly efficient compared to, for example, a switch statement. (It scales much more nicely too.)

Download the source code: id-objmodel-1.1.tar.gz
Browse the source code: id-objmodel-1.1
Read a paper about the object model: objmodel2.pdf
See the example program prim-obj.c for bite-sized serving suggestions. To see the object model used in anger, as the basis for a large programming system implementation, have a look at the COLA languages.

If you want a fun weekend project, use peg (or leg) to write a source-to-source 'preprocessor' for C programs that converts concise syntax for declaring, creating and sending messages to objects into the C code needed to implement those features using the id object model.

id-objmodel is distributed under the MIT license. It will not infect you with a contagious disease if you decide to incorporate it into your own programs.

If you find bugs or have suggestions, please send them to Ian at the domain name of this web site. Thanks!