Page MenuHomePhabricator

Improve mitk::WeakPointer
Closed, ResolvedPublic

Description

mitk::WeakPointer fills in the blank of a missing implementation of a true weak pointer for itk::Object. While there is an itk::WeakPointer, it is simply a raw pointer wrapper and of questionable use. The current implementation of mitk::WeakPointer still has some flaws, though:

  • It has more than a single responsibility by providing mitk::Messages for itk::ModifiedEvent and itk::DeleteEvent of the wrapped itk::Object.
  • It is not as efficient as it should be.
  • It can't be used in a natural way like a pointer because it lacks many comparison operators.
  • It provides direct and dangerous access to its encapsulated resource, the wrapped raw pointer.

I want to replace the current implementation of mitk::WeakPointer with a new, well thought out, high-quality implementation.

Related Objects

Event Timeline

Migration Guide

The public API of mitk::WeakPointer<T> changed completely. It follows the idea of std::weak_ptr<T> now. Access to the object is gained by converting it to T::Pointer resp. itk::SmartPointer<T>. To make clear that the whole principle of mitk::WeakPointer<T> changed, the wording of the public API is also based on std::weak_ptr<T> now.

Check if the weak pointer is expired

Old API
if (weakPointer.IsNull())
  ...
New API
if (weakPointer.IsExpired())
  ...

// or

if (!weakPointer)
  ...

// or

if (nullptr == weakPointer)
  ...

Access the object

Old API
if (weakPointer.IsNotNull())
  weakPointer->Foo();
New API
if (weakPointer)
{
  auto object = weakPointer.Lock();
  object->Foo();
}

Observe events

You can get register a single callable for the deletie event. You can use another weak pointer if you need to have more than one hook or observe the wrapped object directly instead of using mitk::WeakPointer<T> as a proxy.