forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkDataArrayDispatcher.h
177 lines (150 loc) · 5.37 KB
/
vtkDataArrayDispatcher.h
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkDataArrayDispatcher.h
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.
=========================================================================*/
/**
* @class vtkDataArrayDispatcher
* @brief Dispatch to functor vtkDataArrayType
*
* vtkDataArrayDispatcher is a class that allows calling a functor based
* on the data type of the vtkDataArray subclass. This is a wrapper
* around the vtkTemplateMacro (VTK_TT) to allow easier implementation and
* readability, while at the same time the ability to use statefull functors.
*
* Note: By default the return type is void.
* Note: The functor parameter must be of type vtkDataArrayDispatcherPointer
*
* The functors that are passed around can contain state, and are allowed
* to be const or non const. If you are using a functor that does have state,
* make sure your copy constructor is correct.
*
* \code
* struct sizeOfFunctor{
* template<typename T>
* int operator()(const vtkDataArrayDispatcherPointer<T>& t) const
* {
* return t.NumberOfComponents * t.NumberOfTuples;
* }
* };
*
* Here is an example of using the dispatcher.
* \code
* vtkDataArrayDispatcher<sizeOfFunctor,int> dispatcher;
* int arrayLength = dispatcher.Go(vtkDataArrayPtr);
* \endcode
*
*
* @sa
* vtkDispatcher vtkDoubleDispatcher
*/
#ifndef vtkDataArrayDispatcher_h
#define vtkDataArrayDispatcher_h
#include "vtkType.h" //Required for vtkIdType
#include "vtkDataArray.h" //required for constructor of the vtkDataArrayFunctor
#include <map> //Required for the storage of template params to runtime params
////////////////////////////////////////////////////////////////////////////////
// Object that is passed to all functor that are used with this class
// This allows the user the ability to find info about the size
////////////////////////////////////////////////////////////////////////////////
template<typename T>
struct vtkDataArrayDispatcherPointer
{
typedef T ValueType;
vtkIdType NumberOfTuples;
vtkIdType NumberOfComponents;
ValueType* RawPointer;
explicit vtkDataArrayDispatcherPointer(vtkDataArray* array):
NumberOfTuples(array->GetNumberOfTuples()),
NumberOfComponents(array->GetNumberOfComponents()),
RawPointer(static_cast<ValueType*>(array->GetVoidPointer(0)))
{}
};
////////////////////////////////////////////////////////////////////////////////
// class template FunctorDispatcher
////////////////////////////////////////////////////////////////////////////////
template
<
class DefaultFunctorType,
typename ReturnType = void
>
class vtkDataArrayDispatcher
{
public:
/**
* Specify the functor that is to be used when dispatching. This allows
* you to specify a statefull functor.
* \code
* struct storeLengthFunctor
* {
* int length;
* storeLengthFunctor():length(0){}
* template<typename T>
* void operator()(vtkDataArrayDispatcherPointer<T> array)
* {
* length += array.NumberOfComponents * array.NumberOfTuples;
* }
* };
* storeLengthFunctor storedLength;
* vtkDataArrayDispatcher<storeLengthFunctor> dispatcher(storedLength);
* dispatcher.Go(exampleDataArray);
* \endcode
*/
vtkDataArrayDispatcher(DefaultFunctorType& f);
/**
* Default constructor which will create an instance of the DefaultFunctorType
* and use that single instance for all calls.
*/
vtkDataArrayDispatcher();
virtual ~vtkDataArrayDispatcher();
/**
* Execute the default functor with the passed in vtkDataArray;
*/
ReturnType Go(vtkDataArray* lhs);
protected:
DefaultFunctorType* DefaultFunctor;
bool OwnsFunctor;
};
//We are making all these method non-inline to reduce compile time overhead
//----------------------------------------------------------------------------
template<class DefaultFunctorType,typename ReturnType>
vtkDataArrayDispatcher<DefaultFunctorType,ReturnType>::vtkDataArrayDispatcher(DefaultFunctorType& fun):
DefaultFunctor(&fun),
OwnsFunctor(false)
{
}
//----------------------------------------------------------------------------
template<class DefaultFunctorType,typename ReturnType>
vtkDataArrayDispatcher<DefaultFunctorType,ReturnType>::vtkDataArrayDispatcher():
DefaultFunctor(new DefaultFunctorType()),
OwnsFunctor(true)
{
}
//----------------------------------------------------------------------------
template<class DefaultFunctorType,typename ReturnType>
vtkDataArrayDispatcher<DefaultFunctorType,ReturnType>::~vtkDataArrayDispatcher()
{
if(OwnsFunctor)
{
delete this->DefaultFunctor;
}
}
//----------------------------------------------------------------------------
template <class DefaultFunctorType,typename ReturnType>
ReturnType vtkDataArrayDispatcher<DefaultFunctorType,ReturnType>
::Go(vtkDataArray* lhs)
{
switch(lhs->GetDataType())
{
vtkTemplateMacro(return (*this->DefaultFunctor) (
vtkDataArrayDispatcherPointer<VTK_TT>(lhs) ));
}
return ReturnType();
}
#endif // vtkDataArrayDispatcher_h
// VTK-HeaderTest-Exclude: vtkDataArrayDispatcher.h