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