VTK: Infovis/Boost/vtkTryDowncast.h Source File

| VTK 9.3.1

vtkTryDowncast.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-FileCopyrightText: Copyright 2008 Sandia Corporation
3 // SPDX-License-Identifier: LicenseRef-BSD-3-Clause-Sandia-USGov
4
5 #ifndef vtkTryDowncast_h
6 #define vtkTryDowncast_h
7
8 #include "vtkDenseArray.h"
9 #include "vtkSmartPointer.h"
10 #include "vtkSparseArray.h"
11
12 #include <boost/mpl/for_each.hpp>
13 #include <boost/mpl/joint_view.hpp>
14 #include <boost/mpl/vector.hpp>
15
16 // These are lists of standard VTK types. End-users will have to choose these when they implement
17 // their algorithms.
18
19 // Description:
20 // Enumerates all integer VTK types
21 VTK_ABI_NAMESPACE_BEGIN
22 typedef boost::mpl::vector<vtkTypeUInt8, vtkTypeInt8, vtkTypeUInt16, vtkTypeInt16, vtkTypeUInt32,
23 vtkTypeInt32, vtkTypeUInt64, vtkTypeInt64, vtkIdType>
24 vtkIntegerTypes;
25 // Description:
26 // Enumerates all floating-point VTK types
27 typedef boost::mpl::vector<vtkTypeFloat32, vtkTypeFloat64> vtkFloatingPointTypes;
28 // Description:
29 // Enumerates all numeric VTK types
30 typedef boost::mpl::joint_view<vtkIntegerTypes, vtkFloatingPointTypes> vtkNumericTypes;
31 // Description:
32 // Enumerates all string VTK types
33 typedef boost::mpl::vector<vtkStdString> vtkStringTypes;
34 // Description:
35 // Enumerates all VTK types
36 typedef boost::mpl::joint_view<vtkNumericTypes, vtkStringTypes> vtkAllTypes;
37
38 // End-users can ignore these, they're the guts of the beast ...
39 template <template <typename> class TargetT, typename FunctorT>
40 class vtkTryDowncastHelper1
41 {
42 public:
43 vtkTryDowncastHelper1(vtkObject* source1, FunctorT functor, bool& succeeded)
44 : Source1(source1)
45 , Functor(functor)
46 , Succeeded(succeeded)
47 {
48 }
49
50 template <typename ValueT>
51 void operator()(ValueT) const
52 {
53 if (Succeeded)
54 return;
55
56 TargetT<ValueT>* const target1 = TargetT<ValueT>::SafeDownCast(Source1);
57 if (target1)
58 {
59 Succeeded = true;
60 this->Functor(target1);
61 }
62 }
63
64 vtkObject* Source1;
65 FunctorT Functor;
66 bool& Succeeded;
67
68 private:
69 vtkTryDowncastHelper1& operator=(const vtkTryDowncastHelper1&);
70 };
71
72 template <template <typename> class TargetT, typename FunctorT>
73 class vtkTryDowncastHelper2
74 {
75 public:
76 vtkTryDowncastHelper2(vtkObject* source1, vtkObject* source2, FunctorT functor, bool& succeeded)
77 : Source1(source1)
78 , Source2(source2)
79 , Functor(functor)
80 , Succeeded(succeeded)
81 {
82 }
83
84 template <typename ValueT>
85 void operator()(ValueT) const
86 {
87 if (Succeeded)
88 return;
89
90 TargetT<ValueT>* const target1 = TargetT<ValueT>::SafeDownCast(Source1);
91 TargetT<ValueT>* const target2 = TargetT<ValueT>::SafeDownCast(Source2);
92 if (target1 && target2)
93 {
94 Succeeded = true;
95 this->Functor(target1, target2);
96 }
97 }
98
99 vtkObject* Source1;
100 vtkObject* Source2;
101 FunctorT Functor;
102 bool& Succeeded;
103
104 private:
105 vtkTryDowncastHelper2& operator=(const vtkTryDowncastHelper2&);
106 };
107
108 template <template <typename> class TargetT, typename FunctorT>
109 class vtkTryDowncastHelper3
110 {
111 public:
112 vtkTryDowncastHelper3(
113 vtkObject* source1, vtkObject* source2, vtkObject* source3, FunctorT functor, bool& succeeded)
114 : Source1(source1)
115 , Source2(source2)
116 , Source3(source3)
117 , Functor(functor)
118 , Succeeded(succeeded)
119 {
120 }
121
122 template <typename ValueT>
123 void operator()(ValueT) const
124 {
125 if (Succeeded)
126 return;
127
128 TargetT<ValueT>* const target1 = TargetT<ValueT>::SafeDownCast(Source1);
129 TargetT<ValueT>* const target2 = TargetT<ValueT>::SafeDownCast(Source2);
130 TargetT<ValueT>* const target3 = TargetT<ValueT>::SafeDownCast(Source3);
131 if (target1 && target2 && target3)
132 {
133 Succeeded = true;
134 this->Functor(target1, target2, target3);
135 }
136 }
137
138 vtkObject* Source1;
139 vtkObject* Source2;
140 vtkObject* Source3;
141 FunctorT Functor;
142 bool& Succeeded;
143
144 private:
145 vtkTryDowncastHelper3& operator=(const vtkTryDowncastHelper3&);
146 };
147
148 template <template <typename> class TargetT, typename TypesT, typename FunctorT>
149 bool vtkTryDowncast(vtkObject* source1, FunctorT functor)
150 {
151 bool succeeded = false;
152 boost::mpl::for_each<TypesT>(
153 vtkTryDowncastHelper1<TargetT, FunctorT>(source1, functor, succeeded));
154 return succeeded;
155 }
156
157 template <template <typename> class TargetT, typename TypesT, typename FunctorT>
158 bool vtkTryDowncast(vtkObject* source1, vtkObject* source2, FunctorT functor)
159 {
160 bool succeeded = false;
161 boost::mpl::for_each<TypesT>(
162 vtkTryDowncastHelper2<TargetT, FunctorT>(source1, source2, functor, succeeded));
163 return succeeded;
164 }
165
166 template <template <typename> class TargetT, typename TypesT, typename FunctorT>
167 bool vtkTryDowncast(vtkObject* source1, vtkObject* source2, vtkObject* source3, FunctorT functor)
168 {
169 bool succeeded = false;
170 boost::mpl::for_each<TypesT>(
171 vtkTryDowncastHelper3<TargetT, FunctorT>(source1, source2, source3, functor, succeeded));
172 return succeeded;
173 }
174
175 VTK_ABI_NAMESPACE_END
176 #endif
177 // VTK-HeaderTest-Exclude: vtkTryDowncast.h
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:61
vtkTryDowncastHelper1
Definition: vtkTryDowncast.h:41
vtkTryDowncastHelper1::vtkTryDowncastHelper1
vtkTryDowncastHelper1(vtkObject *source1, FunctorT functor, bool &succeeded)
Definition: vtkTryDowncast.h:43
vtkTryDowncastHelper1::Functor
FunctorT Functor
Definition: vtkTryDowncast.h:65
vtkTryDowncastHelper1::Succeeded
bool & Succeeded
Definition: vtkTryDowncast.h:66
vtkTryDowncastHelper1::operator()
void operator()(ValueT) const
Definition: vtkTryDowncast.h:51
vtkTryDowncastHelper1::Source1
vtkObject * Source1
Definition: vtkTryDowncast.h:64
vtkTryDowncastHelper2
Definition: vtkTryDowncast.h:74
vtkTryDowncastHelper2::operator()
void operator()(ValueT) const
Definition: vtkTryDowncast.h:85
vtkTryDowncastHelper2::vtkTryDowncastHelper2
vtkTryDowncastHelper2(vtkObject *source1, vtkObject *source2, FunctorT functor, bool &succeeded)
Definition: vtkTryDowncast.h:76
vtkTryDowncastHelper2::Functor
FunctorT Functor
Definition: vtkTryDowncast.h:101
vtkTryDowncastHelper2::Succeeded
bool & Succeeded
Definition: vtkTryDowncast.h:102
vtkTryDowncastHelper2::Source2
vtkObject * Source2
Definition: vtkTryDowncast.h:100
vtkTryDowncastHelper2::Source1
vtkObject * Source1
Definition: vtkTryDowncast.h:99
vtkTryDowncastHelper3
Definition: vtkTryDowncast.h:110
vtkTryDowncastHelper3::Source2
vtkObject * Source2
Definition: vtkTryDowncast.h:139
vtkTryDowncastHelper3::Source3
vtkObject * Source3
Definition: vtkTryDowncast.h:140
vtkTryDowncastHelper3::Functor
FunctorT Functor
Definition: vtkTryDowncast.h:141
vtkTryDowncastHelper3::Succeeded
bool & Succeeded
Definition: vtkTryDowncast.h:142
vtkTryDowncastHelper3::vtkTryDowncastHelper3
vtkTryDowncastHelper3(vtkObject *source1, vtkObject *source2, vtkObject *source3, FunctorT functor, bool &succeeded)
Definition: vtkTryDowncast.h:112
vtkTryDowncastHelper3::operator()
void operator()(ValueT) const
Definition: vtkTryDowncast.h:123
vtkTryDowncastHelper3::Source1
vtkObject * Source1
Definition: vtkTryDowncast.h:138
vtkX3D::vector
@ vector
Definition: vtkX3D.h:237
vtkDenseArray.h
vtkSmartPointer.h
vtkSparseArray.h
vtkNumericTypes
boost::mpl::joint_view< vtkIntegerTypes, vtkFloatingPointTypes > vtkNumericTypes
Definition: vtkTryDowncast.h:30
vtkAllTypes
boost::mpl::joint_view< vtkNumericTypes, vtkStringTypes > vtkAllTypes
Definition: vtkTryDowncast.h:36
vtkStringTypes
boost::mpl::vector< vtkStdString > vtkStringTypes
Definition: vtkTryDowncast.h:33
vtkTryDowncast
bool vtkTryDowncast(vtkObject *source1, FunctorT functor)
Definition: vtkTryDowncast.h:149
vtkFloatingPointTypes
boost::mpl::vector< vtkTypeFloat32, vtkTypeFloat64 > vtkFloatingPointTypes
Definition: vtkTryDowncast.h:27
vtkIntegerTypes
boost::mpl::vector< vtkTypeUInt8, vtkTypeInt8, vtkTypeUInt16, vtkTypeInt16, vtkTypeUInt32, vtkTypeInt32, vtkTypeUInt64, vtkTypeInt64, vtkIdType > vtkIntegerTypes
Definition: vtkTryDowncast.h:24
vtkIdType
int vtkIdType
Definition: vtkType.h:315