VTK  9.2.6
vtkSMPThreadLocalObject.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkSMPThreadLocalObject.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
77#ifndef vtkSMPThreadLocalObject_h
78#define vtkSMPThreadLocalObject_h
79
80#include "vtkSMPThreadLocal.h"
81
82template <typename T>
84{
86 typedef typename vtkSMPThreadLocal<T*>::iterator TLSIter;
87
88 // Hide the copy constructor for now and assignment
89 // operator for now.
91 void operator=(const vtkSMPThreadLocalObject&) = delete;
92
93public:
98 : Internal(nullptr)
99 , Exemplar(nullptr)
100 {
101 }
102
103 vtkSMPThreadLocalObject(T* const& exemplar)
104 : Internal(0)
105 , Exemplar(exemplar)
106 {
107 }
108
110 {
111 iterator iter = this->begin();
112 while (iter != this->end())
113 {
114 if (*iter)
115 {
116 (*iter)->Delete();
117 }
118 ++iter;
119 }
120 }
121
123
128 T*& Local()
129 {
130 T*& vtkobject = this->Internal.Local();
131 if (!vtkobject)
132 {
133 if (this->Exemplar)
134 {
135 vtkobject = this->Exemplar->NewInstance();
136 }
137 else
138 {
139 vtkobject = T::SafeDownCast(T::New());
140 }
141 }
142 return vtkobject;
143 }
145
149 size_t size() const { return this->Internal.size(); }
150
152
159 {
160 public:
162 {
163 ++this->Iter;
164 return *this;
165 }
167
169 {
170 iterator copy = *this;
171 ++this->Iter;
172 return copy;
173 }
174
175 bool operator==(const iterator& other) { return this->Iter == other.Iter; }
176
177 bool operator!=(const iterator& other) { return this->Iter != other.Iter; }
178
179 T*& operator*() { return *this->Iter; }
180
181 T** operator->() { return &*this->Iter; }
182
183 private:
184 TLSIter Iter;
185
186 friend class vtkSMPThreadLocalObject<T>;
187 };
188
190 {
191 iterator iter;
192 iter.Iter = this->Internal.begin();
193 return iter;
194 };
195
197 {
198 iterator iter;
199 iter.Iter = this->Internal.end();
200 return iter;
201 }
202
203private:
204 TLS Internal;
205 T* Exemplar;
206};
207
208#endif
209// 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.