VTK  9.4.20241117
vtkSMPThreadLocalObject.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
65#ifndef vtkSMPThreadLocalObject_h
66#define vtkSMPThreadLocalObject_h
67
68#include "vtkSMPThreadLocal.h"
69
70VTK_ABI_NAMESPACE_BEGIN
71template <typename T>
73{
75 typedef typename vtkSMPThreadLocal<T*>::iterator TLSIter;
76
77 // Hide the copy constructor for now and assignment
78 // operator for now.
80 void operator=(const vtkSMPThreadLocalObject&) = delete;
81
82public:
87 : Internal(nullptr)
88 , Exemplar(nullptr)
89 {
90 }
91
92 vtkSMPThreadLocalObject(T* const& exemplar)
93 : Internal(0)
94 , Exemplar(exemplar)
95 {
96 }
97
99 {
100 iterator iter = this->begin();
101 while (iter != this->end())
102 {
103 if (*iter)
104 {
105 (*iter)->Delete();
106 }
107 ++iter;
108 }
109 }
110
112
117 T*& Local()
118 {
119 T*& vtkobject = this->Internal.Local();
120 if (!vtkobject)
121 {
122 if (this->Exemplar)
123 {
124 vtkobject = this->Exemplar->NewInstance();
125 }
126 else
127 {
128 vtkobject = T::SafeDownCast(T::New());
129 }
130 }
131 return vtkobject;
132 }
134
138 size_t size() const { return this->Internal.size(); }
139
141
148 {
149 public:
151 {
152 ++this->Iter;
153 return *this;
154 }
156
158 {
159 iterator copy = *this;
160 ++this->Iter;
161 return copy;
162 }
163
164 bool operator==(const iterator& other) { return this->Iter == other.Iter; }
165
166 bool operator!=(const iterator& other) { return this->Iter != other.Iter; }
167
168 T*& operator*() { return *this->Iter; }
169
170 T** operator->() { return &*this->Iter; }
171
172 private:
173 TLSIter Iter;
174
175 friend class vtkSMPThreadLocalObject<T>;
176 };
177
179 {
180 iterator iter;
181 iter.Iter = this->Internal.begin();
182 return iter;
183 }
184
186 {
187 iterator iter;
188 iter.Iter = this->Internal.end();
189 return iter;
190 }
191
192private:
193 TLS Internal;
194 T* Exemplar;
195};
196
197VTK_ABI_NAMESPACE_END
198#endif
199// VTK-HeaderTest-Exclude: vtkSMPThreadLocalObject.h
Subset of the standard iterator API.
Thread local storage for VTK objects.
size_t size() const
Return the number of thread local objects that have been initialized.
vtkSMPThreadLocalObject(T *const &exemplar)
T *& Local()
Returns an object local to the current thread.
vtkSMPThreadLocalObject()
Default constructor.
Thread local storage for VTK objects.
iterator end()
Returns a new iterator pointing to past the end of the local storage container.
iterator begin()
Returns a new iterator pointing to the beginning of the local storage container.
size_t size()
Return the number of thread local objects that have been initialized.
T & Local()
This needs to be called mainly within a threaded execution path.