Hello,

I wrote dynamic_cast<>() and const_cast<>() like operators to work with
itk::SmartPointer<> for the OTB/Monteverdi2 project.

I think they could be contributed to ITK (for example in the
itkSmartPointer.h header file).

You will find enclosed a itkCasts.h file with the source code of these
operators. This file is an extract from:
http://hg.orfeo-toolbox.org/Monteverdi2/file/df0184bc51a3/Code/Common/Core/mvdTypes.h#l63
.

Best regards,

Stéphane

-- 

http://orfeo-toolbox.org

Stéphane ALBERT
Ingénieur d'études et développement
Business Unit E-SPACE & Geo Information, Département APPLICATIONS

CS Systèmes d'Information
Parc de la Grande Plaine - 5, Rue Brindejonc des Moulinais - BP 15872
31506 Toulouse Cedex 05 - France
/*=========================================================================
 * TODO: Add licence notice.
 *=======================================================================*/

// Submitted by Stéphane ALBERT (otb.salb...@gmail.com).

#ifndef __itkCasts_h
#define __itkCasts_h

namespace itk
{
/**
 * \brief dynamic_cast<>() like operator to work with itk::SmartPointer<>.
 *
 * Works like C++ dynamic_cast<>() operator:
 *
 *   DerivedClass::Pointer pointer(
 *     itk::DynamicCast< DerivedClass >( smartPointerToBaseClass )
 *   );
 */
template< typename T2, typename T1 >
inline
itk::SmartPointer< T2 >
DynamicCast( const itk::SmartPointer< T1 > & p1 )
{
  return
    typename itk::SmartPointer< T2 >(
      dynamic_cast< typename itk::SmartPointer< T2 >::ObjectType * >(
	p1.GetPointer()
      )
    );
}

/**
 * \brief const_cast<>() like operator to work with itk::SmartPointer<>.
 *
 * Works like C++ const_cast<>() operator:
 *
 *   MyClass::Pointer pointer(
 *     itk::ConstCast< DerivedClass >( smartPointerToConstInstance )
 *   );
 */
template< typename T2, typename T1 >
inline
itk::SmartPointer< const T2 >
ConstCast( const itk::SmartPointer< T1 > & p1 )
{
  return
    typename itk::SmartPointer< const T2 >(
      const_cast< typename itk::SmartPointer< const T2 >::ObjectType * >(
	p1.GetPointer()
      )
    );
}

/**
 * \brief const_cast<>() like operator to work with itk::SmartPointer<>.
 *
 * Works like C++ const_cast<>() operator:
 *
 *   MyClass::ConstPointer constPointer(
 *     itk::ConstCast< const DerivedClass >(
 *       smartPointerToNonConstInstance
 *     )
 *   );
 */
template< typename T2, typename T1 >
inline
itk::SmartPointer< T2 >
ConstCast( const itk::SmartPointer< const T1 > & p1 )
{
  return
    typename itk::SmartPointer< T2 >(
      const_cast< typename itk::SmartPointer< T2 >::ObjectType * >(
	p1.GetPointer()
      )
    );
}

} // end namespace itk.

#endif // __itkCasts_h
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://kitware.com/products/protraining.php

Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/insight-developers

Reply via email to