[vtk-developers] Alternatives to vtkTemplateMacro

Brad King brad.king at kitware.com
Thu Sep 17 16:45:02 EDT 2009

Timothy M. Shead wrote:
> 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.

Any approach needs to list the types that the functor
supports, essentially duplicating the overloads written
as members of the functor.  Given this functor:

   struct my_functor
     template <typename T>
     void operator()(vtkTypedArray<T>&) { ... }

In Tim's approach we write

   // In execute method
   array->Apply<vtkNumericTypes, vtkTypedArray>(my_functor(this));

In my approach we write something like

   // In constructor
   this->ApplyMap->Set<vtkNumericTypes, vtkTypedArray>(my_functor(this));

   // In execute method

Effectively the "extra step" Ken Moreland mentioned is
just the last line which is the same in every filter.

Actually there is no need to choose one of these methods
now.  The two approaches are not mutually exclusive.  We
could use Tim's approach for the simple/common cases and
later introduce my approach for more complicated cases.

I suggest that we use Tim's approach with a few changes:

-  Primarily I'd like to keep the Apply method template out
    of vtkArray because it is useable for other types too.
    Perhaps it can be a namespace-level function template
    (just as vtkTemplateMacro appears to be).

- The type lists could be members or traits of the functor
   types themselves:

   struct my_functor
     template <typename T>
     void operator()(vtkTypedArray<T>&) { ... }

     typedef vtkTypeList<vtkNumericTypes, vtkTypedArray> types;

   The Apply template can look up the type list from the
   functor it is given.


More information about the vtk-developers mailing list