Back to index

lightning-sunbird  0.9+nobinonly
PyISimpleEnumerator.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 <nsISimpleEnumerator.h>
00050 
00051 static nsISimpleEnumerator *GetI(PyObject *self) {
00052        nsIID iid = NS_GET_IID(nsISimpleEnumerator);
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 (nsISimpleEnumerator *)Py_nsISupports::GetI(self);
00059 }
00060 
00061 
00062 static PyObject *PyHasMoreElements(PyObject *self, PyObject *args)
00063 {
00064        if (!PyArg_ParseTuple(args, ":HasMoreElements"))
00065               return NULL;
00066 
00067        nsISimpleEnumerator *pI = GetI(self);
00068        if (pI==NULL)
00069               return NULL;
00070 
00071        nsresult r;
00072        PRBool more;
00073        Py_BEGIN_ALLOW_THREADS;
00074        r = pI->HasMoreElements(&more);
00075        Py_END_ALLOW_THREADS;
00076        if ( NS_FAILED(r) )
00077               return PyXPCOM_BuildPyException(r);
00078        return PyInt_FromLong(more);
00079 }
00080 
00081 static PyObject *PyGetNext(PyObject *self, PyObject *args)
00082 {
00083        PyObject *obIID = NULL;
00084        if (!PyArg_ParseTuple(args, "|O:GetNext", &obIID))
00085               return NULL;
00086 
00087        nsIID iid(NS_GET_IID(nsISupports));
00088        if (obIID != NULL && !Py_nsIID::IIDFromPyObject(obIID, &iid))
00089               return NULL;
00090        nsISimpleEnumerator *pI = GetI(self);
00091        if (pI==NULL)
00092               return NULL;
00093 
00094        nsISupports *pRet = nsnull;
00095        nsresult r;
00096        Py_BEGIN_ALLOW_THREADS;
00097        r = pI->GetNext(&pRet);
00098        Py_END_ALLOW_THREADS;
00099        if ( NS_FAILED(r) )
00100               return PyXPCOM_BuildPyException(r);
00101        if (obIID) {
00102               nsISupports *temp;
00103               Py_BEGIN_ALLOW_THREADS;
00104               r = pRet->QueryInterface(iid, (void **)&temp);
00105               pRet->Release();
00106               Py_END_ALLOW_THREADS;
00107               if ( NS_FAILED(r) ) {
00108                      return PyXPCOM_BuildPyException(r);
00109               }
00110               pRet = temp;
00111        }
00112        return Py_nsISupports::PyObjectFromInterface(pRet, iid, PR_FALSE);
00113 }
00114 
00115 // A method added for Python performance if you really need
00116 // it.  Allows you to fetch a block of objects in one
00117 // hit, allowing the loop to remain implemented in C.
00118 static PyObject *PyFetchBlock(PyObject *self, PyObject *args)
00119 {
00120        PyObject *obIID = NULL;
00121        int n_wanted;
00122        int n_fetched = 0;
00123        if (!PyArg_ParseTuple(args, "i|O:FetchBlock", &n_wanted, &obIID))
00124               return NULL;
00125 
00126        nsIID iid(NS_GET_IID(nsISupports));
00127        if (obIID != NULL && !Py_nsIID::IIDFromPyObject(obIID, &iid))
00128               return NULL;
00129        nsISimpleEnumerator *pI = GetI(self);
00130        if (pI==NULL)
00131               return NULL;
00132 
00133        // We want to fetch with the thread-lock released,
00134        // but this means we can not append to the PyList
00135        nsISupports **fetched = new nsISupports*[n_wanted];
00136        if (fetched==nsnull) {
00137               PyErr_NoMemory();
00138               return NULL;
00139        }
00140        memset(fetched, 0, sizeof(nsISupports *) * n_wanted);
00141        nsresult r = NS_OK;
00142        PRBool more;
00143        Py_BEGIN_ALLOW_THREADS;
00144        for (;n_fetched<n_wanted;) {
00145               r = pI->HasMoreElements(&more);
00146               if (NS_FAILED(r))
00147                      break; // this _is_ an error!
00148               if (!more)
00149                      break; // Normal enum end.
00150               nsISupports *pNew;
00151               r = pI->GetNext(&pNew);
00152               if (NS_FAILED(r)) // IS an error
00153                      break;
00154               if (obIID) {
00155                      nsISupports *temp;
00156                      r = pNew->QueryInterface(iid, (void **)&temp);
00157                      pNew->Release();
00158                      if ( NS_FAILED(r) ) {
00159                             break;
00160                      }
00161                      pNew = temp;
00162               }
00163               fetched[n_fetched] = pNew;
00164               n_fetched++;
00165        }
00166        Py_END_ALLOW_THREADS;
00167        PyObject *ret;
00168        if (NS_SUCCEEDED(r)) {
00169               ret = PyList_New(n_fetched);
00170               if (ret)
00171                      for (int i=0;i<n_fetched;i++) {
00172                             PyObject *new_ob = Py_nsISupports::PyObjectFromInterface(fetched[i], iid, PR_FALSE);
00173                             PyList_SET_ITEM(ret, i, new_ob);
00174                      }
00175        } else
00176               ret = PyXPCOM_BuildPyException(r);
00177 
00178        if ( ret == NULL ) {
00179               // Free the objects we consumed.
00180               for (int i=0;i<n_fetched;i++)
00181                      fetched[i]->Release();
00182 
00183        }
00184        delete [] fetched;
00185        return ret;
00186 }
00187 
00188 
00189 struct PyMethodDef 
00190 PyMethods_ISimpleEnumerator[] =
00191 {
00192        { "HasMoreElements", PyHasMoreElements, 1},
00193        { "hasMoreElements", PyHasMoreElements, 1},
00194        { "GetNext", PyGetNext, 1},
00195        { "getNext", PyGetNext, 1},
00196        { "FetchBlock", PyFetchBlock, 1},
00197        { "fetchBlock", PyFetchBlock, 1},
00198        {NULL}
00199 };