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 <MarkH@ActiveState.com> (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 | #include <nsISimpleEnumerator.h>
|
---|
50 |
|
---|
51 | static nsISimpleEnumerator *GetI(PyObject *self) {
|
---|
52 | nsIID iid = NS_GET_IID(nsISimpleEnumerator);
|
---|
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 (nsISimpleEnumerator *)Py_nsISupports::GetI(self);
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | static PyObject *PyHasMoreElements(PyObject *self, PyObject *args)
|
---|
63 | {
|
---|
64 | if (!PyArg_ParseTuple(args, ":HasMoreElements"))
|
---|
65 | return NULL;
|
---|
66 |
|
---|
67 | nsISimpleEnumerator *pI = GetI(self);
|
---|
68 | if (pI==NULL)
|
---|
69 | return NULL;
|
---|
70 |
|
---|
71 | nsresult r;
|
---|
72 | PRBool more;
|
---|
73 | Py_BEGIN_ALLOW_THREADS;
|
---|
74 | r = pI->HasMoreElements(&more);
|
---|
75 | Py_END_ALLOW_THREADS;
|
---|
76 | if ( NS_FAILED(r) )
|
---|
77 | return PyXPCOM_BuildPyException(r);
|
---|
78 | return PyInt_FromLong(more);
|
---|
79 | }
|
---|
80 |
|
---|
81 | static PyObject *PyGetNext(PyObject *self, PyObject *args)
|
---|
82 | {
|
---|
83 | PyObject *obIID = NULL;
|
---|
84 | if (!PyArg_ParseTuple(args, "|O:GetNext", &obIID))
|
---|
85 | return NULL;
|
---|
86 |
|
---|
87 | nsIID iid(NS_GET_IID(nsISupports));
|
---|
88 | if (obIID != NULL && !Py_nsIID::IIDFromPyObject(obIID, &iid))
|
---|
89 | return NULL;
|
---|
90 | nsISimpleEnumerator *pI = GetI(self);
|
---|
91 | if (pI==NULL)
|
---|
92 | return NULL;
|
---|
93 |
|
---|
94 | nsISupports *pRet = nsnull;
|
---|
95 | nsresult r;
|
---|
96 | Py_BEGIN_ALLOW_THREADS;
|
---|
97 | r = pI->GetNext(&pRet);
|
---|
98 | Py_END_ALLOW_THREADS;
|
---|
99 | if ( NS_FAILED(r) )
|
---|
100 | return PyXPCOM_BuildPyException(r);
|
---|
101 | if (obIID) {
|
---|
102 | nsISupports *temp;
|
---|
103 | Py_BEGIN_ALLOW_THREADS;
|
---|
104 | r = pRet->QueryInterface(iid, (void **)&temp);
|
---|
105 | pRet->Release();
|
---|
106 | Py_END_ALLOW_THREADS;
|
---|
107 | if ( NS_FAILED(r) ) {
|
---|
108 | return PyXPCOM_BuildPyException(r);
|
---|
109 | }
|
---|
110 | pRet = temp;
|
---|
111 | }
|
---|
112 | PyObject *ret = Py_nsISupports::PyObjectFromInterface(pRet, iid);
|
---|
113 | NS_IF_RELEASE(pRet);
|
---|
114 | return ret;
|
---|
115 | }
|
---|
116 |
|
---|
117 | // A method added for Python performance if you really need
|
---|
118 | // it. Allows you to fetch a block of objects in one
|
---|
119 | // hit, allowing the loop to remain implemented in C.
|
---|
120 | static PyObject *PyFetchBlock(PyObject *self, PyObject *args)
|
---|
121 | {
|
---|
122 | PyObject *obIID = NULL;
|
---|
123 | int n_wanted;
|
---|
124 | int n_fetched = 0;
|
---|
125 | if (!PyArg_ParseTuple(args, "i|O:FetchBlock", &n_wanted, &obIID))
|
---|
126 | return NULL;
|
---|
127 |
|
---|
128 | nsIID iid(NS_GET_IID(nsISupports));
|
---|
129 | if (obIID != NULL && !Py_nsIID::IIDFromPyObject(obIID, &iid))
|
---|
130 | return NULL;
|
---|
131 | nsISimpleEnumerator *pI = GetI(self);
|
---|
132 | if (pI==NULL)
|
---|
133 | return NULL;
|
---|
134 |
|
---|
135 | // We want to fetch with the thread-lock released,
|
---|
136 | // but this means we can not append to the PyList
|
---|
137 | nsISupports **fetched = new nsISupports*[n_wanted];
|
---|
138 | if (fetched==nsnull) {
|
---|
139 | PyErr_NoMemory();
|
---|
140 | return NULL;
|
---|
141 | }
|
---|
142 | memset(fetched, 0, sizeof(nsISupports *) * n_wanted);
|
---|
143 | nsresult r = NS_OK;
|
---|
144 | PRBool more;
|
---|
145 | Py_BEGIN_ALLOW_THREADS;
|
---|
146 | for (;n_fetched<n_wanted;) {
|
---|
147 | r = pI->HasMoreElements(&more);
|
---|
148 | if (NS_FAILED(r))
|
---|
149 | break; // this _is_ an error!
|
---|
150 | if (!more)
|
---|
151 | break; // Normal enum end.
|
---|
152 | nsISupports *pNew;
|
---|
153 | r = pI->GetNext(&pNew);
|
---|
154 | if (NS_FAILED(r)) // IS an error
|
---|
155 | break;
|
---|
156 | if (obIID) {
|
---|
157 | nsISupports *temp;
|
---|
158 | r = pNew->QueryInterface(iid, (void **)&temp);
|
---|
159 | pNew->Release();
|
---|
160 | if ( NS_FAILED(r) ) {
|
---|
161 | break;
|
---|
162 | }
|
---|
163 | pNew = temp;
|
---|
164 | }
|
---|
165 | fetched[n_fetched] = pNew;
|
---|
166 | n_fetched++;
|
---|
167 | }
|
---|
168 | Py_END_ALLOW_THREADS;
|
---|
169 | PyObject *ret;
|
---|
170 | if (NS_SUCCEEDED(r)) {
|
---|
171 | ret = PyList_New(n_fetched);
|
---|
172 | if (ret)
|
---|
173 | for (int i=0;i<n_fetched;i++) {
|
---|
174 | PyObject *new_ob = Py_nsISupports::PyObjectFromInterface(fetched[i], iid);
|
---|
175 | NS_IF_RELEASE(fetched[i]);
|
---|
176 | PyList_SET_ITEM(ret, i, new_ob);
|
---|
177 | }
|
---|
178 | } else
|
---|
179 | ret = PyXPCOM_BuildPyException(r);
|
---|
180 |
|
---|
181 | if ( ret == NULL ) {
|
---|
182 | // Free the objects we consumed.
|
---|
183 | for (int i=0;i<n_fetched;i++)
|
---|
184 | fetched[i]->Release();
|
---|
185 |
|
---|
186 | }
|
---|
187 | delete [] fetched;
|
---|
188 | return ret;
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | struct PyMethodDef
|
---|
193 | PyMethods_ISimpleEnumerator[] =
|
---|
194 | {
|
---|
195 | { "HasMoreElements", PyHasMoreElements, 1},
|
---|
196 | { "hasMoreElements", PyHasMoreElements, 1},
|
---|
197 | { "GetNext", PyGetNext, 1},
|
---|
198 | { "getNext", PyGetNext, 1},
|
---|
199 | { "FetchBlock", PyFetchBlock, 1},
|
---|
200 | { "fetchBlock", PyFetchBlock, 1},
|
---|
201 | {NULL}
|
---|
202 | };
|
---|