VTK  9.0.20200804
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 
52  MTimeWatcher() = default;
53  explicit MTimeWatcher(vtkObject* o)
54  : MTime{ o->GetMTime() }
55  {
56  }
57  bool operator()(vtkObject* o) const { return o->GetMTime() <= this->MTime; }
58  void Reset(vtkObject* o) { this->MTime = o->GetMTime(); }
59  bool MTimeIsValid(vtkObject* o) const { return o->GetMTime() <= this->MTime; }
60 };
61 
62 // empty, transparent, does nothing. operator() always returns true.
64 {
65  NoOpMTimeWatcher() = default;
67  bool operator()(vtkObject*) const { return true; }
68  void Reset(vtkObject*) {}
69  bool MTimeIsValid(vtkObject*) const { return true; }
70 };
71 
72 // Debug-dependent version:
73 #ifndef _NDEBUG
75 #else
77 #endif
78 
79 //------------------------------------------------------------------------------
80 // DebugWeakPointer: Defined to vtkWeakPointer on debugging builds, T* on
81 // non-debugging builds.
82 #ifndef _NDEBUG
83 template <class ObjectType>
85 #else
86 template <class ObjectType>
87 using DebugWeakPointer = ObjectType*;
88 #endif
89 
90 } // end namespace detail
91 
146 template <typename IteratorType,
147  typename OwnerType>
149  : private detail::DebugMTimeWatcher // empty-base optimization when NDEBUG
150 {
152  "CompositeDataSetNodeReference's IteratorType must be a "
153  "subclass of vtkCompositeDataIterator.");
154 
155  // Either a vtkWeakPointer (debug builds) or raw pointer (non-debug builds)
156  mutable detail::DebugWeakPointer<IteratorType> Iterator{ nullptr };
157 
158  // Check that the reference has not been invalidated by having the
159  // borrowed internal iterator modified.
160  void AssertValid() const
161  {
162 
163  // Test that the weak pointer hasn't been cleared
164  assert(
165  "Invalid CompositeDataNodeReference accessed (iterator freed)." && this->Iterator != nullptr);
166  // Check MTime:
167  assert("Invalid CompositeDataNodeReference accessed (iterator modified)." &&
168  this->MTimeIsValid(this->Iterator));
169  }
170 
171 protected:
172  explicit CompositeDataSetNodeReference(IteratorType* iterator)
173  : detail::DebugMTimeWatcher(iterator)
174  , Iterator(iterator)
175  {
176  }
177 
178 public:
179  friend OwnerType; // To allow access to protected methods/base class
180 
184  ~CompositeDataSetNodeReference() = default;
185 
186  // Assigns the DataObject from src to this:
188  {
189  this->SetDataObject(src.GetDataObject());
190  return *this;
191  }
192 
193  // Compares data object and flat index:
194  friend bool operator==(
196  {
197  return lhs.GetDataObject() == rhs.GetDataObject() && lhs.GetFlatIndex() == rhs.GetFlatIndex();
198  }
199 
200  // Compares data object and flat index:
201  friend bool operator!=(
203  {
204  return lhs != rhs;
205  }
206 
208  {
209  this->AssertValid();
210  // GetCurrentDataObject is buggy -- the iterator caches the current dataset
211  // internally, so if the object has changed since the iterator was
212  // incremented, the changes will not be visible through the iterator's
213  // API. See VTK issue #17529.
214  // Instead, look it up in the dataset. It's a bit slower, but will always be
215  // correct.
216  // return this->Iterator->GetCurrentDataObject();
217  return this->Iterator->GetDataSet()->GetDataSet(this->Iterator);
218  }
219 
221  {
222  this->AssertValid();
223  return other->GetDataSet(this->Iterator);
224  }
225 
226  operator bool() const { return this->GetDataObject() != nullptr; }
227 
228  operator vtkDataObject*() const { return this->GetDataObject(); }
229 
230  vtkDataObject* operator->() const { return this->GetDataObject(); }
231 
233  {
234  this->AssertValid();
235  vtkCompositeDataSet* cds = this->Iterator->GetDataSet();
236  cds->SetDataSet(this->Iterator, obj);
237  }
238 
240  {
241  this->AssertValid();
242  other->SetDataSet(this->Iterator, dObj);
243  }
244 
246  {
247  this->SetDataObject(obj);
248  return *this;
249  }
250 
251  unsigned int GetFlatIndex() const
252  {
253  this->AssertValid();
254  return this->Iterator->GetCurrentFlatIndex();
255  }
256 
257  bool HasMetaData() const
258  {
259  this->AssertValid();
260  return this->Iterator->HasCurrentMetaData() != 0;
261  }
262 
264  {
265  this->AssertValid();
266  return this->Iterator->GetCurrentMetaData();
267  }
268 };
269 
270 } // end namespace vtk
271 
272 #endif // __VTK_WRAP__
273 
274 #endif // vtkCompositeDataSetNodeReference_h
275 
276 // VTK-HeaderTest-Exclude: vtkCompositeDataSetNodeReference.h
vtk::detail::NoOpMTimeWatcher::Reset
void Reset(vtkObject *)
Definition: vtkCompositeDataSetNodeReference.h:68
vtk::detail::MTimeWatcher::MTimeIsValid
bool MTimeIsValid(vtkObject *o) const
Definition: vtkCompositeDataSetNodeReference.h:59
vtk::detail::DebugMTimeWatcher
MTimeWatcher DebugMTimeWatcher
Definition: vtkCompositeDataSetNodeReference.h:74
vtk::CompositeDataSetNodeReference::operator->
vtkDataObject * operator->() const
Definition: vtkCompositeDataSetNodeReference.h:230
vtk::detail::NoOpMTimeWatcher::MTimeIsValid
bool MTimeIsValid(vtkObject *) const
Definition: vtkCompositeDataSetNodeReference.h:69
vtk::detail::MTimeWatcher
Definition: vtkCompositeDataSetNodeReference.h:48
vtkX3D::value
Definition: vtkX3D.h:226
vtkCompositeDataSet::SetDataSet
virtual void SetDataSet(vtkCompositeDataIterator *iter, vtkDataObject *dataObj)=0
Sets the data set at the location pointed by the iterator.
vtkCompositeDataIterator.h
vtk::detail::NoOpMTimeWatcher::NoOpMTimeWatcher
NoOpMTimeWatcher()=default
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:62
detail
Definition: vtkGenericDataArrayLookupHelper.h:31
vtk::CompositeDataSetNodeReference::OwnerType
friend OwnerType
Definition: vtkCompositeDataSetNodeReference.h:179
vtk::CompositeDataSetNodeReference::SetDataObject
void SetDataObject(vtkDataObject *obj)
Definition: vtkCompositeDataSetNodeReference.h:232
vtk::detail::MTimeWatcher::MTime
vtkMTimeType MTime
Definition: vtkCompositeDataSetNodeReference.h:50
vtk::detail::NoOpMTimeWatcher::NoOpMTimeWatcher
NoOpMTimeWatcher(vtkObject *)
Definition: vtkCompositeDataSetNodeReference.h:66
vtkCompositeDataSet
abstract superclass for composite (multi-block or AMR) datasets
Definition: vtkCompositeDataSet.h:45
vtk::CompositeDataSetNodeReference::GetFlatIndex
unsigned int GetFlatIndex() const
Definition: vtkCompositeDataSetNodeReference.h:251
vtk::detail::MTimeWatcher::operator()
bool operator()(vtkObject *o) const
Definition: vtkCompositeDataSetNodeReference.h:57
vtk::CompositeDataSetNodeReference::GetDataObject
vtkDataObject * GetDataObject(vtkCompositeDataSet *other)
Definition: vtkCompositeDataSetNodeReference.h:220
vtk::detail::NoOpMTimeWatcher::operator()
bool operator()(vtkObject *) const
Definition: vtkCompositeDataSetNodeReference.h:67
vtkObject::GetMTime
virtual vtkMTimeType GetMTime()
Return this object's modified time.
vtk::CompositeDataSetNodeReference::HasMetaData
bool HasMetaData() const
Definition: vtkCompositeDataSetNodeReference.h:257
vtkWeakPointer.h
vtk::CompositeDataSetNodeReference::operator=
CompositeDataSetNodeReference & operator=(vtkDataObject *obj)
Definition: vtkCompositeDataSetNodeReference.h:245
vtk::detail::NoOpMTimeWatcher
Definition: vtkCompositeDataSetNodeReference.h:63
vtkCompositeDataSet.h
vtkInformation
Store vtkAlgorithm input/output information.
Definition: vtkInformation.h:73
vtk::CompositeDataSetNodeReference::operator!=
friend bool operator!=(const CompositeDataSetNodeReference &lhs, const CompositeDataSetNodeReference &rhs)
Definition: vtkCompositeDataSetNodeReference.h:201
vtk::CompositeDataSetNodeReference::CompositeDataSetNodeReference
CompositeDataSetNodeReference()=delete
vtk::CompositeDataSetNodeReference::GetMetaData
vtkInformation * GetMetaData() const
Definition: vtkCompositeDataSetNodeReference.h:263
vtk
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
Definition: vtkCollectionRange.h:28
vtk::CompositeDataSetNodeReference::operator==
friend bool operator==(const CompositeDataSetNodeReference &lhs, const CompositeDataSetNodeReference &rhs)
Definition: vtkCompositeDataSetNodeReference.h:194
vtk::CompositeDataSetNodeReference::CompositeDataSetNodeReference
CompositeDataSetNodeReference(IteratorType *iterator)
Definition: vtkCompositeDataSetNodeReference.h:172
vtkCompositeDataSet::GetDataSet
virtual vtkDataObject * GetDataSet(vtkCompositeDataIterator *iter)=0
Returns the dataset located at the positiong pointed by the iterator.
vtk::CompositeDataSetNodeReference::SetDataObject
void SetDataObject(vtkCompositeDataSet *other, vtkDataObject *dObj)
Definition: vtkCompositeDataSetNodeReference.h:239
vtkDataObject
general representation of visualization data
Definition: vtkDataObject.h:59
vtk::detail::MTimeWatcher::Reset
void Reset(vtkObject *o)
Definition: vtkCompositeDataSetNodeReference.h:58
vtk::CompositeDataSetNodeReference::GetDataObject
vtkDataObject * GetDataObject() const
Definition: vtkCompositeDataSetNodeReference.h:207
vtkWeakPointer
a weak reference to a vtkObject.
Definition: vtkMeta.h:34
vtk::CompositeDataSetNodeReference
A reference proxy into a vtkCompositeDataSet, obtained by dereferencing an iterator from the vtk::Ran...
Definition: vtkCompositeDataSetNodeReference.h:148
vtk::detail::MTimeWatcher::MTimeWatcher
MTimeWatcher(vtkObject *o)
Definition: vtkCompositeDataSetNodeReference.h:53
vtkMTimeType
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:285
vtk::detail::MTimeWatcher::MTimeWatcher
MTimeWatcher()=default