Back to index

lightning-sunbird  0.9+nobinonly
Pyxpt_info.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 // Pyxpt_info.cpp - wrappers for the xpt_info objects.
00039 //
00040 // This code is part of the XPCOM extensions for Python.
00041 //
00042 // Written May 2000 by Mark Hammond.
00043 //
00044 // Based heavily on the Python COM support, which is
00045 // (c) Mark Hammond and Greg Stein.
00046 //
00047 // (c) 2000, ActiveState corp.
00048 #include "PyXPCOM_std.h"
00049 
00050 PyObject *PyObject_FromXPTType( const nsXPTType *d)
00051 {
00052        if (d==nsnull) {
00053               Py_INCREF(Py_None);
00054               return Py_None;
00055        }
00056     // build an object using the same format as a TypeDescriptor.
00057        return Py_BuildValue("bzzz", 
00058               d->flags,
00059               NULL, NULL, NULL);
00060 }
00061 
00062 PyObject *PyObject_FromXPTTypeDescriptor( const XPTTypeDescriptor *d)
00063 {
00064        if (d==nsnull) {
00065               Py_INCREF(Py_None);
00066               return Py_None;
00067        }
00068        return Py_BuildValue("bbbh", 
00069               d->prefix.flags,
00070               d->argnum,
00071               d->argnum2,
00072               d->type.iface // this is actually a union!
00073               );
00074 }
00075 
00076 PyObject *PyObject_FromXPTParamDescriptor( const XPTParamDescriptor *d)
00077 {
00078        if (d==nsnull) {
00079               Py_INCREF(Py_None);
00080               return Py_None;
00081        }
00082        PyObject *ob = PyObject_FromXPTTypeDescriptor(&d->type);
00083        PyObject *ret = Py_BuildValue("bO", d->flags, ob);
00084        Py_DECREF(ob);
00085        return ret;
00086 }
00087 
00088 PyObject *PyObject_FromXPTMethodDescriptor( const XPTMethodDescriptor *d)
00089 {
00090        if (d==nsnull) {
00091               Py_INCREF(Py_None);
00092               return Py_None;
00093        }
00094        PyObject *ob_params = PyTuple_New(d->num_args);
00095        if (ob_params==NULL)
00096               return NULL;
00097        for (int i=0;i<d->num_args;i++)
00098               PyTuple_SET_ITEM(ob_params, i, PyObject_FromXPTParamDescriptor(d->params+i));
00099        PyObject *ob_ret = PyObject_FromXPTParamDescriptor(d->result);
00100        PyObject *ret = Py_BuildValue("bsOO", d->flags, d->name, ob_params, ob_ret);
00101        Py_XDECREF(ob_ret);
00102        Py_XDECREF(ob_params);
00103        return ret;
00104 }
00105 
00106 PyObject *PyObject_FromXPTConstant( const XPTConstDescriptor *c)
00107 {
00108        if (c==nsnull) {
00109               Py_INCREF(Py_None);
00110               return Py_None;
00111        }
00112        PyObject *ob_type = PyObject_FromXPTTypeDescriptor(&c->type);
00113        if (ob_type==NULL)
00114               return NULL;
00115        PyObject *v = NULL;
00116        switch (c->type.prefix.flags) {
00117               case TD_INT8:
00118                      v = PyInt_FromLong( c->value.i8 );
00119                      break;
00120               case TD_INT16:
00121                      v = PyInt_FromLong( c->value.i16 );
00122                      break;
00123               case TD_INT32:
00124                      v = PyInt_FromLong( c->value.i32 );
00125                      break;
00126               case TD_INT64:
00127                      v = PyLong_FromLongLong(c->value.i64);
00128                      break;
00129               case TD_UINT8:
00130                      v = PyInt_FromLong( c->value.ui8 );
00131                      break;
00132               case TD_UINT16:
00133                      v = PyInt_FromLong( c->value.ui16 );
00134                      break;
00135               case TD_UINT32:
00136                      v = PyInt_FromLong( c->value.ui32 );
00137                      break;
00138               case TD_UINT64:
00139                      v = PyLong_FromUnsignedLongLong(c->value.ui64);
00140                      break;
00141               case TD_FLOAT:
00142                      v = PyFloat_FromDouble(c->value.flt);
00143                      break;
00144               case TD_DOUBLE:
00145                      v = PyFloat_FromDouble(c->value.dbl);
00146                      break;
00147               case TD_BOOL:
00148                      v = c->value.bul ? Py_True : Py_False;
00149                      Py_INCREF(v);
00150                      break;
00151               case TD_CHAR:
00152                      v = PyString_FromStringAndSize(&c->value.ch, 1);
00153                      break;
00154               case TD_WCHAR:
00155                      v = PyUnicode_FromPRUnichar((PRUnichar *)&c->value.wch, 1);
00156                      break;
00157        //    TD_VOID              = 13,  
00158               case TD_PNSIID:
00159                      v = Py_nsIID::PyObjectFromIID(*c->value.iid);
00160                      break;
00161        //    TD_DOMSTRING         = 15,
00162               case TD_PSTRING:
00163                      v = PyString_FromString(c->value.str);
00164                      break;
00165               case TD_PWSTRING:
00166                      v = PyUnicode_FromPRUnichar((PRUnichar *)c->value.wstr, nsCRT::strlen((PRUnichar *)c->value.wstr));
00167                      break;
00168        //    TD_INTERFACE_TYPE    = 18,
00169        //    TD_INTERFACE_IS_TYPE = 19,
00170        //    TD_ARRAY             = 20,
00171        //    TD_PSTRING_SIZE_IS   = 21,
00172        //    TD_PWSTRING_SIZE_IS  = 22
00173        //    TD_UTF8STRING        = 23,
00174        //    TD_CSTRING           = 24,
00175        //    TD_ASTRING           = 25
00176               default:
00177                      v = PyString_FromString("Unknown type code!!");
00178                      break;
00179 
00180        }
00181        PyObject *ret = Py_BuildValue("sbO", c->name, ob_type, v);
00182        Py_DECREF(ob_type);
00183        Py_DECREF(v);
00184        return ret;
00185 }