VTK
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 
82 template <typename T>
84 {
85  typedef vtkSMPThreadLocal<T*> TLS;
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&);
92 
93 public:
97  vtkSMPThreadLocalObject() : Internal(nullptr), Exemplar(nullptr)
98  {
99  }
100 
101  vtkSMPThreadLocalObject(T* const& exemplar) : Internal(0), Exemplar(exemplar)
102  {
103  }
104 
106  {
107  iterator iter = this->begin();
108  while (iter != this->end())
109  {
110  if (*iter)
111  {
112  (*iter)->Delete();
113  }
114  ++iter;
115  }
116  }
117 
119 
124  T*& Local()
125  {
126  T*& vtkobject = this->Internal.Local();
127  if (!vtkobject)
128  {
129  if (this->Exemplar)
130  {
131  vtkobject = this->Exemplar->NewInstance();
132  }
133  else
134  {
135  vtkobject = T::SafeDownCast(T::New());
136  }
137  }
138  return vtkobject;
139  }
141 
145  size_t size() const
146  {
147  return this->Internal.size();
148  }
149 
151 
157  class iterator
158  {
159  public:
161  {
162  ++this->Iter;
163  return *this;
164  }
166 
168  {
169  iterator copy = *this;
170  ++this->Iter;
171  return copy;
172  }
173 
174  bool operator==(const iterator& other)
175  {
176  return this->Iter == other.Iter;
177  }
178 
179  bool operator!=(const iterator& other)
180  {
181  return this->Iter != other.Iter;
182  }
183 
184  T*& operator*()
185  {
186  return *this->Iter;
187  }
188 
190  {
191  return &*this->Iter;
192  }
193 
194  private:
195  TLSIter Iter;
196 
197  friend class vtkSMPThreadLocalObject<T>;
198  };
199 
200  iterator begin()
201  {
202  iterator iter;
203  iter.Iter = this->Internal.begin();
204  return iter;
205  };
206 
207  iterator end()
208  {
209  iterator iter;
210  iter.Iter = this->Internal.end();
211  return iter;
212  }
213 
214 private:
215  TLS Internal;
216  T* Exemplar;
217 };
218 
219 #endif
220 // VTK-HeaderTest-Exclude: vtkSMPThreadLocalObject.h
bool operator!=(const iterator &other)
vtkSMPThreadLocalObject(T *const &exemplar)
Thread local storage for VTK objects.
Subset of the standard iterator API.
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.