VTK  9.6.20260219
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 "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
34VTK_ABI_NAMESPACE_BEGIN
36
38
39class VTKCOMMONCORE_EXPORT VTK_MARSHALAUTO vtkCollection : public vtkObject
40{
41public:
42 vtkTypeMacro(vtkCollection, vtkObject);
43 void PrintSelf(ostream& os, vtkIndent indent) override;
44
48 static vtkCollection* New();
49
57
67 void InsertItem(int i, vtkObject*);
68
76 void ReplaceItem(int i, vtkObject*);
77
83 void RemoveItem(int i);
84
94
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
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 = this->Objects.begin(); }
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.")
165
169 std::vector<vtkObject*>::iterator begin() { return this->Objects.begin(); }
170 std::vector<vtkObject*>::iterator end() { return this->Objects.end(); }
171
177 void Sort(std::function<bool(vtkObject*, vtkObject*)> f)
178 {
179 std::sort(this->Objects.begin(), this->Objects.end(), f);
180 }
181
183
186 bool UsesGarbageCollector() const override { return true; }
188
189protected:
191 ~vtkCollection() override;
192
193 // See vtkGarbageCollector.h:
194 void ReportReferences(vtkGarbageCollector* collector) override;
195
196private:
197 std::vector<vtkObject*>::iterator Current;
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.end())
207 {
208 return nullptr;
209 }
210 vtkObject* obj = *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
228VTK_ABI_NAMESPACE_END
229#endif
iterator through a vtkCollection.
bool UsesGarbageCollector() const override
Participate in garbage collection.
int IndexOfFirstOccurence(vtkObject *a) VTK_FUTURE_CONST
Just calls IndexOfFirstOccurrence.
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.
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_DEPRECATED_IN_9_6_0(reason)
#define VTK_MARSHALAUTO
#define VTK_NEWINSTANCE