VTK  9.3.0
vtkCompositeDataSetNodeReference.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 
4 #ifndef vtkCompositeDataSetNodeReference_h
5 #define vtkCompositeDataSetNodeReference_h
6 
8 #include "vtkCompositeDataSet.h"
9 #include "vtkWeakPointer.h"
10 
11 #include <cassert>
12 #include <type_traits>
13 
14 namespace vtk
15 {
16 namespace detail
17 {
18 VTK_ABI_NAMESPACE_BEGIN
19 
20 //------------------------------------------------------------------------------
21 // MTimeWatcher:
22 // operator() return true if the MTime of its argument is less than or equal
23 // to the MTime of the object used to construct it.
24 //
25 // Create/reset using `mtime_watcher = MTimeWatcher{obj};`
26 //
27 // Test using `bool cacheIsValid = mtime_watcher(obj);`
28 //
29 // There are two variants of this:
30 // - MTimeWatcher can be used to ALWAYS check for valid mtimes.
31 // - DebugMTimeWatcher can be used to check mtimes ONLY in debugging builds,
32 // and is defined as an empty, transparent no-op object in optimized builds.
33 // The optimized version will always return true from operator().
35 {
37 
38  MTimeWatcher() = default;
39  explicit MTimeWatcher(vtkObject* o)
40  : MTime{ o->GetMTime() }
41  {
42  }
43  bool operator()(vtkObject* o) const { return o->GetMTime() <= this->MTime; }
44  void Reset(vtkObject* o) { this->MTime = o->GetMTime(); }
45  bool MTimeIsValid(vtkObject* o) const { return o->GetMTime() <= this->MTime; }
46 };
47 
48 // empty, transparent, does nothing. operator() always returns true.
50 {
51  NoOpMTimeWatcher() = default;
53  bool operator()(vtkObject*) const { return true; }
54  void Reset(vtkObject*) {}
55  bool MTimeIsValid(vtkObject*) const { return true; }
56 };
57 
58 // Debug-dependent version:
59 #ifndef _NDEBUG
61 #else
63 #endif
64 
65 //------------------------------------------------------------------------------
66 // DebugWeakPointer: Defined to vtkWeakPointer on debugging builds, T* on
67 // non-debugging builds.
68 #ifndef _NDEBUG
69 template <class ObjectType>
71 #else
72 template <class ObjectType>
73 using DebugWeakPointer = ObjectType*;
74 #endif
75 
76 VTK_ABI_NAMESPACE_END
77 } // end namespace detail
78 
79 VTK_ABI_NAMESPACE_BEGIN
80 
135 template <typename IteratorType,
136  typename OwnerType>
138  : private detail::DebugMTimeWatcher // empty-base optimization when NDEBUG
139 {
141  "CompositeDataSetNodeReference's IteratorType must be a "
142  "subclass of vtkCompositeDataIterator.");
143 
144  // Either a vtkWeakPointer (debug builds) or raw pointer (non-debug builds)
145  mutable detail::DebugWeakPointer<IteratorType> Iterator{ nullptr };
146 
147  // Check that the reference has not been invalidated by having the
148  // borrowed internal iterator modified.
149  void AssertValid() const
150  {
151 
152  // Test that the weak pointer hasn't been cleared
153  assert(
154  "Invalid CompositeDataNodeReference accessed (iterator freed)." && this->Iterator != nullptr);
155  // Check MTime:
156  assert("Invalid CompositeDataNodeReference accessed (iterator modified)." &&
157  this->MTimeIsValid(this->Iterator));
158  }
159 
160 protected:
161  explicit CompositeDataSetNodeReference(IteratorType* iterator)
162  : detail::DebugMTimeWatcher(iterator)
163  , Iterator(iterator)
164  {
165  }
166 
167 public:
168  friend OwnerType; // To allow access to protected methods/base class
169 
174 
175  // Assigns the DataObject from src to this:
177  {
178  this->SetDataObject(src.GetDataObject());
179  return *this;
180  }
181 
182  // Compares data object and flat index:
183  friend bool operator==(
185  {
186  return lhs.GetDataObject() == rhs.GetDataObject() && lhs.GetFlatIndex() == rhs.GetFlatIndex();
187  }
188 
189  // Compares data object and flat index:
190  friend bool operator!=(
192  {
193  return lhs != rhs;
194  }
195 
197  {
198  this->AssertValid();
199  // GetCurrentDataObject is buggy -- the iterator caches the current dataset
200  // internally, so if the object has changed since the iterator was
201  // incremented, the changes will not be visible through the iterator's
202  // API. See VTK issue #17529.
203  // Instead, look it up in the dataset. It's a bit slower, but will always be
204  // correct.
205  // return this->Iterator->GetCurrentDataObject();
206  return this->Iterator->GetDataSet()->GetDataSet(this->Iterator);
207  }
208 
210  {
211  this->AssertValid();
212  return other->GetDataSet(this->Iterator);
213  }
214 
215  operator bool() const { return this->GetDataObject() != nullptr; }
216 
217  operator vtkDataObject*() const { return this->GetDataObject(); }
218 
219  vtkDataObject* operator->() const { return this->GetDataObject(); }
220 
222  {
223  this->AssertValid();
224  vtkCompositeDataSet* cds = this->Iterator->GetDataSet();
225  cds->SetDataSet(this->Iterator, obj);
226  }
227 
229  {
230  this->AssertValid();
231  other->SetDataSet(this->Iterator, dObj);
232  }
233 
235  {
236  this->SetDataObject(obj);
237  return *this;
238  }
239 
240  unsigned int GetFlatIndex() const
241  {
242  this->AssertValid();
243  return this->Iterator->GetCurrentFlatIndex();
244  }
245 
246  bool HasMetaData() const
247  {
248  this->AssertValid();
249  return this->Iterator->HasCurrentMetaData() != 0;
250  }
251 
253  {
254  this->AssertValid();
255  return this->Iterator->GetCurrentMetaData();
256  }
257 };
258 
259 VTK_ABI_NAMESPACE_END
260 } // end namespace vtk
261 
262 #endif // vtkCompositeDataSetNodeReference_h
263 
264 // VTK-HeaderTest-Exclude: vtkCompositeDataSetNodeReference.h
abstract superclass for composite (multi-block or AMR) datasets
virtual void SetDataSet(vtkCompositeDataIterator *iter, vtkDataObject *dataObj)=0
Sets the data set at the location pointed by the iterator.
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:55
Store vtkAlgorithm input/output information.
abstract base class for most VTK objects
Definition: vtkObject.h:52
virtual vtkMTimeType GetMTime()
Return this object's modified time.
a weak reference to a vtkObject.
A reference proxy into a vtkCompositeDataSet, obtained by dereferencing an iterator from the vtk::Ran...
friend bool operator!=(const CompositeDataSetNodeReference &lhs, const CompositeDataSetNodeReference &rhs)
void SetDataObject(vtkCompositeDataSet *other, vtkDataObject *dObj)
CompositeDataSetNodeReference & operator=(vtkDataObject *obj)
friend bool operator==(const CompositeDataSetNodeReference &lhs, const CompositeDataSetNodeReference &rhs)
CompositeDataSetNodeReference(const CompositeDataSetNodeReference &src)=default
CompositeDataSetNodeReference(CompositeDataSetNodeReference &&) noexcept=default
vtkDataObject * GetDataObject(vtkCompositeDataSet *other)
@ value
Definition: vtkX3D.h:220
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:270