VTK  9.1.0
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
28namespace vtk
29{
30
31namespace 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;
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
83template <class ObjectType>
85#else
86template <class ObjectType>
87using DebugWeakPointer = ObjectType*;
88#endif
89
90} // end namespace detail
91
146template <typename IteratorType,
147 typename OwnerType>
149 : private detail::DebugMTimeWatcher // empty-base optimization when NDEBUG
150{
151 static_assert(std::is_base_of<vtkCompositeDataIterator, IteratorType>::value,
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
171protected:
172 explicit CompositeDataSetNodeReference(IteratorType* iterator)
173 : detail::DebugMTimeWatcher(iterator)
174 , Iterator(iterator)
175 {
176 }
177
178public:
179 friend OwnerType; // To allow access to protected methods/base class
180
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
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 positiong pointed by the iterator.
general representation of visualization data
Store vtkAlgorithm input/output information.
abstract base class for most VTK objects
Definition: vtkObject.h:82
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)
vtkDataObject * GetDataObject(vtkCompositeDataSet *other)
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
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:287