[Insight-developers] Questions on Program Style/Design Functors/Function Parameters

Luis Ibanez luis . ibanez at kitware . com
Mon, 02 Jun 2003 11:32:54 -0400


Hi Kent,

1) Do you need constructors with arguments in the Functor ?

    In the way other functors work, you just construct them
    without parameters and then initialize them with their
    Set/Get methods. You can still set reasonable default
    values for the ivars in the constructor without arguments.

2) The Functor must be an instance inside the class using it.
    As you pointed out, the assignment has to be an exact match.
    Functors are not designed to be used with a hierarchy of
    derived classes and virtual functions. In fact, Functors are
    the opposite of this approach. We want to avoid virtual calls
    in pixel-wise access.  The functor is intended to be a template
    parameter so its code can be thightly integrated with the host
    class at compile time. Every new functor is completly independent,
    it does not have to derive from any base class. It only has to
    satisfy the API that is used from the host class.

    In practice all what you can do with derived classes at run-time
    can be done with Functor instances at compile time, with the
    advantage that it will run faster with Functors.


3) The functor in the host class is initialized with the Set(functor)
    class. Note that the functor is not 'assigned', instead the
    operator=() method of the functor is invoked so all the parameters
    of the user-defined functor are passed to the Functor private ivar
    in the host class. It is fundamental then to define the operator=()
    method in the Functor.

4) An easy way to remind programmers to initialize classes is to
    throw exceptions from them when an attempt is made to use them
    uninitialized.

    After a couple of crashes most programmers get the point  :-)

    This practice is already enforced in several ITK classes.



Regards,


   Luis


---------------------
Kent Williams wrote:
> Thanks Luis.  I have been looking at using a templated functor class, and I'll 
> look at the classes you suggest for the right style to implement. After 
> sending that E-mail I did start revising the class to use a functor template 
> parameter.
> 
> The only open issue with using the Functors that I can see are these (unless 
> I'm misinterpreting the code):
> 
> 1. Functors have to have a constructor that takes no arguments.  An instance 
> of the Functor type is kept as private member data.
> 
> 2. In the UnaryFunctorImageFilter template class, there are access functions 
> to set the Functor member, but since the member is an Instance, and not a 
> pointer or a reference, there has to be an exact class match; one can't pass 
> in a derived class instance.
> 
> In practice, these are probably not onerous restrictions; they may in fact 
> constrain programmers to not twist the system away from it's intended pattern 
> with cute class derivations. But it does kind of itch at my designer's mind 
> that the Functor patten can't be completely general.
> 
> Issue 2 can be finessed by defining a functor with an Init function to pass in 
> configuration parameters, but that always bothers me a little since it allows 
> a programmer to create an instance and forget to call the initializer.
> 
> Anyway thanks for the help!
> 
> 
> _______________________________________________
> Insight-developers mailing list
> Insight-developers at public . kitware . com
> http://public . kitware . com/mailman/listinfo/insight-developers
>