[Insight-developers] Set/Get Macros : Set/Get Object macro

Luis Ibanez luis . ibanez at kitware . com
Fri, 20 Jun 2003 11:27:11 -0400


Hi,

This is just a reminder about the use of Set/Get
macros for getting access to member variables
in ITK classes.


The macros

   itkSetMacro( variableName, type )
   itkGetMacro( variableName, type )

are intended to be used with native types
and class types, but *not* with types using
SmartPointers.

The macros

    itkSetObjectMacro( variableName, type )
    itkGetObjectMacro( variableName, type )

should be used when setting/getting a
member variable that is a derived class
of the itk::Object.

SmartPointers should not be used as 'type'.

For example,
if the class FilteFOO has an itk Transform
as parameter (itkTransform derives from
itk::Object)


the filter should look like


class FilterFoo
{

    itkSetObjectMacro( Transform, TransformType );
    itkGetObjectMacro( Transform, TransformType );

private:
    TransformType::Pointer m_Transform;
};


Note that there are also variants for supporting
const-correctness.


    itkGetConstMacro( variableName, type )


will return a const & to the variable

Along the same lines, for itk::Objects, the
macro

    itkGetConstObjectMacro( variableName, type )

will return a const * to the variable.


The macro

    itkSetConstObjectMacro( variableName, type )


should be used for member variables that are
Const smart pointers.  E.g. for parameters
that are only going to be used by the filter
but shouldn't be changed by the filter.
These ivars will be declared as:


class FilterFoo_ConstIsGood
{
private:
   TransformType::ConstPointer m_Transform;
};


Note the use of ConstPointer instead of Pointer.





    Luis