// Template_init.h
// ----------------------------------------------------------------
// COMP 290-001: Algorithm Library Design, Lutz Kettner, 01/11/2000
// Example of a template-only C++ Library with initialization and housekeeping.
// See Stroustrup, C++ Progr. Lang. 3rd Ed., page 640.
// Common prefix EX for Examples.

// This header needs to be included by all other header files that
// depend on the library being initialized.

#ifndef EX_TEMPLATE_INIT_H
#define EX_TEMPLATE_INIT_H

#include <iostream>

// Library initialization and housekeeping. Is triggered whenever
// libExample is linked to a program, here only output to cerr.
template <class T> // note, T is only a dummy
class EX_Template_init {
    static unsigned int count;
public:
    EX_Template_init();
    ~EX_Template_init();
};

// Trigger constructor and destructor calls in each compilation unit.
// Unnamed namespace can be used to avoid name collisions.
namespace {
    static EX_Template_init<int> EX__template_init_var; 
};

// The static member definition with templates can remain in the header,
// this is the main difference to Init.h and Init.C.
template <class T>
unsigned int EX_Template_init<T>::count = 0;

template <class T>
EX_Template_init<T>::EX_Template_init() { // default constructor
    if ( 0 == count++) {
	// perform initialization
	std::cerr << "template library initialization called." << std::endl;
    }
}
template <class T>
EX_Template_init<T>::~EX_Template_init() { // destructor
    if ( 0 == --count) {
	// perform housekeeping
	std::cerr << "template library housekeeping called." << std::endl;
    }
}

#endif // EX_TEMPLATE_INIT_H //
// EOF //

