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 | * Pierre Phaneuf <[email protected]>
|
---|
26 | *
|
---|
27 | * Alternatively, the contents of this file may be used under the terms of
|
---|
28 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
29 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
30 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
31 | * of those above. If you wish to allow use of your version of this file only
|
---|
32 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
33 | * use your version of this file under the terms of the MPL, indicate your
|
---|
34 | * decision by deleting the provisions above and replace them with the notice
|
---|
35 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
36 | * the provisions above, a recipient may use your version of this file under
|
---|
37 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
38 | *
|
---|
39 | * ***** END LICENSE BLOCK ***** */
|
---|
40 |
|
---|
41 | // nsWeakReference.cpp
|
---|
42 |
|
---|
43 | #include "nsWeakReference.h"
|
---|
44 | #include "nsCOMPtr.h"
|
---|
45 |
|
---|
46 | nsresult
|
---|
47 | nsQueryReferent::operator()( const nsIID& aIID, void** answer ) const
|
---|
48 | {
|
---|
49 | nsresult status;
|
---|
50 | if ( mWeakPtr )
|
---|
51 | {
|
---|
52 | if ( NS_FAILED(status = mWeakPtr->QueryReferent(aIID, answer)) )
|
---|
53 | *answer = 0;
|
---|
54 | }
|
---|
55 | else
|
---|
56 | status = NS_ERROR_NULL_POINTER;
|
---|
57 |
|
---|
58 | if ( mErrorPtr )
|
---|
59 | *mErrorPtr = status;
|
---|
60 | return status;
|
---|
61 | }
|
---|
62 |
|
---|
63 | nsresult
|
---|
64 | nsGetWeakReference::operator()( const nsIID&, void** aResult ) const
|
---|
65 | {
|
---|
66 | nsresult status;
|
---|
67 | // nsIWeakReference** result = &NS_STATIC_CAST(nsIWeakReference*, *aResult);
|
---|
68 | *aResult = 0;
|
---|
69 |
|
---|
70 | if ( mRawPtr )
|
---|
71 | {
|
---|
72 | nsCOMPtr<nsISupportsWeakReference> factoryPtr = do_QueryInterface(mRawPtr, &status);
|
---|
73 | NS_ASSERTION(factoryPtr, "Oops! You're asking for a weak reference to an object that doesn't support that.");
|
---|
74 | if ( factoryPtr )
|
---|
75 | {
|
---|
76 | nsIWeakReference* temp;
|
---|
77 | status = factoryPtr->GetWeakReference(&temp);
|
---|
78 | *aResult = temp;
|
---|
79 | }
|
---|
80 | // else, |status| has already been set by |do_QueryInterface|
|
---|
81 | }
|
---|
82 | else
|
---|
83 | status = NS_ERROR_NULL_POINTER;
|
---|
84 |
|
---|
85 | if ( mErrorPtr )
|
---|
86 | *mErrorPtr = status;
|
---|
87 | return status;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | NS_COM nsIWeakReference* // or else |already_AddRefed<nsIWeakReference>|
|
---|
92 | NS_GetWeakReference( nsISupports* aInstancePtr, nsresult* aErrorPtr )
|
---|
93 | {
|
---|
94 | void* result = 0;
|
---|
95 | nsGetWeakReference(aInstancePtr, aErrorPtr)(NS_GET_IID(nsIWeakReference), &result);
|
---|
96 | return NS_STATIC_CAST(nsIWeakReference*, result);
|
---|
97 | }
|
---|
98 |
|
---|
99 | NS_COM nsresult
|
---|
100 | nsSupportsWeakReference::GetWeakReference( nsIWeakReference** aInstancePtr )
|
---|
101 | {
|
---|
102 | if ( !aInstancePtr )
|
---|
103 | return NS_ERROR_NULL_POINTER;
|
---|
104 |
|
---|
105 | if ( !mProxy )
|
---|
106 | mProxy = new nsWeakReference(this);
|
---|
107 | *aInstancePtr = mProxy;
|
---|
108 |
|
---|
109 | nsresult status;
|
---|
110 | if ( !*aInstancePtr )
|
---|
111 | status = NS_ERROR_OUT_OF_MEMORY;
|
---|
112 | else
|
---|
113 | {
|
---|
114 | NS_ADDREF(*aInstancePtr);
|
---|
115 | status = NS_OK;
|
---|
116 | }
|
---|
117 |
|
---|
118 | return status;
|
---|
119 | }
|
---|
120 |
|
---|
121 | NS_IMETHODIMP_(nsrefcnt)
|
---|
122 | nsWeakReference::AddRef()
|
---|
123 | {
|
---|
124 | return ++mRefCount;
|
---|
125 | }
|
---|
126 |
|
---|
127 | NS_IMETHODIMP_(nsrefcnt)
|
---|
128 | nsWeakReference::Release()
|
---|
129 | {
|
---|
130 | nsrefcnt temp = --mRefCount;
|
---|
131 | if ( !mRefCount )
|
---|
132 | delete this;
|
---|
133 | return temp;
|
---|
134 | }
|
---|
135 |
|
---|
136 | NS_IMETHODIMP
|
---|
137 | nsWeakReference::QueryInterface( const nsIID& aIID, void** aInstancePtr )
|
---|
138 | {
|
---|
139 | NS_ASSERTION(aInstancePtr, "QueryInterface requires a non-NULL destination!");
|
---|
140 |
|
---|
141 | if ( !aInstancePtr )
|
---|
142 | return NS_ERROR_NULL_POINTER;
|
---|
143 |
|
---|
144 | nsISupports* foundInterface;
|
---|
145 | if ( aIID.Equals(NS_GET_IID(nsIWeakReference)) )
|
---|
146 | foundInterface = NS_STATIC_CAST(nsIWeakReference*, this);
|
---|
147 | else if ( aIID.Equals(NS_GET_IID(nsISupports)) )
|
---|
148 | foundInterface = NS_STATIC_CAST(nsISupports*, this);
|
---|
149 | else
|
---|
150 | foundInterface = 0;
|
---|
151 |
|
---|
152 | nsresult status;
|
---|
153 | if ( !foundInterface )
|
---|
154 | status = NS_NOINTERFACE;
|
---|
155 | else
|
---|
156 | {
|
---|
157 | NS_ADDREF(foundInterface);
|
---|
158 | status = NS_OK;
|
---|
159 | }
|
---|
160 |
|
---|
161 | *aInstancePtr = foundInterface;
|
---|
162 | return status;
|
---|
163 | }
|
---|
164 |
|
---|
165 | NS_IMETHODIMP
|
---|
166 | nsWeakReference::QueryReferent( const nsIID& aIID, void** aInstancePtr )
|
---|
167 | {
|
---|
168 | return mReferent ? mReferent->QueryInterface(aIID, aInstancePtr) : NS_ERROR_NULL_POINTER;
|
---|
169 | }
|
---|