#include <static_table.h>
Inheritance diagram for base::
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 |
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:
Problem
Several static tables need to be implemented. It must be possible to initialize these tables from different parts of the program (e.g., different files). Initialization must be done at static constructor time, so that the table exists when the rest of the code starts executing. It must be possible to add to the tables (e.g., they must be able to grow) as the program runs.
Solution
Create specific tables by deriving them from the base template instantiated for the table element type. From this class derive classes that extend the class derived from base by adding a constructor that adds an element list to the table.
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.
|
00164 { INITIAL_SIZE = 20 } bogosity; |
|
00172 {} |
|
00173 {}; |
|
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 |
|
00219 { 00220 if (tbl != 0 && len > 0) { 00221 for (int i = 0; i < len; i++) { 00222 addElem( tbl[i] ); 00223 } 00224 } 00225 } // addTable |
|
|
|
Reimplemented in StringTable, and IntTable. |
|
|
|
|
|
|
|
Declare and initialize static variables. Is this twisted syntax or what? |