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 astd::string
holding the name of theObject
template-parameter. This is defined inCommon/Core/TypeName.h
. It works via the semi-standardabi::__cxa_demangle()
call that most C++ compilers (including gcc, clang, icc, and msvc) provide. -
vtk::TypeToken<Object>()
returns avtkStringToken
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 beconstexpr
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 theSuperclass
type-aliases. Because this function requires the type alias to be present, it generally only works onvtkObject
and its subclasses.
See TestInherits for examples of their use.