sbWeakReference.h
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
25 #ifndef __SB_WEAKREFERENCE_H__
26 #define __SB_WEAKREFERENCE_H__
27 
28 #include <nsIWeakReference.h>
29 #include <nsIWeakReferenceUtils.h>
30 
31 #include <nsAutoLock.h>
32 
33 class sbWeakReference;
34 
35 // Set IMETHOD_VISIBILITY to empty so that the class-level NS_COM declaration
36 // controls member method visibility.
37 #undef IMETHOD_VISIBILITY
38 #define IMETHOD_VISIBILITY NS_COM_GLUE
39 
41 {
42 public:
44  : mProxy(nsnull)
45  , mProxyLock(nsnull) {
46  mProxyLock = nsAutoLock::NewLock("sbSupportsWeakReference::mProxyLock");
47  NS_WARN_IF_FALSE(mProxyLock, "Failed to create lock.");
48  }
49 
50  NS_DECL_NSISUPPORTSWEAKREFERENCE
51 
52 protected:
53  inline ~sbSupportsWeakReference();
54 
55 private:
56  friend class sbWeakReference;
57 
58  void NoticeProxyDestruction() {
59  NS_ENSURE_TRUE(mProxyLock, /*void*/);
60  nsAutoLock lock(mProxyLock);
61  // ...called (only) by an |nsWeakReference| from _its_ dtor.
62  mProxy = nsnull;
63  }
64 
65  sbWeakReference* mProxy;
66  // Lock to protect mProxy.
67  PRLock* mProxyLock;
68 
69 protected:
70  void ClearWeakReferences();
71  PRBool HasWeakReferences() const {
72  NS_ENSURE_TRUE(mProxyLock, PR_FALSE);
73  nsAutoLock lock(mProxyLock);
74  return mProxy != 0;
75  }
76 };
77 
78 #undef IMETHOD_VISIBILITY
79 #define IMETHOD_VISIBILITY NS_VISIBILITY_HIDDEN
80 
81 inline
84 
85  if (mProxyLock) {
86  nsAutoLock::DestroyLock(mProxyLock);
87  }
88 }
89 
90 #endif // __SB_WEAKREFERENCE_H__
PRBool HasWeakReferences() const