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