1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
---|
2 | *
|
---|
3 | * ***** BEGIN LICENSE BLOCK *****
|
---|
4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
5 | *
|
---|
6 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
7 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
8 | * the License. You may obtain a copy of the License at
|
---|
9 | * http://www.mozilla.org/MPL/
|
---|
10 | *
|
---|
11 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
13 | * for the specific language governing rights and limitations under the
|
---|
14 | * License.
|
---|
15 | *
|
---|
16 | * The Original Code is the Mozilla browser.
|
---|
17 | *
|
---|
18 | * The Initial Developer of the Original Code is
|
---|
19 | * Netscape Communications, Inc.
|
---|
20 | * Portions created by the Initial Developer are Copyright (C) 1999
|
---|
21 | * the Initial Developer. All Rights Reserved.
|
---|
22 | *
|
---|
23 | * Contributor(s):
|
---|
24 | * Scott Collins <[email protected]>
|
---|
25 | *
|
---|
26 | * Alternatively, the contents of this file may be used under the terms of
|
---|
27 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
28 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
29 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
30 | * of those above. If you wish to allow use of your version of this file only
|
---|
31 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
32 | * use your version of this file under the terms of the MPL, indicate your
|
---|
33 | * decision by deleting the provisions above and replace them with the notice
|
---|
34 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
35 | * the provisions above, a recipient may use your version of this file under
|
---|
36 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
37 | *
|
---|
38 | * ***** END LICENSE BLOCK ***** */
|
---|
39 |
|
---|
40 | #ifndef nsWeakReference_h__
|
---|
41 | #define nsWeakReference_h__
|
---|
42 |
|
---|
43 | // nsWeakReference.h
|
---|
44 |
|
---|
45 | #include "nsIWeakReference.h"
|
---|
46 |
|
---|
47 | class nsWeakReference;
|
---|
48 |
|
---|
49 | #undef IMETHOD_VISIBILITY
|
---|
50 | #define IMETHOD_VISIBILITY NS_VISIBILITY_DEFAULT
|
---|
51 |
|
---|
52 | class NS_COM nsSupportsWeakReference : public nsISupportsWeakReference
|
---|
53 | {
|
---|
54 | public:
|
---|
55 | nsSupportsWeakReference()
|
---|
56 | : mProxy(0)
|
---|
57 | {
|
---|
58 | // nothing else to do here
|
---|
59 | }
|
---|
60 |
|
---|
61 | NS_DECL_NSISUPPORTSWEAKREFERENCE
|
---|
62 |
|
---|
63 | protected:
|
---|
64 | inline ~nsSupportsWeakReference();
|
---|
65 |
|
---|
66 | private:
|
---|
67 | friend class nsWeakReference;
|
---|
68 |
|
---|
69 | void
|
---|
70 | NoticeProxyDestruction()
|
---|
71 | // ...called (only) by an |nsWeakReference| from _its_ dtor.
|
---|
72 | {
|
---|
73 | mProxy = 0;
|
---|
74 | }
|
---|
75 |
|
---|
76 | nsWeakReference* mProxy;
|
---|
77 |
|
---|
78 | protected:
|
---|
79 |
|
---|
80 | inline void ClearWeakReferences();
|
---|
81 | PRBool HasWeakReferences() const {return mProxy != 0;}
|
---|
82 | };
|
---|
83 |
|
---|
84 | #undef IMETHOD_VISIBILITY
|
---|
85 | #define IMETHOD_VISIBILITY NS_VISIBILITY_HIDDEN
|
---|
86 |
|
---|
87 | class NS_COM nsWeakReference : public nsIWeakReference
|
---|
88 | {
|
---|
89 | public:
|
---|
90 | // nsISupports...
|
---|
91 | NS_IMETHOD_(nsrefcnt) AddRef();
|
---|
92 | NS_IMETHOD_(nsrefcnt) Release();
|
---|
93 | NS_IMETHOD QueryInterface( const nsIID&, void** );
|
---|
94 |
|
---|
95 | // nsIWeakReference...
|
---|
96 | NS_DECL_NSIWEAKREFERENCE
|
---|
97 |
|
---|
98 | private:
|
---|
99 | friend class nsSupportsWeakReference;
|
---|
100 |
|
---|
101 | nsWeakReference( nsSupportsWeakReference* referent )
|
---|
102 | : mRefCount(0),
|
---|
103 | mReferent(referent)
|
---|
104 | // ...I can only be constructed by an |nsSupportsWeakReference|
|
---|
105 | {
|
---|
106 | // nothing else to do here
|
---|
107 | }
|
---|
108 |
|
---|
109 | ~nsWeakReference()
|
---|
110 | // ...I will only be destroyed by calling |delete| myself.
|
---|
111 | {
|
---|
112 | if ( mReferent )
|
---|
113 | mReferent->NoticeProxyDestruction();
|
---|
114 | }
|
---|
115 |
|
---|
116 | void
|
---|
117 | NoticeReferentDestruction()
|
---|
118 | // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor.
|
---|
119 | {
|
---|
120 | mReferent = 0;
|
---|
121 | }
|
---|
122 |
|
---|
123 | nsrefcnt mRefCount;
|
---|
124 | nsSupportsWeakReference* mReferent;
|
---|
125 | };
|
---|
126 |
|
---|
127 | inline
|
---|
128 | void
|
---|
129 | nsSupportsWeakReference::ClearWeakReferences()
|
---|
130 | /*
|
---|
131 | Usually being called from |nsSupportsWeakReference::~nsSupportsWeakReference|
|
---|
132 | will be good enough, but you may have a case where you need to call disconnect
|
---|
133 | your weak references in an outer destructor (to prevent some client holding a
|
---|
134 | weak reference from re-entering your destructor).
|
---|
135 | */
|
---|
136 | {
|
---|
137 | if ( mProxy )
|
---|
138 | {
|
---|
139 | mProxy->NoticeReferentDestruction();
|
---|
140 | mProxy = 0;
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | inline
|
---|
145 | nsSupportsWeakReference::~nsSupportsWeakReference()
|
---|
146 | {
|
---|
147 | ClearWeakReferences();
|
---|
148 | }
|
---|
149 |
|
---|
150 | #endif
|
---|