VTK
/Users/kitware/Dashboards/MyTests/VTK_BLD_Release_docs/Utilities/Doxygen/dox/Common/Core/vtkSMPThreadLocalObject.h
Go to the documentation of this file.
00001  /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkSMPThreadLocalObject.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00079 #ifndef vtkSMPThreadLocalObject_h
00080 #define vtkSMPThreadLocalObject_h
00081 
00082 #include "vtkSMPThreadLocal.h"
00083 
00084 template <typename T>
00085 class vtkSMPThreadLocalObject
00086 {
00087   typedef vtkSMPThreadLocal<T*> TLS;
00088   typedef typename vtkSMPThreadLocal<T*>::iterator TLSIter;
00089 
00090   // Hide the copy constructor for now and assignment
00091   // operator for now.
00092   vtkSMPThreadLocalObject(const vtkSMPThreadLocalObject&);
00093   void operator=(const vtkSMPThreadLocalObject&);
00094 
00095 public:
00097 
00098   vtkSMPThreadLocalObject() : Internal(0), Exemplar(0)
00099   {
00100   }
00102 
00103   vtkSMPThreadLocalObject(T* const& exemplar) : Internal(0), Exemplar(exemplar)
00104   {
00105   }
00106 
00107   virtual ~vtkSMPThreadLocalObject()
00108   {
00109     iterator iter = this->begin();
00110     while (iter != this->end())
00111       {
00112       if (*iter)
00113         {
00114         (*iter)->Delete();
00115         }
00116       ++iter;
00117       }
00118   }
00119 
00121 
00124   T*& Local()
00125   {
00126     T*& vtkobject = this->Internal.Local();
00127     if (!vtkobject)
00128       {
00129       if (this->Exemplar)
00130         {
00131         vtkobject = this->Exemplar->NewInstance();
00132         }
00133       else
00134         {
00135         vtkobject = T::SafeDownCast(T::New());
00136         }
00137       }
00138     return vtkobject;
00139   }
00141 
00143 
00144   size_t size() const
00145     {
00146     return this->Internal.size();
00147     }
00149 
00151 
00154   class iterator
00155   {
00156   public:
00157     iterator& operator++()
00158     {
00159       ++this->Iter;
00160       return *this;
00161     }
00163 
00164     iterator operator++(int)
00165       {
00166         iterator copy = *this;
00167         ++this->Iter;
00168         return copy;
00169       }
00170 
00171     bool operator==(const iterator& other)
00172       {
00173       return this->Iter == other.Iter;
00174       }
00175 
00176     bool operator!=(const iterator& other)
00177     {
00178       return this->Iter != other.Iter;
00179     }
00180 
00181     T*& operator*()
00182     {
00183       return *this->Iter;
00184     }
00185 
00186     T** operator->()
00187       {
00188         return &*this->Iter;
00189       }
00190 
00191   private:
00192     TLSIter Iter;
00193 
00194     friend class vtkSMPThreadLocalObject<T>;
00195   };
00196 
00197   iterator begin()
00198     {
00199       iterator iter;
00200       iter.Iter = this->Internal.begin();
00201       return iter;
00202     };
00203 
00204   iterator end()
00205     {
00206       iterator iter;
00207       iter.Iter = this->Internal.end();
00208       return iter;
00209     }
00210 
00211 private:
00212   TLS Internal;
00213   T* Exemplar;
00214 };
00215 
00216 #endif
00217 // VTK-HeaderTest-Exclude: vtkSMPThreadLocalObject.h