VTK  9.3.1
vtkCollection.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
22 #ifndef vtkCollection_h
23 #define vtkCollection_h
24 
25 #include "vtkCommonCoreModule.h" // For export macro
26 #include "vtkObject.h"
27 
28 VTK_ABI_NAMESPACE_BEGIN
29 class vtkCollectionElement //;prevents pick-up by man page generator
30 {
31 public:
33  : Item(nullptr)
34  , Next(nullptr)
35  {
36  }
39 };
41 
43 
44 class VTKCOMMONCORE_EXPORT vtkCollection : public vtkObject
45 {
46 public:
47  vtkTypeMacro(vtkCollection, vtkObject);
48  void PrintSelf(ostream& os, vtkIndent indent) override;
49 
53  static vtkCollection* New();
54 
58  void AddItem(vtkObject*);
59 
64  void InsertItem(int i, vtkObject*);
65 
69  void ReplaceItem(int i, vtkObject*);
70 
78  void RemoveItem(int i);
79 
85  void RemoveItem(vtkObject*);
86 
90  void RemoveAllItems();
91 
97  int IsItemPresent(vtkObject* a);
98 
104  int IndexOfFirstOccurence(vtkObject* a);
105 
109  int GetNumberOfItems() { return this->NumberOfItems; }
110 
115  void InitTraversal() { this->Current = this->Top; }
116 
122  {
123  cookie = static_cast<vtkCollectionSimpleIterator>(this->Top);
124  }
125 
130  vtkObject* GetNextItemAsObject();
131 
136  vtkObject* GetItemAsObject(int i);
137 
142  vtkObject* GetNextItemAsObject(vtkCollectionSimpleIterator& cookie);
143 
147  VTK_NEWINSTANCE vtkCollectionIterator* NewIterator();
148 
150 
153  bool UsesGarbageCollector() const override { return true; }
155 
156 protected:
157  vtkCollection();
158  ~vtkCollection() override;
159 
160  virtual void RemoveElement(vtkCollectionElement* element, vtkCollectionElement* previous);
161  virtual void DeleteElement(vtkCollectionElement*);
166 
167  friend class vtkCollectionIterator;
168 
169  // See vtkGarbageCollector.h:
170  void ReportReferences(vtkGarbageCollector* collector) override;
171 
172 private:
173  vtkCollection(const vtkCollection&) = delete;
174  void operator=(const vtkCollection&) = delete;
175 };
176 
178 {
179  vtkCollectionElement* elem = this->Current;
180 
181  if (elem != nullptr)
182  {
183  this->Current = elem->Next;
184  return elem->Item;
185  }
186  else
187  {
188  return nullptr;
189  }
190 }
191 
193 {
194  vtkCollectionElement* elem = static_cast<vtkCollectionElement*>(cookie);
195 
196  if (elem != nullptr)
197  {
198  cookie = static_cast<void*>(elem->Next);
199  return elem->Item;
200  }
201  else
202  {
203  return nullptr;
204  }
205 }
206 
207 VTK_ABI_NAMESPACE_END
208 #endif
void * vtkCollectionSimpleIterator
Definition: vtkCollection.h:40
abstract base class for most VTK objects
Definition: vtkObject.h:51
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
void InitTraversal()
Initialize the traversal of the collection.
vtkCollectionElement * Current
bool UsesGarbageCollector() const override
Participate in garbage collection.
Detect and break reference loops.
a simple class to control print indentation
Definition: vtkIndent.h:28
virtual void ReportReferences(vtkGarbageCollector *)
#define VTK_NEWINSTANCE
vtkCollectionElement * Bottom
iterator through a vtkCollection.
int GetNumberOfItems()
Return the number of objects in the list.
create and manipulate ordered lists of objects
Definition: vtkCollection.h:44
void InitTraversal(vtkCollectionSimpleIterator &cookie)
A reentrant safe way to iterate through a collection.
vtkCollectionElement * Next
Definition: vtkCollection.h:38
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
vtkObject * GetNextItemAsObject()
Get the next item in the collection.
vtkCollectionElement * Top