Page MenuHomePhabricator

Design Decision: Create a ConvertTo Method for MITK Objects
Closed, ResolvedPublic

Description

Casting between mitk::BaseData and derived classes is usually straight forward by converting to a pointer and using a dynamic_cast. But this solution is not readilly available in the wrapping scenario.

I wrote a (very) basic ConvertTo<T>(BaseData::Pointer) that casts the input BaseData object to a Smartpointer of the type T. Right now I implmented a very basic version, without any tpye checking. And I implemented this version within the files used for the wrapping, e.g. it cannot be used within the "traditional" MITK.

The question is: Do we want such a function within the general mitk code (perhaps even the core?)

My thoughs so far:

  • I will need this function within the wrapping, so we will have it at some place in the code. It might be beneficial to have it accessible for a wider range of people.
  • There is no absolute hard need to have such a method, it won't add any new functionality.
  • If this function is added to the core, i would suggest to extend it.
    • An error instead of an Null-Pointer is returned if the casting fails
    • Template over two parameters, the last one could define the input. Using template parameter deduction, a fast covnersion from any BaseData-Derived type to any other type would be possible.
  • An advantage of such a solution could be code that is easier to read and more fail-safe:
    • Avoid calling .GetPointer(),
    • no need to define the "dynamic_cast<Class *> construct,
    • No NULL-Pointers but exceptions.

What are your thoughts on this?

Event Timeline

goetzm triaged this task as Wishlist priority.Feb 27 2018, 4:43 PM
goetzm created this task.

I do not really mind either way. Implementation should be pretty straightforward, it does not really save any lines of code. Instead of

mitk::Image::Pointer image = dynamic_cast<mitk::Image*>(node->GetData());
if(image.IsNull())
{
  //Error handling here
}

it would be

try
{
  mitk::Image::Pointer image = mitk::ConvertTo<mitk::Image*>(node->GetData());
}
catch(mitk::ConversionFailedException)
{
  //Error handling here
}

I assume most would not use it anyway due to that fact. If there are ideas or a need for more complex conversions it might warrant its own MitkConversions module, but otherwise I do not see a burning need to not have it in the core.

Ok, so it seems there is not too much of an interest for this topic. Therefore i won't include this code into the core. Feel free to change the status and leave a comment if you have a new argument.