VTK  9.3.1
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 
70 VTK_ABI_NAMESPACE_BEGIN
71 template <typename T>
73 {
74  typedef vtkSMPThreadLocal<T*> TLS;
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 
82 public:
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 
147  class iterator
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 
178  iterator begin()
179  {
180  iterator iter;
181  iter.Iter = this->Internal.begin();
182  return iter;
183  };
184 
185  iterator end()
186  {
187  iterator iter;
188  iter.Iter = this->Internal.end();
189  return iter;
190  }
191 
192 private:
193  TLS Internal;
194  T* Exemplar;
195 };
196 
197 VTK_ABI_NAMESPACE_END
198 #endif
199 // VTK-HeaderTest-Exclude: vtkSMPThreadLocalObject.h
T & Local()
This needs to be called mainly within a threaded execution path.
size_t size()
Return the number of thread local objects that have been initialized.
bool operator!=(const iterator &other)
vtkSMPThreadLocalObject(T *const &exemplar)
Thread local storage for VTK objects.
Subset of the standard iterator API.
iterator end()
Returns a new iterator pointing to past the end of the local storage container.
size_t size() const
Return the number of thread local objects that have been initialized.
bool operator==(const iterator &other)
vtkSMPThreadLocalObject()
Default constructor.
T *& Local()
Returns an object local to the current thread.
iterator begin()
Returns a new iterator pointing to the beginning of the local storage container.