Since templates are something kind of mystical to several programmers but I make extensive use of them in this software I want to give a very short introduction to them - telling just enough so you have a sufficient idea of them for doing basic work with the simulator (like creating your own controller). It's not that hard actually...
A template describes a pice of source code that has a type as an argument - same as a function has a variable as argument. A possible example is a linked list of integers: You need a struct RSIntLinkedList
holding an integer value TInt nValue
and a pointer to the next entry RSIntLinkedList* pNext
. Of course you will write some functions to manipulate that list and maybe even write it as a class with advanced functions like traversing the list.
A little later you might need a linked list of another data type. Copy-Paste-Adapt would be one way - another would be to write the code as a template where the data type of the member Value
is the template argument. The old struct RSIntLinkedList
would than become TRSLinkedList<TSInt>
. Note that you should not write a class like that on your own - except for training purpose maybe - because there exists an STL (standard template library) that provides already tons of containers.
When programming templates you need to keep in mind that no assembler code is generated from your template code until it is actually instanciated - resulting in the compiler not finding several errors until he tries to compile that code for a specific type. This also implies that the implementation of templates must not be in the cpp-file but in the hpp- file (or in an inl-file included from the hpp-file as I do it) because it must be known by the compiler when processing another cpp-file using that template. Sometimes it's not even possible to spit the implementation from the declaration so you have to implement it implicite inlined (directly into the declaration)...
One concept I often use in simbob - didn't see it anywhere else yet - is to provide a template base class expecting the type of the derived class as template argument. This is the case for all components. It allows for instance the base class to call the constructor of the derived class and, thus, to implement more of the commonly required functions simplifying to implement you own components.
When writing your own component you will most likely have to use templates as parameters as well. Ony commonly used is TRCRtlComponentPointer
where you just need to specify the type of the interface-type you want to access. Behind the macros used to declare member parameters also several templates are used but this is of no interest for a pure user - that's why I've put it behind macros...