Object Oriented C programming style » History » Version 6

Martin Willi, 08.02.2011 14:51
INIT/METHOD macros are in use now

1 4 Andreas Steffen
h1. Object Oriented C programming style
2 4 Andreas Steffen
3 1 Martin Willi
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.
4 1 Martin Willi
5 4 Andreas Steffen
6 4 Andreas Steffen
h2. Concept
7 4 Andreas Steffen
8 4 Andreas Steffen
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":
9 1 Martin Willi
To get an idea of the whole concept we recommend to examine the code directly.
10 1 Martin Willi
11 4 Andreas Steffen
12 6 Martin Willi
h2. Type safety
13 4 Andreas Steffen
14 6 Martin Willi
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.
15 4 Andreas Steffen
16 1 Martin Willi
h2. Gedit Snippets
17 4 Andreas Steffen
18 5 Tobias Brunner
Being a gedit user, I created some snippets (attachment:gedit-snippets-strongswan.tar.gz) to create interfaces, methods (including implementations and pointer assignement) and class implementations.