[Insight-users] template instantation patterns?

Harri Tapio Jaalinoja harri.jaalinoja@helsinki.fi
Wed, 6 Nov 2002 16:33:43 +0200 (EET)


Hi!

This is not directly related to ITK, more a generic programming problem,
but since ITK uses templates a lot, maybe you have encountered something
similar:

I use BSoft library to read in image files. The package takes care of
getting the endianess, data type etc correctly, and returns me a struct
that includes among other things a pointer to the data and an enumeration
that tells me the type of data.

My code now looks something like this:

  // BSoft library call to read the image file
  Bimage* p = read_img(argv[1], 0, -1);

  itk::ImportImageFilter<float, 3>::Pointer breader =
    itk::ImportImageFilter<float, 3>::New();

  // here is the pointer to the image data
  float* data = (float *)p->data;
  breader->SetImportPointer(data ,p->x * p->y * p->z, false);

This compiles ok.

The problem above obviously is that it states directly that the data type
is "float", when it can be many other things as well. Can you suggest an
elegant way to code this in a way that allows the data type be set at
runtime? Do I just have to copy the above snippet in switch cases for all
the possible data types? Or maybe make a template of the above, so at
least the switch cases will be a bit smaller? Actually the code that would
go in the switch case is longer, since I would also have to declare all
the filters along the pipeline in the same manner.

Thanks for your input!

Harri