VTK  9.2.6
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 namespace vtk
27 {
28 
29 namespace detail
30 {
31 
32 //------------------------------------------------------------------------------
33 // MTimeWatcher:
34 // operator() return true if the MTime of its argument is less than or equal
35 // to the MTime of the object used to construct it.
36 //
37 // Create/reset using `mtime_watcher = MTimeWatcher{obj};`
38 //
39 // Test using `bool cacheIsValid = mtime_watcher(obj);`
40 //
41 // There are two variants of this:
42 // - MTimeWatcher can be used to ALWAYS check for valid mtimes.
43 // - DebugMTimeWatcher can be used to check mtimes ONLY in debugging builds,
44 // and is defined as an empty, transparent no-op object in optimized builds.
45 // The optimized version will always return true from operator().
47 {
49 
50  MTimeWatcher() = default;
51  explicit MTimeWatcher(vtkObject* o)
52  : MTime{ o->GetMTime() }
53  {
54  }
55  bool operator()(vtkObject* o) const { return o->GetMTime() <= this->MTime; }
56  void Reset(vtkObject* o) { this->MTime = o->GetMTime(); }
57  bool MTimeIsValid(vtkObject* o) const { return o->GetMTime() <= this->MTime; }
58 };
59 
60 // empty, transparent, does nothing. operator() always returns true.
62 {
63  NoOpMTimeWatcher() = default;
65  bool operator()(vtkObject*) const { return true; }
66  void Reset(vtkObject*) {}
67  bool MTimeIsValid(vtkObject*) const { return true; }
68 };
69 
70 // Debug-dependent version:
71 #ifndef _NDEBUG
73 #else
75 #endif
76 
77 //------------------------------------------------------------------------------
78 // DebugWeakPointer: Defined to vtkWeakPointer on debugging builds, T* on
79 // non-debugging builds.
80 #ifndef _NDEBUG
81 template <class ObjectType>
83 #else
84 template <class ObjectType>
85 using DebugWeakPointer = ObjectType*;
86 #endif
87 
88 } // end namespace detail
89 
144 template <typename IteratorType,
145  typename OwnerType>
147  : private detail::DebugMTimeWatcher // empty-base optimization when NDEBUG
148 {
150  "CompositeDataSetNodeReference's IteratorType must be a "
151  "subclass of vtkCompositeDataIterator.");
152 
153  // Either a vtkWeakPointer (debug builds) or raw pointer (non-debug builds)
154  mutable detail::DebugWeakPointer<IteratorType> Iterator{ nullptr };
155 
156  // Check that the reference has not been invalidated by having the
157  // borrowed internal iterator modified.
158  void AssertValid() const
159  {
160 
161  // Test that the weak pointer hasn't been cleared
162  assert(
163  "Invalid CompositeDataNodeReference accessed (iterator freed)." && this->Iterator != nullptr);
164  // Check MTime:
165  assert("Invalid CompositeDataNodeReference accessed (iterator modified)." &&
166  this->MTimeIsValid(this->Iterator));
167  }
168 
169 protected:
170  explicit CompositeDataSetNodeReference(IteratorType* iterator)
171  : detail::DebugMTimeWatcher(iterator)
172  , Iterator(iterator)
173  {
174  }
175 
176 public:
177  friend OwnerType; // To allow access to protected methods/base class
178 
182  ~CompositeDataSetNodeReference() = default;
183 
184  // Assigns the DataObject from src to this:
186  {
187  this->SetDataObject(src.GetDataObject());
188  return *this;
189  }
190 
191  // Compares data object and flat index:
192  friend bool operator==(
194  {
195  return lhs.GetDataObject() == rhs.GetDataObject() && lhs.GetFlatIndex() == rhs.GetFlatIndex();
196  }
197 
198  // Compares data object and flat index:
199  friend bool operator!=(
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 { return this->GetDataObject() != nullptr; }
225 
226  operator vtkDataObject*() const { return this->GetDataObject(); }
227 
228  vtkDataObject* operator->() const { return this->GetDataObject(); }
229 
231  {
232  this->AssertValid();
233  vtkCompositeDataSet* cds = this->Iterator->GetDataSet();
234  cds->SetDataSet(this->Iterator, obj);
235  }
236 
238  {
239  this->AssertValid();
240  other->SetDataSet(this->Iterator, dObj);
241  }
242 
244  {
245  this->SetDataObject(obj);
246  return *this;
247  }
248 
249  unsigned int GetFlatIndex() const
250  {
251  this->AssertValid();
252  return this->Iterator->GetCurrentFlatIndex();
253  }
254 
255  bool HasMetaData() const
256  {
257  this->AssertValid();
258  return this->Iterator->HasCurrentMetaData() != 0;
259  }
260 
262  {
263  this->AssertValid();
264  return this->Iterator->GetCurrentMetaData();
265  }
266 };
267 
268 } // end namespace vtk
269 
270 #endif // vtkCompositeDataSetNodeReference_h
271 
272 // VTK-HeaderTest-Exclude: vtkCompositeDataSetNodeReference.h
CompositeDataSetNodeReference & operator=(vtkDataObject *obj)
abstract base class for most VTK objects
Definition: vtkObject.h:62
vtkDataObject * GetDataObject(vtkCompositeDataSet *other)
Store vtkAlgorithm input/output information.
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:287
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:34
virtual vtkMTimeType GetMTime()
Return this object'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 position pointed by the iterator.
general representation of visualization data
Definition: vtkDataObject.h:65
friend bool operator==(const CompositeDataSetNodeReference &lhs, const CompositeDataSetNodeReference &rhs)