VTK
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 =========================================================================*/
00076 #ifndef __vtkSMPThreadLocalObject_h
00077 #define __vtkSMPThreadLocalObject_h
00078 
00079 #include <vtkSMPThreadLocal.h>
00080 
00081 template <typename T>
00082 class vtkSMPThreadLocalObject
00083 {
00084   typedef vtkSMPThreadLocal<T*> TLS;
00085   typedef typename vtkSMPThreadLocal<T*>::iterator TLSIter;
00086 
00087   // Hide the copy constructor for now and assignment
00088   // operator for now.
00089   vtkSMPThreadLocalObject(const vtkSMPThreadLocalObject&);
00090   void operator=(const vtkSMPThreadLocalObject&);
00091 
00092 public:
00094 
00095   vtkSMPThreadLocalObject() : Internal(0)
00096     {
00097     }
00099 
00100   virtual ~vtkSMPThreadLocalObject()
00101     {
00102       iterator iter = this->begin();
00103       while (iter != this->end())
00104         {
00105         if (*iter)
00106           {
00107           (*iter)->Delete();
00108           }
00109         ++iter;
00110         }
00111     }
00112 
00114 
00117   T*& Local()
00118     {
00119       T*& vtkobject = this->Internal.Local();
00120       if (!vtkobject)
00121         {
00122         vtkobject = T::New();
00123         }
00124       return vtkobject;
00125     }
00127 
00129 
00132   class iterator
00133   {
00134   public:
00135     iterator& operator++()
00136       {
00137         ++this->Iter;
00138         return *this;
00139       }
00141 
00142     bool operator!=(const iterator& other)
00143       {
00144         return this->Iter != other.Iter;
00145       }
00146 
00147     T*& operator*()
00148       {
00149         return *this->Iter;
00150       }
00151 
00152   private:
00153     TLSIter Iter;
00154 
00155     friend class vtkSMPThreadLocalObject<T>;
00156   };
00157 
00158   iterator begin()
00159     {
00160       iterator iter;
00161       iter.Iter = this->Internal.begin();
00162       return iter;
00163     };
00164 
00165   iterator end()
00166     {
00167       iterator iter;
00168       iter.Iter = this->Internal.end();
00169       return iter;
00170     }
00171 
00172 private:
00173   TLS Internal;
00174 };
00175 
00176 #endif
00177 // VTK-HeaderTest-Exclude: vtkSMPThreadLocalObject.h