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 #include "vtkDenseArray.h"
23 #include "vtkSmartPointer.h"
24 #include "vtkSparseArray.h"
25 
26 #include <boost/mpl/for_each.hpp>
27 #include <boost/mpl/joint_view.hpp>
28 #include <boost/mpl/vector.hpp>
29 
30 // These are lists of standard VTK types. End-users will have to choose these when they implement
31 // their algorithms.
32 
33 // Description:
34 // Enumerates all integer VTK types
35 typedef boost::mpl::vector<vtkTypeUInt8, vtkTypeInt8, vtkTypeUInt16, vtkTypeInt16, vtkTypeUInt32, vtkTypeInt32, vtkTypeUInt64, vtkTypeInt64, vtkIdType> vtkIntegerTypes;
36 // Description:
37 // Enumerates all floating-point VTK types
38 typedef boost::mpl::vector<vtkTypeFloat32, vtkTypeFloat64> vtkFloatingPointTypes;
39 // Description:
40 // Enumerates all numeric VTK types
41 typedef boost::mpl::joint_view<vtkIntegerTypes, vtkFloatingPointTypes> vtkNumericTypes;
42 // Description:
43 // Enumerates all string VTK types
44 typedef boost::mpl::vector<vtkStdString, vtkUnicodeString> vtkStringTypes;
45 // Description:
46 // Enumerates all VTK types
47 typedef boost::mpl::joint_view<vtkNumericTypes, vtkStringTypes> vtkAllTypes;
48 
49 // End-users can ignore these, they're the guts of the beast ...
50 template<template <typename> class TargetT, typename FunctorT>
52 {
53 public:
54  vtkTryDowncastHelper1(vtkObject* source1, FunctorT functor, bool& succeeded) :
55  Source1(source1),
56  Functor(functor),
57  Succeeded(succeeded)
58  {
59  }
60 
61  template<typename ValueT>
62  void operator()(ValueT) const
63  {
64  if(Succeeded)
65  return;
66 
67  TargetT<ValueT>* const target1 = TargetT<ValueT>::SafeDownCast(Source1);
68  if(target1)
69  {
70  Succeeded = true;
71  this->Functor(target1);
72  }
73  }
74 
76  FunctorT Functor;
77  bool& Succeeded;
78 
79 private:
81 };
82 
83 template<template <typename> class TargetT, typename FunctorT>
85 {
86 public:
87  vtkTryDowncastHelper2(vtkObject* source1, vtkObject* source2, FunctorT functor, bool& succeeded) :
88  Source1(source1),
89  Source2(source2),
90  Functor(functor),
91  Succeeded(succeeded)
92  {
93  }
94 
95  template<typename ValueT>
96  void operator()(ValueT) const
97  {
98  if(Succeeded)
99  return;
100 
101  TargetT<ValueT>* const target1 = TargetT<ValueT>::SafeDownCast(Source1);
102  TargetT<ValueT>* const target2 = TargetT<ValueT>::SafeDownCast(Source2);
103  if(target1 && target2)
104  {
105  Succeeded = true;
106  this->Functor(target1, target2);
107  }
108  }
109 
112  FunctorT Functor;
113  bool& Succeeded;
114 
115 private:
116  vtkTryDowncastHelper2& operator=(const vtkTryDowncastHelper2&);
117 };
118 
119 template<template <typename> class TargetT, typename FunctorT>
121 {
122 public:
123  vtkTryDowncastHelper3(vtkObject* source1, vtkObject* source2, vtkObject* source3, FunctorT functor, bool& succeeded) :
124  Source1(source1),
125  Source2(source2),
126  Source3(source3),
127  Functor(functor),
128  Succeeded(succeeded)
129  {
130  }
131 
132  template<typename ValueT>
133  void operator()(ValueT) const
134  {
135  if(Succeeded)
136  return;
137 
138  TargetT<ValueT>* const target1 = TargetT<ValueT>::SafeDownCast(Source1);
139  TargetT<ValueT>* const target2 = TargetT<ValueT>::SafeDownCast(Source2);
140  TargetT<ValueT>* const target3 = TargetT<ValueT>::SafeDownCast(Source3);
141  if(target1 && target2 && target3)
142  {
143  Succeeded = true;
144  this->Functor(target1, target2, target3);
145  }
146  }
147 
151  FunctorT Functor;
152  bool& Succeeded;
153 
154 private:
155  vtkTryDowncastHelper3& operator=(const vtkTryDowncastHelper3&);
156 };
157 
158 template<template <typename> class TargetT, typename TypesT, typename FunctorT>
159 bool vtkTryDowncast(vtkObject* source1, FunctorT functor)
160 {
161  bool succeeded = false;
162  boost::mpl::for_each<TypesT>(vtkTryDowncastHelper1<TargetT, FunctorT>(source1, functor, succeeded));
163  return succeeded;
164 }
165 
166 template<template <typename> class TargetT, typename TypesT, typename FunctorT>
167 bool vtkTryDowncast(vtkObject* source1, vtkObject* source2, FunctorT functor)
168 {
169  bool succeeded = false;
170  boost::mpl::for_each<TypesT>(vtkTryDowncastHelper2<TargetT, FunctorT>(source1, source2, functor, succeeded));
171  return succeeded;
172 }
173 
174 
175 template<template <typename> class TargetT, typename TypesT, typename FunctorT>
176 bool vtkTryDowncast(vtkObject* source1, vtkObject* source2, vtkObject* source3, FunctorT functor)
177 {
178  bool succeeded = false;
179  boost::mpl::for_each<TypesT>(vtkTryDowncastHelper3<TargetT, FunctorT>(source1, source2, source3, functor, succeeded));
180  return succeeded;
181 }
182 
183 // 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:61
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