VTK  9.7.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 "vtkDeprecation.h" // For VTK_DEPRECATED_IN_9_6_0
27 #include "vtkObject.h"
28 #include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
29 
30 #include <algorithm>
31 #include <functional>
32 #include <vector>
33 
34 VTK_ABI_NAMESPACE_BEGIN
36 
38 
39 class VTKCOMMONCORE_EXPORT VTK_MARSHALAUTO vtkCollection : public vtkObject
40 {
41 public:
42  vtkTypeMacro(vtkCollection, vtkObject);
43  void PrintSelf(ostream& os, vtkIndent indent) override;
44 
48  static vtkCollection* New();
49 
56  void AddItem(vtkObject*);
57 
67  void InsertItem(int i, vtkObject*);
68 
76  void ReplaceItem(int i, vtkObject*);
77 
83  void RemoveItem(int i);
84 
93  void RemoveItem(vtkObject*);
94 
100  void RemoveAllItems();
101 
107  int IsItemPresent(vtkObject* a) VTK_FUTURE_CONST;
108 
112  VTK_DEPRECATED_IN_9_6_0("Use correctly spelled IndexOfFirstOccurrence instead.")
113  int IndexOfFirstOccurence(vtkObject* a) VTK_FUTURE_CONST;
114 
120  int IndexOfFirstOccurrence(vtkObject* a) const;
121 
125  int GetNumberOfItems() VTK_FUTURE_CONST { return static_cast<int>(this->Objects.size()); }
126 
131  vtkObject* GetItemAsObject(int i) VTK_FUTURE_CONST;
132 
137  void InitTraversal() { this->Current = 0; }
138 
144  {
145  cookie = static_cast<vtkCollectionSimpleIterator>(this->Objects.data());
146  }
147 
152  vtkObject* GetNextItemAsObject();
153 
158  vtkObject* GetNextItemAsObject(vtkCollectionSimpleIterator& cookie) VTK_FUTURE_CONST;
159 
163  VTK_DEPRECATED_IN_9_7_0("Use vtk::Range instead.")
164  VTK_NEWINSTANCE vtkCollectionIterator* NewIterator();
165 
169  std::vector<vtkObject*>::iterator begin() { return this->Objects.begin(); }
170  std::vector<vtkObject*>::iterator end() { return this->Objects.end(); }
171 
178  {
179  std::sort(this->Objects.begin(), this->Objects.end(), f);
180  }
181 
183 
186  bool UsesGarbageCollector() const override { return true; }
188 
189 protected:
190  vtkCollection() = default;
191  ~vtkCollection() override;
192 
193  // See vtkGarbageCollector.h:
194  void ReportReferences(vtkGarbageCollector* collector) override;
195 
196 private:
197  std::size_t Current = 0;
198  std::vector<vtkObject*> Objects;
199 
200  vtkCollection(const vtkCollection&) = delete;
201  void operator=(const vtkCollection&) = delete;
202 };
203 
205 {
206  if (this->Current >= this->Objects.size())
207  {
208  return nullptr;
209  }
210  vtkObject* obj = this->Objects[this->Current];
211  this->Current++;
212  return obj;
213 }
214 
216  vtkCollectionSimpleIterator& cookie) VTK_FUTURE_CONST
217 {
218  vtkObject** elem = static_cast<vtkObject**>(cookie);
219 
220  if (elem >= this->Objects.data() + this->Objects.size())
221  {
222  return nullptr;
223  }
224  cookie = static_cast<vtkCollectionSimpleIterator>(elem + 1);
225  return *elem;
226 }
227 
228 VTK_ABI_NAMESPACE_END
229 #endif
void * vtkCollectionSimpleIterator
Definition: vtkCollection.h:35
abstract base class for most VTK objects
Definition: vtkObject.h:52
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.
bool UsesGarbageCollector() const override
Participate in garbage collection.
Detect and break reference loops.
#define VTK_DEPRECATED_IN_9_6_0(reason)
a simple class to control print indentation
Definition: vtkIndent.h:28
virtual void ReportReferences(vtkGarbageCollector *)
#define VTK_NEWINSTANCE
iterator through a vtkCollection.
std::vector< vtkObject * >::iterator end()
create and manipulate ordered lists of objects
Definition: vtkCollection.h:39
void InitTraversal(vtkCollectionSimpleIterator &cookie)
A reentrant safe way to iterate through a collection.
void Sort(std::function< bool(vtkObject *, vtkObject *)> f)
Sort the collection according to a given std::function that should return true if the first vtkObject...
#define VTK_MARSHALAUTO
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.
#define VTK_DEPRECATED_IN_9_7_0(reason)