VTK  9.0.1
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
38 typedef boost::mpl::vector<vtkTypeUInt8, vtkTypeInt8, vtkTypeUInt16, vtkTypeInt16, vtkTypeUInt32,
39  vtkTypeInt32, vtkTypeUInt64, vtkTypeInt64, vtkIdType>
41 // Description:
42 // Enumerates all floating-point VTK types
43 typedef boost::mpl::vector<vtkTypeFloat32, vtkTypeFloat64> vtkFloatingPointTypes;
44 // Description:
45 // Enumerates all numeric VTK types
46 typedef boost::mpl::joint_view<vtkIntegerTypes, vtkFloatingPointTypes> vtkNumericTypes;
47 // Description:
48 // Enumerates all string VTK types
49 typedef boost::mpl::vector<vtkStdString, vtkUnicodeString> vtkStringTypes;
50 // Description:
51 // Enumerates all VTK types
52 typedef boost::mpl::joint_view<vtkNumericTypes, vtkStringTypes> vtkAllTypes;
53 
54 // End-users can ignore these, they're the guts of the beast ...
55 template <template <typename> class TargetT, typename FunctorT>
57 {
58 public:
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 
84 private:
86 };
87 
88 template <template <typename> class TargetT, typename FunctorT>
90 {
91 public:
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;
118  bool& Succeeded;
119 
120 private:
121  vtkTryDowncastHelper2& operator=(const vtkTryDowncastHelper2&);
122 };
123 
124 template <template <typename> class TargetT, typename FunctorT>
126 {
127 public:
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;
158  bool& Succeeded;
159 
160 private:
161  vtkTryDowncastHelper3& operator=(const vtkTryDowncastHelper3&);
162 };
163 
164 template <template <typename> class TargetT, typename TypesT, typename FunctorT>
165 bool 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 
173 template <template <typename> class TargetT, typename TypesT, typename FunctorT>
174 bool 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 
182 template <template <typename> class TargetT, typename TypesT, typename FunctorT>
183 bool 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
vtkTryDowncastHelper2::Functor
FunctorT Functor
Definition: vtkTryDowncast.h:117
vtkTryDowncastHelper1::vtkTryDowncastHelper1
vtkTryDowncastHelper1(vtkObject *source1, FunctorT functor, bool &succeeded)
Definition: vtkTryDowncast.h:59
vtkX3D::vector
@ vector
Definition: vtkX3D.h:243
vtkTryDowncastHelper1::Functor
FunctorT Functor
Definition: vtkTryDowncast.h:81
vtkTryDowncastHelper2::vtkTryDowncastHelper2
vtkTryDowncastHelper2(vtkObject *source1, vtkObject *source2, FunctorT functor, bool &succeeded)
Definition: vtkTryDowncast.h:92
vtkSparseArray.h
vtkIdType
int vtkIdType
Definition: vtkType.h:338
vtkTryDowncastHelper2::Source2
vtkObject * Source2
Definition: vtkTryDowncast.h:116
vtkTryDowncastHelper3::Source3
vtkObject * Source3
Definition: vtkTryDowncast.h:156
vtkTryDowncast
bool vtkTryDowncast(vtkObject *source1, FunctorT functor)
Definition: vtkTryDowncast.h:165
vtkTryDowncastHelper3
Definition: vtkTryDowncast.h:125
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:62
vtkTryDowncastHelper3::Functor
FunctorT Functor
Definition: vtkTryDowncast.h:157
vtkTryDowncastHelper3::vtkTryDowncastHelper3
vtkTryDowncastHelper3(vtkObject *source1, vtkObject *source2, vtkObject *source3, FunctorT functor, bool &succeeded)
Definition: vtkTryDowncast.h:128
vtkStringTypes
boost::mpl::vector< vtkStdString, vtkUnicodeString > vtkStringTypes
Definition: vtkTryDowncast.h:49
vtkTryDowncastHelper3::Source2
vtkObject * Source2
Definition: vtkTryDowncast.h:155
vtkTryDowncastHelper1::Source1
vtkObject * Source1
Definition: vtkTryDowncast.h:80
vtkNumericTypes
boost::mpl::joint_view< vtkIntegerTypes, vtkFloatingPointTypes > vtkNumericTypes
Definition: vtkTryDowncast.h:46
vtkTryDowncastHelper2::Source1
vtkObject * Source1
Definition: vtkTryDowncast.h:115
vtkIntegerTypes
boost::mpl::vector< vtkTypeUInt8, vtkTypeInt8, vtkTypeUInt16, vtkTypeInt16, vtkTypeUInt32, vtkTypeInt32, vtkTypeUInt64, vtkTypeInt64, vtkIdType > vtkIntegerTypes
Definition: vtkTryDowncast.h:40
vtkDenseArray.h
vtkTryDowncastHelper2::operator()
void operator()(ValueT) const
Definition: vtkTryDowncast.h:101
vtkAllTypes
boost::mpl::joint_view< vtkNumericTypes, vtkStringTypes > vtkAllTypes
Definition: vtkTryDowncast.h:52
vtkFloatingPointTypes
boost::mpl::vector< vtkTypeFloat32, vtkTypeFloat64 > vtkFloatingPointTypes
Definition: vtkTryDowncast.h:43
vtkSmartPointer.h
vtkTryDowncastHelper2
Definition: vtkTryDowncast.h:89
vtkTryDowncastHelper2::Succeeded
bool & Succeeded
Definition: vtkTryDowncast.h:118
vtkTryDowncastHelper1::operator()
void operator()(ValueT) const
Definition: vtkTryDowncast.h:67
vtkTryDowncastHelper3::Succeeded
bool & Succeeded
Definition: vtkTryDowncast.h:158
vtkTryDowncastHelper1::Succeeded
bool & Succeeded
Definition: vtkTryDowncast.h:82
vtkTryDowncastHelper3::Source1
vtkObject * Source1
Definition: vtkTryDowncast.h:154
vtkTryDowncastHelper3::operator()
void operator()(ValueT) const
Definition: vtkTryDowncast.h:139
vtkTryDowncastHelper1
Definition: vtkTryDowncast.h:56