Back to index

lightning-sunbird  0.9+nobinonly
PyIEnumerator.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Python XPCOM language bindings.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * ActiveState Tool Corp.
00018  * Portions created by the Initial Developer are Copyright (C) 2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Mark Hammond <MarkH@ActiveState.com> (original author)
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 //
00039 // This code is part of the XPCOM extensions for Python.
00040 //
00041 // Written May 2000 by Mark Hammond.
00042 //
00043 // Based heavily on the Python COM support, which is
00044 // (c) Mark Hammond and Greg Stein.
00045 //
00046 // (c) 2000, ActiveState corp.
00047 
00048 #include "PyXPCOM_std.h"
00049 #include <nsIEnumerator.h>
00050 
00051 static nsIEnumerator *GetI(PyObject *self) {
00052        nsIID iid = NS_GET_IID(nsIEnumerator);
00053 
00054        if (!Py_nsISupports::Check(self, iid)) {
00055               PyErr_SetString(PyExc_TypeError, "This object is not the correct interface");
00056               return NULL;
00057        }
00058        return (nsIEnumerator *)Py_nsISupports::GetI(self);
00059 }
00060 
00061 static PyObject *PyFirst(PyObject *self, PyObject *args)
00062 {
00063        if (!PyArg_ParseTuple(args, ":First"))
00064               return NULL;
00065 
00066        nsIEnumerator *pI = GetI(self);
00067        if (pI==NULL)
00068               return NULL;
00069 
00070        nsresult r;
00071        Py_BEGIN_ALLOW_THREADS;
00072        r = pI->First();
00073        Py_END_ALLOW_THREADS;
00074        return PyInt_FromLong(r);
00075 }
00076 
00077 static PyObject *PyNext(PyObject *self, PyObject *args)
00078 {
00079        if (!PyArg_ParseTuple(args, ":Next"))
00080               return NULL;
00081 
00082        nsIEnumerator *pI = GetI(self);
00083        if (pI==NULL)
00084               return NULL;
00085 
00086        nsresult r;
00087        Py_BEGIN_ALLOW_THREADS;
00088        r = pI->Next();
00089        Py_END_ALLOW_THREADS;
00090        return PyInt_FromLong(r);
00091 }
00092 
00093 static PyObject *PyCurrentItem(PyObject *self, PyObject *args)
00094 {
00095        PyObject *obIID = NULL;
00096        if (!PyArg_ParseTuple(args, "|O:CurrentItem", &obIID))
00097               return NULL;
00098 
00099        nsIID iid(NS_GET_IID(nsISupports));
00100        if (obIID != NULL && !Py_nsIID::IIDFromPyObject(obIID, &iid))
00101               return NULL;
00102        nsIEnumerator *pI = GetI(self);
00103        if (pI==NULL)
00104               return NULL;
00105 
00106        nsISupports *pRet = nsnull;
00107        nsresult r;
00108        Py_BEGIN_ALLOW_THREADS;
00109        r = pI->CurrentItem(&pRet);
00110        Py_END_ALLOW_THREADS;
00111        if ( NS_FAILED(r) )
00112               return PyXPCOM_BuildPyException(r);
00113        if (obIID) {
00114               nsISupports *temp;
00115               Py_BEGIN_ALLOW_THREADS;
00116               r = pRet->QueryInterface(iid, (void **)&temp);
00117               pRet->Release();
00118               Py_END_ALLOW_THREADS;
00119               if ( NS_FAILED(r) ) {
00120                      return PyXPCOM_BuildPyException(r);
00121               }
00122               pRet = temp;
00123        }
00124        return Py_nsISupports::PyObjectFromInterface(pRet, iid, PR_FALSE);
00125 }
00126 
00127 // A method added for Python performance if you really need
00128 // it.  Allows you to fetch a block of objects in one
00129 // hit, allowing the loop to remain implemented in C.
00130 static PyObject *PyFetchBlock(PyObject *self, PyObject *args)
00131 {
00132        PyObject *obIID = NULL;
00133        int n_wanted;
00134        int n_fetched = 0;
00135        if (!PyArg_ParseTuple(args, "i|O:FetchBlock", &n_wanted, &obIID))
00136               return NULL;
00137 
00138        nsIID iid(NS_GET_IID(nsISupports));
00139        if (obIID != NULL && !Py_nsIID::IIDFromPyObject(obIID, &iid))
00140               return NULL;
00141        nsIEnumerator *pI = GetI(self);
00142        if (pI==NULL)
00143               return NULL;
00144 
00145        // We want to fetch with the thread-lock released,
00146        // but this means we can not append to the PyList
00147        nsISupports **fetched = new nsISupports*[n_wanted];
00148        if (fetched==nsnull) {
00149               PyErr_NoMemory();
00150               return NULL;
00151        }
00152        memset(fetched, 0, sizeof(nsISupports *) * n_wanted);
00153        nsresult r = NS_OK;
00154        Py_BEGIN_ALLOW_THREADS;
00155        for (;n_fetched<n_wanted;) {
00156               nsISupports *pNew;
00157               r = pI->CurrentItem(&pNew);
00158               if (NS_FAILED(r)) {
00159                      r = 0; // Normal enum end
00160                      break;
00161               }
00162               if (obIID) {
00163                      nsISupports *temp;
00164                      r = pNew->QueryInterface(iid, (void **)&temp);
00165                      pNew->Release();
00166                      if ( NS_FAILED(r) ) {
00167                             break;
00168                      }
00169                      pNew = temp;
00170               }
00171               fetched[n_fetched] = pNew;
00172               n_fetched++; // must increment before breaking out.
00173               if (NS_FAILED(pI->Next()))
00174                      break; // not an error condition.
00175        }
00176        Py_END_ALLOW_THREADS;
00177        PyObject *ret;
00178        if (NS_SUCCEEDED(r)) {
00179               ret = PyList_New(n_fetched);
00180               if (ret)
00181                      for (int i=0;i<n_fetched;i++) {
00182                             PyObject *new_ob = Py_nsISupports::PyObjectFromInterface(fetched[i], iid, PR_FALSE);
00183                             PyList_SET_ITEM(ret, i, new_ob);
00184                      }
00185        } else
00186               ret = PyXPCOM_BuildPyException(r);
00187 
00188        if ( ret == NULL ) {
00189               // Free the objects we consumed.
00190               for (int i=0;i<n_fetched;i++)
00191                      fetched[i]->Release();
00192 
00193        }
00194        delete [] fetched;
00195        return ret;
00196 }
00197 
00198 static PyObject *PyIsDone(PyObject *self, PyObject *args)
00199 {
00200        if (!PyArg_ParseTuple(args, ":IsDone"))
00201               return NULL;
00202 
00203        nsIEnumerator *pI = GetI(self);
00204        nsresult r;
00205        if (pI==NULL)
00206               return NULL;
00207 
00208        Py_BEGIN_ALLOW_THREADS;
00209        r = pI->IsDone();
00210        Py_END_ALLOW_THREADS;
00211        if (NS_FAILED(r))
00212               return PyXPCOM_BuildPyException(r);
00213        PyObject *ret = r==NS_OK ? Py_True : Py_False;
00214        Py_INCREF(ret);
00215        return ret;
00216 }
00217 
00218 struct PyMethodDef 
00219 PyMethods_IEnumerator[] =
00220 {
00221        { "First", PyFirst, 1},
00222        { "first", PyFirst, 1},
00223        { "Next", PyNext, 1},
00224        { "next", PyNext, 1},
00225        { "CurrentItem", PyCurrentItem, 1},
00226        { "currentItem", PyCurrentItem, 1},
00227        { "IsDone", PyIsDone, 1},
00228        { "isDone", PyIsDone, 1},
00229        { "FetchBlock", PyFetchBlock, 1},
00230        { "fetchBlock", PyFetchBlock, 1},
00231        {NULL}
00232 };