VTK  9.3.20240422
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
21VTK_ABI_NAMESPACE_BEGIN
22typedef boost::mpl::vector<vtkTypeUInt8, vtkTypeInt8, vtkTypeUInt16, vtkTypeInt16, vtkTypeUInt32,
23 vtkTypeInt32, vtkTypeUInt64, vtkTypeInt64, vtkIdType>
25// Description:
26// Enumerates all floating-point VTK types
27typedef boost::mpl::vector<vtkTypeFloat32, vtkTypeFloat64> vtkFloatingPointTypes;
28// Description:
29// Enumerates all numeric VTK types
30typedef boost::mpl::joint_view<vtkIntegerTypes, vtkFloatingPointTypes> vtkNumericTypes;
31// Description:
32// Enumerates all string VTK types
33typedef boost::mpl::vector<vtkStdString> vtkStringTypes;
34// Description:
35// Enumerates all VTK types
36typedef boost::mpl::joint_view<vtkNumericTypes, vtkStringTypes> vtkAllTypes;
37
38// End-users can ignore these, they're the guts of the beast ...
39template <template <typename> class TargetT, typename FunctorT>
41{
42public:
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
65 FunctorT Functor;
66 bool& Succeeded;
67
68private:
70};
71
72template <template <typename> class TargetT, typename FunctorT>
74{
75public:
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
101 FunctorT Functor;
103
104private:
106};
107
108template <template <typename> class TargetT, typename FunctorT>
110{
111public:
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
141 FunctorT Functor;
143
144private:
146};
147
148template <template <typename> class TargetT, typename TypesT, typename FunctorT>
149bool 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
157template <template <typename> class TargetT, typename TypesT, typename FunctorT>
158bool 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
166template <template <typename> class TargetT, typename TypesT, typename FunctorT>
167bool 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
175VTK_ABI_NAMESPACE_END
176#endif
177// VTK-HeaderTest-Exclude: vtkTryDowncast.h
abstract base class for most VTK objects
Definition vtkObject.h:162
vtkTryDowncastHelper1(vtkObject *source1, FunctorT functor, bool &succeeded)
void operator()(ValueT) const
void operator()(ValueT) const
vtkTryDowncastHelper2(vtkObject *source1, vtkObject *source2, FunctorT functor, bool &succeeded)
vtkTryDowncastHelper3(vtkObject *source1, vtkObject *source2, vtkObject *source3, FunctorT functor, bool &succeeded)
void operator()(ValueT) const
boost::mpl::joint_view< vtkIntegerTypes, vtkFloatingPointTypes > vtkNumericTypes
boost::mpl::joint_view< vtkNumericTypes, vtkStringTypes > vtkAllTypes
boost::mpl::vector< vtkStdString > vtkStringTypes
bool vtkTryDowncast(vtkObject *source1, FunctorT functor)
boost::mpl::vector< vtkTypeFloat32, vtkTypeFloat64 > vtkFloatingPointTypes
boost::mpl::vector< vtkTypeUInt8, vtkTypeInt8, vtkTypeUInt16, vtkTypeInt16, vtkTypeUInt32, vtkTypeInt32, vtkTypeUInt64, vtkTypeInt64, vtkIdType > vtkIntegerTypes
int vtkIdType
Definition vtkType.h:315