resources

Clone Factory

A factory creates objects of a certain type. The factory takes an ID of some sort as an argument and creates the object which has been registered with the factory with the respective ID. This code sample shows one way to make a factory. All objects are stored in an interal (intrusive) list inside the factory together with their ID. To create an object given its ID, the factory searches the list for the ID and calls the copy constructor of the object to create a new object of the same type.

Memory Pool

The memory pool is really fast in allocating and deallocating memory (O(1)). For real-time systems this is the right choice. It works by preallocating a pool of memory blocks. These memory blocks must all have the same size. So this is the downside to live with: This memory pool will only provide memory blocks of a certain size. If you need another size: Simply make another memory pool with that other size.

Signal ID to function call in state machines

Mapping Signal IDs to function calls The state machine pattern assumes, that function calls are made by each incoming signal. But what if the signals exist only as a number or ID? How is the ID transformed into a function call? Of course, you could simply write a switch case, converting the integer ID into a function call. But switch cases “smell” (look into Martin Fowlers book on Refactoring). This example uses the function process(Signal s) (line 61, lines 76 and 77) to process the signal which is given as an ID.

Thread Control

How to stop a thread. It is a nice idea to create a functor (overload operator()) and pass a functor object to a thread. The operator()() (line 9) should generally contain a while loop (event loop)(line 10). To be able to stop the while loop from the outside, one can use a member variable of the functor, e.g. bool IsStopped or bool IsRunning (line 15). The loop would then look like this