/[pcsx2_0.9.7]/branch/debug/0.X/0.9.X/0.9.7/ramdump-lateset/common/include/Utilities/ScopedPtrMT.h
ViewVC logotype

Diff of /branch/debug/0.X/0.9.X/0.9.7/ramdump-lateset/common/include/Utilities/ScopedPtrMT.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 329 by william, Sun Dec 26 08:32:43 2010 UTC revision 330 by william, Tue Dec 28 04:24:23 2010 UTC
# Line 1  Line 1 
1  /*  PCSX2 - PS2 Emulator for PCs  /*  PCSX2 - PS2 Emulator for PCs
2   *  Copyright (C) 2002-2010  PCSX2 Dev Team   *  Copyright (C) 2002-2010  PCSX2 Dev Team
3   *   *
4   *  PCSX2 is free software: you can redistribute it and/or modify it under the terms   *  PCSX2 is free software: you can redistribute it and/or modify it under the terms
5   *  of the GNU Lesser General Public License as published by the Free Software Found-   *  of the GNU Lesser General Public License as published by the Free Software Found-
6   *  ation, either version 3 of the License, or (at your option) any later version.   *  ation, either version 3 of the License, or (at your option) any later version.
7   *   *
8   *  PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;   *  PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9   *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR   *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10   *  PURPOSE.  See the GNU General Public License for more details.   *  PURPOSE.  See the GNU General Public License for more details.
11   *   *
12   *  You should have received a copy of the GNU General Public License along with PCSX2.   *  You should have received a copy of the GNU General Public License along with PCSX2.
13   *  If not, see <http://www.gnu.org/licenses/>.   *  If not, see <http://www.gnu.org/licenses/>.
14   */   */
15    
16  #pragma once  #pragma once
17    
18  #include "Threading.h"  #include "Threading.h"
19  using Threading::ScopedLock;  using Threading::ScopedLock;
20    
21  // --------------------------------------------------------------------------------------  // --------------------------------------------------------------------------------------
22  //  ScopedPtrMT  //  ScopedPtrMT
23  // --------------------------------------------------------------------------------------  // --------------------------------------------------------------------------------------
24    
25  template< typename T >  template< typename T >
26  class ScopedPtrMT  class ScopedPtrMT
27  {  {
28          DeclareNoncopyableObject(ScopedPtrMT);          DeclareNoncopyableObject(ScopedPtrMT);
29    
30          typedef T* TPtr;          typedef T* TPtr;
31    
32  protected:  protected:
33          volatile TPtr           m_ptr;          volatile TPtr           m_ptr;
34          Threading::Mutex        m_mtx;          Threading::Mutex        m_mtx;
35    
36  public:  public:
37          typedef T element_type;          typedef T element_type;
38    
39          wxEXPLICIT ScopedPtrMT(T * ptr = NULL)          wxEXPLICIT ScopedPtrMT(T * ptr = NULL)
40          {          {
41                  m_ptr = ptr;                  m_ptr = ptr;
42          }          }
43    
44          ~ScopedPtrMT() throw() { _Delete_unlocked(); }          ~ScopedPtrMT() throw() { _Delete_unlocked(); }
45    
46          ScopedPtrMT& Reassign(T * ptr = NULL)          ScopedPtrMT& Reassign(T * ptr = NULL)
47          {          {
48                  TPtr doh = (TPtr)Threading::AtomicExchangePointer( m_ptr, ptr );                  TPtr doh = (TPtr)Threading::AtomicExchangePointer( m_ptr, ptr );
49                  if ( ptr != doh ) delete doh;                  if ( ptr != doh ) delete doh;
50                  return *this;                  return *this;
51          }          }
52    
53          ScopedPtrMT& Delete() throw()          ScopedPtrMT& Delete() throw()
54          {          {
55                  ScopedLock lock( m_mtx );                  ScopedLock lock( m_mtx );
56                  _Delete_unlocked();                  _Delete_unlocked();
57          }          }
58                    
59          // Removes the pointer from scoped management, but does not delete!          // Removes the pointer from scoped management, but does not delete!
60          // (ScopedPtr will be NULL after this method)          // (ScopedPtr will be NULL after this method)
61          T *DetachPtr()          T *DetachPtr()
62          {          {
63                  ScopedLock lock( m_mtx );                  ScopedLock lock( m_mtx );
64    
65                  T *ptr = m_ptr;                  T *ptr = m_ptr;
66                  m_ptr = NULL;                  m_ptr = NULL;
67                  return ptr;                  return ptr;
68          }          }
69    
70          // Returns the managed pointer.  Can return NULL as a valid result if the ScopedPtrMT          // Returns the managed pointer.  Can return NULL as a valid result if the ScopedPtrMT
71          // has no object in management.          // has no object in management.
72          T* GetPtr() const          T* GetPtr() const
73          {          {
74                  return m_ptr;                  return m_ptr;
75          }          }
76    
77          void SwapPtr(ScopedPtrMT& other)          void SwapPtr(ScopedPtrMT& other)
78          {          {
79                  ScopedLock lock( m_mtx );                  ScopedLock lock( m_mtx );
80                  T * const tmp = other.m_ptr;                  T * const tmp = other.m_ptr;
81                  other.m_ptr = m_ptr;                  other.m_ptr = m_ptr;
82                  m_ptr = tmp;                  m_ptr = tmp;
83          }          }
84    
85          // ----------------------------------------------------------------------------          // ----------------------------------------------------------------------------
86          //  ScopedPtrMT Operators          //  ScopedPtrMT Operators
87          // ----------------------------------------------------------------------------          // ----------------------------------------------------------------------------
88          // I've decided to use the ATL's approach to pointer validity tests, opposed to          // I've decided to use the ATL's approach to pointer validity tests, opposed to
89          // the wx/boost approach (which uses some bizarre member method pointer crap, and can't          // the wx/boost approach (which uses some bizarre member method pointer crap, and can't
90          // allow the T* implicit casting.          // allow the T* implicit casting.
91    
92          bool operator!() const throw()          bool operator!() const throw()
93          {          {
94                  return m_ptr == NULL;                  return m_ptr == NULL;
95          }          }
96    
97          // Equality          // Equality
98          bool operator==(T* pT) const throw()          bool operator==(T* pT) const throw()
99          {          {
100                  return m_ptr == pT;                  return m_ptr == pT;
101          }          }
102    
103          // Inequality          // Inequality
104          bool operator!=(T* pT) const  throw()          bool operator!=(T* pT) const  throw()
105          {          {
106                  return !operator==(pT);                  return !operator==(pT);
107          }          }
108    
109          // Convenient assignment operator.  ScopedPtrMT = NULL will issue an automatic deletion          // Convenient assignment operator.  ScopedPtrMT = NULL will issue an automatic deletion
110          // of the managed pointer.          // of the managed pointer.
111          ScopedPtrMT& operator=( T* src )          ScopedPtrMT& operator=( T* src )
112          {          {
113                  return Reassign( src );                  return Reassign( src );
114          }          }
115    
116          #if 0          #if 0
117          operator T*() const          operator T*() const
118          {          {
119                  return m_ptr;                  return m_ptr;
120          }          }
121    
122          // Dereference operator, returns a handle to the managed pointer.          // Dereference operator, returns a handle to the managed pointer.
123          // Generates a debug assertion if the object is NULL!          // Generates a debug assertion if the object is NULL!
124          T& operator*() const          T& operator*() const
125          {          {
126                  pxAssert(m_ptr != NULL);                  pxAssert(m_ptr != NULL);
127                  return *m_ptr;                  return *m_ptr;
128          }          }
129    
130          T* operator->() const          T* operator->() const
131          {          {
132                  pxAssert(m_ptr != NULL);                  pxAssert(m_ptr != NULL);
133                  return m_ptr;                  return m_ptr;
134          }          }
135          #endif          #endif
136    
137  protected:  protected:
138          void _Delete_unlocked() throw()          void _Delete_unlocked() throw()
139          {          {
140                  delete m_ptr;                  delete m_ptr;
141                  m_ptr = NULL;                  m_ptr = NULL;
142          }          }
143  };  };

Legend:
Removed from v.329  
changed lines
  Added in v.330

  ViewVC Help
Powered by ViewVC 1.1.22