VTK  9.7.20260715
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
21
22#ifndef vtkCollection_h
23#define vtkCollection_h
24
25#include "vtkCommonCoreModule.h" // For export macro
26#include "vtkObject.h"
27#include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
28
29#include <algorithm>
30#include <functional>
31#include <vector>
32
33VTK_ABI_NAMESPACE_BEGIN
35
37
38class VTKCOMMONCORE_EXPORT VTK_MARSHALAUTO vtkCollection : public vtkObject
39{
40public:
41 vtkTypeMacro(vtkCollection, vtkObject);
42 void PrintSelf(ostream& os, vtkIndent indent) override;
43
47 static vtkCollection* New();
48
56
66 void InsertItem(int i, vtkObject*);
67
75 void ReplaceItem(int i, vtkObject*);
76
82 void RemoveItem(int i);
83
93
100
106 int IsItemPresent(vtkObject* a) VTK_FUTURE_CONST;
107
114
118 int GetNumberOfItems() VTK_FUTURE_CONST { return static_cast<int>(this->Objects.size()); }
119
124 vtkObject* GetItemAsObject(int i) VTK_FUTURE_CONST;
125
130 void InitTraversal() { this->Current = 0; }
131
137 {
138 cookie = static_cast<vtkCollectionSimpleIterator>(this->Objects.data());
139 }
140
145 vtkObject* GetNextItemAsObject();
146
151 vtkObject* GetNextItemAsObject(vtkCollectionSimpleIterator& cookie) VTK_FUTURE_CONST;
152
156 VTK_DEPRECATED_IN_9_7_0("Use vtk::Range instead.")
158
162 std::vector<vtkObject*>::iterator begin() { return this->Objects.begin(); }
163 std::vector<vtkObject*>::iterator end() { return this->Objects.end(); }
164
170 void Sort(std::function<bool(vtkObject*, vtkObject*)> f)
171 {
172 std::sort(this->Objects.begin(), this->Objects.end(), f);
173 }
174
176
179 bool UsesGarbageCollector() const override { return true; }
181
182protected:
183 vtkCollection() = default;
184 ~vtkCollection() override;
185
186 // See vtkGarbageCollector.h:
187 void ReportReferences(vtkGarbageCollector* collector) override;
188
189private:
190 std::size_t Current = 0;
191 std::vector<vtkObject*> Objects;
192
193 vtkCollection(const vtkCollection&) = delete;
194 void operator=(const vtkCollection&) = delete;
195};
196
198{
199 if (this->Current >= this->Objects.size())
200 {
201 return nullptr;
202 }
203 vtkObject* obj = this->Objects[this->Current];
204 this->Current++;
205 return obj;
206}
207
209 vtkCollectionSimpleIterator& cookie) VTK_FUTURE_CONST
210{
211 vtkObject** elem = static_cast<vtkObject**>(cookie);
212
213 if (elem >= this->Objects.data() + this->Objects.size())
214 {
215 return nullptr;
216 }
217 cookie = static_cast<vtkCollectionSimpleIterator>(elem + 1);
218 return *elem;
219}
220
221VTK_ABI_NAMESPACE_END
222#endif
iterator through a vtkCollection.
bool UsesGarbageCollector() const override
Participate in garbage collection.
void ReportReferences(vtkGarbageCollector *collector) override
std::vector< vtkObject * >::iterator end()
void RemoveItem(int i)
Remove the i'th item in the collection.
~vtkCollection() override
void ReplaceItem(int i, vtkObject *)
Replace the i'th item in the collection with the given item.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
int IndexOfFirstOccurrence(vtkObject *a) const
Search for the given item and return the 0-based index of its first occurrence in the collection.
vtkCollection()=default
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...
vtkCollectionIterator * NewIterator()
Get an iterator to traverse the items in this collection.
void RemoveItem(vtkObject *)
Remove the first occurrence of the given item from the collection.
int IsItemPresent(vtkObject *a) VTK_FUTURE_CONST
Search for the given item and return the 1-based index of its first occurrence in the collection.
void AddItem(vtkObject *)
Add given item to the bottom (end) of the collection.
void InsertItem(int i, vtkObject *)
Insert given item into the collection after the i'th item.
void InitTraversal()
Initialize the traversal of the collection.
std::vector< vtkObject * >::iterator begin()
Add support for C++11 range-based for loops.
void RemoveAllItems()
Remove all items from the collection.
static vtkCollection * New()
Construct an empty collection.
void InitTraversal(vtkCollectionSimpleIterator &cookie)
A reentrant safe way to iterate through a collection.
int GetNumberOfItems() VTK_FUTURE_CONST
Return the number of items in the collection.
vtkObject * GetItemAsObject(int i) VTK_FUTURE_CONST
Get the i'th item in the collection.
vtkObject * GetNextItemAsObject()
Get the next item in the collection.
a simple class to control print indentation
Definition vtkIndent.h:108
friend class vtkGarbageCollector
Some classes need to clear the reference counts manually due to the way they work.
abstract base class for most VTK objects
Definition vtkObject.h:162
iterator end()
Returns a new iterator pointing to past the end of the local storage container.
void * vtkCollectionSimpleIterator
#define VTK_DEPRECATED_IN_9_7_0(reason)
#define VTK_MARSHALAUTO
#define VTK_NEWINSTANCE