VTK  9.1.0
vtkTryDowncast.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkTryDowncast.h
5
6-------------------------------------------------------------------------
7 Copyright 2008 Sandia Corporation.
8 Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 the U.S. Government retains certain rights in this software.
10-------------------------------------------------------------------------
11
12 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
13 All rights reserved.
14 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
15
16 This software is distributed WITHOUT ANY WARRANTY; without even
17 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 PURPOSE. See the above copyright notice for more information.
19
20=========================================================================*/
21
22#ifndef vtkTryDowncast_h
23#define vtkTryDowncast_h
24
25#include "vtkDenseArray.h"
26#include "vtkSmartPointer.h"
27#include "vtkSparseArray.h"
28
29#include <boost/mpl/for_each.hpp>
30#include <boost/mpl/joint_view.hpp>
31#include <boost/mpl/vector.hpp>
32
33// These are lists of standard VTK types. End-users will have to choose these when they implement
34// their algorithms.
35
36// Description:
37// Enumerates all integer VTK types
38typedef boost::mpl::vector<vtkTypeUInt8, vtkTypeInt8, vtkTypeUInt16, vtkTypeInt16, vtkTypeUInt32,
39 vtkTypeInt32, vtkTypeUInt64, vtkTypeInt64, vtkIdType>
41// Description:
42// Enumerates all floating-point VTK types
43typedef boost::mpl::vector<vtkTypeFloat32, vtkTypeFloat64> vtkFloatingPointTypes;
44// Description:
45// Enumerates all numeric VTK types
46typedef boost::mpl::joint_view<vtkIntegerTypes, vtkFloatingPointTypes> vtkNumericTypes;
47// Description:
48// Enumerates all string VTK types
49typedef boost::mpl::vector<vtkStdString, vtkUnicodeString> vtkStringTypes;
50// Description:
51// Enumerates all VTK types
52typedef boost::mpl::joint_view<vtkNumericTypes, vtkStringTypes> vtkAllTypes;
53
54// End-users can ignore these, they're the guts of the beast ...
55template <template <typename> class TargetT, typename FunctorT>
57{
58public:
59 vtkTryDowncastHelper1(vtkObject* source1, FunctorT functor, bool& succeeded)
60 : Source1(source1)
61 , Functor(functor)
62 , Succeeded(succeeded)
63 {
64 }
65
66 template <typename ValueT>
67 void operator()(ValueT) const
68 {
69 if (Succeeded)
70 return;
71
72 TargetT<ValueT>* const target1 = TargetT<ValueT>::SafeDownCast(Source1);
73 if (target1)
74 {
75 Succeeded = true;
76 this->Functor(target1);
77 }
78 }
79
81 FunctorT Functor;
82 bool& Succeeded;
83
84private:
86};
87
88template <template <typename> class TargetT, typename FunctorT>
90{
91public:
92 vtkTryDowncastHelper2(vtkObject* source1, vtkObject* source2, FunctorT functor, bool& succeeded)
93 : Source1(source1)
94 , Source2(source2)
95 , Functor(functor)
96 , Succeeded(succeeded)
97 {
98 }
99
100 template <typename ValueT>
101 void operator()(ValueT) const
102 {
103 if (Succeeded)
104 return;
105
106 TargetT<ValueT>* const target1 = TargetT<ValueT>::SafeDownCast(Source1);
107 TargetT<ValueT>* const target2 = TargetT<ValueT>::SafeDownCast(Source2);
108 if (target1 && target2)
109 {
110 Succeeded = true;
111 this->Functor(target1, target2);
112 }
113 }
114
117 FunctorT Functor;
119
120private:
122};
123
124template <template <typename> class TargetT, typename FunctorT>
126{
127public:
129 vtkObject* source1, vtkObject* source2, vtkObject* source3, FunctorT functor, bool& succeeded)
130 : Source1(source1)
131 , Source2(source2)
132 , Source3(source3)
133 , Functor(functor)
134 , Succeeded(succeeded)
135 {
136 }
137
138 template <typename ValueT>
139 void operator()(ValueT) const
140 {
141 if (Succeeded)
142 return;
143
144 TargetT<ValueT>* const target1 = TargetT<ValueT>::SafeDownCast(Source1);
145 TargetT<ValueT>* const target2 = TargetT<ValueT>::SafeDownCast(Source2);
146 TargetT<ValueT>* const target3 = TargetT<ValueT>::SafeDownCast(Source3);
147 if (target1 && target2 && target3)
148 {
149 Succeeded = true;
150 this->Functor(target1, target2, target3);
151 }
152 }
153
157 FunctorT Functor;
159
160private:
162};
163
164template <template <typename> class TargetT, typename TypesT, typename FunctorT>
165bool vtkTryDowncast(vtkObject* source1, FunctorT functor)
166{
167 bool succeeded = false;
168 boost::mpl::for_each<TypesT>(
169 vtkTryDowncastHelper1<TargetT, FunctorT>(source1, functor, succeeded));
170 return succeeded;
171}
172
173template <template <typename> class TargetT, typename TypesT, typename FunctorT>
174bool vtkTryDowncast(vtkObject* source1, vtkObject* source2, FunctorT functor)
175{
176 bool succeeded = false;
177 boost::mpl::for_each<TypesT>(
178 vtkTryDowncastHelper2<TargetT, FunctorT>(source1, source2, functor, succeeded));
179 return succeeded;
180}
181
182template <template <typename> class TargetT, typename TypesT, typename FunctorT>
183bool vtkTryDowncast(vtkObject* source1, vtkObject* source2, vtkObject* source3, FunctorT functor)
184{
185 bool succeeded = false;
186 boost::mpl::for_each<TypesT>(
187 vtkTryDowncastHelper3<TargetT, FunctorT>(source1, source2, source3, functor, succeeded));
188 return succeeded;
189}
190
191#endif
192// VTK-HeaderTest-Exclude: vtkTryDowncast.h
abstract base class for most VTK objects
Definition: vtkObject.h:82
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, vtkUnicodeString > 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:332