Bug 895720 - Actually use the fuzz value.
[gecko.git] / xpcom / glue / nsISupportsImpl.h
blob1a2c91807e7217a383395bf28b74ff64bf34d87c
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsISupportsImpl_h__
8 #define nsISupportsImpl_h__
10 #ifndef nscore_h___
11 #include "nscore.h"
12 #endif
14 #ifndef nsISupportsBase_h__
15 #include "nsISupportsBase.h"
16 #endif
18 #ifndef nsISupportsUtils_h__
19 #include "nsISupportsUtils.h"
20 #endif
23 #if !defined(XPCOM_GLUE_AVOID_NSPR)
24 #include "prthread.h" /* needed for thread-safety checks */
25 #ifdef DEBUG
26 #include "nsCycleCollectorUtils.h" /* for NS_IsCycleCollectorThread */
27 #endif // DEBUG
28 #endif // !XPCOM_GLUE_AVOID_NSPR
30 #include "nsDebug.h"
31 #include "nsTraceRefcnt.h"
32 #ifndef XPCOM_GLUE
33 #include "mozilla/Atomics.h"
34 #endif
35 #include "mozilla/Attributes.h"
36 #include "mozilla/Assertions.h"
37 #include "mozilla/Likely.h"
39 inline nsISupports*
40 ToSupports(nsISupports* p)
42 return p;
45 inline nsISupports*
46 ToCanonicalSupports(nsISupports* p)
48 return NULL;
51 ////////////////////////////////////////////////////////////////////////////////
52 // Macros to help detect thread-safety:
54 #if defined(DEBUG) && !defined(XPCOM_GLUE_AVOID_NSPR)
56 class nsAutoOwningThread {
57 public:
58 nsAutoOwningThread() { mThread = PR_GetCurrentThread(); }
59 void *GetThread() const { return mThread; }
61 private:
62 void *mThread;
65 #define NS_DECL_OWNINGTHREAD nsAutoOwningThread _mOwningThread;
66 #define NS_ASSERT_OWNINGTHREAD(_class) \
67 NS_CheckThreadSafe(_mOwningThread.GetThread(), #_class " not thread-safe")
68 #define NS_ASSERT_OWNINGTHREAD_AND_NOT_CCTHREAD(_class) \
69 do { \
70 if (NS_IsCycleCollectorThread()) { \
71 MOZ_CRASH("Changing refcount of " #_class " object during Traverse is " \
72 "not permitted!"); \
73 } \
74 else { \
75 NS_ASSERT_OWNINGTHREAD(_class); \
76 } \
77 } while (0)
79 #else // !DEBUG
81 #define NS_DECL_OWNINGTHREAD /* nothing */
82 #define NS_ASSERT_OWNINGTHREAD(_class) ((void)0)
83 #define NS_ASSERT_OWNINGTHREAD_AND_NOT_CCTHREAD(_class) ((void)0)
85 #endif // DEBUG
87 // Support for ISupports classes which interact with cycle collector.
89 struct nsPurpleBufferEntry {
90 union {
91 void *mObject; // when low bit unset
92 nsPurpleBufferEntry *mNextInFreeList; // when low bit set
95 nsCycleCollectingAutoRefCnt *mRefCnt;
97 nsCycleCollectionParticipant *mParticipant; // NULL for nsISupports
100 #define NS_NUMBER_OF_FLAGS_IN_REFCNT 2
101 #define NS_IN_PURPLE_BUFFER (1 << 0)
102 #define NS_IS_PURPLE (1 << 1)
103 #define NS_REFCOUNT_CHANGE (1 << NS_NUMBER_OF_FLAGS_IN_REFCNT)
104 #define NS_REFCOUNT_VALUE(_val) (_val >> NS_NUMBER_OF_FLAGS_IN_REFCNT)
106 class nsCycleCollectingAutoRefCnt {
108 public:
109 nsCycleCollectingAutoRefCnt()
110 : mRefCntAndFlags(0)
113 nsCycleCollectingAutoRefCnt(uintptr_t aValue)
114 : mRefCntAndFlags(aValue << NS_NUMBER_OF_FLAGS_IN_REFCNT)
118 MOZ_ALWAYS_INLINE uintptr_t incr()
120 mRefCntAndFlags += NS_REFCOUNT_CHANGE;
121 mRefCntAndFlags &= ~NS_IS_PURPLE;
122 return NS_REFCOUNT_VALUE(mRefCntAndFlags);
125 MOZ_ALWAYS_INLINE void stabilizeForDeletion()
127 // Set refcnt to 1 and mark us to be in the purple buffer.
128 // This way decr won't call suspect again.
129 mRefCntAndFlags = NS_REFCOUNT_CHANGE | NS_IN_PURPLE_BUFFER;
132 MOZ_ALWAYS_INLINE uintptr_t decr(nsISupports *owner,
133 bool *shouldDelete = nullptr)
135 return decr(owner, nullptr, shouldDelete);
138 MOZ_ALWAYS_INLINE uintptr_t decr(void *owner, nsCycleCollectionParticipant *p,
139 bool *shouldDelete = nullptr)
141 MOZ_ASSERT(get() > 0);
142 if (!IsInPurpleBuffer()) {
143 mRefCntAndFlags -= NS_REFCOUNT_CHANGE;
144 mRefCntAndFlags |= (NS_IN_PURPLE_BUFFER | NS_IS_PURPLE);
145 uintptr_t retval = NS_REFCOUNT_VALUE(mRefCntAndFlags);
146 // Suspect may delete 'owner' and 'this'!
147 NS_CycleCollectorSuspect3(owner, p, this, shouldDelete);
148 return retval;
150 mRefCntAndFlags -= NS_REFCOUNT_CHANGE;
151 mRefCntAndFlags |= (NS_IN_PURPLE_BUFFER | NS_IS_PURPLE);
152 return NS_REFCOUNT_VALUE(mRefCntAndFlags);
155 MOZ_ALWAYS_INLINE void RemovePurple()
157 MOZ_ASSERT(IsPurple(), "must be purple");
158 mRefCntAndFlags &= ~NS_IS_PURPLE;
161 MOZ_ALWAYS_INLINE void RemoveFromPurpleBuffer()
163 MOZ_ASSERT(IsInPurpleBuffer());
164 mRefCntAndFlags &= ~(NS_IS_PURPLE | NS_IN_PURPLE_BUFFER);
167 MOZ_ALWAYS_INLINE bool IsPurple() const
169 return !!(mRefCntAndFlags & NS_IS_PURPLE);
172 MOZ_ALWAYS_INLINE bool IsInPurpleBuffer() const
174 return !!(mRefCntAndFlags & NS_IN_PURPLE_BUFFER);
177 MOZ_ALWAYS_INLINE nsrefcnt get() const
179 return NS_REFCOUNT_VALUE(mRefCntAndFlags);
182 MOZ_ALWAYS_INLINE operator nsrefcnt() const
184 return get();
187 private:
188 uintptr_t mRefCntAndFlags;
191 class nsAutoRefCnt {
193 public:
194 nsAutoRefCnt() : mValue(0) {}
195 nsAutoRefCnt(nsrefcnt aValue) : mValue(aValue) {}
197 // only support prefix increment/decrement
198 nsrefcnt operator++() { return ++mValue; }
199 nsrefcnt operator--() { return --mValue; }
201 nsrefcnt operator=(nsrefcnt aValue) { return (mValue = aValue); }
202 operator nsrefcnt() const { return mValue; }
203 nsrefcnt get() const { return mValue; }
205 static const bool isThreadSafe = false;
206 private:
207 nsrefcnt operator++(int) MOZ_DELETE;
208 nsrefcnt operator--(int) MOZ_DELETE;
209 nsrefcnt mValue;
212 #ifndef XPCOM_GLUE
213 namespace mozilla {
214 class ThreadSafeAutoRefCnt {
215 public:
216 ThreadSafeAutoRefCnt() : mValue(0) {}
217 ThreadSafeAutoRefCnt(nsrefcnt aValue) : mValue(aValue) {}
219 // only support prefix increment/decrement
220 MOZ_ALWAYS_INLINE nsrefcnt operator++() { return ++mValue; }
221 MOZ_ALWAYS_INLINE nsrefcnt operator--() { return --mValue; }
223 MOZ_ALWAYS_INLINE nsrefcnt operator=(nsrefcnt aValue) { return (mValue = aValue); }
224 MOZ_ALWAYS_INLINE operator nsrefcnt() const { return mValue; }
225 MOZ_ALWAYS_INLINE nsrefcnt get() const { return mValue; }
227 static const bool isThreadSafe = true;
228 private:
229 nsrefcnt operator++(int) MOZ_DELETE;
230 nsrefcnt operator--(int) MOZ_DELETE;
231 // In theory, RelaseAcquire consistency (but no weaker) is sufficient for
232 // the counter. Making it weaker could speed up builds on ARM (but not x86),
233 // but could break pre-existing code that assumes sequential consistency.
234 Atomic<nsrefcnt> mValue;
237 #endif
239 ///////////////////////////////////////////////////////////////////////////////
242 * Declare the reference count variable and the implementations of the
243 * AddRef and QueryInterface methods.
246 #define NS_DECL_ISUPPORTS \
247 public: \
248 NS_IMETHOD QueryInterface(REFNSIID aIID, \
249 void** aInstancePtr); \
250 NS_IMETHOD_(nsrefcnt) AddRef(void); \
251 NS_IMETHOD_(nsrefcnt) Release(void); \
252 protected: \
253 nsAutoRefCnt mRefCnt; \
254 NS_DECL_OWNINGTHREAD \
255 public:
257 #define NS_DECL_THREADSAFE_ISUPPORTS \
258 public: \
259 NS_IMETHOD QueryInterface(REFNSIID aIID, \
260 void** aInstancePtr); \
261 NS_IMETHOD_(nsrefcnt) AddRef(void); \
262 NS_IMETHOD_(nsrefcnt) Release(void); \
263 protected: \
264 ::mozilla::ThreadSafeAutoRefCnt mRefCnt; \
265 NS_DECL_OWNINGTHREAD \
266 public:
268 #define NS_DECL_CYCLE_COLLECTING_ISUPPORTS \
269 public: \
270 NS_IMETHOD QueryInterface(REFNSIID aIID, \
271 void** aInstancePtr); \
272 NS_IMETHOD_(nsrefcnt) AddRef(void); \
273 NS_IMETHOD_(nsrefcnt) Release(void); \
274 NS_IMETHOD_(void) DeleteCycleCollectable(void); \
275 protected: \
276 nsCycleCollectingAutoRefCnt mRefCnt; \
277 NS_DECL_OWNINGTHREAD \
278 public:
281 ///////////////////////////////////////////////////////////////////////////////
284 * Implementation of AddRef and Release for non-nsISupports (ie "native")
285 * cycle-collected classes that use the purple buffer to avoid leaks.
288 #define NS_IMPL_CC_NATIVE_ADDREF_BODY(_class) \
289 MOZ_ASSERT(int32_t(mRefCnt) >= 0, "illegal refcnt"); \
290 NS_ASSERT_OWNINGTHREAD_AND_NOT_CCTHREAD(_class); \
291 nsrefcnt count = mRefCnt.incr(); \
292 NS_LOG_ADDREF(this, count, #_class, sizeof(*this)); \
293 return count;
295 #define NS_IMPL_CC_NATIVE_RELEASE_BODY(_class) \
296 MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release"); \
297 NS_ASSERT_OWNINGTHREAD_AND_NOT_CCTHREAD(_class); \
298 nsrefcnt count = \
299 mRefCnt.decr(static_cast<void*>(this), \
300 _class::NS_CYCLE_COLLECTION_INNERCLASS::GetParticipant()); \
301 NS_LOG_RELEASE(this, count, #_class); \
302 return count;
304 #define NS_IMPL_CYCLE_COLLECTING_NATIVE_ADDREF(_class) \
305 NS_METHOD_(nsrefcnt) _class::AddRef(void) \
307 NS_IMPL_CC_NATIVE_ADDREF_BODY(_class) \
310 #define NS_IMPL_CYCLE_COLLECTING_NATIVE_RELEASE_WITH_LAST_RELEASE(_class, _last) \
311 NS_METHOD_(nsrefcnt) _class::Release(void) \
313 MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release"); \
314 NS_ASSERT_OWNINGTHREAD_AND_NOT_CCTHREAD(_class); \
315 bool shouldDelete = false; \
316 nsrefcnt count = \
317 mRefCnt.decr(static_cast<void*>(this), \
318 _class::NS_CYCLE_COLLECTION_INNERCLASS::GetParticipant(), \
319 &shouldDelete); \
320 NS_LOG_RELEASE(this, count, #_class); \
321 if (count == 0) { \
322 mRefCnt.incr(); \
323 _last; \
324 mRefCnt.decr(static_cast<void*>(this), \
325 _class::NS_CYCLE_COLLECTION_INNERCLASS::GetParticipant()); \
326 if (shouldDelete) { \
327 mRefCnt.stabilizeForDeletion(); \
328 DeleteCycleCollectable(); \
331 return count; \
334 #define NS_IMPL_CYCLE_COLLECTING_NATIVE_RELEASE(_class) \
335 NS_METHOD_(nsrefcnt) _class::Release(void) \
337 NS_IMPL_CC_NATIVE_RELEASE_BODY(_class) \
340 #define NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(_class) \
341 public: \
342 NS_METHOD_(nsrefcnt) AddRef(void) { \
343 NS_IMPL_CC_NATIVE_ADDREF_BODY(_class) \
345 NS_METHOD_(nsrefcnt) Release(void) { \
346 NS_IMPL_CC_NATIVE_RELEASE_BODY(_class) \
348 protected: \
349 nsCycleCollectingAutoRefCnt mRefCnt; \
350 NS_DECL_OWNINGTHREAD \
351 public:
354 ///////////////////////////////////////////////////////////////////////////////
357 * Previously used to initialize the reference count, but no longer needed.
359 * DEPRECATED.
361 #define NS_INIT_ISUPPORTS() ((void)0)
364 * Use this macro to declare and implement the AddRef & Release methods for a
365 * given non-XPCOM <i>_class</i>.
367 * @param _class The name of the class implementing the method
369 #define NS_INLINE_DECL_REFCOUNTING(_class) \
370 public: \
371 NS_METHOD_(nsrefcnt) AddRef(void) { \
372 MOZ_ASSERT(int32_t(mRefCnt) >= 0, "illegal refcnt"); \
373 NS_ASSERT_OWNINGTHREAD_AND_NOT_CCTHREAD(_class); \
374 ++mRefCnt; \
375 NS_LOG_ADDREF(this, mRefCnt, #_class, sizeof(*this)); \
376 return mRefCnt; \
378 NS_METHOD_(nsrefcnt) Release(void) { \
379 MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release"); \
380 NS_ASSERT_OWNINGTHREAD_AND_NOT_CCTHREAD(_class); \
381 --mRefCnt; \
382 NS_LOG_RELEASE(this, mRefCnt, #_class); \
383 if (mRefCnt == 0) { \
384 NS_ASSERT_OWNINGTHREAD(_class); \
385 mRefCnt = 1; /* stabilize */ \
386 delete this; \
387 return 0; \
389 return mRefCnt; \
391 protected: \
392 nsAutoRefCnt mRefCnt; \
393 NS_DECL_OWNINGTHREAD \
394 public:
397 * Use this macro to declare and implement the AddRef & Release methods for a
398 * given non-XPCOM <i>_class</i> in a threadsafe manner.
400 * DOES NOT DO REFCOUNT STABILIZATION!
402 * @param _class The name of the class implementing the method
404 #define NS_INLINE_DECL_THREADSAFE_REFCOUNTING(_class) \
405 public: \
406 NS_METHOD_(nsrefcnt) AddRef(void) { \
407 MOZ_ASSERT(int32_t(mRefCnt) >= 0, "illegal refcnt"); \
408 nsrefcnt count = ++mRefCnt; \
409 NS_LOG_ADDREF(this, count, #_class, sizeof(*this)); \
410 return (nsrefcnt) count; \
412 NS_METHOD_(nsrefcnt) Release(void) { \
413 MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release"); \
414 nsrefcnt count = --mRefCnt; \
415 NS_LOG_RELEASE(this, count, #_class); \
416 if (count == 0) { \
417 delete (this); \
418 return 0; \
420 return count; \
422 protected: \
423 ::mozilla::ThreadSafeAutoRefCnt mRefCnt; \
424 public:
427 * Use this macro to implement the AddRef method for a given <i>_class</i>
428 * @param _class The name of the class implementing the method
430 #define NS_IMPL_ADDREF(_class) \
431 NS_IMETHODIMP_(nsrefcnt) _class::AddRef(void) \
433 MOZ_ASSERT(int32_t(mRefCnt) >= 0, "illegal refcnt"); \
434 if (!mRefCnt.isThreadSafe) \
435 NS_ASSERT_OWNINGTHREAD_AND_NOT_CCTHREAD(_class); \
436 nsrefcnt count = ++mRefCnt; \
437 NS_LOG_ADDREF(this, count, #_class, sizeof(*this)); \
438 return count; \
442 * Use this macro to implement the AddRef method for a given <i>_class</i>
443 * implemented as a wholly owned aggregated object intended to implement
444 * interface(s) for its owner
445 * @param _class The name of the class implementing the method
446 * @param _aggregator the owning/containing object
448 #define NS_IMPL_ADDREF_USING_AGGREGATOR(_class, _aggregator) \
449 NS_IMETHODIMP_(nsrefcnt) _class::AddRef(void) \
451 NS_PRECONDITION(_aggregator, "null aggregator"); \
452 return (_aggregator)->AddRef(); \
456 * Use this macro to implement the Release method for a given
457 * <i>_class</i>.
458 * @param _class The name of the class implementing the method
459 * @param _destroy A statement that is executed when the object's
460 * refcount drops to zero.
462 * For example,
464 * NS_IMPL_RELEASE_WITH_DESTROY(Foo, Destroy(this))
466 * will cause
468 * Destroy(this);
470 * to be invoked when the object's refcount drops to zero. This
471 * allows for arbitrary teardown activity to occur (e.g., deallocation
472 * of object allocated with placement new).
474 #define NS_IMPL_RELEASE_WITH_DESTROY(_class, _destroy) \
475 NS_IMETHODIMP_(nsrefcnt) _class::Release(void) \
477 MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release"); \
478 if (!mRefCnt.isThreadSafe) \
479 NS_ASSERT_OWNINGTHREAD_AND_NOT_CCTHREAD(_class); \
480 nsrefcnt count = --mRefCnt; \
481 NS_LOG_RELEASE(this, count, #_class); \
482 if (count == 0) { \
483 if (!mRefCnt.isThreadSafe) \
484 NS_ASSERT_OWNINGTHREAD(_class); \
485 mRefCnt = 1; /* stabilize */ \
486 _destroy; \
487 return 0; \
489 return count; \
493 * Use this macro to implement the Release method for a given <i>_class</i>
494 * @param _class The name of the class implementing the method
496 * A note on the 'stabilization' of the refcnt to one. At that point,
497 * the object's refcount will have gone to zero. The object's
498 * destructor may trigger code that attempts to QueryInterface() and
499 * Release() 'this' again. Doing so will temporarily increment and
500 * decrement the refcount. (Only a logic error would make one try to
501 * keep a permanent hold on 'this'.) To prevent re-entering the
502 * destructor, we make sure that no balanced refcounting can return
503 * the refcount to |0|.
505 #define NS_IMPL_RELEASE(_class) \
506 NS_IMPL_RELEASE_WITH_DESTROY(_class, delete (this))
509 * Use this macro to implement the Release method for a given <i>_class</i>
510 * implemented as a wholly owned aggregated object intended to implement
511 * interface(s) for its owner
512 * @param _class The name of the class implementing the method
513 * @param _aggregator the owning/containing object
515 #define NS_IMPL_RELEASE_USING_AGGREGATOR(_class, _aggregator) \
516 NS_IMETHODIMP_(nsrefcnt) _class::Release(void) \
518 NS_PRECONDITION(_aggregator, "null aggregator"); \
519 return (_aggregator)->Release(); \
523 #define NS_IMPL_CYCLE_COLLECTING_ADDREF(_class) \
524 NS_IMETHODIMP_(nsrefcnt) _class::AddRef(void) \
526 MOZ_ASSERT(int32_t(mRefCnt) >= 0, "illegal refcnt"); \
527 NS_ASSERT_OWNINGTHREAD_AND_NOT_CCTHREAD(_class); \
528 nsrefcnt count = mRefCnt.incr(); \
529 NS_LOG_ADDREF(this, count, #_class, sizeof(*this)); \
530 return count; \
533 #define NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_DESTROY(_class, _destroy) \
534 NS_IMETHODIMP_(nsrefcnt) _class::Release(void) \
536 MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release"); \
537 NS_ASSERT_OWNINGTHREAD_AND_NOT_CCTHREAD(_class); \
538 nsISupports *base = NS_CYCLE_COLLECTION_CLASSNAME(_class)::Upcast(this); \
539 nsrefcnt count = mRefCnt.decr(base); \
540 NS_LOG_RELEASE(this, count, #_class); \
541 return count; \
543 NS_IMETHODIMP_(void) _class::DeleteCycleCollectable(void) \
545 _destroy; \
548 #define NS_IMPL_CYCLE_COLLECTING_RELEASE(_class) \
549 NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_DESTROY(_class, delete (this))
551 // _LAST_RELEASE can be useful when certain resources should be released
552 // as soon as we know the object will be deleted.
553 #define NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE(_class, _last) \
554 NS_IMETHODIMP_(nsrefcnt) _class::Release(void) \
556 MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release"); \
557 NS_ASSERT_OWNINGTHREAD_AND_NOT_CCTHREAD(_class); \
558 bool shouldDelete = false; \
559 nsISupports *base = NS_CYCLE_COLLECTION_CLASSNAME(_class)::Upcast(this); \
560 nsrefcnt count = mRefCnt.decr(base, &shouldDelete); \
561 NS_LOG_RELEASE(this, count, #_class); \
562 if (count == 0) { \
563 mRefCnt.incr(); \
564 _last; \
565 mRefCnt.decr(base); \
566 if (shouldDelete) { \
567 mRefCnt.stabilizeForDeletion(); \
568 DeleteCycleCollectable(); \
571 return count; \
573 NS_IMETHODIMP_(void) _class::DeleteCycleCollectable(void) \
575 delete this; \
578 ///////////////////////////////////////////////////////////////////////////////
581 * There are two ways of implementing QueryInterface, and we use both:
583 * Table-driven QueryInterface uses a static table of IID->offset mappings
584 * and a shared helper function. Using it tends to reduce codesize and improve
585 * runtime performance (due to processor cache hits).
587 * Macro-driven QueryInterface generates a QueryInterface function directly
588 * using common macros. This is necessary if special QueryInterface features
589 * are being used (such as tearoffs and conditional interfaces).
591 * These methods can be combined into a table-driven function call followed
592 * by custom code for tearoffs and conditionals.
595 struct QITableEntry
597 const nsIID *iid; // null indicates end of the QITableEntry array
598 int32_t offset;
601 NS_COM_GLUE nsresult NS_FASTCALL
602 NS_TableDrivenQI(void* aThis, const QITableEntry* entries,
603 REFNSIID aIID, void **aInstancePtr);
606 * Implement table-driven queryinterface
609 #define NS_INTERFACE_TABLE_HEAD(_class) \
610 NS_IMETHODIMP _class::QueryInterface(REFNSIID aIID, void** aInstancePtr) \
612 NS_ASSERTION(aInstancePtr, \
613 "QueryInterface requires a non-NULL destination!"); \
614 nsresult rv = NS_ERROR_FAILURE;
616 #define NS_INTERFACE_TABLE_BEGIN \
617 static const QITableEntry table[] = {
619 #define NS_INTERFACE_TABLE_ENTRY(_class, _interface) \
620 { &_interface::COMTypeInfo<int>::kIID, \
621 int32_t(reinterpret_cast<char*>( \
622 static_cast<_interface*>((_class*) 0x1000)) - \
623 reinterpret_cast<char*>((_class*) 0x1000)) \
626 #define NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(_class, _interface, _implClass) \
627 { &_interface::COMTypeInfo<int>::kIID, \
628 int32_t(reinterpret_cast<char*>( \
629 static_cast<_interface*>( \
630 static_cast<_implClass*>( \
631 (_class*) 0x1000))) - \
632 reinterpret_cast<char*>((_class*) 0x1000)) \
635 #define NS_INTERFACE_TABLE_END_WITH_PTR(_ptr) \
636 { nullptr, 0 } }; \
637 rv = NS_TableDrivenQI(static_cast<void*>(_ptr), \
638 table, aIID, aInstancePtr);
640 #define NS_INTERFACE_TABLE_END \
641 NS_INTERFACE_TABLE_END_WITH_PTR(this)
643 #define NS_INTERFACE_TABLE_TAIL \
644 return rv; \
647 #define NS_INTERFACE_TABLE_TAIL_INHERITING(_baseclass) \
648 if (NS_SUCCEEDED(rv)) \
649 return rv; \
650 return _baseclass::QueryInterface(aIID, aInstancePtr); \
653 #define NS_INTERFACE_TABLE_TAIL_USING_AGGREGATOR(_aggregator) \
654 if (NS_SUCCEEDED(rv)) \
655 return rv; \
656 NS_ASSERTION(_aggregator, "null aggregator"); \
657 return _aggregator->QueryInterface(aIID, aInstancePtr) \
661 * This implements query interface with two assumptions: First, the
662 * class in question implements nsISupports and its own interface and
663 * nothing else. Second, the implementation of the class's primary
664 * inheritance chain leads to its own interface.
666 * @param _class The name of the class implementing the method
667 * @param _classiiddef The name of the #define symbol that defines the IID
668 * for the class (e.g. NS_ISUPPORTS_IID)
671 #define NS_IMPL_QUERY_HEAD(_class) \
672 NS_IMETHODIMP _class::QueryInterface(REFNSIID aIID, void** aInstancePtr) \
674 NS_ASSERTION(aInstancePtr, \
675 "QueryInterface requires a non-NULL destination!"); \
676 nsISupports* foundInterface;
678 #define NS_IMPL_QUERY_BODY(_interface) \
679 if ( aIID.Equals(NS_GET_IID(_interface)) ) \
680 foundInterface = static_cast<_interface*>(this); \
681 else
683 #define NS_IMPL_QUERY_BODY_CONDITIONAL(_interface, condition) \
684 if ( (condition) && aIID.Equals(NS_GET_IID(_interface))) \
685 foundInterface = static_cast<_interface*>(this); \
686 else
688 #define NS_IMPL_QUERY_BODY_AMBIGUOUS(_interface, _implClass) \
689 if ( aIID.Equals(NS_GET_IID(_interface)) ) \
690 foundInterface = static_cast<_interface*>( \
691 static_cast<_implClass*>(this)); \
692 else
694 #define NS_IMPL_QUERY_BODY_AGGREGATED(_interface, _aggregate) \
695 if ( aIID.Equals(NS_GET_IID(_interface)) ) \
696 foundInterface = static_cast<_interface*>(_aggregate); \
697 else
699 #define NS_IMPL_QUERY_TAIL_GUTS \
700 foundInterface = 0; \
701 nsresult status; \
702 if ( !foundInterface ) \
704 /* nsISupports should be handled by this point. If not, fail. */ \
705 MOZ_ASSERT(!aIID.Equals(NS_GET_IID(nsISupports))); \
706 status = NS_NOINTERFACE; \
708 else \
710 NS_ADDREF(foundInterface); \
711 status = NS_OK; \
713 *aInstancePtr = foundInterface; \
714 return status; \
717 #define NS_IMPL_QUERY_TAIL_INHERITING(_baseclass) \
718 foundInterface = 0; \
719 nsresult status; \
720 if ( !foundInterface ) \
721 status = _baseclass::QueryInterface(aIID, (void**)&foundInterface); \
722 else \
724 NS_ADDREF(foundInterface); \
725 status = NS_OK; \
727 *aInstancePtr = foundInterface; \
728 return status; \
731 #define NS_IMPL_QUERY_TAIL_USING_AGGREGATOR(_aggregator) \
732 foundInterface = 0; \
733 nsresult status; \
734 if ( !foundInterface ) { \
735 NS_ASSERTION(_aggregator, "null aggregator"); \
736 status = _aggregator->QueryInterface(aIID, (void**)&foundInterface); \
737 } else \
739 NS_ADDREF(foundInterface); \
740 status = NS_OK; \
742 *aInstancePtr = foundInterface; \
743 return status; \
746 #define NS_IMPL_QUERY_TAIL(_supports_interface) \
747 NS_IMPL_QUERY_BODY_AMBIGUOUS(nsISupports, _supports_interface) \
748 NS_IMPL_QUERY_TAIL_GUTS
752 This is the new scheme. Using this notation now will allow us to switch to
753 a table driven mechanism when it's ready. Note the difference between this
754 and the (currently) underlying NS_IMPL_QUERY_INTERFACE mechanism. You must
755 explicitly mention |nsISupports| when using the interface maps.
757 #define NS_INTERFACE_MAP_BEGIN(_implClass) NS_IMPL_QUERY_HEAD(_implClass)
758 #define NS_INTERFACE_MAP_ENTRY(_interface) NS_IMPL_QUERY_BODY(_interface)
759 #define NS_INTERFACE_MAP_ENTRY_CONDITIONAL(_interface, condition) \
760 NS_IMPL_QUERY_BODY_CONDITIONAL(_interface, condition)
761 #define NS_INTERFACE_MAP_ENTRY_AGGREGATED(_interface,_aggregate) \
762 NS_IMPL_QUERY_BODY_AGGREGATED(_interface,_aggregate)
764 #define NS_INTERFACE_MAP_END NS_IMPL_QUERY_TAIL_GUTS
765 #define NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(_interface, _implClass) \
766 NS_IMPL_QUERY_BODY_AMBIGUOUS(_interface, _implClass)
767 #define NS_INTERFACE_MAP_END_INHERITING(_baseClass) \
768 NS_IMPL_QUERY_TAIL_INHERITING(_baseClass)
769 #define NS_INTERFACE_MAP_END_AGGREGATED(_aggregator) \
770 NS_IMPL_QUERY_TAIL_USING_AGGREGATOR(_aggregator)
772 #define NS_INTERFACE_TABLE0(_class) \
773 NS_INTERFACE_TABLE_BEGIN \
774 NS_INTERFACE_TABLE_ENTRY(_class, nsISupports) \
775 NS_INTERFACE_TABLE_END
777 #define NS_INTERFACE_TABLE1(_class, _i1) \
778 NS_INTERFACE_TABLE_BEGIN \
779 NS_INTERFACE_TABLE_ENTRY(_class, _i1) \
780 NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(_class, nsISupports, _i1) \
781 NS_INTERFACE_TABLE_END
783 #define NS_INTERFACE_TABLE2(_class, _i1, _i2) \
784 NS_INTERFACE_TABLE_BEGIN \
785 NS_INTERFACE_TABLE_ENTRY(_class, _i1) \
786 NS_INTERFACE_TABLE_ENTRY(_class, _i2) \
787 NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(_class, nsISupports, _i1) \
788 NS_INTERFACE_TABLE_END
790 #define NS_INTERFACE_TABLE3(_class, _i1, _i2, _i3) \
791 NS_INTERFACE_TABLE_BEGIN \
792 NS_INTERFACE_TABLE_ENTRY(_class, _i1) \
793 NS_INTERFACE_TABLE_ENTRY(_class, _i2) \
794 NS_INTERFACE_TABLE_ENTRY(_class, _i3) \
795 NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(_class, nsISupports, _i1) \
796 NS_INTERFACE_TABLE_END
798 #define NS_INTERFACE_TABLE4(_class, _i1, _i2, _i3, _i4) \
799 NS_INTERFACE_TABLE_BEGIN \
800 NS_INTERFACE_TABLE_ENTRY(_class, _i1) \
801 NS_INTERFACE_TABLE_ENTRY(_class, _i2) \
802 NS_INTERFACE_TABLE_ENTRY(_class, _i3) \
803 NS_INTERFACE_TABLE_ENTRY(_class, _i4) \
804 NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(_class, nsISupports, _i1) \
805 NS_INTERFACE_TABLE_END
807 #define NS_INTERFACE_TABLE5(_class, _i1, _i2, _i3, _i4, _i5) \
808 NS_INTERFACE_TABLE_BEGIN \
809 NS_INTERFACE_TABLE_ENTRY(_class, _i1) \
810 NS_INTERFACE_TABLE_ENTRY(_class, _i2) \
811 NS_INTERFACE_TABLE_ENTRY(_class, _i3) \
812 NS_INTERFACE_TABLE_ENTRY(_class, _i4) \
813 NS_INTERFACE_TABLE_ENTRY(_class, _i5) \
814 NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(_class, nsISupports, _i1) \
815 NS_INTERFACE_TABLE_END
817 #define NS_INTERFACE_TABLE6(_class, _i1, _i2, _i3, _i4, _i5, _i6) \
818 NS_INTERFACE_TABLE_BEGIN \
819 NS_INTERFACE_TABLE_ENTRY(_class, _i1) \
820 NS_INTERFACE_TABLE_ENTRY(_class, _i2) \
821 NS_INTERFACE_TABLE_ENTRY(_class, _i3) \
822 NS_INTERFACE_TABLE_ENTRY(_class, _i4) \
823 NS_INTERFACE_TABLE_ENTRY(_class, _i5) \
824 NS_INTERFACE_TABLE_ENTRY(_class, _i6) \
825 NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(_class, nsISupports, _i1) \
826 NS_INTERFACE_TABLE_END
828 #define NS_INTERFACE_TABLE7(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7) \
829 NS_INTERFACE_TABLE_BEGIN \
830 NS_INTERFACE_TABLE_ENTRY(_class, _i1) \
831 NS_INTERFACE_TABLE_ENTRY(_class, _i2) \
832 NS_INTERFACE_TABLE_ENTRY(_class, _i3) \
833 NS_INTERFACE_TABLE_ENTRY(_class, _i4) \
834 NS_INTERFACE_TABLE_ENTRY(_class, _i5) \
835 NS_INTERFACE_TABLE_ENTRY(_class, _i6) \
836 NS_INTERFACE_TABLE_ENTRY(_class, _i7) \
837 NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(_class, nsISupports, _i1) \
838 NS_INTERFACE_TABLE_END
840 #define NS_INTERFACE_TABLE8(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7, _i8) \
841 NS_INTERFACE_TABLE_BEGIN \
842 NS_INTERFACE_TABLE_ENTRY(_class, _i1) \
843 NS_INTERFACE_TABLE_ENTRY(_class, _i2) \
844 NS_INTERFACE_TABLE_ENTRY(_class, _i3) \
845 NS_INTERFACE_TABLE_ENTRY(_class, _i4) \
846 NS_INTERFACE_TABLE_ENTRY(_class, _i5) \
847 NS_INTERFACE_TABLE_ENTRY(_class, _i6) \
848 NS_INTERFACE_TABLE_ENTRY(_class, _i7) \
849 NS_INTERFACE_TABLE_ENTRY(_class, _i8) \
850 NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(_class, nsISupports, _i1) \
851 NS_INTERFACE_TABLE_END
853 #define NS_INTERFACE_TABLE9(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7, \
854 _i8, _i9) \
855 NS_INTERFACE_TABLE_BEGIN \
856 NS_INTERFACE_TABLE_ENTRY(_class, _i1) \
857 NS_INTERFACE_TABLE_ENTRY(_class, _i2) \
858 NS_INTERFACE_TABLE_ENTRY(_class, _i3) \
859 NS_INTERFACE_TABLE_ENTRY(_class, _i4) \
860 NS_INTERFACE_TABLE_ENTRY(_class, _i5) \
861 NS_INTERFACE_TABLE_ENTRY(_class, _i6) \
862 NS_INTERFACE_TABLE_ENTRY(_class, _i7) \
863 NS_INTERFACE_TABLE_ENTRY(_class, _i8) \
864 NS_INTERFACE_TABLE_ENTRY(_class, _i9) \
865 NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(_class, nsISupports, _i1) \
866 NS_INTERFACE_TABLE_END
868 #define NS_INTERFACE_TABLE10(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7, \
869 _i8, _i9, _i10) \
870 NS_INTERFACE_TABLE_BEGIN \
871 NS_INTERFACE_TABLE_ENTRY(_class, _i1) \
872 NS_INTERFACE_TABLE_ENTRY(_class, _i2) \
873 NS_INTERFACE_TABLE_ENTRY(_class, _i3) \
874 NS_INTERFACE_TABLE_ENTRY(_class, _i4) \
875 NS_INTERFACE_TABLE_ENTRY(_class, _i5) \
876 NS_INTERFACE_TABLE_ENTRY(_class, _i6) \
877 NS_INTERFACE_TABLE_ENTRY(_class, _i7) \
878 NS_INTERFACE_TABLE_ENTRY(_class, _i8) \
879 NS_INTERFACE_TABLE_ENTRY(_class, _i9) \
880 NS_INTERFACE_TABLE_ENTRY(_class, _i10) \
881 NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(_class, nsISupports, _i1) \
882 NS_INTERFACE_TABLE_END
884 #define NS_INTERFACE_TABLE11(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7, \
885 _i8, _i9, _i10, _i11) \
886 NS_INTERFACE_TABLE_BEGIN \
887 NS_INTERFACE_TABLE_ENTRY(_class, _i1) \
888 NS_INTERFACE_TABLE_ENTRY(_class, _i2) \
889 NS_INTERFACE_TABLE_ENTRY(_class, _i3) \
890 NS_INTERFACE_TABLE_ENTRY(_class, _i4) \
891 NS_INTERFACE_TABLE_ENTRY(_class, _i5) \
892 NS_INTERFACE_TABLE_ENTRY(_class, _i6) \
893 NS_INTERFACE_TABLE_ENTRY(_class, _i7) \
894 NS_INTERFACE_TABLE_ENTRY(_class, _i8) \
895 NS_INTERFACE_TABLE_ENTRY(_class, _i9) \
896 NS_INTERFACE_TABLE_ENTRY(_class, _i10) \
897 NS_INTERFACE_TABLE_ENTRY(_class, _i11) \
898 NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(_class, nsISupports, _i1) \
899 NS_INTERFACE_TABLE_END
901 #define NS_IMPL_QUERY_INTERFACE0(_class) \
902 NS_INTERFACE_TABLE_HEAD(_class) \
903 NS_INTERFACE_TABLE0(_class) \
904 NS_INTERFACE_TABLE_TAIL
906 #define NS_IMPL_QUERY_INTERFACE1(_class, _i1) \
907 NS_INTERFACE_TABLE_HEAD(_class) \
908 NS_INTERFACE_TABLE1(_class, _i1) \
909 NS_INTERFACE_TABLE_TAIL
911 #define NS_IMPL_QUERY_INTERFACE2(_class, _i1, _i2) \
912 NS_INTERFACE_TABLE_HEAD(_class) \
913 NS_INTERFACE_TABLE2(_class, _i1, _i2) \
914 NS_INTERFACE_TABLE_TAIL
916 #define NS_IMPL_QUERY_INTERFACE3(_class, _i1, _i2, _i3) \
917 NS_INTERFACE_TABLE_HEAD(_class) \
918 NS_INTERFACE_TABLE3(_class, _i1, _i2, _i3) \
919 NS_INTERFACE_TABLE_TAIL
921 #define NS_IMPL_QUERY_INTERFACE4(_class, _i1, _i2, _i3, _i4) \
922 NS_INTERFACE_TABLE_HEAD(_class) \
923 NS_INTERFACE_TABLE4(_class, _i1, _i2, _i3, _i4) \
924 NS_INTERFACE_TABLE_TAIL
926 #define NS_IMPL_QUERY_INTERFACE5(_class, _i1, _i2, _i3, _i4, _i5) \
927 NS_INTERFACE_TABLE_HEAD(_class) \
928 NS_INTERFACE_TABLE5(_class, _i1, _i2, _i3, _i4, _i5) \
929 NS_INTERFACE_TABLE_TAIL
931 #define NS_IMPL_QUERY_INTERFACE6(_class, _i1, _i2, _i3, _i4, _i5, _i6) \
932 NS_INTERFACE_TABLE_HEAD(_class) \
933 NS_INTERFACE_TABLE6(_class, _i1, _i2, _i3, _i4, _i5, _i6) \
934 NS_INTERFACE_TABLE_TAIL
936 #define NS_IMPL_QUERY_INTERFACE7(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7) \
937 NS_INTERFACE_TABLE_HEAD(_class) \
938 NS_INTERFACE_TABLE7(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7) \
939 NS_INTERFACE_TABLE_TAIL
941 #define NS_IMPL_QUERY_INTERFACE8(_class, _i1, _i2, _i3, _i4, _i5, _i6, \
942 _i7, _i8) \
943 NS_INTERFACE_TABLE_HEAD(_class) \
944 NS_INTERFACE_TABLE8(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7, _i8) \
945 NS_INTERFACE_TABLE_TAIL
947 #define NS_IMPL_QUERY_INTERFACE9(_class, _i1, _i2, _i3, _i4, _i5, _i6, \
948 _i7, _i8, _i9) \
949 NS_INTERFACE_TABLE_HEAD(_class) \
950 NS_INTERFACE_TABLE9(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7, _i8, _i9) \
951 NS_INTERFACE_TABLE_TAIL
953 #define NS_IMPL_QUERY_INTERFACE10(_class, _i1, _i2, _i3, _i4, _i5, _i6, \
954 _i7, _i8, _i9, _i10) \
955 NS_INTERFACE_TABLE_HEAD(_class) \
956 NS_INTERFACE_TABLE10(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7, _i8, \
957 _i9, _i10) \
958 NS_INTERFACE_TABLE_TAIL
960 #define NS_IMPL_QUERY_INTERFACE11(_class, _i1, _i2, _i3, _i4, _i5, _i6, \
961 _i7, _i8, _i9, _i10, _i11) \
962 NS_INTERFACE_TABLE_HEAD(_class) \
963 NS_INTERFACE_TABLE11(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7, _i8, \
964 _i9, _i10, _i11) \
965 NS_INTERFACE_TABLE_TAIL
969 * Declare that you're going to inherit from something that already
970 * implements nsISupports, but also implements an additional interface, thus
971 * causing an ambiguity. In this case you don't need another mRefCnt, you
972 * just need to forward the definitions to the appropriate superclass. E.g.
974 * class Bar : public Foo, public nsIBar { // both provide nsISupports
975 * public:
976 * NS_DECL_ISUPPORTS_INHERITED
977 * ...other nsIBar and Bar methods...
978 * };
980 #define NS_DECL_ISUPPORTS_INHERITED \
981 public: \
982 NS_IMETHOD QueryInterface(REFNSIID aIID, \
983 void** aInstancePtr); \
984 NS_IMETHOD_(nsrefcnt) AddRef(void); \
985 NS_IMETHOD_(nsrefcnt) Release(void); \
988 * These macros can be used in conjunction with NS_DECL_ISUPPORTS_INHERITED
989 * to implement the nsISupports methods, forwarding the invocations to a
990 * superclass that already implements nsISupports.
992 * Note that I didn't make these inlined because they're virtual methods.
995 #define NS_IMPL_ADDREF_INHERITED(Class, Super) \
996 NS_IMETHODIMP_(nsrefcnt) Class::AddRef(void) \
998 nsrefcnt r = Super::AddRef(); \
999 NS_LOG_ADDREF(this, r, #Class, sizeof(*this)); \
1000 return r; \
1003 #define NS_IMPL_RELEASE_INHERITED(Class, Super) \
1004 NS_IMETHODIMP_(nsrefcnt) Class::Release(void) \
1006 nsrefcnt r = Super::Release(); \
1007 NS_LOG_RELEASE(this, r, #Class); \
1008 return r; \
1012 * As above but not logging the addref/release; needed if the base
1013 * class might be aggregated.
1015 #define NS_IMPL_NONLOGGING_ADDREF_INHERITED(Class, Super) \
1016 NS_IMETHODIMP_(nsrefcnt) Class::AddRef(void) \
1018 return Super::AddRef(); \
1021 #define NS_IMPL_NONLOGGING_RELEASE_INHERITED(Class, Super) \
1022 NS_IMETHODIMP_(nsrefcnt) Class::Release(void) \
1024 return Super::Release(); \
1027 #define NS_INTERFACE_TABLE_INHERITED0(Class) /* Nothing to do here */
1029 #define NS_INTERFACE_TABLE_INHERITED1(Class, i1) \
1030 NS_INTERFACE_TABLE_BEGIN \
1031 NS_INTERFACE_TABLE_ENTRY(Class, i1) \
1032 NS_INTERFACE_TABLE_END
1034 #define NS_INTERFACE_TABLE_INHERITED2(Class, i1, i2) \
1035 NS_INTERFACE_TABLE_BEGIN \
1036 NS_INTERFACE_TABLE_ENTRY(Class, i1) \
1037 NS_INTERFACE_TABLE_ENTRY(Class, i2) \
1038 NS_INTERFACE_TABLE_END
1040 #define NS_INTERFACE_TABLE_INHERITED3(Class, i1, i2, i3) \
1041 NS_INTERFACE_TABLE_BEGIN \
1042 NS_INTERFACE_TABLE_ENTRY(Class, i1) \
1043 NS_INTERFACE_TABLE_ENTRY(Class, i2) \
1044 NS_INTERFACE_TABLE_ENTRY(Class, i3) \
1045 NS_INTERFACE_TABLE_END
1047 #define NS_INTERFACE_TABLE_INHERITED4(Class, i1, i2, i3, i4) \
1048 NS_INTERFACE_TABLE_BEGIN \
1049 NS_INTERFACE_TABLE_ENTRY(Class, i1) \
1050 NS_INTERFACE_TABLE_ENTRY(Class, i2) \
1051 NS_INTERFACE_TABLE_ENTRY(Class, i3) \
1052 NS_INTERFACE_TABLE_ENTRY(Class, i4) \
1053 NS_INTERFACE_TABLE_END
1055 #define NS_INTERFACE_TABLE_INHERITED5(Class, i1, i2, i3, i4, i5) \
1056 NS_INTERFACE_TABLE_BEGIN \
1057 NS_INTERFACE_TABLE_ENTRY(Class, i1) \
1058 NS_INTERFACE_TABLE_ENTRY(Class, i2) \
1059 NS_INTERFACE_TABLE_ENTRY(Class, i3) \
1060 NS_INTERFACE_TABLE_ENTRY(Class, i4) \
1061 NS_INTERFACE_TABLE_ENTRY(Class, i5) \
1062 NS_INTERFACE_TABLE_END
1064 #define NS_INTERFACE_TABLE_INHERITED6(Class, i1, i2, i3, i4, i5, i6) \
1065 NS_INTERFACE_TABLE_BEGIN \
1066 NS_INTERFACE_TABLE_ENTRY(Class, i1) \
1067 NS_INTERFACE_TABLE_ENTRY(Class, i2) \
1068 NS_INTERFACE_TABLE_ENTRY(Class, i3) \
1069 NS_INTERFACE_TABLE_ENTRY(Class, i4) \
1070 NS_INTERFACE_TABLE_ENTRY(Class, i5) \
1071 NS_INTERFACE_TABLE_ENTRY(Class, i6) \
1072 NS_INTERFACE_TABLE_END
1074 #define NS_INTERFACE_TABLE_INHERITED7(Class, i1, i2, i3, i4, i5, i6, i7) \
1075 NS_INTERFACE_TABLE_BEGIN \
1076 NS_INTERFACE_TABLE_ENTRY(Class, i1) \
1077 NS_INTERFACE_TABLE_ENTRY(Class, i2) \
1078 NS_INTERFACE_TABLE_ENTRY(Class, i3) \
1079 NS_INTERFACE_TABLE_ENTRY(Class, i4) \
1080 NS_INTERFACE_TABLE_ENTRY(Class, i5) \
1081 NS_INTERFACE_TABLE_ENTRY(Class, i6) \
1082 NS_INTERFACE_TABLE_ENTRY(Class, i7) \
1083 NS_INTERFACE_TABLE_END
1085 #define NS_INTERFACE_TABLE_INHERITED8(Class, i1, i2, i3, i4, i5, i6, i7, i8) \
1086 NS_INTERFACE_TABLE_BEGIN \
1087 NS_INTERFACE_TABLE_ENTRY(Class, i1) \
1088 NS_INTERFACE_TABLE_ENTRY(Class, i2) \
1089 NS_INTERFACE_TABLE_ENTRY(Class, i3) \
1090 NS_INTERFACE_TABLE_ENTRY(Class, i4) \
1091 NS_INTERFACE_TABLE_ENTRY(Class, i5) \
1092 NS_INTERFACE_TABLE_ENTRY(Class, i6) \
1093 NS_INTERFACE_TABLE_ENTRY(Class, i7) \
1094 NS_INTERFACE_TABLE_ENTRY(Class, i8) \
1095 NS_INTERFACE_TABLE_END
1097 #define NS_INTERFACE_TABLE_INHERITED9(Class, i1, i2, i3, i4, i5, i6, i7, \
1098 i8, i9) \
1099 NS_INTERFACE_TABLE_BEGIN \
1100 NS_INTERFACE_TABLE_ENTRY(Class, i1) \
1101 NS_INTERFACE_TABLE_ENTRY(Class, i2) \
1102 NS_INTERFACE_TABLE_ENTRY(Class, i3) \
1103 NS_INTERFACE_TABLE_ENTRY(Class, i4) \
1104 NS_INTERFACE_TABLE_ENTRY(Class, i5) \
1105 NS_INTERFACE_TABLE_ENTRY(Class, i6) \
1106 NS_INTERFACE_TABLE_ENTRY(Class, i7) \
1107 NS_INTERFACE_TABLE_ENTRY(Class, i8) \
1108 NS_INTERFACE_TABLE_ENTRY(Class, i9) \
1109 NS_INTERFACE_TABLE_END
1111 #define NS_INTERFACE_TABLE_INHERITED10(Class, i1, i2, i3, i4, i5, i6, i7, \
1112 i8, i9, i10) \
1113 NS_INTERFACE_TABLE_BEGIN \
1114 NS_INTERFACE_TABLE_ENTRY(Class, i1) \
1115 NS_INTERFACE_TABLE_ENTRY(Class, i2) \
1116 NS_INTERFACE_TABLE_ENTRY(Class, i3) \
1117 NS_INTERFACE_TABLE_ENTRY(Class, i4) \
1118 NS_INTERFACE_TABLE_ENTRY(Class, i5) \
1119 NS_INTERFACE_TABLE_ENTRY(Class, i6) \
1120 NS_INTERFACE_TABLE_ENTRY(Class, i7) \
1121 NS_INTERFACE_TABLE_ENTRY(Class, i8) \
1122 NS_INTERFACE_TABLE_ENTRY(Class, i9) \
1123 NS_INTERFACE_TABLE_ENTRY(Class, i10) \
1124 NS_INTERFACE_TABLE_END
1126 #define NS_INTERFACE_TABLE_INHERITED11(Class, i1, i2, i3, i4, i5, i6, i7, \
1127 i8, i9, i10, i11) \
1128 NS_INTERFACE_TABLE_BEGIN \
1129 NS_INTERFACE_TABLE_ENTRY(Class, i1) \
1130 NS_INTERFACE_TABLE_ENTRY(Class, i2) \
1131 NS_INTERFACE_TABLE_ENTRY(Class, i3) \
1132 NS_INTERFACE_TABLE_ENTRY(Class, i4) \
1133 NS_INTERFACE_TABLE_ENTRY(Class, i5) \
1134 NS_INTERFACE_TABLE_ENTRY(Class, i6) \
1135 NS_INTERFACE_TABLE_ENTRY(Class, i7) \
1136 NS_INTERFACE_TABLE_ENTRY(Class, i8) \
1137 NS_INTERFACE_TABLE_ENTRY(Class, i9) \
1138 NS_INTERFACE_TABLE_ENTRY(Class, i10) \
1139 NS_INTERFACE_TABLE_ENTRY(Class, i11) \
1140 NS_INTERFACE_TABLE_END
1142 #define NS_INTERFACE_TABLE_INHERITED12(Class, i1, i2, i3, i4, i5, i6, i7, \
1143 i8, i9, i10, i11, i12) \
1144 NS_INTERFACE_TABLE_BEGIN \
1145 NS_INTERFACE_TABLE_ENTRY(Class, i1) \
1146 NS_INTERFACE_TABLE_ENTRY(Class, i2) \
1147 NS_INTERFACE_TABLE_ENTRY(Class, i3) \
1148 NS_INTERFACE_TABLE_ENTRY(Class, i4) \
1149 NS_INTERFACE_TABLE_ENTRY(Class, i5) \
1150 NS_INTERFACE_TABLE_ENTRY(Class, i6) \
1151 NS_INTERFACE_TABLE_ENTRY(Class, i7) \
1152 NS_INTERFACE_TABLE_ENTRY(Class, i8) \
1153 NS_INTERFACE_TABLE_ENTRY(Class, i9) \
1154 NS_INTERFACE_TABLE_ENTRY(Class, i10) \
1155 NS_INTERFACE_TABLE_ENTRY(Class, i11) \
1156 NS_INTERFACE_TABLE_ENTRY(Class, i12) \
1157 NS_INTERFACE_TABLE_END
1159 #define NS_IMPL_QUERY_INTERFACE_INHERITED0(Class, Super) \
1160 NS_INTERFACE_TABLE_HEAD(Class) \
1161 NS_INTERFACE_TABLE_INHERITED0(Class) \
1162 NS_INTERFACE_TABLE_TAIL_INHERITING(Super)
1164 #define NS_IMPL_QUERY_INTERFACE_INHERITED1(Class, Super, i1) \
1165 NS_INTERFACE_TABLE_HEAD(Class) \
1166 NS_INTERFACE_TABLE_INHERITED1(Class, i1) \
1167 NS_INTERFACE_TABLE_TAIL_INHERITING(Super)
1169 #define NS_IMPL_QUERY_INTERFACE_INHERITED2(Class, Super, i1, i2) \
1170 NS_INTERFACE_TABLE_HEAD(Class) \
1171 NS_INTERFACE_TABLE_INHERITED2(Class, i1, i2) \
1172 NS_INTERFACE_TABLE_TAIL_INHERITING(Super)
1174 #define NS_IMPL_QUERY_INTERFACE_INHERITED3(Class, Super, i1, i2, i3) \
1175 NS_INTERFACE_TABLE_HEAD(Class) \
1176 NS_INTERFACE_TABLE_INHERITED3(Class, i1, i2, i3) \
1177 NS_INTERFACE_TABLE_TAIL_INHERITING(Super)
1179 #define NS_IMPL_QUERY_INTERFACE_INHERITED4(Class, Super, i1, i2, i3, i4) \
1180 NS_INTERFACE_TABLE_HEAD(Class) \
1181 NS_INTERFACE_TABLE_INHERITED4(Class, i1, i2, i3, i4) \
1182 NS_INTERFACE_TABLE_TAIL_INHERITING(Super)
1184 #define NS_IMPL_QUERY_INTERFACE_INHERITED5(Class,Super,i1,i2,i3,i4,i5) \
1185 NS_INTERFACE_TABLE_HEAD(Class) \
1186 NS_INTERFACE_TABLE_INHERITED5(Class, i1, i2, i3, i4, i5) \
1187 NS_INTERFACE_TABLE_TAIL_INHERITING(Super)
1189 #define NS_IMPL_QUERY_INTERFACE_INHERITED6(Class,Super,i1,i2,i3,i4,i5,i6) \
1190 NS_INTERFACE_TABLE_HEAD(Class) \
1191 NS_INTERFACE_TABLE_INHERITED6(Class, i1, i2, i3, i4, i5, i6) \
1192 NS_INTERFACE_TABLE_TAIL_INHERITING(Super)
1194 #define NS_IMPL_QUERY_INTERFACE_INHERITED7(Class,Super,i1,i2,i3,i4,i5,i6,i7) \
1195 NS_INTERFACE_TABLE_HEAD(Class) \
1196 NS_INTERFACE_TABLE_INHERITED7(Class, i1, i2, i3, i4, i5, i6, i7) \
1197 NS_INTERFACE_TABLE_TAIL_INHERITING(Super)
1199 #define NS_IMPL_QUERY_INTERFACE_INHERITED8(Class,Super,i1,i2,i3,i4,i5,i6, \
1200 i7,i8) \
1201 NS_INTERFACE_TABLE_HEAD(Class) \
1202 NS_INTERFACE_TABLE_INHERITED8(Class, i1, i2, i3, i4, i5, i6, i7, i8) \
1203 NS_INTERFACE_TABLE_TAIL_INHERITING(Super)
1205 #define NS_IMPL_QUERY_INTERFACE_INHERITED9(Class,Super,i1,i2,i3,i4,i5,i6, \
1206 i7,i8,i9) \
1207 NS_INTERFACE_TABLE_HEAD(Class) \
1208 NS_INTERFACE_TABLE_INHERITED9(Class, i1, i2, i3, i4, i5, i6, i7, i8, i9) \
1209 NS_INTERFACE_TABLE_TAIL_INHERITING(Super)
1211 #define NS_IMPL_QUERY_INTERFACE_INHERITED10(Class,Super,i1,i2,i3,i4,i5,i6, \
1212 i7,i8,i9,i10) \
1213 NS_INTERFACE_TABLE_HEAD(Class) \
1214 NS_INTERFACE_TABLE_INHERITED10(Class, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10) \
1215 NS_INTERFACE_TABLE_TAIL_INHERITING(Super)
1218 * Convenience macros for implementing all nsISupports methods for
1219 * a simple class.
1220 * @param _class The name of the class implementing the method
1221 * @param _classiiddef The name of the #define symbol that defines the IID
1222 * for the class (e.g. NS_ISUPPORTS_IID)
1225 #define NS_IMPL_ISUPPORTS0(_class) \
1226 NS_IMPL_ADDREF(_class) \
1227 NS_IMPL_RELEASE(_class) \
1228 NS_IMPL_QUERY_INTERFACE0(_class)
1230 #define NS_IMPL_ISUPPORTS1(_class, _interface) \
1231 NS_IMPL_ADDREF(_class) \
1232 NS_IMPL_RELEASE(_class) \
1233 NS_IMPL_QUERY_INTERFACE1(_class, _interface)
1235 #define NS_IMPL_ISUPPORTS2(_class, _i1, _i2) \
1236 NS_IMPL_ADDREF(_class) \
1237 NS_IMPL_RELEASE(_class) \
1238 NS_IMPL_QUERY_INTERFACE2(_class, _i1, _i2)
1240 #define NS_IMPL_ISUPPORTS3(_class, _i1, _i2, _i3) \
1241 NS_IMPL_ADDREF(_class) \
1242 NS_IMPL_RELEASE(_class) \
1243 NS_IMPL_QUERY_INTERFACE3(_class, _i1, _i2, _i3)
1245 #define NS_IMPL_ISUPPORTS4(_class, _i1, _i2, _i3, _i4) \
1246 NS_IMPL_ADDREF(_class) \
1247 NS_IMPL_RELEASE(_class) \
1248 NS_IMPL_QUERY_INTERFACE4(_class, _i1, _i2, _i3, _i4)
1250 #define NS_IMPL_ISUPPORTS5(_class, _i1, _i2, _i3, _i4, _i5) \
1251 NS_IMPL_ADDREF(_class) \
1252 NS_IMPL_RELEASE(_class) \
1253 NS_IMPL_QUERY_INTERFACE5(_class, _i1, _i2, _i3, _i4, _i5)
1255 #define NS_IMPL_ISUPPORTS6(_class, _i1, _i2, _i3, _i4, _i5, _i6) \
1256 NS_IMPL_ADDREF(_class) \
1257 NS_IMPL_RELEASE(_class) \
1258 NS_IMPL_QUERY_INTERFACE6(_class, _i1, _i2, _i3, _i4, _i5, _i6)
1260 #define NS_IMPL_ISUPPORTS7(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7) \
1261 NS_IMPL_ADDREF(_class) \
1262 NS_IMPL_RELEASE(_class) \
1263 NS_IMPL_QUERY_INTERFACE7(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7)
1265 #define NS_IMPL_ISUPPORTS8(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7, _i8) \
1266 NS_IMPL_ADDREF(_class) \
1267 NS_IMPL_RELEASE(_class) \
1268 NS_IMPL_QUERY_INTERFACE8(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7, _i8)
1270 #define NS_IMPL_ISUPPORTS9(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7, _i8, \
1271 _i9) \
1272 NS_IMPL_ADDREF(_class) \
1273 NS_IMPL_RELEASE(_class) \
1274 NS_IMPL_QUERY_INTERFACE9(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7, _i8, _i9)
1276 #define NS_IMPL_ISUPPORTS10(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7, _i8, \
1277 _i9, _i10) \
1278 NS_IMPL_ADDREF(_class) \
1279 NS_IMPL_RELEASE(_class) \
1280 NS_IMPL_QUERY_INTERFACE10(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7, _i8, \
1281 _i9, _i10)
1283 #define NS_IMPL_ISUPPORTS11(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7, _i8, \
1284 _i9, _i10, _i11) \
1285 NS_IMPL_ADDREF(_class) \
1286 NS_IMPL_RELEASE(_class) \
1287 NS_IMPL_QUERY_INTERFACE11(_class, _i1, _i2, _i3, _i4, _i5, _i6, _i7, _i8, \
1288 _i9, _i10, _i11)
1290 #define NS_IMPL_ISUPPORTS_INHERITED0(Class, Super) \
1291 NS_IMPL_QUERY_INTERFACE_INHERITED0(Class, Super) \
1292 NS_IMPL_ADDREF_INHERITED(Class, Super) \
1293 NS_IMPL_RELEASE_INHERITED(Class, Super) \
1295 #define NS_IMPL_ISUPPORTS_INHERITED1(Class, Super, i1) \
1296 NS_IMPL_QUERY_INTERFACE_INHERITED1(Class, Super, i1) \
1297 NS_IMPL_ADDREF_INHERITED(Class, Super) \
1298 NS_IMPL_RELEASE_INHERITED(Class, Super) \
1300 #define NS_IMPL_ISUPPORTS_INHERITED2(Class, Super, i1, i2) \
1301 NS_IMPL_QUERY_INTERFACE_INHERITED2(Class, Super, i1, i2) \
1302 NS_IMPL_ADDREF_INHERITED(Class, Super) \
1303 NS_IMPL_RELEASE_INHERITED(Class, Super) \
1305 #define NS_IMPL_ISUPPORTS_INHERITED3(Class, Super, i1, i2, i3) \
1306 NS_IMPL_QUERY_INTERFACE_INHERITED3(Class, Super, i1, i2, i3) \
1307 NS_IMPL_ADDREF_INHERITED(Class, Super) \
1308 NS_IMPL_RELEASE_INHERITED(Class, Super) \
1310 #define NS_IMPL_ISUPPORTS_INHERITED4(Class, Super, i1, i2, i3, i4) \
1311 NS_IMPL_QUERY_INTERFACE_INHERITED4(Class, Super, i1, i2, i3, i4) \
1312 NS_IMPL_ADDREF_INHERITED(Class, Super) \
1313 NS_IMPL_RELEASE_INHERITED(Class, Super) \
1315 #define NS_IMPL_ISUPPORTS_INHERITED5(Class, Super, i1, i2, i3, i4, i5) \
1316 NS_IMPL_QUERY_INTERFACE_INHERITED5(Class, Super, i1, i2, i3, i4, i5) \
1317 NS_IMPL_ADDREF_INHERITED(Class, Super) \
1318 NS_IMPL_RELEASE_INHERITED(Class, Super) \
1320 #define NS_IMPL_ISUPPORTS_INHERITED6(Class, Super, i1, i2, i3, i4, i5, i6) \
1321 NS_IMPL_QUERY_INTERFACE_INHERITED6(Class, Super, i1, i2, i3, i4, i5, i6) \
1322 NS_IMPL_ADDREF_INHERITED(Class, Super) \
1323 NS_IMPL_RELEASE_INHERITED(Class, Super) \
1325 #define NS_IMPL_ISUPPORTS_INHERITED7(Class, Super, i1, i2, i3, i4, i5, i6, i7) \
1326 NS_IMPL_QUERY_INTERFACE_INHERITED7(Class, Super, i1, i2, i3, i4, i5, i6, i7) \
1327 NS_IMPL_ADDREF_INHERITED(Class, Super) \
1328 NS_IMPL_RELEASE_INHERITED(Class, Super) \
1330 #define NS_IMPL_ISUPPORTS_INHERITED8(Class, Super, i1, i2, i3, i4, i5, i6, i7, i8) \
1331 NS_IMPL_QUERY_INTERFACE_INHERITED8(Class, Super, i1, i2, i3, i4, i5, i6, i7, i8) \
1332 NS_IMPL_ADDREF_INHERITED(Class, Super) \
1333 NS_IMPL_RELEASE_INHERITED(Class, Super) \
1335 #define NS_IMPL_ISUPPORTS_INHERITED9(Class, Super, i1, i2, i3, i4, i5, i6, i7, i8, i9) \
1336 NS_IMPL_QUERY_INTERFACE_INHERITED9(Class, Super, i1, i2, i3, i4, i5, i6, i7, i8, i9) \
1337 NS_IMPL_ADDREF_INHERITED(Class, Super) \
1338 NS_IMPL_RELEASE_INHERITED(Class, Super) \
1340 #define NS_IMPL_ISUPPORTS_INHERITED10(Class, Super, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10) \
1341 NS_IMPL_QUERY_INTERFACE_INHERITED10(Class, Super, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10) \
1342 NS_IMPL_ADDREF_INHERITED(Class, Super) \
1343 NS_IMPL_RELEASE_INHERITED(Class, Super) \
1345 * Macro to glue together a QI that starts with an interface table
1346 * and segues into an interface map (e.g. it uses singleton classinfo
1347 * or tearoffs).
1349 #define NS_INTERFACE_TABLE_TO_MAP_SEGUE \
1350 if (rv == NS_OK) return rv; \
1351 nsISupports* foundInterface;
1354 ///////////////////////////////////////////////////////////////////////////////
1357 * Threadsafe implementations of the ISupports convenience macros.
1359 * @note These are not available when linking against the standalone glue,
1360 * because the implementation requires PR_ symbols.
1362 #define NS_INTERFACE_MAP_END_THREADSAFE NS_IMPL_QUERY_TAIL_GUTS
1365 * Macro to generate nsIClassInfo methods for classes which do not have
1366 * corresponding nsIFactory implementations.
1368 #define NS_IMPL_THREADSAFE_CI(_class) \
1369 NS_IMETHODIMP \
1370 _class::GetInterfaces(uint32_t* _count, nsIID*** _array) \
1372 return NS_CI_INTERFACE_GETTER_NAME(_class)(_count, _array); \
1375 NS_IMETHODIMP \
1376 _class::GetHelperForLanguage(uint32_t _language, nsISupports** _retval) \
1378 *_retval = nullptr; \
1379 return NS_OK; \
1382 NS_IMETHODIMP \
1383 _class::GetContractID(char** _contractID) \
1385 *_contractID = nullptr; \
1386 return NS_OK; \
1389 NS_IMETHODIMP \
1390 _class::GetClassDescription(char** _classDescription) \
1392 *_classDescription = nullptr; \
1393 return NS_OK; \
1396 NS_IMETHODIMP \
1397 _class::GetClassID(nsCID** _classID) \
1399 *_classID = nullptr; \
1400 return NS_OK; \
1403 NS_IMETHODIMP \
1404 _class::GetImplementationLanguage(uint32_t* _language) \
1406 *_language = nsIProgrammingLanguage::CPLUSPLUS; \
1407 return NS_OK; \
1410 NS_IMETHODIMP \
1411 _class::GetFlags(uint32_t* _flags) \
1413 *_flags = nsIClassInfo::THREADSAFE; \
1414 return NS_OK; \
1417 NS_IMETHODIMP \
1418 _class::GetClassIDNoAlloc(nsCID* _classIDNoAlloc) \
1420 return NS_ERROR_NOT_AVAILABLE; \
1423 #endif