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 =========================================================================*/
76 #ifndef vtkSMPThreadLocalObject_h
77 #define vtkSMPThreadLocalObject_h
78 
79 #include "vtkSMPThreadLocal.h"
80 
81 template <typename T>
83 {
84  typedef vtkSMPThreadLocal<T*> TLS;
85  typedef typename vtkSMPThreadLocal<T*>::iterator TLSIter;
86 
87  // Hide the copy constructor for now and assignment
88  // operator for now.
90  void operator=(const vtkSMPThreadLocalObject&);
91 
92 public:
94 
95  vtkSMPThreadLocalObject() : Internal(0), Exemplar(0)
96  {
97  }
99 
100  vtkSMPThreadLocalObject(T* const& exemplar) : Internal(0), Exemplar(exemplar)
101  {
102  }
103 
105  {
106  iterator iter = this->begin();
107  while (iter != this->end())
108  {
109  if (*iter)
110  {
111  (*iter)->Delete();
112  }
113  ++iter;
114  }
115  }
116 
118 
121  T*& Local()
122  {
123  T*& vtkobject = this->Internal.Local();
124  if (!vtkobject)
125  {
126  if (this->Exemplar)
127  {
128  vtkobject = this->Exemplar->NewInstance();
129  }
130  else
131  {
132  vtkobject = T::SafeDownCast(T::New());
133  }
134  }
135  return vtkobject;
136  }
138 
140 
141  size_t size() const
142  {
143  return this->Internal.size();
144  }
146 
148 
151  class iterator
152  {
153  public:
155  {
156  ++this->Iter;
157  return *this;
158  }
160 
162  {
163  iterator copy = *this;
164  ++this->Iter;
165  return copy;
166  }
167 
168  bool operator==(const iterator& other)
169  {
170  return this->Iter == other.Iter;
171  }
172 
173  bool operator!=(const iterator& other)
174  {
175  return this->Iter != other.Iter;
176  }
177 
178  T*& operator*()
179  {
180  return *this->Iter;
181  }
182 
184  {
185  return &*this->Iter;
186  }
187 
188  private:
189  TLSIter Iter;
190 
191  friend class vtkSMPThreadLocalObject<T>;
192  };
193 
194  iterator begin()
195  {
196  iterator iter;
197  iter.Iter = this->Internal.begin();
198  return iter;
199  };
200 
201  iterator end()
202  {
203  iterator iter;
204  iter.Iter = this->Internal.end();
205  return iter;
206  }
207 
208 private:
209  TLS Internal;
210  T* Exemplar;
211 };
212 
213 #endif
214 // 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