VTK
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, vtkTypeInt32, vtkTypeUInt64, vtkTypeInt64, vtkIdType> vtkIntegerTypes;
39 // Description:
40 // Enumerates all floating-point VTK types
41 typedef boost::mpl::vector<vtkTypeFloat32, vtkTypeFloat64> vtkFloatingPointTypes;
42 // Description:
43 // Enumerates all numeric VTK types
44 typedef boost::mpl::joint_view<vtkIntegerTypes, vtkFloatingPointTypes> vtkNumericTypes;
45 // Description:
46 // Enumerates all string VTK types
47 typedef boost::mpl::vector<vtkStdString, vtkUnicodeString> vtkStringTypes;
48 // Description:
49 // Enumerates all VTK types
50 typedef boost::mpl::joint_view<vtkNumericTypes, vtkStringTypes> vtkAllTypes;
51 
52 // End-users can ignore these, they're the guts of the beast ...
53 template<template <typename> class TargetT, typename FunctorT>
55 {
56 public:
57  vtkTryDowncastHelper1(vtkObject* source1, FunctorT functor, bool& succeeded) :
58  Source1(source1),
59  Functor(functor),
60  Succeeded(succeeded)
61  {
62  }
63 
64  template<typename ValueT>
65  void operator()(ValueT) const
66  {
67  if(Succeeded)
68  return;
69 
70  TargetT<ValueT>* const target1 = TargetT<ValueT>::SafeDownCast(Source1);
71  if(target1)
72  {
73  Succeeded = true;
74  this->Functor(target1);
75  }
76  }
77 
79  FunctorT Functor;
80  bool& Succeeded;
81 
82 private:
84 };
85 
86 template<template <typename> class TargetT, typename FunctorT>
88 {
89 public:
90  vtkTryDowncastHelper2(vtkObject* source1, vtkObject* source2, FunctorT functor, bool& succeeded) :
91  Source1(source1),
92  Source2(source2),
93  Functor(functor),
94  Succeeded(succeeded)
95  {
96  }
97 
98  template<typename ValueT>
99  void operator()(ValueT) const
100  {
101  if(Succeeded)
102  return;
103 
104  TargetT<ValueT>* const target1 = TargetT<ValueT>::SafeDownCast(Source1);
105  TargetT<ValueT>* const target2 = TargetT<ValueT>::SafeDownCast(Source2);
106  if(target1 && target2)
107  {
108  Succeeded = true;
109  this->Functor(target1, target2);
110  }
111  }
112 
115  FunctorT Functor;
116  bool& Succeeded;
117 
118 private:
119  vtkTryDowncastHelper2& operator=(const vtkTryDowncastHelper2&);
120 };
121 
122 template<template <typename> class TargetT, typename FunctorT>
124 {
125 public:
126  vtkTryDowncastHelper3(vtkObject* source1, vtkObject* source2, vtkObject* source3, FunctorT functor, bool& succeeded) :
127  Source1(source1),
128  Source2(source2),
129  Source3(source3),
130  Functor(functor),
131  Succeeded(succeeded)
132  {
133  }
134 
135  template<typename ValueT>
136  void operator()(ValueT) const
137  {
138  if(Succeeded)
139  return;
140 
141  TargetT<ValueT>* const target1 = TargetT<ValueT>::SafeDownCast(Source1);
142  TargetT<ValueT>* const target2 = TargetT<ValueT>::SafeDownCast(Source2);
143  TargetT<ValueT>* const target3 = TargetT<ValueT>::SafeDownCast(Source3);
144  if(target1 && target2 && target3)
145  {
146  Succeeded = true;
147  this->Functor(target1, target2, target3);
148  }
149  }
150 
154  FunctorT Functor;
155  bool& Succeeded;
156 
157 private:
158  vtkTryDowncastHelper3& operator=(const vtkTryDowncastHelper3&);
159 };
160 
161 template<template <typename> class TargetT, typename TypesT, typename FunctorT>
162 bool vtkTryDowncast(vtkObject* source1, FunctorT functor)
163 {
164  bool succeeded = false;
165  boost::mpl::for_each<TypesT>(vtkTryDowncastHelper1<TargetT, FunctorT>(source1, functor, succeeded));
166  return succeeded;
167 }
168 
169 template<template <typename> class TargetT, typename TypesT, typename FunctorT>
170 bool vtkTryDowncast(vtkObject* source1, vtkObject* source2, FunctorT functor)
171 {
172  bool succeeded = false;
173  boost::mpl::for_each<TypesT>(vtkTryDowncastHelper2<TargetT, FunctorT>(source1, source2, functor, succeeded));
174  return succeeded;
175 }
176 
177 
178 template<template <typename> class TargetT, typename TypesT, typename FunctorT>
179 bool vtkTryDowncast(vtkObject* source1, vtkObject* source2, vtkObject* source3, FunctorT functor)
180 {
181  bool succeeded = false;
182  boost::mpl::for_each<TypesT>(vtkTryDowncastHelper3<TargetT, FunctorT>(source1, source2, source3, functor, succeeded));
183  return succeeded;
184 }
185 
186 #endif
187 // VTK-HeaderTest-Exclude: vtkTryDowncast.h
boost::mpl::joint_view< vtkIntegerTypes, vtkFloatingPointTypes > vtkNumericTypes
vtkTryDowncastHelper2(vtkObject *source1, vtkObject *source2, FunctorT functor, bool &succeeded)
void operator()(ValueT) const
abstract base class for most VTK objects
Definition: vtkObject.h:59
vtkTryDowncastHelper3(vtkObject *source1, vtkObject *source2, vtkObject *source3, FunctorT functor, bool &succeeded)
boost::mpl::vector< vtkTypeUInt8, vtkTypeInt8, vtkTypeUInt16, vtkTypeInt16, vtkTypeUInt32, vtkTypeInt32, vtkTypeUInt64, vtkTypeInt64, vtkIdType > vtkIntegerTypes
vtkTryDowncastHelper1(vtkObject *source1, FunctorT functor, bool &succeeded)
boost::mpl::joint_view< vtkNumericTypes, vtkStringTypes > vtkAllTypes
bool vtkTryDowncast(vtkObject *source1, FunctorT functor)
boost::mpl::vector< vtkTypeFloat32, vtkTypeFloat64 > vtkFloatingPointTypes
void operator()(ValueT) const
void operator()(ValueT) const
boost::mpl::vector< vtkStdString, vtkUnicodeString > vtkStringTypes