CXXR (C++ R)
CXXR::SchwarzCounter< T > Class Template Reference

Schwarz counter. More...

#include <SchwarzCounter.hpp>

List of all members.


Detailed Description

template<class T>
class CXXR::SchwarzCounter< T >

Schwarz counter.

The Schwarz counter (see for example Stephen C. Dewhurst's book 'C++ Gotchas') is a programming idiom to ensure that a class T (including particularly its static members) is initialized before any client of the class requires to use it, and that on program exit the class's static resources are not cleaned up prematurely (e.g. while the class is still in use by another class's static members). Devices such as this are necessitated by the fact that the standard does not prescribe the order in which objects of file and global scope in different compilation units are initialized: it specifies only that the order of destruction must be the reverse of the order of initialization.

The necessary control is achieved by the unusual stratagem of including the definition of a lightweight data item within the header file of class T. This data item is of type SchwarzCounter<T>, and is declared within an anonymous namespace. Each file that #includes this header file will therefore include a definition of a SchwarzCounter object, and this definition will precede any data definitions within the enclosing file that depend on class T. Consequently, the SchwarzCounter object will be constructed before any data objects of the client file. The constructor of SchwarzCounter is so defined that when the first such object is created, the class MemoryBank will itself be initialized.

Conversely, when the program exits, data items within each client file will have their destructors invoked before the file's SchwarzCounter object has its destructor invoked. This SchwarzCounter destructor is so defined that only when the last SchwarzCounter object is destroyed is class T itself cleaned up.

Template Parameters:
Tclass whose initialization and clean-up are to be controlled by the Schwarz counter. This class must defined static methods initialize() and cleanup(), capable of being invoked with no parameters.

The documentation for this class was generated from the following file: