Back to index

lightning-sunbird  0.9+nobinonly
PyIVariant.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  * Mark Hammond.
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 <mhammond@skippinet.com.au> (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 // This code is part of the XPCOM extensions for Python.
00039 //
00040 // Written April 2002
00041 
00042 #include "PyXPCOM_std.h"
00043 #include "nsIVariant.h"
00044 
00045 // Prevents us needing to use an nsIScriptableInputStream
00046 // (and even that can't read binary data!!!)
00047 
00048 static nsIVariant *GetI(PyObject *self) {
00049        nsIID iid = NS_GET_IID(nsIVariant);
00050 
00051        if (!Py_nsISupports::Check(self, iid)) {
00052               PyErr_SetString(PyExc_TypeError, "This object is not the correct interface");
00053               return NULL;
00054        }
00055        return (nsIVariant *)Py_nsISupports::GetI(self);
00056 }
00057 
00058 static PyObject *MyBool( PRBool v) {
00059        PyObject *ret = v ? Py_True : Py_False;
00060        Py_INCREF(ret);
00061        return ret;
00062 }
00063 static PyObject *MyChar( char c) {
00064        return PyString_FromStringAndSize(&c, 1);
00065 }
00066 static PyObject *MyUChar( PRUnichar c) {
00067        return PyUnicode_FromPRUnichar(&c, 1);
00068 }
00069 static PyObject *MyUnicode( PRUnichar *p) {
00070        return PyUnicode_FromPRUnichar(p, nsCRT::strlen(p));
00071 }
00072 static PyObject *MyISupports( nsISupports *p) {
00073        return Py_nsISupports::PyObjectFromInterface(p, NS_GET_IID(nsISupports), PR_FALSE);
00074 }
00075 
00076 #define GET_SIMPLE(Type, FuncGet, FuncConvert) \
00077 static PyObject *FuncGet(PyObject *self, PyObject *args) { \
00078        nsIVariant *pI = GetI(self); \
00079        if (pI==NULL) return NULL; \
00080        if (!PyArg_ParseTuple(args, ":" #FuncGet)) return NULL; \
00081        Type t; \
00082        nsresult nr = pI->FuncGet(&t); \
00083        if (NS_FAILED(nr)) return PyXPCOM_BuildPyException(nr); \
00084        return FuncConvert(t); \
00085 }
00086 
00087 #define GET_ALLOCATED(Type, FuncGet, FuncConvert, FuncFree) \
00088 static PyObject *FuncGet(PyObject *self, PyObject *args) { \
00089        nsIVariant *pI = GetI(self); \
00090        if (pI==NULL) return NULL; \
00091        if (!PyArg_ParseTuple(args, ":" #FuncGet)) return NULL; \
00092        Type t; \
00093        nsresult nr = pI->FuncGet(&t); \
00094        if (NS_FAILED(nr)) return PyXPCOM_BuildPyException(nr); \
00095        PyObject *ret = FuncConvert(t); \
00096        FuncFree(t); \
00097        return ret; \
00098 }
00099 
00100 #define GET_ALLOCATED_SIZE(Type, FuncGet, FuncConvert, FuncFree) \
00101 static PyObject *FuncGet(PyObject *self, PyObject *args) { \
00102        nsIVariant *pI = GetI(self); \
00103        if (pI==NULL) return NULL; \
00104        if (!PyArg_ParseTuple(args, ":" #FuncGet)) return NULL; \
00105        Type t; PRUint32 size; \
00106        nsresult nr = pI->FuncGet(&size, &t); \
00107        if (NS_FAILED(nr)) return PyXPCOM_BuildPyException(nr); \
00108        PyObject *ret = FuncConvert(t, size); \
00109        FuncFree(t); \
00110        return ret; \
00111 }
00112 
00113 GET_SIMPLE(PRUint8, GetAsInt8, PyInt_FromLong)
00114 GET_SIMPLE(PRUint8, GetAsUint8, PyInt_FromLong)
00115 GET_SIMPLE(PRInt16, GetAsInt16, PyInt_FromLong)
00116 GET_SIMPLE(PRUint16, GetAsUint16, PyInt_FromLong)
00117 GET_SIMPLE(PRInt32, GetAsInt32, PyInt_FromLong)
00118 GET_SIMPLE(PRUint32, GetAsUint32, PyInt_FromLong)
00119 GET_SIMPLE(PRInt64, GetAsInt64, PyLong_FromLongLong)
00120 GET_SIMPLE(PRUint64, GetAsUint64, PyLong_FromUnsignedLongLong)
00121 GET_SIMPLE(float, GetAsFloat, PyFloat_FromDouble)
00122 GET_SIMPLE(double, GetAsDouble, PyFloat_FromDouble)
00123 GET_SIMPLE(PRBool, GetAsBool, MyBool)
00124 GET_SIMPLE(char, GetAsChar, MyChar)
00125 GET_SIMPLE(PRUnichar, GetAsWChar, MyUChar)
00126 GET_SIMPLE(nsISupports *, GetAsISupports, MyISupports)
00127 GET_SIMPLE(nsIID, GetAsID, Py_nsIID::PyObjectFromIID)
00128 
00129 GET_ALLOCATED(char *, GetAsString, PyString_FromString, nsMemory::Free)
00130 GET_ALLOCATED(PRUnichar *, GetAsWString, MyUnicode, nsMemory::Free)
00131 GET_ALLOCATED_SIZE(char *, GetAsStringWithSize, PyString_FromStringAndSize, nsMemory::Free)
00132 GET_ALLOCATED_SIZE(PRUnichar *, GetAsWStringWithSize, PyUnicode_FromPRUnichar, nsMemory::Free)
00133 
00134 static PyObject *GetAsInterface(PyObject *self, PyObject *args) {
00135        nsIVariant *pI = GetI(self);
00136        if (pI==NULL) return NULL;
00137        if (!PyArg_ParseTuple(args, ":GetAsInterface")) return NULL;
00138        nsISupports *p;
00139        nsIID *iid;
00140        nsresult nr = pI->GetAsInterface(&iid, (void **)&p);
00141        if (NS_FAILED(nr)) return PyXPCOM_BuildPyException(nr);
00142        return Py_nsISupports::PyObjectFromInterface(p, *iid, PR_FALSE);
00143 }
00144 
00145 extern PyObject *PyObject_FromVariantArray( nsIVariant *v);
00146 
00147 static PyObject *GetAsArray(PyObject *self, PyObject *args) {
00148        nsIVariant *pI = GetI(self);
00149        if (pI==NULL) return NULL;
00150        if (!PyArg_ParseTuple(args, ":GetAsArray")) return NULL;
00151        return PyObject_FromVariantArray(pI);
00152 }
00153 
00154 struct PyMethodDef 
00155 PyMethods_IVariant[] =
00156 {
00157        { "getAsInt8", GetAsInt8, 1},
00158        { "getAsUint8", GetAsUint8, 1},
00159        { "getAsInt16", GetAsInt16, 1},
00160        { "getAsUint16", GetAsUint16, 1},
00161        { "getAsInt32", GetAsInt32, 1},
00162        { "getAsUint32", GetAsUint32, 1},
00163        { "getAsInt64", GetAsInt64, 1},
00164        { "getAsUint64", GetAsUint64, 1},
00165        { "getAsFloat", GetAsFloat, 1},
00166        { "getAsDouble", GetAsDouble, 1},
00167        { "getAsBool", GetAsBool, 1},
00168        { "getAsChar", GetAsChar, 1},
00169        { "getAsWChar", GetAsWChar, 1},
00170        { "getAsString", GetAsString, 1},
00171        { "getAsWString", GetAsWString, 1},
00172        { "getAsStringWithSize", GetAsStringWithSize, 1},
00173        { "getAsWStringWithSize", GetAsWStringWithSize, 1},
00174        { "getAsISupports", GetAsISupports, 1},
00175        { "getAsInterface", GetAsInterface, 1},
00176        { "getAsArray", GetAsArray, 1},
00177        { "getAsID", GetAsID, 1},
00178        {NULL}
00179 };
00180 
00181 PyObject *
00182 Py_nsIVariant::getattr(const char *name)
00183 {
00184 
00185        PyObject *ret = NULL;
00186        if (strcmp(name, "dataType")==0) {
00187         nsIVariant *pI = ::GetI(this);
00188         if (pI) {
00189             PRUint16 dt;
00190             nsresult nr = pI->GetDataType(&dt);
00191             if (NS_FAILED(nr)) return PyXPCOM_BuildPyException(nr);
00192             ret = PyInt_FromLong(dt);
00193         }
00194        } else {
00195               ret = Py_nsISupports::getattr(name);
00196        }
00197        return ret;
00198 }
00199 
00200 int
00201 Py_nsIVariant::setattr(const char *name, PyObject *v)
00202 {
00203        return Py_nsISupports::setattr(name, v);
00204 }