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
|
---|
4 | *
|
---|
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/
|
---|
9 | *
|
---|
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.
|
---|
14 | *
|
---|
15 | * The Original Code is mozilla.org code.
|
---|
16 | *
|
---|
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.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
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.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #ifndef nsAgg_h___
|
---|
39 | #define nsAgg_h___
|
---|
40 |
|
---|
41 | #include "nsISupports.h"
|
---|
42 |
|
---|
43 |
|
---|
44 | ////////////////////////////////////////////////////////////////////////////////
|
---|
45 |
|
---|
46 | // Put this in your class's declaration:
|
---|
47 | #define NS_DECL_AGGREGATED \
|
---|
48 | NS_DECL_ISUPPORTS \
|
---|
49 | \
|
---|
50 | public: \
|
---|
51 | \
|
---|
52 | /* You must implement this operation instead of the nsISupports */ \
|
---|
53 | /* methods if you inherit from nsAggregated. */ \
|
---|
54 | NS_IMETHOD \
|
---|
55 | AggregatedQueryInterface(const nsIID& aIID, void** aInstancePtr); \
|
---|
56 | \
|
---|
57 | protected: \
|
---|
58 | \
|
---|
59 | class Internal : public nsISupports { \
|
---|
60 | public: \
|
---|
61 | \
|
---|
62 | Internal() {} \
|
---|
63 | \
|
---|
64 | NS_IMETHOD QueryInterface(const nsIID& aIID, \
|
---|
65 | void** aInstancePtr); \
|
---|
66 | NS_IMETHOD_(nsrefcnt) AddRef(void); \
|
---|
67 | NS_IMETHOD_(nsrefcnt) Release(void); \
|
---|
68 | \
|
---|
69 | }; \
|
---|
70 | \
|
---|
71 | friend class Internal; \
|
---|
72 | \
|
---|
73 | nsISupports* fOuter; \
|
---|
74 | Internal fAggregated; \
|
---|
75 | \
|
---|
76 | nsISupports* GetInner(void) { return &fAggregated; } \
|
---|
77 | \
|
---|
78 | public: \
|
---|
79 |
|
---|
80 |
|
---|
81 | // Put this in your class's constructor:
|
---|
82 | #define NS_INIT_AGGREGATED(outer) \
|
---|
83 | PR_BEGIN_MACRO \
|
---|
84 | fOuter = outer ? outer : &fAggregated; \
|
---|
85 | PR_END_MACRO
|
---|
86 |
|
---|
87 |
|
---|
88 | // Put this in your class's implementation file:
|
---|
89 | #define NS_IMPL_AGGREGATED(_class) \
|
---|
90 | NS_IMETHODIMP \
|
---|
91 | _class::QueryInterface(const nsIID& aIID, void** aInstancePtr) \
|
---|
92 | { \
|
---|
93 | return fOuter->QueryInterface(aIID, aInstancePtr); \
|
---|
94 | } \
|
---|
95 | \
|
---|
96 | NS_IMETHODIMP_(nsrefcnt) \
|
---|
97 | _class::AddRef(void) \
|
---|
98 | { \
|
---|
99 | return fOuter->AddRef(); \
|
---|
100 | } \
|
---|
101 | \
|
---|
102 | NS_IMETHODIMP_(nsrefcnt) \
|
---|
103 | _class::Release(void) \
|
---|
104 | { \
|
---|
105 | return fOuter->Release(); \
|
---|
106 | } \
|
---|
107 | \
|
---|
108 | NS_IMETHODIMP \
|
---|
109 | _class::Internal::QueryInterface(const nsIID& aIID, void** aInstancePtr) \
|
---|
110 | { \
|
---|
111 | _class* agg = (_class*)((char*)(this) - offsetof(_class, fAggregated)); \
|
---|
112 | return agg->AggregatedQueryInterface(aIID, aInstancePtr); \
|
---|
113 | } \
|
---|
114 | \
|
---|
115 | NS_IMETHODIMP_(nsrefcnt) \
|
---|
116 | _class::Internal::AddRef(void) \
|
---|
117 | { \
|
---|
118 | _class* agg = (_class*)((char*)(this) - offsetof(_class, fAggregated)); \
|
---|
119 | NS_PRECONDITION(PRInt32(agg->mRefCnt) >= 0, "illegal refcnt"); \
|
---|
120 | ++agg->mRefCnt; \
|
---|
121 | NS_LOG_ADDREF(this, agg->mRefCnt, #_class, sizeof(*this)); \
|
---|
122 | return agg->mRefCnt; \
|
---|
123 | } \
|
---|
124 | \
|
---|
125 | NS_IMETHODIMP_(nsrefcnt) \
|
---|
126 | _class::Internal::Release(void) \
|
---|
127 | { \
|
---|
128 | _class* agg = (_class*)((char*)(this) - offsetof(_class, fAggregated)); \
|
---|
129 | NS_PRECONDITION(0 != agg->mRefCnt, "dup release"); \
|
---|
130 | --agg->mRefCnt; \
|
---|
131 | NS_LOG_RELEASE(this, agg->mRefCnt, #_class); \
|
---|
132 | if (agg->mRefCnt == 0) { \
|
---|
133 | agg->mRefCnt = 1; /* stabilize */ \
|
---|
134 | NS_DELETEXPCOM(agg); \
|
---|
135 | return 0; \
|
---|
136 | } \
|
---|
137 | return agg->mRefCnt; \
|
---|
138 | } \
|
---|
139 |
|
---|
140 | // for use with QI macros in nsISupportsUtils.h:
|
---|
141 |
|
---|
142 | #define NS_INTERFACE_MAP_BEGIN_AGGREGATED(_class) \
|
---|
143 | NS_IMPL_AGGREGATED_QUERY_HEAD(_class)
|
---|
144 |
|
---|
145 | #define NS_IMPL_AGGREGATED_QUERY_HEAD(_class) \
|
---|
146 | NS_IMETHODIMP \
|
---|
147 | _class::AggregatedQueryInterface(REFNSIID aIID, void** aInstancePtr) \
|
---|
148 | { \
|
---|
149 | NS_ASSERTION(aInstancePtr, \
|
---|
150 | "AggregatedQueryInterface requires a non-NULL result ptr!"); \
|
---|
151 | if ( !aInstancePtr ) \
|
---|
152 | return NS_ERROR_NULL_POINTER; \
|
---|
153 | nsISupports* foundInterface;
|
---|
154 |
|
---|
155 | #endif /* nsAgg_h___ */
|
---|