VTK
Containers.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 vtkToDax_Containers_h
18 #define vtkToDax_Containers_h
19 
20 #include "vtkPoints.h"
21 #include "vtkCellArray.h"
22 
23 #include "Allocators.h"
24 #include "FieldTypeToType.h"
25 #include "Portals.h"
26 
27 namespace vtkToDax {
28 
29 //tag to say we are creating an array container
30 //of type vtkIdTypeDataArray, vtkFloatDataArray, etc
31 template<typename VTKArrayType>
33 {
34  typedef VTKArrayType Type;
35 };
36 
37 //this tag is used to construct points coordinates
39 {
40 };
41 
42 //this tag is used to construct a vtkIdArray that is
43 //used for cells
44 template<typename CellType>
46 {
47  typedef CellType Type;
48 };
49 }
50 
51 namespace dax {
52 namespace cont {
53 namespace internal {
54 
55 
56 template <typename DaxValueType, typename VTKArrayType>
57 class ArrayContainerControl<DaxValueType,vtkToDax::vtkArrayContainerTag<VTKArrayType> >
58 {
59  typedef typename vtkToDax::FieldTypeToType<
60  VTKArrayType,
61  dax::VectorTraits<DaxValueType>::NUM_COMPONENTS>::VTKComponentType
62  VTKComponentType;
63 public:
64  typedef DaxValueType ValueType;
67 
68 private:
69  //determine the number of components we need to allocate in the vtkArray
70  static const int NUM_COMPONENTS = dax::VectorTraits<ValueType>::NUM_COMPONENTS;
71 
72  //construct the allocator with the right number of elements
74 
75  //the allocated type from the allocator
76  typedef typename AllocatorType::pointer PointerType;
77 
78 public:
79 
80  ArrayContainerControl() : Array(NULL), NumberOfValues(0) { }
81 
83  {
84  this->ReleaseResources();
85  }
86 
88  {
89  if (this->NumberOfValues > 0)
90  {
91  DAX_ASSERT_CONT(this->Array != NULL);
92  AllocatorType allocator;
93  allocator.deallocate(this->Array, this->NumberOfValues);
94  this->Array = NULL;
95  this->NumberOfValues = 0;
96  }
97  else
98  {
99  DAX_ASSERT_CONT(this->Array == NULL);
100  }
101  }
102 
103  void Allocate(dax::Id numberOfValues)
104  {
105  if (this->NumberOfValues == numberOfValues) return;
106 
107  this->ReleaseResources();
108  try
109  {
110  if (numberOfValues > 0)
111  {
112  AllocatorType allocator;
113  this->Array = allocator.allocate(numberOfValues);
114  this->NumberOfValues = numberOfValues;
115  }
116  else
117  {
118  // ReleaseResources should have already set NumberOfValues to 0.
119  DAX_ASSERT_CONT(this->NumberOfValues == 0);
120  }
121  }
122  catch (const std::bad_alloc&)
123  {
124  // Make sureour state is OK.
125  this->Array = NULL;
126  this->NumberOfValues = 0;
127  throw dax::cont::ErrorControlOutOfMemory(
128  "Could not allocate basic control array.");
129  }
130  }
131 
132  dax::Id GetNumberOfValues() const
133  {
134  return this->NumberOfValues;
135  }
136 
137  void Shrink(dax::Id numberOfValues)
138  {
139  if (numberOfValues > this->GetNumberOfValues())
140  {
141  throw dax::cont::ErrorControlBadValue(
142  "Shrink method cannot be used to grow array.");
143  }
144 
145  this->NumberOfValues = numberOfValues;
146  }
147 
148  PortalType GetPortal()
149  {
150  return PortalType(this->Array, this->NumberOfValues);
151  }
152 
153  PortalConstType GetPortalConst() const
154  {
155  return PortalConstType(this->Array, this->NumberOfValues);
156  }
157 
158 private:
159  ArrayContainerControl(const ArrayContainerControl<ValueType, vtkToDax::vtkPointsContainerTag> &src) VTK_DELETE_FUNCTION;
160  void operator=(const ArrayContainerControl<ValueType, vtkToDax::vtkPointsContainerTag> &src) VTK_DELETE_FUNCTION;
161 
162  PointerType Array;
163  dax::Id NumberOfValues;
164 };
165 
166 template <typename ValueT>
167 class ArrayContainerControl<ValueT,vtkToDax::vtkPointsContainerTag>
168 {
169 public:
170  typedef ValueT ValueType;
171  //construct the portals type to be used with this container
174 
175 private:
176  //determine the allocator type and pointer type for this container
178  //the pointer type tells us the type of what the allocator returns
179  typedef typename AllocatorType::pointer PointerType;
180 
181 public:
182 
183  ArrayContainerControl() : Array(NULL), NumberOfValues(0) { }
184 
186  {
187  this->ReleaseResources();
188  }
189 
191  {
192  if (this->NumberOfValues > 0)
193  {
194  DAX_ASSERT_CONT(this->Array != NULL);
195  AllocatorType allocator;
196  allocator.deallocate(this->Array, this->NumberOfValues);
197  this->Array = NULL;
198  this->NumberOfValues = 0;
199  }
200  else
201  {
202  DAX_ASSERT_CONT(this->Array == NULL);
203  }
204  }
205 
206  void Allocate(dax::Id numberOfValues)
207  {
208  if (this->NumberOfValues == numberOfValues) return;
209 
210  this->ReleaseResources();
211  try
212  {
213  if (numberOfValues > 0)
214  {
215  AllocatorType allocator;
216  this->Array = allocator.allocate(numberOfValues);
217  this->NumberOfValues = numberOfValues;
218  }
219  else
220  {
221  // ReleaseResources should have already set NumberOfValues to 0.
222  DAX_ASSERT_CONT(this->NumberOfValues == 0);
223  }
224  }
225  catch (const std::bad_alloc&)
226  {
227  // Make sureour state is OK.
228  this->Array = NULL;
229  this->NumberOfValues = 0;
230  throw dax::cont::ErrorControlOutOfMemory(
231  "Could not allocate basic control array.");
232  }
233  }
234 
235  dax::Id GetNumberOfValues() const
236  {
237  return this->NumberOfValues;
238  }
239 
240  void Shrink(dax::Id numberOfValues)
241  {
242  if (numberOfValues > this->GetNumberOfValues())
243  {
244  throw dax::cont::ErrorControlBadValue(
245  "Shrink method cannot be used to grow array.");
246  }
247 
248  this->NumberOfValues = numberOfValues;
249  }
250 
251  PortalType GetPortal()
252  {
253  return PortalType(this->Array, this->NumberOfValues);
254  }
255 
256  PortalConstType GetPortalConst() const
257  {
258  return PortalConstType(this->Array, this->NumberOfValues);
259  }
260 
261 private:
262  ArrayContainerControl(const ArrayContainerControl<ValueType, vtkToDax::vtkPointsContainerTag> &src) VTK_DELETE_FUNCTION;
263  void operator=(const ArrayContainerControl<ValueType, vtkToDax::vtkPointsContainerTag> &src) VTK_DELETE_FUNCTION;
264 
265  PointerType Array;
266  dax::Id NumberOfValues;
267 };
268 
269 template <typename ValueT, typename CellType>
270 class ArrayContainerControl<ValueT,vtkToDax::vtkTopologyContainerTag<CellType> >
271 {
272 public:
273  typedef ValueT ValueType;
276 
277 private:
278  //determine the allocator type and pointer type for this container
280  //the pointer type tells us the type of what the allocator returns
281  typedef typename AllocatorType::pointer PointerType;
282 
283 public:
284 
285  ArrayContainerControl() : Array(NULL), NumberOfValues(0) { }
286 
288  {
289  this->ReleaseResources();
290  }
291 
293  {
294  if (this->NumberOfValues > 0)
295  {
296  DAX_ASSERT_CONT(this->Array != NULL);
297  AllocatorType allocator;
298  allocator.deallocate(this->Array, this->NumberOfValues);
299  this->Array = NULL;
300  this->NumberOfValues = 0;
301  }
302  else
303  {
304  DAX_ASSERT_CONT(this->Array == NULL);
305  }
306  }
307 
308  void Allocate(dax::Id numberOfValues)
309  {
310  if (this->NumberOfValues == numberOfValues) return;
311 
312  this->ReleaseResources();
313  try
314  {
315  if (numberOfValues > 0)
316  {
317  AllocatorType allocator;
318  this->Array = allocator.allocate(numberOfValues);
319  this->NumberOfValues = numberOfValues;
320  }
321  else
322  {
323  // ReleaseResources should have already set NumberOfValues to 0.
324  DAX_ASSERT_CONT(this->NumberOfValues == 0);
325  }
326  }
327  catch (const std::bad_alloc&)
328  {
329  // Make sureour state is OK.
330  this->Array = NULL;
331  this->NumberOfValues = 0;
332  throw dax::cont::ErrorControlOutOfMemory(
333  "Could not allocate basic control array.");
334  }
335  }
336 
337  dax::Id GetNumberOfValues() const
338  {
339  return this->NumberOfValues;
340  }
341 
342  void Shrink(dax::Id numberOfValues)
343  {
344  if (numberOfValues > this->GetNumberOfValues())
345  {
346  throw dax::cont::ErrorControlBadValue(
347  "Shrink method cannot be used to grow array.");
348  }
349 
350  this->NumberOfValues = numberOfValues;
351  }
352 
353  PortalType GetPortal()
354  {
355  return PortalType(this->Array, this->NumberOfValues);
356  }
357 
358  PortalConstType GetPortalConst() const
359  {
360  return PortalConstType(this->Array, this->NumberOfValues);
361  }
362 
363 private:
364  ArrayContainerControl(const ArrayContainerControl<ValueType, vtkToDax::vtkPointsContainerTag> &src) VTK_DELETE_FUNCTION;
365  void operator=(const ArrayContainerControl<ValueType, vtkToDax::vtkPointsContainerTag> &src) VTK_DELETE_FUNCTION;
366 
367  PointerType Array;
368  dax::Id NumberOfValues;
369 };
370 
371 }
372 }
373 }
374 
375 #endif //vtkToDax_CONTAINERS_H
void deallocate(self::pointer p, self::size_type)
Definition: Allocators.h:61
pointer allocate(size_type n, self::const_pointer hint=0)
Definition: Allocators.h:53
vtkToDax::vtkTopologyPortal< const ValueType, CellType::NUM_POINTS > PortalConstType
Definition: Containers.h:275
Definition: Containers.h:51
vtkToDax::vtkArrayPortal< const DaxValueType, const VTKComponentType > PortalConstType
Definition: Containers.h:66
vtkToDax::vtkTopologyPortal< ValueType, CellType::NUM_POINTS > PortalType
Definition: Containers.h:274