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.
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, which also ensure that all members that are not explicitly initialized are set to zero.
Details and Examples¶
The following explanations and examples are from the xine docs but are slightly adapted to better match our code style (refer to our actual code for concrete examples).
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.