Totalview® for HPC Reference Guide : PART II Transformations : Chapter 6 Creating Type Transformations : C++View : Templates
Templates
C++View can also be used with template classes. Consider this container class:
template <class T> class BoundsCheckedArray {
private:
int size;
T *array;
 
public:
typedef T value_type;
 
T ( int s ) { ... }
...
};
Writing a collection of overloaded display functions for each instantiated BoundsCheckedArray can rapidly become an overwhelming maintenance burden. Instead, consider whether you can write a template function.
One potential difficulty is getting the name of the type parameter to pass to TV_ttf_add_row. Here we follow the convention used by the container classes in the standard library which typedefs the template type parameter to the standard name value_type.
We can construct our template function like this:
template <class T>
int TV_ttf_display_type ( const BoundsCheckedArray<T> *a )
{
char type [ 4096 ];
 
snprintf ( type, sizeof ( type ), "value_type[%d]", \
a->get_size () );
 
TV_ttf_add_row ( "array_values", type, a->get_array () );
return TV_ttf_format_ok;
}
What we've done here is constructed the type of a fixed-sized array of the type named by the template type parameter. (In some cases you may need to use the compiler's demangler to get the name of the type. See also "Tips and Tricks".)
This one definition can be used for any instance of the template class. In some cases, however, you may want a specialized implementation of the display function. As an illustration, consider this:
int TV_ttf_display_type ( const BoundsCheckedArray<char> *s )
{
TV_ttf_add_row ( "string", TV_ttf_type_ascii_string, \
s->get_array () );
return TV_ttf_format_ok;
}
Here we want to tell TotalView to display the array horizontally as a string instead of vertically as an array. For this reason, we want to pass TV_ttf_type_ascii_string to TV_ttf_add_row as the name of the type instead of the name constructed by the implementation of the general template display function. We therefore define a special version of the display function to handle BoundsCheckedArray<char>.
One remaining issue relating to templates is arranging for the various template display function instances to be instantiated. It is unlikely that display functions will be called directly by your program. (Indeed, we mentioned earlier that TV_ttf_add_row should not be called other than as a result of a call initiated by TotalView.) Consequently, the template functions may well not be generated automatically. You can either arrange for functions to be referenced, such as by calling them in a controlled manner, or by explicit template instantiation:
 
template int TV_ttf_display_type \
( const BoundsCheckedArray<int> * );
template int TV_ttf_display_type \
( const BoundsCheckedArray<double> * );
.
.