[Insight-developers] Compile-time decorator pattern

Bradley Lowekamp blowekamp at mail.nih.gov
Sat Jan 8 15:30:16 EST 2011


Luis,

Thanks for taking a look at this. The usage of the CRTP in the Mesh classes looks interesting, I really should study the mesh classes on day to understand them, The CRTP is a bit more complicated then the pattern I intended to describe. In it's basic form the structure of this pattern is:

class SuperBase{..};

template <typename TSuperBase>
class Base :
	public TSuperBase {...};

class Derived : 
	public Base<SuperBase> {...};

This pattern is more linear then recursive. That is the derived classes do not know about their parents.

Brad


On Jan 8, 2011, at 1:35 PM, Luis Ibanez wrote:

> Hi Brad,
> 
> The name of this pattern is:
> 
>         "The Curiously Recurring Template Pattern"
> 
> or CRTP for short.
> 
> http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
> 
> 
> It is used abundantly in ITK in the Mesh hierarchy and the CellTypes.
> 
> See for example the TriangleCell:
> 
> template< typename TCellInterface >
> class ITK_EXPORT TriangleCell:
>  public TCellInterface, private TriangleCellTopology
> {
> 
> 
> ----
> 
> 
> It certainly will be interesting to explore it use in the GPU
> classes than Won-Ki posted to Gerrit:
> 
>   http://review.source.kitware.com/#change,673
> 
> 
> 
>    Luis
> 
> 
> 
> -----------------------------------------------------------------------------------
> On Fri, Jan 7, 2011 at 1:42 PM, Bradley Lowekamp <blowekamp at mail.nih.gov> wrote:
>> Hello,
>> I have just push a little branch which shows the basis for a potentially
>> very useful design pattern for ITK filters:
>> http://review.source.kitware.com/#change,684
>> I could not find an official name for this patterns, I have read about it
>> before and used it in a few places as well in other projects. I decided to
>> call it "compile-time decorator" because it's design to add features to a
>> multiple class hierarchies.
>> The motivation is to be able add reuse functionality, without changing
>> exiting hierarchy structures. This an alternative to multiple inheritance in
>> for some situations.
>> 
>> The implementation of the pattern is to have the parent of a class be a
>> template argument to the class.
>> template< class TInputImage, class TOutputImage = TInputImage,
>> class TParentImageFilter = ImageToImageFilter< TInputImage, TOutputImage > >
>> class ITK_EXPORT InPlaceImageFilter: public TParentImageFilter {...}
>> 
>> For example say I have a good inplace algorithm for WhiteTopHatImageFilter:
>> http://www.itk.org/Doxygen/html/classitk_1_1WhiteTopHatImageFilter.html
>> However, it's already embedded in an existing complex hierarchy, so I can
>> not re-organize the class to be derived from InPlaceFilter. However, it's is
>> good practice to reuse the existing code in the InPlace filter. This can be
>> over come with this pattern by declaring the class in the following fashion.
>> template< class TInputImage, class TOuputImage, class TKernel >
>> class ITK_EXPORT WhiteTopHatImageFilter:
>> public InPlaceImageFilter<TInputImageFilter, TOutputImageFilter,
>> KernelImageFilter< TInputImage, TOutputImage, TKernel > >
>> {...}
>> 
>> This may be a useful design pattern for GPU implementations of filters,
>> along with adding certain streaming functionality to sinc like filters.
>> Brad
>> 
>> ========================================================
>> 
>> Bradley Lowekamp
>> 
>> Lockheed Martin Contractor for
>> 
>> Office of High Performance Computing and Communications
>> 
>> National Library of Medicine
>> 
>> blowekamp at mail.nih.gov
>> 
>> 
>> _______________________________________________
>> 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.html
>> 
>> 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://www.itk.org/mailman/listinfo/insight-developers
>> 
>> 



More information about the Insight-developers mailing list