Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

base Class Template Reference

A generic, growable, static table. More...

#include <static_table.h>

Inheritance diagram for base::

IntTable StringTable StringTable_A StringTable_B StringTable_C StringTable_D List of all members.

Public Methods

 base ()
 ~base ()
void addElem (T elem)
void addTable (T *table, int len)
virtual void insertElem (T elem)=0
virtual void pr ()=0

Static Protected Attributes

T* table = 0
 Declare and initialize static variables. More...

int ix = 0

Private Types

enum  { INITIAL_SIZE = 20 }

Private Attributes

enum base:: { ... }  bogosity

Static Private Attributes

int size = 0

Detailed Description

template<class T> class base

A generic, growable, static table.

This template allows the creation of a growable static table. All instances of a template of a given type will share this static table.

The base class template is an abstract class template. It cannot be instantiated by itself, other classes can only be derived from it.

Classes derived from the base template must provide implementations for the insertElem() and pr() functions.

Example:

Creating a specific instance of the base template

The base template is an abstract class (e.g., it cannot be instantiated, only derived from). The derived class must define specific instances of the functions insertElem(T elem) and pr().

Some notes on templates with static class variables.

Each base template that is instantiated with a different type will have a different set of static class variables implementing the table. For example, the class definitions below for StringTable and IntTable define two static tables, one with int and one with char * elements. Each table has a separate set of static variables.


  class StringTable : public base< char *> {
  public:
    StringTable();
    void insertElem( char * str );
    void pr();
  };

  class IntTable : public base< int > 
  {
     public:
     IntTable();
     void insertElem( int i );
     void pr();
  };

If another class with the same base type (e.g., the same instantiation of the base template) is defined it will share the static variables (since all instances of a given type share the static variables). For example, StringTable2 will share the same set of static variables as StringTable, even though it is a separate class.

  class StringTable2 : public base< char *> {
  public:
    StringTable();
    void insertElem( char * str );
    void pr();
  };

The power of the static table template is that it allows a local object, derived from the template, to become a window into the table. For example

  
  void add_name(char *name )
  {
    if (name != 0) {
       int len = strlen(name);
       char *new_name = new char[ len+1 ];
       strncpy(new_name, name, len+1);

       StringTable strTab;

       strTab.addElem( new_name );
    }
  }

Here the local object strTab serves as a window into the global table.

The static table is never deallocated. It persists for the life the of the program. The table cannot be deallocated because the destructor will be called when add_name() exits. This would result in deallocating the table after the call to add_name(), destroying the data in the table.


Member Enumeration Documentation

template<class T>
anonymous enum [private]
 

Enumeration values:
INITIAL_SIZE  
00164 { INITIAL_SIZE = 20 } bogosity;


Constructor & Destructor Documentation

template<class T>
base<T>::base<T> ( ) [inline]
 

00172 {}

template<class T>
base<T>::~base<T> ( ) [inline]
 

00173 {};


Member Function Documentation

template<class T>
void base< T >::addElem ( T elem )
 

00192                        {
00193    if (ix == size) {
00194       int NewSize;
00195       if (size == 0)
00196          NewSize = INITIAL_SIZE;
00197       else
00198          NewSize = size * 2;
00199       T* old_table = table;
00200       table = new T [ NewSize ];
00201       assert( table != 0 );
00202       ix = 0;
00203       for (int i = 0; i < size; i++) {
00204          insertElem( old_table[i] );
00205          ix++;
00206       } // for
00207       if (old_table != 0) {
00208          delete [] old_table;
00209       }
00210       size = NewSize;
00211    }
00212    insertElem( elem );
00213    ix++;
00214 } // addElem

template<class T>
void base< T >::addTable ( T * tbl,
int len )
 

00219 {
00220    if (tbl != 0 && len > 0) {
00221       for (int i = 0; i < len; i++) {
00222          addElem( tbl[i] );
00223       }
00224    }
00225 } // addTable

template<class T>
void base<T>::insertElem ( T elem ) [pure virtual]
 

template<class T>
void base<T>::pr ( ) [pure virtual]
 

Reimplemented in StringTable, and IntTable.


Member Data Documentation

enum { ... } base::bogosity [private]
 

template<class T>
int base< T >::ix = 0 [static, protected]
 

template<class T>
int base< T >::size = 0 [static, private]
 

template<class T>
T * base< T >::table = 0 [static, protected]
 

Declare and initialize static variables.

Is this twisted syntax or what?


The documentation for this class was generated from the following file:
Generated at Thu Jun 14 21:06:18 2001 for C++ Templates: the power, the swamp by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001