VTK
Storage.h
Go to the documentation of this file.
1 //=============================================================================
2 //
3 // Copyright (c) Kitware, Inc.
4 // All rights reserved.
5 // See LICENSE.txt for details.
6 //
7 // This software is distributed WITHOUT ANY WARRANTY; without even
8 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9 // PURPOSE. See the above copyright notice for more information.
10 //
11 // Copyright 2012 Sandia Corporation.
12 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
13 // the U.S. Government retains certain rights in this software.
14 //
15 //=============================================================================
16 
17 #ifndef vtkmlib_Storage_h
18 #define vtkmlib_Storage_h
19 #ifndef __VTK_WRAP__
20 
21 #include "Portals.h"
22 #include "vtkmTags.h"
23 #include <vtkm/cont/Storage.h>
24 
25 namespace vtkm {
26 namespace cont {
27 namespace internal {
28 
29 template <typename ValueType_>
30 class VTKM_ALWAYS_EXPORT Storage<ValueType_, tovtkm::vtkAOSArrayContainerTag>
31 {
32  static const int NUM_COMPONENTS = tovtkm::vtkPortalTraits<ValueType_>::NUM_COMPONENTS;
33  typedef typename tovtkm::vtkPortalTraits<ValueType_>::ComponentType ComponentType;
34 
35 public:
36  typedef ValueType_ ValueType;
40 
42  : Array(nullptr),
43  NumberOfValues(0),
44  AllocatedSize(0),
45  DeallocateOnRelease(false),
46  UserProvidedMemory(false)
47  {
48  }
49 
51  : Array(array), // NumberOfValues should be equal to NumberOfTuples
52  // all the logic in here is busted
53  NumberOfValues(array->GetNumberOfTuples()),
54  AllocatedSize(array->GetNumberOfTuples() * NUM_COMPONENTS),
55  DeallocateOnRelease(false),
56  UserProvidedMemory(true)
57  {
58  }
59 
61  {
62  this->ReleaseResources();
63  }
64 
65  Storage&
66  operator=(const Storage<ValueType, tovtkm::vtkAOSArrayContainerTag>& src)
67  {
68  if (src.DeallocateOnRelease)
69  {
70  throw vtkm::cont::ErrorBadValue(
71  "Attempted to copy a storage array that needs deallocation. "
72  "This is disallowed to prevent complications with deallocation.");
73  }
74 
75  this->ReleaseResources();
76  this->Array = src.Array;
77  this->NumberOfValues = src.NumberOfValues;
78  this->AllocatedSize = src.AllocatedSize;
79  this->DeallocateOnRelease = src.DeallocateOnRelease;
80  this->UserProvidedMemory = src.UserProvidedMemory;
81 
82  return *this;
83  }
84 
85  void ReleaseResources();
86 
87  void Allocate(vtkm::Id numberOfValues);
88 
89  vtkm::Id GetNumberOfValues() const
90  {
91  return this->NumberOfValues;
92  }
93 
94  void Shrink(vtkm::Id numberOfValues)
95  {
96  if (numberOfValues > this->GetNumberOfValues())
97  {
98  throw vtkm::cont::ErrorBadValue(
99  "Shrink method cannot be used to grow array.");
100  }
101 
102  this->NumberOfValues = numberOfValues;
103  }
104 
105  PortalType GetPortal();
106 
107  PortalConstType GetPortalConst() const;
108 
109  ArrayType* VTKArray() const
110  {
111  return this->Array;
112  }
113 
114 private:
115  ArrayType* Array;
116  vtkm::Id NumberOfValues;
117  vtkm::Id AllocatedSize;
118  bool DeallocateOnRelease;
119  bool UserProvidedMemory;
120 };
121 
122 template <typename ValueType_>
123 class VTKM_ALWAYS_EXPORT Storage<ValueType_, tovtkm::vtkSOAArrayContainerTag>
124 {
125  static const int NUM_COMPONENTS = tovtkm::vtkPortalTraits<ValueType_>::NUM_COMPONENTS;
126  typedef typename tovtkm::vtkPortalTraits<ValueType_>::ComponentType ComponentType;
127 
128 public:
129  typedef ValueType_ ValueType;
130 
134 
136  : Array(nullptr),
137  NumberOfValues(0),
138  AllocatedSize(0),
139  DeallocateOnRelease(false),
140  UserProvidedMemory(false)
141  {
142  }
143 
145  : Array(array),
146  NumberOfValues(array->GetNumberOfTuples()),
147  AllocatedSize(array->GetNumberOfTuples() * NUM_COMPONENTS),
148  DeallocateOnRelease(false),
149  UserProvidedMemory(true)
150  {
151  }
152 
154  {
155  this->ReleaseResources();
156  }
157 
158  Storage&
160  {
161  if (src.DeallocateOnRelease)
162  {
163  throw vtkm::cont::ErrorBadValue(
164  "Attempted to copy a storage array that needs deallocation. "
165  "This is disallowed to prevent complications with deallocation.");
166  }
167 
168  this->ReleaseResources();
169  this->Array = src.Array;
170  this->NumberOfValues = src.NumberOfValues;
171  this->AllocatedSize = src.AllocatedSize;
172  this->DeallocateOnRelease = src.DeallocateOnRelease;
173  this->UserProvidedMemory = src.UserProvidedMemory;
174 
175  return *this;
176  }
177 
178  void ReleaseResources();
179 
180  void Allocate(vtkm::Id numberOfValues);
181 
182  vtkm::Id GetNumberOfValues() const
183  {
184  return this->NumberOfValues;
185  }
186 
187  void Shrink(vtkm::Id numberOfValues)
188  {
189  if (numberOfValues > this->GetNumberOfValues())
190  {
191  throw vtkm::cont::ErrorBadValue(
192  "Shrink method cannot be used to grow array.");
193  }
194 
195  this->NumberOfValues = numberOfValues;
196  }
197 
198  PortalType GetPortal();
199 
200  PortalConstType GetPortalConst() const;
201 
202  ArrayType* VTKArray() const
203  {
204  return this->Array;
205  }
206 
207 private:
208  ArrayType* Array;
209  vtkm::Id NumberOfValues;
210  vtkm::Id AllocatedSize;
211  bool DeallocateOnRelease;
212  bool UserProvidedMemory;
213 };
214 
215 template <typename ValueType_>
216 class VTKM_ALWAYS_EXPORT Storage<ValueType_, tovtkm::vtkCellArrayContainerTag>
217 {
218 public:
219  typedef ValueType_ ValueType;
220  // construct the portals type to be used with this container
224 
226  : Array(nullptr),
227  NumberOfValues(0),
228  AllocatedSize(0),
229  DeallocateOnRelease(false),
230  UserProvidedMemory(false)
231  {
232  }
233 
235  : Array(array),
236  NumberOfValues(array->GetNumberOfConnectivityEntries()),
237  AllocatedSize(array->GetSize()),
238  DeallocateOnRelease(false),
239  UserProvidedMemory(true)
240  {
241  }
242 
244  {
245  this->ReleaseResources();
246  }
247 
248  Storage&
250  {
251  if (src.DeallocateOnRelease)
252  {
253  throw vtkm::cont::ErrorBadValue(
254  "Attempted to copy a storage array that needs deallocation. "
255  "This is disallowed to prevent complications with deallocation.");
256  }
257 
258  this->ReleaseResources();
259  this->Array = src.Array;
260  this->NumberOfValues = src.NumberOfValues;
261  this->AllocatedSize = src.AllocatedSize;
262  this->DeallocateOnRelease = src.DeallocateOnRelease;
263  this->UserProvidedMemory = src.UserProvidedMemory;
264 
265  return *this;
266  }
267 
268  void ReleaseResources();
269 
270  void Allocate(vtkm::Id numberOfValues);
271 
272  vtkm::Id GetNumberOfValues() const
273  {
274  return this->NumberOfValues;
275  }
276 
277  void Shrink(vtkm::Id numberOfValues)
278  {
279  if (numberOfValues > this->GetNumberOfValues())
280  {
281  throw vtkm::cont::ErrorBadValue(
282  "Shrink method cannot be used to grow array.");
283  }
284 
285  this->NumberOfValues = numberOfValues;
286  }
287 
288  PortalType GetPortal();
289 
290  PortalConstType GetPortalConst() const;
291 
293  {
294  return this->Array;
295  }
296 
297 private:
298  vtkCellArray* Array;
299  vtkm::Id NumberOfValues;
300  vtkm::Id AllocatedSize;
301  bool DeallocateOnRelease;
302  bool UserProvidedMemory;
303 };
304 }
305 }
306 }
307 
308 #define VTKM_TEMPLATE_EXPORT_Storage(T, S) \
309  extern template class VTKACCELERATORSVTKM_TEMPLATE_EXPORT Storage<const T, S>; \
310  extern template class VTKACCELERATORSVTKM_TEMPLATE_EXPORT Storage<T, S>; \
311  extern template class VTKACCELERATORSVTKM_TEMPLATE_EXPORT \
312  Storage<const vtkm::Vec<T, 2>, S>; \
313  extern template class VTKACCELERATORSVTKM_TEMPLATE_EXPORT \
314  Storage<vtkm::Vec<T, 2>, S>; \
315  extern template class VTKACCELERATORSVTKM_TEMPLATE_EXPORT \
316  Storage<const vtkm::Vec<T, 3>, S>; \
317  extern template class VTKACCELERATORSVTKM_TEMPLATE_EXPORT \
318  Storage<vtkm::Vec<T, 3>, S>; \
319  extern template class VTKACCELERATORSVTKM_TEMPLATE_EXPORT \
320  Storage<const vtkm::Vec<T, 4>, S>; \
321  extern template class VTKACCELERATORSVTKM_TEMPLATE_EXPORT Storage<vtkm::Vec<T, 4>, S>;
322 
323 #define VTKM_TEMPLATE_IMPORT_Storage(T, S) \
324  template class VTKACCELERATORSVTKM_TEMPLATE_EXPORT Storage<const T, S>; \
325  template class VTKACCELERATORSVTKM_TEMPLATE_EXPORT Storage<T, S>; \
326  template class VTKACCELERATORSVTKM_TEMPLATE_EXPORT Storage<const vtkm::Vec<T, 2>, S>; \
327  template class VTKACCELERATORSVTKM_TEMPLATE_EXPORT Storage<vtkm::Vec<T, 2>, S>; \
328  template class VTKACCELERATORSVTKM_TEMPLATE_EXPORT Storage<const vtkm::Vec<T, 3>, S>; \
329  template class VTKACCELERATORSVTKM_TEMPLATE_EXPORT Storage<vtkm::Vec<T, 3>, S>; \
330  template class VTKACCELERATORSVTKM_TEMPLATE_EXPORT Storage<const vtkm::Vec<T, 4>, S>; \
331  template class VTKACCELERATORSVTKM_TEMPLATE_EXPORT Storage<vtkm::Vec<T, 4>, S>;
332 
333 #ifndef vtkmlib_Storage_cxx
334 namespace vtkm {
335 namespace cont {
336 namespace internal {
337 // T extern template instantiations
349 
361 
362 #if VTKM_SIZE_LONG_LONG == 8
365 
368 #endif
369 
370 extern template class VTKACCELERATORSVTKM_TEMPLATE_EXPORT
371  Storage<vtkIdType, tovtkm::vtkCellArrayContainerTag>;
372 }
373 }
374 }
375 
376 #endif // defined vtkmlib_Storage_cxx
377 
378 #include "Storage.hxx"
379 #endif
380 #endif // vtkmlib_Storage_h
Struct-Of-Arrays implementation of vtkGenericDataArray.
VTKM_TEMPLATE_EXPORT_Storage(char, tovtkm::vtkAOSArrayContainerTag)
tovtkm::vtkArrayPortal< ValueType, ArrayType > PortalType
Definition: Storage.h:222
tovtkm::vtkArrayPortal< ValueType, ArrayType > PortalType
Definition: Storage.h:38
tovtkm::vtkArrayPortal< const ValueType, ArrayType > PortalConstType
Definition: Storage.h:133
Storage & operator=(const Storage< ValueType_, tovtkm::vtkCellArrayContainerTag > &src)
Definition: Storage.h:249
tovtkm::vtkArrayPortal< const ValueType, ArrayType > PortalConstType
Definition: Storage.h:223
Storage & operator=(const Storage< ValueType, tovtkm::vtkAOSArrayContainerTag > &src)
Definition: Storage.h:66
Array-Of-Structs implementation of vtkGenericDataArray.
tovtkm::vtkArrayPortal< const ValueType, ArrayType > PortalConstType
Definition: Storage.h:39
Storage(vtkSOADataArrayTemplate< ComponentType > *array)
Definition: Storage.h:144
object to represent cell connectivity
Definition: vtkCellArray.h:50
Storage & operator=(const Storage< ValueType_, tovtkm::vtkSOAArrayContainerTag > &src)
Definition: Storage.h:159
typename std::remove_const< T >::type ComponentType
Definition: PortalTraits.h:32
tovtkm::vtkArrayPortal< ValueType, ArrayType > PortalType
Definition: Storage.h:132
Storage(vtkAOSDataArrayTemplate< ComponentType > *array)
Definition: Storage.h:50