xpcom/base/nsISupportsObsolete.h

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:1b17e15b1c3e
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
6 #ifndef nsISupportsObsolete_h__
7 #define nsISupportsObsolete_h__
8
9 #include "prcmon.h"
10
11 ///////////////////////////////////////////////////////////////////////////////
12
13
14 #define NS_INIT_REFCNT() NS_INIT_ISUPPORTS()
15
16 /**
17 * Macro to free an array of pointers to nsISupports (or classes
18 * derived from it). A convenience wrapper around
19 * NS_FREE_XPCOM_POINTER_ARRAY.
20 *
21 * Note that if you know that none of your nsISupports pointers are
22 * going to be 0, you can gain a bit of speed by calling
23 * NS_FREE_XPCOM_POINTER_ARRAY directly and using NS_RELEASE as your
24 * free function.
25 *
26 * @param size Number of elements in the array. If not a constant, this
27 * should be a int32_t. Note that this means this macro
28 * will not work if size >= 2^31.
29 * @param array The array to be freed.
30 */
31 #define NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(size, array) \
32 NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_IF_RELEASE)
33
34
35 ///////////////////////////////////////////////////////////////////////////////
36
37 /* use these functions to associate get/set methods with a
38 C++ member variable
39 */
40
41 #define NS_METHOD_GETTER(_method, _type, _member) \
42 _method(_type* aResult) \
43 {\
44 if (!aResult) return NS_ERROR_NULL_POINTER; \
45 *aResult = _member; \
46 return NS_OK; \
47 }
48
49 #define NS_METHOD_SETTER(_method, _type, _member) \
50 _method(_type aResult) \
51 { \
52 _member = aResult; \
53 return NS_OK; \
54 }
55
56 /*
57 * special for strings to get/set char* strings
58 * using PL_strdup and PR_FREEIF
59 */
60 #define NS_METHOD_GETTER_STR(_method,_member) \
61 _method(char* *aString) \
62 { \
63 if (!aString) return NS_ERROR_NULL_POINTER; \
64 if (!(*aString = PL_strdup(_member))) \
65 return NS_ERROR_OUT_OF_MEMORY; \
66 return NS_OK; \
67 }
68
69 #define NS_METHOD_SETTER_STR(_method, _member) \
70 _method(const char *aString) \
71 { \
72 if (_member) PR_Free(_member); \
73 if (!aString) \
74 _member = nullptr; \
75 else if (!(_member = PL_strdup(aString))) \
76 return NS_ERROR_OUT_OF_MEMORY; \
77 return NS_OK; \
78 }
79
80 /* Getter/Setter macros.
81 Usage:
82 NS_IMPL_[CLASS_]GETTER[_<type>](method, [type,] member);
83 NS_IMPL_[CLASS_]SETTER[_<type>](method, [type,] member);
84 NS_IMPL_[CLASS_]GETSET[_<type>]([class, ]postfix, [type,] member);
85
86 where:
87 CLASS_ - implementation is inside a class definition
88 (otherwise the class name is needed)
89 Do NOT use in publicly exported header files, because
90 the implementation may be included many times over.
91 Instead, use the non-CLASS_ version.
92 _<type> - For more complex (STR, IFACE) data types
93 (otherwise the simple data type is needed)
94 method - name of the method, such as GetWidth or SetColor
95 type - simple data type if required
96 member - class member variable such as m_width or mColor
97 class - the class name, such as Window or MyObject
98 postfix - Method part after Get/Set such as "Width" for "GetWidth"
99
100 Example:
101 class Window {
102 public:
103 NS_IMPL_CLASS_GETSET(Width, int, m_width);
104 NS_IMPL_CLASS_GETTER_STR(GetColor, m_color);
105 NS_IMETHOD SetColor(char *color);
106
107 private:
108 int m_width; // read/write
109 char *m_color; // readonly
110 };
111
112 // defined outside of class
113 NS_IMPL_SETTER_STR(Window::GetColor, m_color);
114
115 Questions/Comments to alecf@netscape.com
116 */
117
118
119 /*
120 * Getter/Setter implementation within a class definition
121 */
122
123 /* simple data types */
124 #define NS_IMPL_CLASS_GETTER(_method, _type, _member) \
125 NS_IMETHOD NS_METHOD_GETTER(_method, _type, _member)
126
127 #define NS_IMPL_CLASS_SETTER(_method, _type, _member) \
128 NS_IMETHOD NS_METHOD_SETTER(_method, _type, _member)
129
130 #define NS_IMPL_CLASS_GETSET(_postfix, _type, _member) \
131 NS_IMPL_CLASS_GETTER(Get##_postfix, _type, _member) \
132 NS_IMPL_CLASS_SETTER(Set##_postfix, _type, _member)
133
134 /* strings */
135 #define NS_IMPL_CLASS_GETTER_STR(_method, _member) \
136 NS_IMETHOD NS_METHOD_GETTER_STR(_method, _member)
137
138 #define NS_IMPL_CLASS_SETTER_STR(_method, _member) \
139 NS_IMETHOD NS_METHOD_SETTER_STR(_method, _member)
140
141 #define NS_IMPL_CLASS_GETSET_STR(_postfix, _member) \
142 NS_IMPL_CLASS_GETTER_STR(Get##_postfix, _member) \
143 NS_IMPL_CLASS_SETTER_STR(Set##_postfix, _member)
144
145 /* Getter/Setter implementation outside of a class definition */
146
147 /* simple data types */
148 #define NS_IMPL_GETTER(_method, _type, _member) \
149 NS_IMETHODIMP NS_METHOD_GETTER(_method, _type, _member)
150
151 #define NS_IMPL_SETTER(_method, _type, _member) \
152 NS_IMETHODIMP NS_METHOD_SETTER(_method, _type, _member)
153
154 #define NS_IMPL_GETSET(_class, _postfix, _type, _member) \
155 NS_IMPL_GETTER(_class::Get##_postfix, _type, _member) \
156 NS_IMPL_SETTER(_class::Set##_postfix, _type, _member)
157
158 /* strings */
159 #define NS_IMPL_GETTER_STR(_method, _member) \
160 NS_IMETHODIMP NS_METHOD_GETTER_STR(_method, _member)
161
162 #define NS_IMPL_SETTER_STR(_method, _member) \
163 NS_IMETHODIMP NS_METHOD_SETTER_STR(_method, _member)
164
165 #define NS_IMPL_GETSET_STR(_class, _postfix, _member) \
166 NS_IMPL_GETTER_STR(_class::Get##_postfix, _member) \
167 NS_IMPL_SETTER_STR(_class::Set##_postfix, _member)
168
169 /**
170 * IID for the nsIsThreadsafe interface
171 * {88210890-47a6-11d2-bec3-00805f8a66dc}
172 *
173 * This interface is *only* used for debugging purposes to determine if
174 * a given component is threadsafe.
175 */
176 #define NS_ISTHREADSAFE_IID \
177 { 0x88210890, 0x47a6, 0x11d2, \
178 {0xbe, 0xc3, 0x00, 0x80, 0x5f, 0x8a, 0x66, 0xdc} }
179
180 #define NS_LOCK_INSTANCE() \
181 PR_CEnterMonitor((void*)this)
182 #define NS_UNLOCK_INSTANCE() \
183 PR_CExitMonitor((void*)this)
184
185 /**
186 * This implements query interface with two assumptions: First, the
187 * class in question implements nsISupports and its own interface and
188 * nothing else. Second, the implementation of the class's primary
189 * inheritance chain leads to its own interface.
190 *
191 * @param _class The name of the class implementing the method
192 * @param _classiiddef The name of the #define symbol that defines the IID
193 * for the class (e.g. NS_ISUPPORTS_IID)
194 */
195 #if defined(DEBUG)
196 #define NS_VERIFY_THREADSAFE_INTERFACE(_iface) \
197 if (nullptr != (_iface)) { \
198 nsISupports* tmp; \
199 static NS_DEFINE_IID(kIsThreadsafeIID, NS_ISTHREADSAFE_IID); \
200 NS_PRECONDITION((NS_OK == _iface->QueryInterface(kIsThreadsafeIID, \
201 (void**)&tmp)), \
202 "Interface is not threadsafe"); \
203 }
204 #else
205 #define NS_VERIFY_THREADSAFE_INTERFACE(_iface)
206 #endif
207
208 ////////////////////////////////////////////////////////////////////////////////
209
210
211
212 #endif

mercurial