VTK
vtkCompositeDataSetNodeReference.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkCompositeDataSetNodeReference.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 #ifndef vtkCompositeDataSetNodeReference_h
17 #define vtkCompositeDataSetNodeReference_h
18 
20 #include "vtkCompositeDataSet.h"
21 #include "vtkWeakPointer.h"
22 
23 #include <cassert>
24 #include <type_traits>
25 
26 #ifndef __VTK_WRAP__
27 
28 namespace vtk
29 {
30 
31 namespace detail
32 {
33 
34 //------------------------------------------------------------------------------
35 // MTimeWatcher:
36 // operator() return true if the MTime of its argument is less than or equal
37 // to the MTime of the object used to construct it.
38 //
39 // Create/reset using `mtime_watcher = MTimeWatcher{obj};`
40 //
41 // Test using `bool cacheIsValid = mtime_watcher(obj);`
42 //
43 // There are two variants of this:
44 // - MTimeWatcher can be used to ALWAYS check for valid mtimes.
45 // - DebugMTimeWatcher can be used to check mtimes ONLY in debugging builds,
46 // and is defined as an empty, transparent no-op object in optimized builds.
47 // The optimized version will always return true from operator().
49 {
51 
53  explicit MTimeWatcher(vtkObject *o) : MTime{o->GetMTime()} {}
54  bool operator()(vtkObject *o) const { return o->GetMTime() <= this->MTime; }
55  void Reset(vtkObject *o) { this->MTime = o->GetMTime(); }
56  bool MTimeIsValid(vtkObject *o) const { return o->GetMTime() <= this->MTime; }
57 };
58 
59 // empty, transparent, does nothing. operator() always returns true.
61 {
63  explicit NoOpMTimeWatcher(vtkObject *) {}
64  bool operator()(vtkObject *) const { return true; }
65  void Reset(vtkObject *) {}
66  bool MTimeIsValid(vtkObject *) const { return true; }
67 };
68 
69 // Debug-dependent version:
70 #ifndef _NDEBUG
72 #else
74 #endif
75 
76 //------------------------------------------------------------------------------
77 // DebugWeakPointer: Defined to vtkWeakPointer on debugging builds, T* on
78 // non-debugging builds.
79 #ifndef _NDEBUG
80 template <class ObjectType>
82 #else
83 template <class ObjectType>
84 using DebugWeakPointer = ObjectType*;
85 #endif
86 
87 } // end namespace detail
88 
143 template <typename IteratorType,
144  typename OwnerType>
146  : private detail::DebugMTimeWatcher // empty-base optimization when NDEBUG
147 {
149  "CompositeDataSetNodeReference's IteratorType must be a "
150  "subclass of vtkCompositeDataIterator.");
151 
152  // Either a vtkWeakPointer (debug builds) or raw pointer (non-debug builds)
153  mutable detail::DebugWeakPointer<IteratorType> Iterator{nullptr};
154 
155  // Check that the reference has not been invalidated by having the
156  // borrowed internal iterator modified.
157  void AssertValid() const
158  {
159 
160  // Test that the weak pointer hasn't been cleared
161  assert("Invalid CompositeDataNodeReference accessed (iterator freed)." &&
162  this->Iterator != nullptr);
163  // Check MTime:
164  assert("Invalid CompositeDataNodeReference accessed (iterator modified)." &&
165  this->MTimeIsValid(this->Iterator));
166  }
167 
168 protected:
169  explicit CompositeDataSetNodeReference(IteratorType *iterator)
170  : detail::DebugMTimeWatcher(iterator)
171  , Iterator(iterator)
172  {
173  }
174 
175 public:
176  friend OwnerType; // To allow access to protected methods/base class
177 
181  ~CompositeDataSetNodeReference() = default;
182 
183  // Assigns the DataObject from src to this:
185  {
186  this->SetDataObject(src.GetDataObject());
187  return *this;
188  }
189 
190  // Compares data object and flat index:
193  {
194  return lhs.GetDataObject() == rhs.GetDataObject() &&
195  lhs.GetFlatIndex() == rhs.GetFlatIndex();
196  }
197 
198  // Compares data object and flat index:
201  {
202  return lhs != rhs;
203  }
204 
206  {
207  this->AssertValid();
208  // GetCurrentDataObject is buggy -- the iterator caches the current dataset
209  // internally, so if the object has changed since the iterator was
210  // incremented, the changes will not be visible through the iterator's
211  // API. See VTK issue #17529.
212  // Instead, look it up in the dataset. It's a bit slower, but will always be
213  // correct.
214 // return this->Iterator->GetCurrentDataObject();
215  return this->Iterator->GetDataSet()->GetDataSet(this->Iterator);
216  }
217 
219  {
220  this->AssertValid();
221  return other->GetDataSet(this->Iterator);
222  }
223 
224  operator bool () const
225  {
226  return this->GetDataObject() != nullptr;
227  }
228 
229  operator vtkDataObject* () const
230  {
231  return this->GetDataObject();
232  }
233 
235  {
236  return this->GetDataObject();
237  }
238 
240  {
241  this->AssertValid();
242  vtkCompositeDataSet *cds = this->Iterator->GetDataSet();
243  cds->SetDataSet(this->Iterator, obj);
244  }
245 
247  {
248  this->AssertValid();
249  other->SetDataSet(this->Iterator, dObj);
250  }
251 
253  {
254  this->SetDataObject(obj);
255  return *this;
256  }
257 
258  unsigned int GetFlatIndex() const
259  {
260  this->AssertValid();
261  return this->Iterator->GetCurrentFlatIndex();
262  }
263 
264  bool HasMetaData() const
265  {
266  this->AssertValid();
267  return this->Iterator->HasCurrentMetaData() != 0;
268  }
269 
271  {
272  this->AssertValid();
273  return this->Iterator->GetCurrentMetaData();
274  }
275 };
276 
277 } // end namespace vtk
278 
279 #endif // __VTK_WRAP__
280 
281 #endif // vtkCompositeDataSetNodeReference_h
282 
283 // VTK-HeaderTest-Exclude: vtkCompositeDataSetNodeReference.h
CompositeDataSetNodeReference & operator=(vtkDataObject *obj)
abstract base class for most VTK objects
Definition: vtkObject.h:59
vtkDataObject * GetDataObject(vtkCompositeDataSet *other)
Store vtkAlgorithm input/output information.
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:304
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
virtual void SetDataSet(vtkCompositeDataIterator *iter, vtkDataObject *dataObj)=0
Sets the data set at the location pointed by the iterator.
abstract superclass for composite (multi-block or AMR) datasets
a weak reference to a vtkObject.
Definition: vtkMeta.h:31
virtual vtkMTimeType GetMTime()
Return this object&#39;s modified time.
void SetDataObject(vtkCompositeDataSet *other, vtkDataObject *dObj)
friend bool operator!=(const CompositeDataSetNodeReference &lhs, const CompositeDataSetNodeReference &rhs)
A reference proxy into a vtkCompositeDataSet, obtained by dereferencing an iterator from the vtk::Ran...
virtual vtkDataObject * GetDataSet(vtkCompositeDataIterator *iter)=0
Returns the dataset located at the positiong pointed by the iterator.
general representation of visualization data
Definition: vtkDataObject.h:64
friend bool operator==(const CompositeDataSetNodeReference &lhs, const CompositeDataSetNodeReference &rhs)