VTK  9.1.0
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=========================================================================*/
74#ifndef vtkSMPThreadLocalObject_h
75#define vtkSMPThreadLocalObject_h
76
77#include "vtkSMPThreadLocal.h"
78
79template <typename T>
81{
83 typedef typename vtkSMPThreadLocal<T*>::iterator TLSIter;
84
85 // Hide the copy constructor for now and assignment
86 // operator for now.
88 void operator=(const vtkSMPThreadLocalObject&) = delete;
89
90public:
95 : Internal(nullptr)
96 , Exemplar(nullptr)
97 {
98 }
99
100 vtkSMPThreadLocalObject(T* const& exemplar)
101 : Internal(0)
102 , Exemplar(exemplar)
103 {
104 }
105
107 {
108 iterator iter = this->begin();
109 while (iter != this->end())
110 {
111 if (*iter)
112 {
113 (*iter)->Delete();
114 }
115 ++iter;
116 }
117 }
118
120
125 T*& Local()
126 {
127 T*& vtkobject = this->Internal.Local();
128 if (!vtkobject)
129 {
130 if (this->Exemplar)
131 {
132 vtkobject = this->Exemplar->NewInstance();
133 }
134 else
135 {
136 vtkobject = T::SafeDownCast(T::New());
137 }
138 }
139 return vtkobject;
140 }
142
146 size_t size() const { return this->Internal.size(); }
147
149
156 {
157 public:
159 {
160 ++this->Iter;
161 return *this;
162 }
164
166 {
167 iterator copy = *this;
168 ++this->Iter;
169 return copy;
170 }
171
172 bool operator==(const iterator& other) { return this->Iter == other.Iter; }
173
174 bool operator!=(const iterator& other) { return this->Iter != other.Iter; }
175
176 T*& operator*() { return *this->Iter; }
177
178 T** operator->() { return &*this->Iter; }
179
180 private:
181 TLSIter Iter;
182
183 friend class vtkSMPThreadLocalObject<T>;
184 };
185
187 {
188 iterator iter;
189 iter.Iter = this->Internal.begin();
190 return iter;
191 };
192
194 {
195 iterator iter;
196 iter.Iter = this->Internal.end();
197 return iter;
198 }
199
200private:
201 TLS Internal;
202 T* Exemplar;
203};
204
205#endif
206// VTK-HeaderTest-Exclude: vtkSMPThreadLocalObject.h
Subset of the standard iterator API.
bool operator!=(const iterator &other)
bool operator==(const iterator &other)
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.