Object Oriented C programming style » History » Version 7

« Previous - Version 7/8 (diff) - Next » - Current version
Graham Hudspith, 23.08.2011 17:26
Correct xine url

Object Oriented C programming style

For new code in strongSwan, we use an object oriented programming style. This allows us to employ modern programming paradigms, but still use the standard C compiler and toolset.


This object oriented design is achieved by heavy use of function pointers. Interfaces of classes are defined as a struct with function pointers, and an implementation extends the struct by including the Interface as its first struct member. The idea is based on the coding style of the xine project.
To get an idea of the whole concept we recommend to examine the code directly.

Type safety

To achieve type safety without casting the this parameter in each method, we have introduced some special helper macros. The METHOD macro helps in implementing a method that is compatible to both the public and the private interfaces automatically. The INIT macro initializes objects using readable Designated Initializers. Not all code has been migrated to the new macros yet, but new code should use them.

Gedit Snippets

Being a gedit user, I created some snippets (gedit-snippets-strongswan.tar.gz) to create interfaces, methods (including implementations and pointer assignement) and class implementations.