VTK: Common/Core/vtkInherits.h Source File

| VTK 9.3.1

vtkInherits.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3
4 #ifndef vtkInherits_h
5 #define vtkInherits_h
6
7 #include "vtkTypeName.h"
8
9 #include <string>
10
11 namespace vtk
12 {
13 namespace detail
14 {
15 VTK_ABI_NAMESPACE_BEGIN
16
17 // Used by Inherits with ParentClasses to produce a list of inherited type names.
18 template <typename Container, typename StopAtType = void>
19 struct AddNames
20 {
21 AddNames(Container& c)
22 : m_container(c)
23 {
24 }
25
26 template <typename T>
27 bool operator()()
28 {
29 if (!std::is_same<StopAtType, T>::value)
30 {
31 std::string typeName = vtk::TypeName<T>();
32 m_container.insert(m_container.end(), typeName);
33 return true;
34 }
35 return false;
36 }
37 Container& m_container;
38 };
39
40 VTK_ABI_NAMESPACE_END
41 } // namespace detail
42
43 VTK_ABI_NAMESPACE_BEGIN
45
53 template <typename VTKObjectType>
54 class HasSuperclass
55 {
56 class No
57 {
58 };
59 class Yes
60 {
61 No no[2];
62 };
63
64 template <typename C>
65 static Yes Test(typename C::Superclass*);
66 template <typename C>
67 static No Test(...);
68
69 public:
70 enum
71 {
72 value = sizeof(Test<VTKObjectType>(nullptr)) == sizeof(Yes)
73 };
74 };
76
78
94 template <typename VTKObjectType, bool IsDerived = HasSuperclass<VTKObjectType>::value>
95 struct ParentClasses;
96
97 template <typename VTKObjectType>
98 struct ParentClasses<VTKObjectType, false>
99 {
100 template <typename Functor>
101 inline static void enumerate(Functor& ff)
102 {
103 ff.template operator()<VTKObjectType>();
104 }
105 };
106
107 template <typename VTKObjectType>
108 struct ParentClasses<VTKObjectType, true>
109 {
110 // This variant handles Functors with a void return type.
111 template <typename Functor>
112 inline static typename std::enable_if<
113 std::is_same<decltype(std::declval<Functor>().template operator()<vtkObject>()), void>::value,
114 void>::type
115 enumerate(Functor& ff)
116 {
117 ff.template operator()<VTKObjectType>();
118 ParentClasses<typename VTKObjectType::Superclass>::enumerate(ff);
119 }
120
121 // This variant handles Functors with a bool return type.
122 template <typename Functor>
123 inline static typename std::enable_if<
124 std::is_same<decltype(std::declval<Functor>().template operator()<vtkObject>()), bool>::value,
125 void>::type
126 enumerate(Functor& ff)
127 {
128 if (ff.template operator()<VTKObjectType>())
129 {
130 ParentClasses<typename VTKObjectType::Superclass>::enumerate(ff);
131 }
132 }
133 };
135
137
148 template <typename VTKObjectType, typename Container>
149 void Inherits(Container& container)
150 {
151 detail::AddNames<Container> addNames(container);
152 ParentClasses<VTKObjectType>::enumerate(addNames);
153 }
154
155 template <typename VTKObjectType, typename StopAtType, typename Container>
156 void Inherits(Container& container)
157 {
158 detail::AddNames<Container, StopAtType> addNames(container);
159 ParentClasses<VTKObjectType>::enumerate(addNames);
160 }
162
163 VTK_ABI_NAMESPACE_END
164 } // namespace vtk
165
166 #endif // vtkInherits_h
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:61
vtk::HasSuperclass
Determine whether the provided class (VTKObjectType ) has a parent class.
Definition: vtkInherits.h:55
vtk::HasSuperclass::value
@ value
Definition: vtkInherits.h:72
detail
Definition: vtkCellTreeLocator.h:59
vtkX3D::value
@ value
Definition: vtkX3D.h:220
vtkX3D::type
@ type
Definition: vtkX3D.h:516
vtkX3D::string
@ string
Definition: vtkX3D.h:490
vtk
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
Definition: vtkSMPThreadLocalAPI.h:30
vtk::Inherits
void Inherits(Container &container)
Populate the container with the name of this class and its ancestors.
Definition: vtkInherits.h:149
vtk::ParentClasses< VTKObjectType, false >::enumerate
static void enumerate(Functor &ff)
Definition: vtkInherits.h:101
vtk::ParentClasses< VTKObjectType, true >::enumerate
static std::enable_if< std::is_same< decltype(std::declval< Functor >).template operator()<vtkObject >)), void >::value, void >::type enumerate(Functor &ff)
Definition: vtkInherits.h:115
vtk::ParentClasses< VTKObjectType, true >::enumerate
static std::enable_if< std::is_same< decltype(std::declval< Functor >).template operator()<vtkObject >)), bool >::value, void >::type enumerate(Functor &ff)
Definition: vtkInherits.h:126
vtk::ParentClasses
Invoke a functor on the named type and each of its parent types.
Definition: vtkInherits.h:95
vtk::detail::AddNames
Definition: vtkInherits.h:20
vtk::detail::AddNames::operator()
bool operator()()
Definition: vtkInherits.h:27
vtk::detail::AddNames::m_container
Container & m_container
Definition: vtkInherits.h:37
vtk::detail::AddNames::AddNames
AddNames(Container &c)
Definition: vtkInherits.h:21
vtkTypeName.h