Templates tables

In this lab you'll learn how to template a class. Since it's easiest to understand error messages from a non-templated class, I recommend first using C's typedef syntax to get started on the template.

You'll be making a template allowing you to create a table mapping strings to ints, ints to strings, ints to Robots, or whatever. The main rule is that the indexing class must have the comparators <, >, and ==.

First, typedef

  1. Move your StringToIntTable.cc and StringToIntTable.h and give them more appropriate (and general) names like, Table.cc and Table.h.

  2. Global replace all StringToIntTable with Table in all files.

  3. Remove the = 0 in the class declaration of Table. (Dealing with this optional argument in templates is a bit anoying.) Be sure you never assigned the initial value of = 0 in your code. To get your code to compile, you'll need to change the declaration at the top of main to:
    Table table(0);
    

  4. Make two typedef's. One will be the Key (currently of class string), and one will be the Data (currently of class int). For reference, you may wish to look at the List class definitions from class in
      ~mc38/labs/operator-overloading/files/List.*
    Be sure the code compiles and runs.

  5. Test your typedefs with another main with a table of strings indexed by integers. You'll swap the DATA and KEY typedef's, and use a main such as:
      #include <iostream>
      #include <string>
      #include "Table.h"
      
      int main() {
         Table table (" 0 ");
         table[20] =  " 1 ";
         table[10] =  " 2 " ;
         table[-100] = " 3 ";
         table[10] =  " 4 ";
         table[-100] = " 5 ";
         cout << table[20] << table[10] << table[-100] << table[30] << endl;
      
        return 0;
      }
    

The template

  1. Now, template your class. A sample templated List class is in the directory ~mc38/labs/templates/list/. A few things are worth noting.

  2. Change your Makefile so that it no longer compiles Table.cc. Only your file with your main should be compiled.

  3. Again, test your template twice, this time with a single main combining both programs: One indexing a table of integers by strings, and one indexing a table of strings by integers. To use the template, you'll instantiate the table with a lines like:
       Table<int, string> table (" 0 "); // and
       Table<string, int> table (" 0 ");
    
    

Optional

This optional assignment combines ideas of templates with stream I/O from Chapter 10.

There is one thing that is very annoying about the process of writing a typedef class before writing a template. What if you want to enhance the templated class? You are stuck with two choices: Either (1) Change the typedef class and reconvert it, or (2) change the template class and hope you can debug it.

Write a program to help automate this process. Your program will take a class you've designed with typedef and change it to a templated class. Since you will just use this for your own programs, you can make any assumptions you want to in order to make your task a bit easier. For example, you could assume that the typedef's appear at the top of the .h file. Or you could assume that the typedef takes only one argument.

The right way to do this is using Linux string processing tools such as awk. If you know something about these tools already, you are free to use them.