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

Contents 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


Revision 330 - (show annotations) (download)
Tue Dec 28 04:24:23 2010 UTC (9 years, 9 months ago) by william
File MIME type: text/plain
File size: 3279 byte(s)
merged upstream r4154-r4160
1 /* PCSX2 - PS2 Emulator for PCs
2 * Copyright (C) 2002-2010 PCSX2 Dev Team
3 *
4 * 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-
6 * 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;
9 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 * 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.
13 * If not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #pragma once
17
18 #include "Threading.h"
19 using Threading::ScopedLock;
20
21 // --------------------------------------------------------------------------------------
22 // ScopedPtrMT
23 // --------------------------------------------------------------------------------------
24
25 template< typename T >
26 class ScopedPtrMT
27 {
28 DeclareNoncopyableObject(ScopedPtrMT);
29
30 typedef T* TPtr;
31
32 protected:
33 volatile TPtr m_ptr;
34 Threading::Mutex m_mtx;
35
36 public:
37 typedef T element_type;
38
39 wxEXPLICIT ScopedPtrMT(T * ptr = NULL)
40 {
41 m_ptr = ptr;
42 }
43
44 ~ScopedPtrMT() throw() { _Delete_unlocked(); }
45
46 ScopedPtrMT& Reassign(T * ptr = NULL)
47 {
48 TPtr doh = (TPtr)Threading::AtomicExchangePointer( m_ptr, ptr );
49 if ( ptr != doh ) delete doh;
50 return *this;
51 }
52
53 ScopedPtrMT& Delete() throw()
54 {
55 ScopedLock lock( m_mtx );
56 _Delete_unlocked();
57 }
58
59 // Removes the pointer from scoped management, but does not delete!
60 // (ScopedPtr will be NULL after this method)
61 T *DetachPtr()
62 {
63 ScopedLock lock( m_mtx );
64
65 T *ptr = m_ptr;
66 m_ptr = NULL;
67 return ptr;
68 }
69
70 // Returns the managed pointer. Can return NULL as a valid result if the ScopedPtrMT
71 // has no object in management.
72 T* GetPtr() const
73 {
74 return m_ptr;
75 }
76
77 void SwapPtr(ScopedPtrMT& other)
78 {
79 ScopedLock lock( m_mtx );
80 T * const tmp = other.m_ptr;
81 other.m_ptr = m_ptr;
82 m_ptr = tmp;
83 }
84
85 // ----------------------------------------------------------------------------
86 // ScopedPtrMT Operators
87 // ----------------------------------------------------------------------------
88 // 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
90 // allow the T* implicit casting.
91
92 bool operator!() const throw()
93 {
94 return m_ptr == NULL;
95 }
96
97 // Equality
98 bool operator==(T* pT) const throw()
99 {
100 return m_ptr == pT;
101 }
102
103 // Inequality
104 bool operator!=(T* pT) const throw()
105 {
106 return !operator==(pT);
107 }
108
109 // Convenient assignment operator. ScopedPtrMT = NULL will issue an automatic deletion
110 // of the managed pointer.
111 ScopedPtrMT& operator=( T* src )
112 {
113 return Reassign( src );
114 }
115
116 #if 0
117 operator T*() const
118 {
119 return m_ptr;
120 }
121
122 // Dereference operator, returns a handle to the managed pointer.
123 // Generates a debug assertion if the object is NULL!
124 T& operator*() const
125 {
126 pxAssert(m_ptr != NULL);
127 return *m_ptr;
128 }
129
130 T* operator->() const
131 {
132 pxAssert(m_ptr != NULL);
133 return m_ptr;
134 }
135 #endif
136
137 protected:
138 void _Delete_unlocked() throw()
139 {
140 delete m_ptr;
141 m_ptr = NULL;
142 }
143 };

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.22