[vtk-developers] Alternatives to vtkTemplateMacro
Timothy M. Shead
tshead at sandia.gov
Tue Sep 15 12:45:13 EDT 2009
Jeff Baumes wrote:
> Tim,
>
> Can you flesh your thoughts out in code so that I can understand your
> suggestions better? In particular, what do you mean by "typelists"?
A typelist is a type that stores a collection of types. For example,
you can define lists of numeric and string types:
#include <boost/mpl/vector.hpp>
typedef boost::mpl::vector<vtkTypeUInt8, vtkTypeInt8, ...,
vtkTypeFloat32, vtkTypeFloat64> vtkNumericTypes;
typedef boost::mpl::vector<vtkStdString, vtkUnicodeString>
vtkStringTypes;
Once you've defined appropriate typelists, you can use them as
parameters to a generic algorithm. I imagine adding the typelist as an
additional parameter to Apply():
array1->Apply<vtkNumericTypes, vtkTypedArray>(my_numeric_functor);
array2->Apply<vtkStringTypes, vtkDenseArray>(my_string_functor);
Note that this approach is explicit and compact, while allowing the
caller to define their own typelist when needed.
The Apply() implementation would be altered to perform SafeDownCast
tests based on the contents of the typelist parameter, instead of the
hard-coded list in the original proposal.
Although this example uses Boost.MPL for simplicity, implementing
typelists for this purpose is straightforward, I've used them on a
number of occasions both with and without Boost.
Cheers,
Tim
More information about the vtk-developers
mailing list