To reduce the amount of file inclusion in Sofa and thus compilation time it is possible to use forward declaration. A forward declaration is a an incomplete type ta complete definition is not provided.
Forward declaration can be used in place of the complete type (declaration+definition) when the “inner” details of the type are not needed. A classical scenario is the following
As we are manipulating the ‘a’ object via a pointer and passing it to the doSomethingWith function there is no need to know any details of the BaseObject to compile that properly. Using a forward declaration for BaseObject would save us from the inclusion of the file
Where to put forward declaration
Forward declaration must be in a file called
fwd.h. The fwd.h can be located at the module root directory. Eg:
If there is a lot of forward declaration it is allowed to have a per sub-module
fwd.h file. Eg:
In that case it is mandatory that the module file contains them all. In our example
When it is not desirable to have access the full type definition it is possible to make or use what is called an opaque API. The Opaque API mimmics the methods provided by a class but relying only on forward declaration. Exemple of the “transparent” API:
Exemple of the corresponding “opaque” API:
Opaque API for a given type can be located at the same location where the type is foward declared or if very long in their own dedicated file close to the one where the type definition is.
sofa/simulation/node.h sofa/simulation/node.cpp sofa/simulation/node-fwd.h
If the second solution is chosen, the `
node-fwd.h file must be included by the per-module fwd.h.
Last modified: 20 May 2021