[vtk-developers] Alternatives to vtkTemplateMacro
Timothy M. Shead
tshead at sandia.gov
Mon Sep 21 12:00:24 EDT 2009
Brad King wrote:
> 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).
Sounds reasonable, although Apply would still only work with vtkObject
derivatives that are templated on something. To my knowledge,
vtkTypedArray<> derivatives are the only classes that match this
criteria, i.e. if we want to make Apply usable with vtkAbstractArray,
that'll involve additional work.
> - 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.
I think you'd probably have to do a couple of separate typedefs, along
the lines of
typedef vtkNumericTypes value_types;
typedef vtkTypedArray target_type;
Having said that, I'm not in love with the aesthetics of this approach
... for someone looking at the implementation of a filter,
if(!vtkApply<vtkNumericTypes, vtkTypedArray>(functor, array))
vtkErrorMacro("First operand array must be a numeric type");
seems clearer than
if(!vtkApply(functor, array))
vtkErrorMacro("First operand array must be a numeric type");
to me. It's also the case that a functor with templated operators might
be applied to different typelists at different times, so specifying the
typelist in the algorithm provides a little extra flexibility.
There are a couple other dimensions to the design that I'm looking into,
first is whether we should allow additional arguments to be passed to
the functor by the caller; although callers can make their functors as
complex as they like, Ken makes the observation that it would be nice to
be able to pass parameters to a functor without having to write all the
boilerplate that that entails (creating a constructor, declaring storage
for each parameter, etc).
Second, there's the question of what vtkApply would return. One option
is a bool to indicate whether any of the types in the typelist matched,
which is straightforward and would be useful in many cases. Second
option is return a copy of the functor, which would be consistent with
many standard library algorithms, and would allow the functor to return
arbitrary results to the caller.
Cheers,
Tim
More information about the vtk-developers
mailing list