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 | * Mark Hammond.
|
---|
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 <mhammond@skippinet.com.au> (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 | // This code is part of the XPCOM extensions for Python.
|
---|
39 | //
|
---|
40 | // Written April 2002
|
---|
41 |
|
---|
42 | #include "PyXPCOM_std.h"
|
---|
43 | #include "nsIVariant.h"
|
---|
44 |
|
---|
45 | // Prevents us needing to use an nsIScriptableInputStream
|
---|
46 | // (and even that can't read binary data!!!)
|
---|
47 |
|
---|
48 | static nsIVariant *GetI(PyObject *self) {
|
---|
49 | nsIID iid = NS_GET_IID(nsIVariant);
|
---|
50 |
|
---|
51 | if (!Py_nsISupports::Check(self, iid)) {
|
---|
52 | PyErr_SetString(PyExc_TypeError, "This object is not the correct interface");
|
---|
53 | return NULL;
|
---|
54 | }
|
---|
55 | return (nsIVariant *)Py_nsISupports::GetI(self);
|
---|
56 | }
|
---|
57 |
|
---|
58 | static PyObject *MyBool( PRBool v) {
|
---|
59 | PyObject *ret = v ? Py_True : Py_False;
|
---|
60 | Py_INCREF(ret);
|
---|
61 | return ret;
|
---|
62 | }
|
---|
63 | static PyObject *MyChar( char c) {
|
---|
64 | #if PY_MAJOR_VERSION <= 2
|
---|
65 | return PyString_FromStringAndSize(&c, 1);
|
---|
66 | #else
|
---|
67 | return PyUnicode_FromStringAndSize(&c, 1);
|
---|
68 | #endif
|
---|
69 | }
|
---|
70 | static PyObject *MyUChar( PRUnichar c) {
|
---|
71 | return PyObject_FromNSString( &c, 1);
|
---|
72 | }
|
---|
73 | static PyObject *MyUnicode( PRUnichar *p) {
|
---|
74 | return PyObject_FromNSString(p);
|
---|
75 | }
|
---|
76 |
|
---|
77 | #define GET_SIMPLE(Type, FuncGet, FuncConvert) \
|
---|
78 | static PyObject *FuncGet(PyObject *self, PyObject *args) { \
|
---|
79 | nsIVariant *pI = GetI(self); \
|
---|
80 | if (pI==NULL) return NULL; \
|
---|
81 | if (!PyArg_ParseTuple(args, ":" #FuncGet)) return NULL; \
|
---|
82 | Type t; \
|
---|
83 | nsresult nr = pI->FuncGet(&t); \
|
---|
84 | if (NS_FAILED(nr)) return PyXPCOM_BuildPyException(nr); \
|
---|
85 | return FuncConvert(t); \
|
---|
86 | }
|
---|
87 |
|
---|
88 | #define GET_ALLOCATED(Type, FuncGet, FuncConvert, FuncFree) \
|
---|
89 | static PyObject *FuncGet(PyObject *self, PyObject *args) { \
|
---|
90 | nsIVariant *pI = GetI(self); \
|
---|
91 | if (pI==NULL) return NULL; \
|
---|
92 | if (!PyArg_ParseTuple(args, ":" #FuncGet)) return NULL; \
|
---|
93 | Type t; \
|
---|
94 | nsresult nr = pI->FuncGet(&t); \
|
---|
95 | if (NS_FAILED(nr)) return PyXPCOM_BuildPyException(nr); \
|
---|
96 | PyObject *ret = FuncConvert(t); \
|
---|
97 | FuncFree(t); \
|
---|
98 | return ret; \
|
---|
99 | }
|
---|
100 |
|
---|
101 | #define GET_ALLOCATED_SIZE(Type, FuncGet, FuncConvert, FuncFree) \
|
---|
102 | static PyObject *FuncGet(PyObject *self, PyObject *args) { \
|
---|
103 | nsIVariant *pI = GetI(self); \
|
---|
104 | if (pI==NULL) return NULL; \
|
---|
105 | if (!PyArg_ParseTuple(args, ":" #FuncGet)) return NULL; \
|
---|
106 | Type t; PRUint32 size; \
|
---|
107 | nsresult nr = pI->FuncGet(&size, &t); \
|
---|
108 | if (NS_FAILED(nr)) return PyXPCOM_BuildPyException(nr); \
|
---|
109 | PyObject *ret = FuncConvert(t, size); \
|
---|
110 | FuncFree(t); \
|
---|
111 | return ret; \
|
---|
112 | }
|
---|
113 |
|
---|
114 | GET_SIMPLE(PRUint8, GetAsInt8, PyInt_FromLong)
|
---|
115 | GET_SIMPLE(PRUint8, GetAsUint8, PyInt_FromLong)
|
---|
116 | GET_SIMPLE(PRInt16, GetAsInt16, PyInt_FromLong)
|
---|
117 | GET_SIMPLE(PRUint16, GetAsUint16, PyInt_FromLong)
|
---|
118 | GET_SIMPLE(PRInt32, GetAsInt32, PyInt_FromLong)
|
---|
119 | GET_SIMPLE(PRUint32, GetAsUint32, PyInt_FromLong)
|
---|
120 | GET_SIMPLE(PRInt64, GetAsInt64, PyLong_FromLongLong)
|
---|
121 | GET_SIMPLE(PRUint64, GetAsUint64, PyLong_FromUnsignedLongLong)
|
---|
122 | GET_SIMPLE(float, GetAsFloat, PyFloat_FromDouble)
|
---|
123 | GET_SIMPLE(double, GetAsDouble, PyFloat_FromDouble)
|
---|
124 | GET_SIMPLE(PRBool, GetAsBool, MyBool)
|
---|
125 | GET_SIMPLE(char, GetAsChar, MyChar)
|
---|
126 | GET_SIMPLE(PRUnichar, GetAsWChar, MyUChar)
|
---|
127 | GET_SIMPLE(nsIID, GetAsID, Py_nsIID::PyObjectFromIID)
|
---|
128 |
|
---|
129 | #if PY_MAJOR_VERSION <= 2
|
---|
130 | GET_ALLOCATED(char *, GetAsString, PyString_FromString, nsMemory::Free)
|
---|
131 | #else
|
---|
132 | GET_ALLOCATED(char *, GetAsString, PyUnicode_FromString, nsMemory::Free)
|
---|
133 | #endif
|
---|
134 | GET_ALLOCATED(PRUnichar *, GetAsWString, MyUnicode, nsMemory::Free)
|
---|
135 | #if PY_MAJOR_VERSION <= 2
|
---|
136 | GET_ALLOCATED_SIZE(char *, GetAsStringWithSize, PyString_FromStringAndSize, nsMemory::Free)
|
---|
137 | #else
|
---|
138 | GET_ALLOCATED_SIZE(char *, GetAsStringWithSize, PyUnicode_FromStringAndSize, nsMemory::Free)
|
---|
139 | #endif
|
---|
140 | GET_ALLOCATED_SIZE(PRUnichar *, GetAsWStringWithSize, PyObject_FromNSString, nsMemory::Free)
|
---|
141 |
|
---|
142 | static PyObject *GetAsInterface(PyObject *self, PyObject *args) {
|
---|
143 | nsIVariant *pI = GetI(self);
|
---|
144 | if (pI==NULL) return NULL;
|
---|
145 | if (!PyArg_ParseTuple(args, ":GetAsInterface")) return NULL;
|
---|
146 | nsCOMPtr<nsISupports> p;
|
---|
147 | nsIID *iid;
|
---|
148 | nsresult nr = pI->GetAsInterface(&iid, getter_AddRefs(p));
|
---|
149 | if (NS_FAILED(nr)) return PyXPCOM_BuildPyException(nr);
|
---|
150 | return Py_nsISupports::PyObjectFromInterface(p, *iid);
|
---|
151 | }
|
---|
152 |
|
---|
153 | static PyObject *GetAsISupports(PyObject *self, PyObject *args) {
|
---|
154 | nsIVariant *pI = GetI(self);
|
---|
155 | if (pI==NULL) return NULL;
|
---|
156 | if (!PyArg_ParseTuple(args, ":GetAsInterface")) return NULL;
|
---|
157 | nsCOMPtr<nsISupports> p;
|
---|
158 | nsIID *iid;
|
---|
159 | nsresult nr = pI->GetAsInterface(&iid, getter_AddRefs(p));
|
---|
160 | if (NS_FAILED(nr)) return PyXPCOM_BuildPyException(nr);
|
---|
161 | return Py_nsISupports::PyObjectFromInterface(p, *iid);
|
---|
162 | }
|
---|
163 |
|
---|
164 | static PyObject *GetAsArray(PyObject *self, PyObject *args) {
|
---|
165 | nsIVariant *pI = GetI(self);
|
---|
166 | if (pI==NULL) return NULL;
|
---|
167 | if (!PyArg_ParseTuple(args, ":GetAsArray")) return NULL;
|
---|
168 | return PyObject_FromVariantArray((Py_nsISupports *)self, pI);
|
---|
169 | }
|
---|
170 |
|
---|
171 | static PyObject *Get(PyObject *self, PyObject *args) {
|
---|
172 | nsIVariant *pI = GetI(self);
|
---|
173 | if (pI==NULL) return NULL;
|
---|
174 | if (!PyArg_ParseTuple(args, ":Get")) return NULL;
|
---|
175 | return PyObject_FromVariant((Py_nsISupports *)self, pI);
|
---|
176 | }
|
---|
177 |
|
---|
178 | struct PyMethodDef
|
---|
179 | PyMethods_IVariant[] =
|
---|
180 | {
|
---|
181 | { "getAsInt8", GetAsInt8, 1},
|
---|
182 | { "getAsUint8", GetAsUint8, 1},
|
---|
183 | { "getAsInt16", GetAsInt16, 1},
|
---|
184 | { "getAsUint16", GetAsUint16, 1},
|
---|
185 | { "getAsInt32", GetAsInt32, 1},
|
---|
186 | { "getAsUint32", GetAsUint32, 1},
|
---|
187 | { "getAsInt64", GetAsInt64, 1},
|
---|
188 | { "getAsUint64", GetAsUint64, 1},
|
---|
189 | { "getAsFloat", GetAsFloat, 1},
|
---|
190 | { "getAsDouble", GetAsDouble, 1},
|
---|
191 | { "getAsBool", GetAsBool, 1},
|
---|
192 | { "getAsChar", GetAsChar, 1},
|
---|
193 | { "getAsWChar", GetAsWChar, 1},
|
---|
194 | { "getAsString", GetAsString, 1},
|
---|
195 | { "getAsWString", GetAsWString, 1},
|
---|
196 | { "getAsStringWithSize", GetAsStringWithSize, 1},
|
---|
197 | { "getAsWStringWithSize", GetAsWStringWithSize, 1},
|
---|
198 | { "getAsISupports", GetAsISupports, 1},
|
---|
199 | { "getAsInterface", GetAsInterface, 1},
|
---|
200 | { "getAsArray", GetAsArray, 1},
|
---|
201 | { "getAsID", GetAsID, 1},
|
---|
202 | { "get", Get, 1},
|
---|
203 | {NULL}
|
---|
204 | };
|
---|
205 |
|
---|
206 | PyObject *
|
---|
207 | Py_nsIVariant::getattr(const char *name)
|
---|
208 | {
|
---|
209 |
|
---|
210 | PyObject *ret = NULL;
|
---|
211 | if (strcmp(name, "dataType")==0) {
|
---|
212 | nsIVariant *pI = ::GetI(this);
|
---|
213 | if (pI) {
|
---|
214 | PRUint16 dt;
|
---|
215 | nsresult nr = pI->GetDataType(&dt);
|
---|
216 | if (NS_FAILED(nr)) return PyXPCOM_BuildPyException(nr);
|
---|
217 | ret = PyInt_FromLong(dt);
|
---|
218 | }
|
---|
219 | } else {
|
---|
220 | ret = Py_nsISupports::getattr(name);
|
---|
221 | }
|
---|
222 | return ret;
|
---|
223 | }
|
---|
224 |
|
---|
225 | int
|
---|
226 | Py_nsIVariant::setattr(const char *name, PyObject *v)
|
---|
227 | {
|
---|
228 | return Py_nsISupports::setattr(name, v);
|
---|
229 | }
|
---|