Bug 596580: fix versioning on mozJSSubScriptLoader. (r=sayrer,brendan)
[mozilla-central.git] / xpcom / base / nsAgg.h
blob202bc4da5d2bf8f20fbea7db6f1fcce326b5f710
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef nsAgg_h___
39 #define nsAgg_h___
41 #include "nsISupports.h"
42 #include "nsCycleCollectionParticipant.h"
45 ////////////////////////////////////////////////////////////////////////////////
47 // Put NS_DECL_AGGREGATED or NS_DECL_CYCLE_COLLECTING_AGGREGATED in your class's
48 // declaration.
49 #define NS_DECL_AGGREGATED \
50 NS_DECL_ISUPPORTS \
51 NS_DECL_AGGREGATED_HELPER
53 #define NS_DECL_CYCLE_COLLECTING_AGGREGATED \
54 NS_DECL_CYCLE_COLLECTING_ISUPPORTS \
55 NS_DECL_AGGREGATED_HELPER
57 #define NS_DECL_AGGREGATED_HELPER \
58 public: \
60 /** \
61 * Returns the nsISupports pointer of the inner object (aka the \
62 * aggregatee). This pointer is really only useful to the outer object \
63 * (aka the aggregator), which can use it to hold on to the inner \
64 * object. Anything else wants the nsISupports pointer of the outer \
65 * object (gotten by QI'ing inner or outer to nsISupports). This method \
66 * returns a non-addrefed pointer. \
67 * @return the nsISupports pointer of the inner object \
68 */ \
69 nsISupports* InnerObject(void) { return &fAggregated; } \
71 /** \
72 * Returns PR_TRUE if this object is part of an aggregated object. \
73 */ \
74 PRBool IsPartOfAggregated(void) { return fOuter != InnerObject(); } \
76 private: \
78 /* You must implement this operation instead of the nsISupports */ \
79 /* methods. */ \
80 nsresult \
81 AggregatedQueryInterface(const nsIID& aIID, void** aInstancePtr); \
83 class Internal : public nsISupports { \
84 public: \
86 Internal() {} \
88 NS_IMETHOD QueryInterface(const nsIID& aIID, void** aInstancePtr); \
89 NS_IMETHOD_(nsrefcnt) AddRef(void); \
90 NS_IMETHOD_(nsrefcnt) Release(void); \
92 NS_DECL_OWNINGTHREAD \
93 }; \
95 friend class Internal; \
97 nsISupports* fOuter; \
98 Internal fAggregated; \
100 public: \
102 #define NS_DECL_AGGREGATED_CYCLE_COLLECTION_CLASS(_class) \
103 class NS_CYCLE_COLLECTION_INNERCLASS \
104 : public nsXPCOMCycleCollectionParticipant \
106 public: \
107 NS_IMETHOD Unlink(void *p); \
108 NS_IMETHOD Traverse(void *p, \
109 nsCycleCollectionTraversalCallback &cb); \
110 NS_IMETHOD_(void) UnmarkPurple(nsISupports *p) \
112 Downcast(p)->UnmarkPurple(); \
114 static _class* Downcast(nsISupports* s) \
116 return (_class*)((char*)(s) - offsetof(_class, fAggregated)); \
118 static nsISupports* Upcast(_class *p) \
120 return p->InnerObject(); \
122 }; \
123 NS_CYCLE_COLLECTION_PARTICIPANT_INSTANCE
125 // Put this in your class's constructor:
126 #define NS_INIT_AGGREGATED(outer) \
127 PR_BEGIN_MACRO \
128 fOuter = outer ? outer : &fAggregated; \
129 PR_END_MACRO
132 // Put this in your class's implementation file:
133 #define NS_IMPL_AGGREGATED(_class) \
135 NS_IMPL_AGGREGATED_HELPER(_class) \
137 NS_IMETHODIMP_(nsrefcnt) \
138 _class::Internal::AddRef(void) \
140 _class* agg = (_class*)((char*)(this) - offsetof(_class, fAggregated)); \
141 NS_PRECONDITION(PRInt32(agg->mRefCnt) >= 0, "illegal refcnt"); \
142 NS_ASSERT_OWNINGTHREAD(_class); \
143 ++agg->mRefCnt; \
144 NS_LOG_ADDREF(this, agg->mRefCnt, #_class, sizeof(*this)); \
145 return agg->mRefCnt; \
148 NS_IMETHODIMP_(nsrefcnt) \
149 _class::Internal::Release(void) \
151 _class* agg = (_class*)((char*)(this) - offsetof(_class, fAggregated)); \
152 NS_PRECONDITION(0 != agg->mRefCnt, "dup release"); \
153 NS_ASSERT_OWNINGTHREAD(_class); \
154 --agg->mRefCnt; \
155 NS_LOG_RELEASE(this, agg->mRefCnt, #_class); \
156 if (agg->mRefCnt == 0) { \
157 agg->mRefCnt = 1; /* stabilize */ \
158 delete agg; \
159 return 0; \
161 return agg->mRefCnt; \
164 #define NS_IMPL_CYCLE_COLLECTING_AGGREGATED(_class) \
166 NS_IMPL_AGGREGATED_HELPER(_class) \
168 NS_IMETHODIMP_(nsrefcnt) \
169 _class::Internal::AddRef(void) \
171 _class* agg = NS_CYCLE_COLLECTION_CLASSNAME(_class)::Downcast(this); \
172 NS_PRECONDITION(PRInt32(agg->mRefCnt) >= 0, "illegal refcnt"); \
173 NS_CheckThreadSafe(agg->_mOwningThread.GetThread(), \
174 #_class " not thread-safe"); \
175 nsrefcnt count = agg->mRefCnt.incr(this); \
176 NS_LOG_ADDREF(this, count, #_class, sizeof(*agg)); \
177 return count; \
180 NS_IMETHODIMP_(nsrefcnt) \
181 _class::Internal::Release(void) \
183 _class* agg = NS_CYCLE_COLLECTION_CLASSNAME(_class)::Downcast(this); \
184 NS_PRECONDITION(0 != agg->mRefCnt, "dup release"); \
185 NS_CheckThreadSafe(agg->_mOwningThread.GetThread(), \
186 #_class " not thread-safe"); \
187 nsrefcnt count = agg->mRefCnt.decr(this); \
188 NS_LOG_RELEASE(this, count, #_class); \
189 if (count == 0) { \
190 agg->mRefCnt.stabilizeForDeletion(this); \
191 delete agg; \
192 return 0; \
194 return count; \
197 #define NS_IMPL_AGGREGATED_HELPER(_class) \
198 NS_IMETHODIMP \
199 _class::QueryInterface(const nsIID& aIID, void** aInstancePtr) \
201 return fOuter->QueryInterface(aIID, aInstancePtr); \
204 NS_IMETHODIMP_(nsrefcnt) \
205 _class::AddRef(void) \
207 return fOuter->AddRef(); \
210 NS_IMETHODIMP_(nsrefcnt) \
211 _class::Release(void) \
213 return fOuter->Release(); \
216 NS_IMETHODIMP \
217 _class::Internal::QueryInterface(const nsIID& aIID, void** aInstancePtr) \
219 _class* agg = (_class*)((char*)(this) - offsetof(_class, fAggregated)); \
220 return agg->AggregatedQueryInterface(aIID, aInstancePtr); \
224 * To make aggregated objects participate in cycle collection we need to enable
225 * the outer object (aggregator) to traverse/unlink the objects held by the
226 * inner object (the aggregatee). We can't just make the inner object QI'able to
227 * NS_CYCLECOLLECTIONPARTICIPANT_IID, we don't want to return the inner object's
228 * nsCycleCollectionParticipant for the outer object (which will happen if the
229 * outer object doesn't participate in cycle collection itself).
230 * NS_AGGREGATED_CYCLECOLLECTIONPARTICIPANT_IID enables the outer object to get
231 * the inner objects nsCycleCollectionParticipant.
233 * There are three cases:
234 * - No aggregation
235 * QI'ing to NS_CYCLECOLLECTIONPARTICIPANT_IID will return the inner
236 * object's nsCycleCollectionParticipant.
238 * - Aggregation and outer object does not participate in cycle collection
239 * QI'ing to NS_CYCLECOLLECTIONPARTICIPANT_IID will not return anything.
241 * - Aggregation and outer object does participate in cycle collection
242 * QI'ing to NS_CYCLECOLLECTIONPARTICIPANT_IID will return the outer
243 * object's nsCycleCollectionParticipant. The outer object's
244 * nsCycleCollectionParticipant can then QI the inner object to
245 * NS_AGGREGATED_CYCLECOLLECTIONPARTICIPANT_IID to get the inner object's
246 * nsCycleCollectionParticipant, which it can use to traverse/unlink the
247 * objects reachable from the inner object.
249 #define NS_AGGREGATED_CYCLECOLLECTIONPARTICIPANT_IID \
251 0x32889b7e, \
252 0xe4fe, \
253 0x43f4, \
254 { 0x85, 0x31, 0xb5, 0x28, 0x23, 0xa2, 0xe9, 0xfc } \
258 * Just holds the IID so NS_GET_IID works.
260 class nsAggregatedCycleCollectionParticipant
262 public:
263 NS_DECLARE_STATIC_IID_ACCESSOR(NS_AGGREGATED_CYCLECOLLECTIONPARTICIPANT_IID)
266 NS_DEFINE_STATIC_IID_ACCESSOR(nsAggregatedCycleCollectionParticipant,
267 NS_AGGREGATED_CYCLECOLLECTIONPARTICIPANT_IID)
269 // for use with QI macros in nsISupportsUtils.h:
271 #define NS_INTERFACE_MAP_BEGIN_AGGREGATED(_class) \
272 NS_IMPL_AGGREGATED_QUERY_HEAD(_class)
274 #define NS_INTERFACE_MAP_ENTRY_CYCLE_COLLECTION_AGGREGATED(_class) \
275 NS_IMPL_QUERY_CYCLE_COLLECTION(_class)
277 #define NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION_AGGREGATED(_class) \
278 NS_INTERFACE_MAP_ENTRY_CYCLE_COLLECTION_AGGREGATED(_class) \
279 NS_INTERFACE_MAP_ENTRY_CYCLE_COLLECTION_ISUPPORTS(_class)
281 #define NS_IMPL_AGGREGATED_QUERY_HEAD(_class) \
282 nsresult \
283 _class::AggregatedQueryInterface(REFNSIID aIID, void** aInstancePtr) \
285 NS_ASSERTION(aInstancePtr, \
286 "AggregatedQueryInterface requires a non-NULL result ptr!"); \
287 if ( !aInstancePtr ) \
288 return NS_ERROR_NULL_POINTER; \
289 nsISupports* foundInterface; \
290 if ( aIID.Equals(NS_GET_IID(nsISupports)) ) \
291 foundInterface = InnerObject(); \
292 else
294 #define NS_IMPL_AGGREGATED_QUERY_CYCLE_COLLECTION(_class) \
295 if (aIID.Equals(IsPartOfAggregated() ? \
296 NS_GET_IID(nsCycleCollectionParticipant) : \
297 NS_GET_IID(nsAggregatedCycleCollectionParticipant))) \
298 foundInterface = & NS_CYCLE_COLLECTION_NAME(_class); \
299 else
301 #define NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_AGGREGATED(_class) \
302 NS_IMETHODIMP \
303 NS_CYCLE_COLLECTION_CLASSNAME(_class)::Traverse \
304 (void *p, \
305 nsCycleCollectionTraversalCallback &cb) \
307 nsISupports *s = static_cast<nsISupports*>(p); \
308 NS_ASSERTION(CheckForRightISupports(s), \
309 "not the nsISupports pointer we expect"); \
310 _class *tmp = static_cast<_class*>(Downcast(s)); \
311 if (!tmp->IsPartOfAggregated()) \
312 NS_IMPL_CYCLE_COLLECTION_DESCRIBE(_class, tmp->mRefCnt.get())
314 #define NS_GENERIC_AGGREGATED_CONSTRUCTOR(_InstanceClass) \
315 static nsresult \
316 _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
317 void **aResult) \
319 *aResult = nsnull; \
321 NS_ENSURE_PROPER_AGGREGATION(aOuter, aIID); \
323 _InstanceClass* inst = new _InstanceClass(aOuter); \
324 if (!inst) { \
325 return NS_ERROR_OUT_OF_MEMORY; \
328 nsISupports* inner = inst->InnerObject(); \
329 nsresult rv = inner->QueryInterface(aIID, aResult); \
330 if (NS_FAILED(rv)) { \
331 delete inst; \
334 return rv; \
337 #define NS_GENERIC_AGGREGATED_CONSTRUCTOR_INIT(_InstanceClass, _InitMethod) \
338 static nsresult \
339 _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
340 void **aResult) \
342 *aResult = nsnull; \
344 NS_ENSURE_PROPER_AGGREGATION(aOuter, aIID); \
346 _InstanceClass* inst = new _InstanceClass(aOuter); \
347 if (!inst) { \
348 return NS_ERROR_OUT_OF_MEMORY; \
351 nsISupports* inner = inst->InnerObject(); \
352 NS_ADDREF(inner); \
353 nsresult rv = inst->_InitMethod(); \
354 if (NS_SUCCEEDED(rv)) { \
355 rv = inner->QueryInterface(aIID, aResult); \
357 NS_RELEASE(inner); \
359 return rv; \
362 #endif /* nsAgg_h___ */