Table of Contents
This chapter introduces the concept of Dynamic Compilation within SCIRun.
SCIRun makes extensive use of C++ templates. Templates are a powerful mechanism that allows writing algorithms or data structures once, while allowing them to be usable with many types of data. However, in order to use any particular type of data, the templates need to know about them at compile time. Dynamic compilation allows templates written in the past to be applied to new data types at run time by dynamically compiling code.
The Disclosure directory contains objects that discover information about types, as well as objects that produce previously uncompiled types.
This directory also contains the TypeDescription object, as well as the DynamicLoader. TypeDescription gives a recursive definition of type at runtime. One can query a string that indicates the type from the this object. It also provides information that can by used by the DynamicLoader to compile types that support TypeDescription.
The DynamicLoader writes a .cc file, compiles it, then loads in the .so all at runtime. The main function is to compile algorithms that are templated on various field types in SCIRun. This gives SCIRun a minimal set of template instantiation based on how a user happens to use SCIRun, as opposed to the combinatorial explosion of template bloat that is the alternative.
A developer is likely to use this code from within modules that has a field port. A FieldHandle can be one of *many* types of fields. An algorithm can be templated on the field, and use the DynamicLoader to manage compilation and loading of the algorithm without the module code ever needing to know the exact type of the field.
The main use of code in this directory is to compile and load templated algorithms for use in modules.
There are many of these functions. Each is overloaded on the type that the returned TypeDescription supports. There must be one of these if your type is to be supported in dynamic compilation.
An example of a simple get_type_description for int.
const TypeDescription* get_type_description(int*)
{
static TypeDescription* td = 0;
static string nm("int");
static string path("builtin");
if(!td){
td = scinew TypeDescription(nm, path);
}
return td;
}
An example of a templated get_type_description function.
template <class T >
const TypeDescription* get_type_description(vector <T >*)
{
static TypeDescription* td = 0;
static string v("vector");
static string path("std::vector");
if(!td){
const TypeDescription *sub = SCIRun::get_type_description((T*)0);
TypeDescription::td_vec *subs = scinew TypeDescription::td_vec(1);
(*subs)[0] = sub;
td = scinew TypeDescription(v, subs, path);
}
return td;
}