More C++ Idioms/Nifty Counter

Nifty CounterEdit

IntentEdit

Ensure a non-local static object is initialized before its first use and destroyed only after last use of the object.

Also Known AsEdit

Schwarz Counter

MotivationEdit

When static objects use other static objects, the initialization problem becomes more complex. A static object must be initialized before its use if it has non-trivial initialization. Initialization order of static objects across compilation units is not well-defined. Multiple static objects, spread across multiple compilation units, might be using a single static object. Therefore, it must be initialized before use. One example is std::cout, which is typically used by a number of other static objects.

Solution and Sample CodeEdit

The "nifty counter" or "Schwarz counter" idiom is an example of a reference counting idiom applied to the initialization of static objects.

//Stream.h
class StreamInitializer;
 
class Stream {
   friend class StreamInitializer;
 public:
   Stream () {
   // Constructor must be called before use.
   }
};
static class StreamInitializer {
  public:
    StreamInitializer ();
    ~StreamInitializer ();
} initializer; //Note object here in the header.
//Stream.cpp
static int nifty_counter; 
// The counter is initialized at load-time, i.e., before any of the static objects are initialized.
StreamInitializer::StreamInitializer ()
{
  if (0 == nifty_counter++)
  {
    // Initialize Stream object's static members.
  }
}
StreamInitializer::~StreamInitializer ()
{
  if (0 == --nifty_counter)
  {
    // Clean-up.
  }
}

The header file of the Stream class must be included before any member function can be called on the Stream object. An instance of the StreamInitializer class is included in each compilation unit. Any use of the Stream object follows the inclusion of the header, which ensures that the constructor of the initializer object is called before the Stream object is used.

Known UsesEdit

Standard C++ iostream library std::cout, std::cin, std::cerr, std::clog.

Related IdiomsEdit

ReferencesEdit

Last modified on 13 September 2013, at 10:55