Feature: vtkObject inheritance without instantiation

Summary

The vtkObject class which serves as the basis for nearly all of VTK provides nice run-time methods like GetClassName() and IsA() to query objects for their type. These are implemented using the Superclass type-alias defined by vtkTypeMacro(). However, most of the methods are virtual overrides, making it hard to examine types without an instance of the object in question.

There are now some templated functions you can use to get at the same information without having an instance of a class, discussed the next section.

Details

VTK now provides these free functions, templated on an object type to give you access to type information. They are all in the vtk namespace.

  • vtk::TypeName<Object>() returns a std::string holding the name of the Object template-parameter. This is defined in Common/Core/TypeName.h. It works via the semi-standard abi::__cxa_demangle() call that most C++ compilers (including gcc, clang, icc, and msvc) provide.
  • vtk::TypeToken<Object>() returns a vtkStringToken holding an integer hash of the typename.
    Significant work is required to get uniform names across platforms, so it is unrealistic for this function to be constexpr until VTK allows c++14 and c++17 features.
  • vtk::Inherits<VTKObject>(Container&) populates an STL container you pass (e.g., a vector or set) with all the typenames of an object by traversing the Superclass type-aliases. Because this function requires the type alias to be present, it generally only works on vtkObject and its subclasses.

See TestInherits for examples of their use.

2 Likes