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 =========================================================================*/
79 #ifndef vtkSMPThreadLocalObject_h
80 #define vtkSMPThreadLocalObject_h
81 
82 #include "vtkSMPThreadLocal.h"
83 
84 template <typename T>
86 {
87  typedef vtkSMPThreadLocal<T*> TLS;
88  typedef typename vtkSMPThreadLocal<T*>::iterator TLSIter;
89 
90  // Hide the copy constructor for now and assignment
91  // operator for now.
93  void operator=(const vtkSMPThreadLocalObject&);
94 
95 public:
97 
98  vtkSMPThreadLocalObject() : Internal(0), Exemplar(0)
99  {
100  }
102 
103  vtkSMPThreadLocalObject(T* const& exemplar) : Internal(0), Exemplar(exemplar)
104  {
105  }
106 
108  {
109  iterator iter = this->begin();
110  while (iter != this->end())
111  {
112  if (*iter)
113  {
114  (*iter)->Delete();
115  }
116  ++iter;
117  }
118  }
119 
121 
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 
143 
144  size_t size() const
145  {
146  return this->Internal.size();
147  }
149 
151 
154  class iterator
155  {
156  public:
158  {
159  ++this->Iter;
160  return *this;
161  }
163 
165  {
166  iterator copy = *this;
167  ++this->Iter;
168  return copy;
169  }
170 
171  bool operator==(const iterator& other)
172  {
173  return this->Iter == other.Iter;
174  }
175 
176  bool operator!=(const iterator& other)
177  {
178  return this->Iter != other.Iter;
179  }
180 
181  T*& operator*()
182  {
183  return *this->Iter;
184  }
185 
187  {
188  return &*this->Iter;
189  }
190 
191  private:
192  TLSIter Iter;
193 
194  friend class vtkSMPThreadLocalObject<T>;
195  };
196 
197  iterator begin()
198  {
199  iterator iter;
200  iter.Iter = this->Internal.begin();
201  return iter;
202  };
203 
204  iterator end()
205  {
206  iterator iter;
207  iter.Iter = this->Internal.end();
208  return iter;
209  }
210 
211 private:
212  TLS Internal;
213  T* Exemplar;
214 };
215 
216 #endif
217 // VTK-HeaderTest-Exclude: vtkSMPThreadLocalObject.h
bool operator!=(const iterator &other)
vtkSMPThreadLocalObject(T *const &exemplar)
Thread local storage for VTK objects.
bool operator==(const iterator &other)
size_t size() const