VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/python/src/PyIInterfaceInfo.cpp

Last change on this file was 59809, checked in by vboxsync, 8 years ago

re-applied the remaining changes which were backed out in r105674

  • Property svn:eol-style set to native
File size: 12.5 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is the Python XPCOM language bindings.
15 *
16 * The Initial Developer of the Original Code is
17 * ActiveState Tool Corp.
18 * Portions created by the Initial Developer are Copyright (C) 2000
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Mark Hammond <mhammond@skippinet.com.au> (original author)
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * 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//
39// This code is part of the XPCOM extensions for Python.
40//
41// Written May 2000 by Mark Hammond.
42//
43// Based heavily on the Python COM support, which is
44// (c) Mark Hammond and Greg Stein.
45//
46// (c) 2000, ActiveState corp.
47
48#include "PyXPCOM_std.h"
49
50
51static nsIInterfaceInfo *GetI(PyObject *self) {
52 nsIID iid = NS_GET_IID(nsIInterfaceInfo);
53
54 if (!Py_nsISupports::Check(self, iid)) {
55 PyErr_SetString(PyExc_TypeError, "This object is not the correct interface");
56 return NULL;
57 }
58 return (nsIInterfaceInfo *)Py_nsISupports::GetI(self);
59}
60
61static PyObject *PyGetName(PyObject *self, PyObject *args)
62{
63 if (!PyArg_ParseTuple(args, ":GetName"))
64 return NULL;
65
66 nsIInterfaceInfo *pI = GetI(self);
67 if (pI==NULL)
68 return NULL;
69
70 char *name;
71 nsresult r;
72 Py_BEGIN_ALLOW_THREADS;
73 r = pI->GetName(&name);
74 Py_END_ALLOW_THREADS;
75 if ( NS_FAILED(r) )
76 return PyXPCOM_BuildPyException(r);
77#if PY_MAJOR_VERSION <= 2
78 PyObject *ret = PyString_FromString(name);
79#else
80 PyObject *ret = PyUnicode_FromString(name);
81#endif
82 nsMemory::Free(name);
83 return ret;
84}
85
86static PyObject *PyGetIID(PyObject *self, PyObject *args)
87{
88 if (!PyArg_ParseTuple(args, ":GetIID"))
89 return NULL;
90
91 nsIInterfaceInfo *pI = GetI(self);
92 if (pI==NULL)
93 return NULL;
94
95 nsIID *iid_ret;
96 nsresult r;
97 Py_BEGIN_ALLOW_THREADS;
98 r = pI->GetInterfaceIID(&iid_ret);
99 Py_END_ALLOW_THREADS;
100 if ( NS_FAILED(r) )
101 return PyXPCOM_BuildPyException(r);
102 PyObject *ret = Py_nsIID::PyObjectFromIID(*iid_ret);
103 nsMemory::Free(iid_ret);
104 return ret;
105}
106
107static PyObject *PyIsScriptable(PyObject *self, PyObject *args)
108{
109 if (!PyArg_ParseTuple(args, ":IsScriptable"))
110 return NULL;
111
112 nsIInterfaceInfo *pI = GetI(self);
113 if (pI==NULL)
114 return NULL;
115
116 PRBool b_ret;
117 nsresult r;
118 Py_BEGIN_ALLOW_THREADS;
119 r = pI->IsScriptable(&b_ret);
120 Py_END_ALLOW_THREADS;
121 if ( NS_FAILED(r) )
122 return PyXPCOM_BuildPyException(r);
123 return PyInt_FromLong(b_ret);
124}
125
126static PyObject *PyGetParent(PyObject *self, PyObject *args)
127{
128 if (!PyArg_ParseTuple(args, ":GetParent"))
129 return NULL;
130
131 nsIInterfaceInfo *pI = GetI(self);
132 if (pI==NULL)
133 return NULL;
134
135 nsCOMPtr<nsIInterfaceInfo> pRet;
136 nsresult r;
137 Py_BEGIN_ALLOW_THREADS;
138 r = pI->GetParent(getter_AddRefs(pRet));
139 Py_END_ALLOW_THREADS;
140 if ( NS_FAILED(r) )
141 return PyXPCOM_BuildPyException(r);
142 return Py_nsISupports::PyObjectFromInterface(pRet, NS_GET_IID(nsIInterfaceInfo), PR_FALSE);
143}
144
145static PyObject *PyGetMethodCount(PyObject *self, PyObject *args)
146{
147 if (!PyArg_ParseTuple(args, ":GetMethodCount"))
148 return NULL;
149
150 nsIInterfaceInfo *pI = GetI(self);
151 if (pI==NULL)
152 return NULL;
153
154 PRUint16 ret;
155 nsresult r;
156 Py_BEGIN_ALLOW_THREADS;
157 r = pI->GetMethodCount(&ret);
158 Py_END_ALLOW_THREADS;
159 if ( NS_FAILED(r) )
160 return PyXPCOM_BuildPyException(r);
161 return PyInt_FromLong(ret);
162}
163
164
165static PyObject *PyGetConstantCount(PyObject *self, PyObject *args)
166{
167 if (!PyArg_ParseTuple(args, ":GetConstantCount"))
168 return NULL;
169
170 nsIInterfaceInfo *pI = GetI(self);
171 if (pI==NULL)
172 return NULL;
173
174 PRUint16 ret;
175 nsresult r;
176 Py_BEGIN_ALLOW_THREADS;
177 r = pI->GetConstantCount(&ret);
178 Py_END_ALLOW_THREADS;
179 if ( NS_FAILED(r) )
180 return PyXPCOM_BuildPyException(r);
181 return PyInt_FromLong(ret);
182}
183
184static PyObject *PyGetMethodInfo(PyObject *self, PyObject *args)
185{
186 PRUint16 index;
187 if (!PyArg_ParseTuple(args, "h:GetMethodInfo", &index))
188 return NULL;
189
190 nsIInterfaceInfo *pI = GetI(self);
191 if (pI==NULL)
192 return NULL;
193
194 PRUint16 nmethods;
195 pI->GetMethodCount(&nmethods);
196 if (index>=nmethods) {
197 PyErr_SetString(PyExc_ValueError, "The method index is out of range");
198 return NULL;
199 }
200
201 const nsXPTMethodInfo *pRet;
202 nsresult r;
203 Py_BEGIN_ALLOW_THREADS;
204 r = pI->GetMethodInfo(index, &pRet);
205 Py_END_ALLOW_THREADS;
206 if ( NS_FAILED(r) )
207 return PyXPCOM_BuildPyException(r);
208 return PyObject_FromXPTMethodDescriptor(pRet);
209}
210
211static PyObject *PyGetMethodInfoForName(PyObject *self, PyObject *args)
212{
213 char *name;
214 if (!PyArg_ParseTuple(args, "s:GetMethodInfoForName", &name))
215 return NULL;
216
217 nsIInterfaceInfo *pI = GetI(self);
218 if (pI==NULL)
219 return NULL;
220
221 const nsXPTMethodInfo *pRet;
222 PRUint16 index;
223 nsresult r;
224 Py_BEGIN_ALLOW_THREADS;
225 r = pI->GetMethodInfoForName(name, &index, &pRet);
226 Py_END_ALLOW_THREADS;
227 if ( NS_FAILED(r) )
228 return PyXPCOM_BuildPyException(r);
229 PyObject *ret_i = PyObject_FromXPTMethodDescriptor(pRet);
230 if (ret_i==NULL)
231 return NULL;
232 PyObject *real_ret = Py_BuildValue("iO", (int)index, ret_i);
233 Py_DECREF(ret_i);
234 return real_ret;
235}
236
237
238static PyObject *PyGetConstant(PyObject *self, PyObject *args)
239{
240 PRUint16 index;
241 if (!PyArg_ParseTuple(args, "h:GetConstant", &index))
242 return NULL;
243
244 nsIInterfaceInfo *pI = GetI(self);
245 if (pI==NULL)
246 return NULL;
247
248 const nsXPTConstant *pRet;
249 nsresult r;
250 Py_BEGIN_ALLOW_THREADS;
251 r = pI->GetConstant(index, &pRet);
252 Py_END_ALLOW_THREADS;
253 if ( NS_FAILED(r) )
254 return PyXPCOM_BuildPyException(r);
255 return PyObject_FromXPTConstant(pRet);
256}
257
258static PRBool __GetMethodInfoHelper(nsIInterfaceInfo *pii, int mi, int pi, const nsXPTMethodInfo **ppmi)
259{
260 PRUint16 nmethods=0;
261 pii->GetMethodCount(&nmethods);
262 if (mi<0 || mi>=nmethods) {
263 PyErr_SetString(PyExc_ValueError, "The method index is out of range");
264 return PR_FALSE;
265 }
266 const nsXPTMethodInfo *pmi;
267 nsresult r = pii->GetMethodInfo(mi, &pmi);
268 if ( NS_FAILED(r) ) {
269 PyXPCOM_BuildPyException(r);
270 return PR_FALSE;
271 }
272
273 int nparams=0;
274 nparams = pmi->GetParamCount();
275 if (pi<0 || pi>=nparams) {
276 PyErr_SetString(PyExc_ValueError, "The param index is out of range");
277 return PR_FALSE;
278 }
279 *ppmi = pmi;
280 return PR_TRUE;
281}
282
283static PyObject *PyGetInfoForParam(PyObject *self, PyObject *args)
284{
285 nsIInterfaceInfo *pii = GetI(self);
286 if (pii==NULL)
287 return NULL;
288 PRUint16 mi, pi;
289 if (!PyArg_ParseTuple(args, "hh:GetInfoForParam", &mi, &pi))
290 return NULL;
291 const nsXPTMethodInfo *pmi;
292 if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
293 return NULL;
294 const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
295 nsCOMPtr<nsIInterfaceInfo> pnewii;
296 nsresult n = pii->GetInfoForParam(mi, &param_info, getter_AddRefs(pnewii));
297 if (NS_FAILED(n))
298 return PyXPCOM_BuildPyException(n);
299 return Py_nsISupports::PyObjectFromInterface(pnewii, NS_GET_IID(nsIInterfaceInfo));
300}
301
302static PyObject *PyGetIIDForParam(PyObject *self, PyObject *args)
303{
304 nsIInterfaceInfo *pii = GetI(self);
305 if (pii==NULL)
306 return NULL;
307 PRUint16 mi, pi;
308 if (!PyArg_ParseTuple(args, "hh:GetIIDForParam", &mi, &pi))
309 return NULL;
310 const nsXPTMethodInfo *pmi;
311 if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
312 return NULL;
313 const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
314 nsIID *piid;
315 nsresult n = pii->GetIIDForParam(mi, &param_info, &piid);
316 if (NS_FAILED(n) || piid==nsnull)
317 return PyXPCOM_BuildPyException(n);
318 PyObject *rc = Py_nsIID::PyObjectFromIID(*piid);
319 nsMemory::Free((void*)piid);
320 return rc;
321}
322
323static PyObject *PyGetTypeForParam(PyObject *self, PyObject *args)
324{
325 nsIInterfaceInfo *pii = GetI(self);
326 if (pii==NULL)
327 return NULL;
328 PRUint16 mi, pi, dim;
329 if (!PyArg_ParseTuple(args, "hhh:GetTypeForParam", &mi, &pi, &dim))
330 return NULL;
331 const nsXPTMethodInfo *pmi;
332 if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
333 return NULL;
334 nsXPTType datumType;
335 const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
336 nsresult n = pii->GetTypeForParam(mi, &param_info, dim, &datumType);
337 if (NS_FAILED(n))
338 return PyXPCOM_BuildPyException(n);
339 return PyObject_FromXPTType(&datumType);
340}
341
342static PyObject *PyGetSizeIsArgNumberForParam(PyObject *self, PyObject *args)
343{
344 nsIInterfaceInfo *pii = GetI(self);
345 if (pii==NULL)
346 return NULL;
347 PRUint16 mi, pi, dim;
348 if (!PyArg_ParseTuple(args, "hhh:GetSizeIsArgNumberForParam", &mi, &pi, &dim))
349 return NULL;
350 const nsXPTMethodInfo *pmi;
351 if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
352 return NULL;
353 PRUint8 ret;
354 const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
355 nsresult n = pii->GetSizeIsArgNumberForParam(mi, &param_info, dim, &ret);
356 if (NS_FAILED(n))
357 return PyXPCOM_BuildPyException(n);
358 return PyInt_FromLong(ret);
359}
360
361static PyObject *PyGetLengthIsArgNumberForParam(PyObject *self, PyObject *args)
362{
363 nsIInterfaceInfo *pii = GetI(self);
364 if (pii==NULL)
365 return NULL;
366 PRUint16 mi, pi, dim;
367 if (!PyArg_ParseTuple(args, "hhh:GetLengthIsArgNumberForParam", &mi, &pi, &dim))
368 return NULL;
369 const nsXPTMethodInfo *pmi;
370 if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
371 return NULL;
372 PRUint8 ret;
373 const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
374 nsresult n = pii->GetLengthIsArgNumberForParam(mi, &param_info, dim, &ret);
375 if (NS_FAILED(n))
376 return PyXPCOM_BuildPyException(n);
377 return PyInt_FromLong(ret);
378}
379
380static PyObject *PyGetInterfaceIsArgNumberForParam(PyObject *self, PyObject *args)
381{
382 nsIInterfaceInfo *pii = GetI(self);
383 if (pii==NULL)
384 return NULL;
385 PRUint16 mi, pi;
386 if (!PyArg_ParseTuple(args, "hhh:GetInterfaceIsArgNumberForParam", &mi, &pi))
387 return NULL;
388 const nsXPTMethodInfo *pmi;
389 if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
390 return NULL;
391 PRUint8 ret;
392 const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
393 nsresult n = pii->GetInterfaceIsArgNumberForParam(mi, &param_info, &ret);
394 if (NS_FAILED(n))
395 return PyXPCOM_BuildPyException(n);
396 return PyInt_FromLong(ret);
397}
398
399struct PyMethodDef
400PyMethods_IInterfaceInfo[] =
401{
402 { "GetName", PyGetName, 1},
403 { "GetIID", PyGetIID, 1},
404 { "IsScriptable", PyIsScriptable, 1},
405 { "GetParent", PyGetParent, 1},
406 { "GetMethodCount", PyGetMethodCount, 1},
407 { "GetConstantCount", PyGetConstantCount, 1},
408 { "GetMethodInfo", PyGetMethodInfo, 1},
409 { "GetMethodInfoForName", PyGetMethodInfoForName, 1},
410 { "GetConstant", PyGetConstant, 1},
411 { "GetInfoForParam", PyGetInfoForParam, 1},
412 { "GetIIDForParam", PyGetIIDForParam, 1},
413 { "GetTypeForParam", PyGetTypeForParam, 1},
414 { "GetSizeIsArgNumberForParam", PyGetSizeIsArgNumberForParam, 1},
415 { "GetLengthIsArgNumberForParam", PyGetLengthIsArgNumberForParam, 1},
416 { "GetInterfaceIsArgNumberForParam", PyGetInterfaceIsArgNumberForParam, 1},
417 {NULL}
418};
419
420/*
421 NS_IMETHOD GetMethodInfo(PRUint16 index, const nsXPTMethodInfo * *info) = 0;
422 NS_IMETHOD GetMethodInfoForName(const char *methodName, PRUint16 *index, const nsXPTMethodInfo * *info) = 0;
423 NS_IMETHOD GetConstant(PRUint16 index, const nsXPTConstant * *constant) = 0;
424 NS_IMETHOD GetInfoForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, nsIInterfaceInfo **_retval) = 0;
425 NS_IMETHOD GetIIDForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, nsIID * *_retval) = 0;
426 NS_IMETHOD GetTypeForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, PRUint16 dimension, nsXPTType *_retval) = 0;
427 NS_IMETHOD GetSizeIsArgNumberForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, PRUint16 dimension, PRUint8 *_retval) = 0;
428 NS_IMETHOD GetLengthIsArgNumberForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, PRUint16 dimension, PRUint8 *_retval) = 0;
429 NS_IMETHOD GetInterfaceIsArgNumberForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, PRUint8 *_retval) = 0;
430
431*/
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use