1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkFilterDispatchTest.cxx,v $
Language: C++
Date: $Date: 2007-08-20 13:23:56 $
Version: $Revision: 1.15 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
/**
* Demonstration of filter dispatching. The general problem is to provide
* filters with specialized implementations for each dimension, but in
* only one class, and without partial specialization. Consider a filter
* with separate methods for 2-d, 3-d, and N-d implementations. We would
* like to dispatch a call to the correct implementation from a single
* Execute() call.
*
* The basic challenge here is that a filter that has been instantiated as
* dimension 3, for example, still has execute methods for 2, 3, and N
* dimensions. If the Execute(2-d) is instantiated, it may have compiler
* errors because it assumes that the class is instantiated for 2 dimensions.
* This means we can't have an Execute() method which tests the dimension
* at run-time and calls the appropriate Execute(*-d) method because it will
* instantiate all the Execute(*-d) methods, when only one will compile
* properly.
*
* The solution presented below will allow a single Execute() call to
* instantiate and call only the correct Execute(*-d) method for a particular
* filter instantiation.
*/
#include <itkImage.h>
#include <itkImageToImageFilter.h>
#include <itksys/ios/sstream>
/**
* An example filter.
*/
template <typename TInputImage, typename TOutputImage>
class ITK_EXPORT ExampleImageFilter:
public itk::ImageToImageFilter<TInputImage, TOutputImage>
{
public:
/**
* Standard "Self" typedef.
*/
typedef ExampleImageFilter Self;
/**
* Standard "Superclass" typedef.
*/
typedef itk::ImageToImageFilter<TInputImage, TOutputImage> Superclass;
/**
* Smart pointer typedef support
*/
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
typedef TInputImage InputImageType;
typedef TOutputImage OutputImageType;
enum { ImageDimension = InputImageType::ImageDimension };
void Update(void);
/**
* Method for creation through the object factory.
*/
itkNewMacro(Self);
protected:
ExampleImageFilter() {}
ExampleImageFilter(const Self&) {}
void operator=(const Self&) {}
virtual ~ExampleImageFilter() {}
private:
/**
* Dispatch class base allows automatic use of general implementation
* when no specific dispatch rules match.
*/
struct DispatchBase {};
/**
* Dispatch control class simply holds information in its template
* parameter(s) that is used to control which Execute() method is chosen.
*/
template <unsigned long>
struct Dispatch: public DispatchBase {};
void Execute(const DispatchBase&);
void Execute(Dispatch<2>);
void Execute(Dispatch<3>);
void Execute(Dispatch<0>);
};
/**
* Filter update method called by the user of the filter.
* This just makes the call to the real method by instantiating the
* appropriate Dispatch control class.
*/
template <typename TInputImage, typename TOutputImage>
void ExampleImageFilter<TInputImage, TOutputImage>
::Update(void)
{
this->Execute(Dispatch<ImageDimension>());
}
/**
* General N-dimensional implementation of example filter.
* The Dispatch parameter is not used. It is just used to control
* instantiation.
*/
template <typename TInputImage, typename TOutputImage>
void ExampleImageFilter<TInputImage, TOutputImage>
::Execute(const DispatchBase&)
{
std::cout << "General N-d Execute() has been called." << std::endl;
// Make sure the correct Execute() method has been called.
if((ImageDimension == 2) || (ImageDimension == 3))
{
itksys_ios::ostringstream err;
err << "Error: N-d filter implementation called for "
<< ImageDimension
<< "-d filter, even though specific implementation exists."
<< std::endl;
throw std::string(err.str().c_str());
}
}
/**
* 2-dimensional implementation of example filter.
* The Dispatch parameter is not used. It is just used to control
* instantiation.
*/
template <typename TInputImage, typename TOutputImage>
void ExampleImageFilter<TInputImage, TOutputImage>
::Execute(Dispatch<2>)
{
std::cout << "2d-specific Execute() has been called." << std::endl;
// Make sure the correct Execute() method has been called.
if(ImageDimension != 2)
{
itksys_ios::ostringstream err;
err << "Error: 2-d filter implementation called for "
<< ImageDimension
<< "-d filter." << std::endl;
throw std::string(err.str().c_str());
}
}
/**
* 3-dimensional implementation of example filter.
* The Dispatch parameter is not used. It is just used to control
* instantiation.
*/
template <typename TInputImage, typename TOutputImage>
void ExampleImageFilter<TInputImage, TOutputImage>
::Execute(Dispatch<3>)
{
std::cout << "3d-specific Execute() has been called." << std::endl;
// Make sure the correct Execute() method has been called.
if(ImageDimension != 3)
{
itksys_ios::ostringstream err;
err << "Error: 3-d filter implementation called for "
<< ImageDimension
<< "-d filter." << std::endl;
throw std::string(err.str().c_str());
}
}
/**
* Zero-dimensional implementation of filter. This should never be called,
* or even instantiated. If a compiler instantiates this, the test will
* fail to compile.
*/
template <typename TInputImage, typename TOutputImage>
void ExampleImageFilter<TInputImage, TOutputImage>
::Execute(Dispatch<0>)
{
// this_should_not_have_been_instantiated();
throw std::string("The 0-Dispatch method should not have been called.");
}
/**
* Filter dispatch test creates several ExampleImageFilter instantiations
* and calls them to check if the dispatch rules are working correctly.
*/
int itkFilterDispatchTest(int, char* [] )
{
bool passed = true;
// Define an image of each dimension.
typedef itk::Image<float, 2> Image2d;
typedef itk::Image<float, 3> Image3d;
typedef itk::Image<float, 4> Image4d;
typedef itk::Image<float, 5> Image5d;
// Define a filter of each dimension.
typedef ExampleImageFilter<Image2d, Image2d> Filter2d;
typedef ExampleImageFilter<Image3d, Image3d> Filter3d;
typedef ExampleImageFilter<Image4d, Image4d> Filter4d;
typedef ExampleImageFilter<Image5d, Image5d> Filter5d;
// Instantiate a filter of each dimension.
Filter2d::Pointer filter2d = Filter2d::New();
Filter3d::Pointer filter3d = Filter3d::New();
Filter4d::Pointer filter4d = Filter4d::New();
Filter5d::Pointer filter5d = Filter5d::New();
// Try running each of the filters. If the wrong Execute() method is
// invoked by one of these calls, a std::string() exception will be
// thrown with the error description.
try
{
std::cout << "Executing 2-d filter: ";
filter2d->Update();
std::cout << "Executing 3-d filter: ";
filter3d->Update();
std::cout << "Executing 4-d filter: ";
filter4d->Update();
std::cout << "Executing 5-d filter: ";
filter5d->Update();
}
catch (std::string err)
{
std::cout << err;
passed = false;
}
if(passed)
{
std::cout << "The test has passed." << std::endl;
return EXIT_SUCCESS;
}
else
{
std::cout << "The test has failed." << std::endl;
return EXIT_FAILURE;
}
}
|