Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : : #include "pyuno_impl.hxx"
20 : :
21 : : #include <rtl/ustrbuf.hxx>
22 : : #include <rtl/strbuf.hxx>
23 : :
24 : : #include <typelib/typedescription.hxx>
25 : :
26 : : using rtl::OString;
27 : : using rtl::OUString;
28 : : using rtl::OUStringBuffer;
29 : : using rtl::OUStringToOString;
30 : : using rtl::OStringBuffer;
31 : :
32 : : using com::sun::star::uno::TypeClass;
33 : : using com::sun::star::uno::Type;
34 : : using com::sun::star::uno::RuntimeException;
35 : : using com::sun::star::uno::Any;
36 : : using com::sun::star::uno::XInterface;
37 : : using com::sun::star::uno::Reference;
38 : : using com::sun::star::uno::TypeDescription;
39 : :
40 : : #define USTR_ASCII(x) rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
41 : : namespace pyuno
42 : : {
43 : 786 : const char *typeClassToString( TypeClass t )
44 : : {
45 : 786 : const char * ret = 0;
46 [ - - - - : 786 : switch (t)
- - - - -
- - - - -
- - - - -
+ - - - -
- - - - ]
47 : : {
48 : : case com::sun::star::uno::TypeClass_VOID:
49 : 0 : ret = "VOID"; break;
50 : : case com::sun::star::uno::TypeClass_CHAR:
51 : 0 : ret = "CHAR"; break;
52 : : case com::sun::star::uno::TypeClass_BOOLEAN:
53 : 0 : ret = "BOOLEAN"; break;
54 : : case com::sun::star::uno::TypeClass_BYTE:
55 : 0 : ret = "BYTE"; break;
56 : : case com::sun::star::uno::TypeClass_SHORT:
57 : 0 : ret = "SHORT"; break;
58 : : case com::sun::star::uno::TypeClass_UNSIGNED_SHORT:
59 : 0 : ret = "UNSIGNED_SHORT"; break;
60 : : case com::sun::star::uno::TypeClass_LONG:
61 : 0 : ret = "LONG"; break;
62 : : case com::sun::star::uno::TypeClass_UNSIGNED_LONG:
63 : 0 : ret = "UNSIGNED_LONG"; break;
64 : : case com::sun::star::uno::TypeClass_HYPER:
65 : 0 : ret = "HYPER"; break;
66 : : case com::sun::star::uno::TypeClass_UNSIGNED_HYPER:
67 : 0 : ret = "UNSIGNED_HYPER"; break;
68 : : case com::sun::star::uno::TypeClass_FLOAT:
69 : 0 : ret = "FLOAT"; break;
70 : : case com::sun::star::uno::TypeClass_DOUBLE:
71 : 0 : ret = "DOUBLE"; break;
72 : : case com::sun::star::uno::TypeClass_STRING:
73 : 0 : ret = "STRING"; break;
74 : : case com::sun::star::uno::TypeClass_TYPE:
75 : 0 : ret = "TYPE"; break;
76 : : case com::sun::star::uno::TypeClass_ANY:
77 : 0 : ret = "ANY";break;
78 : : case com::sun::star::uno::TypeClass_ENUM:
79 : 0 : ret = "ENUM";break;
80 : : case com::sun::star::uno::TypeClass_STRUCT:
81 : 0 : ret = "STRUCT"; break;
82 : : case com::sun::star::uno::TypeClass_EXCEPTION:
83 : 0 : ret = "EXCEPTION"; break;
84 : : case com::sun::star::uno::TypeClass_SEQUENCE:
85 : 0 : ret = "SEQUENCE"; break;
86 : : case com::sun::star::uno::TypeClass_INTERFACE:
87 : 786 : ret = "INTERFACE"; break;
88 : : case com::sun::star::uno::TypeClass_TYPEDEF:
89 : 0 : ret = "TYPEDEF"; break;
90 : : case com::sun::star::uno::TypeClass_UNION:
91 : 0 : ret = "UNION"; break;
92 : : case com::sun::star::uno::TypeClass_ARRAY:
93 : 0 : ret = "ARRAY"; break;
94 : : case com::sun::star::uno::TypeClass_SERVICE:
95 : 0 : ret = "SERVICE"; break;
96 : : case com::sun::star::uno::TypeClass_MODULE:
97 : 0 : ret = "MODULE"; break;
98 : : case com::sun::star::uno::TypeClass_INTERFACE_METHOD:
99 : 0 : ret = "INTERFACE_METHOD"; break;
100 : : case com::sun::star::uno::TypeClass_INTERFACE_ATTRIBUTE:
101 : 0 : ret = "INTERFACE_ATTRIBUTE"; break;
102 : : default:
103 : 0 : ret = "UNKNOWN"; break;
104 : : }
105 : 786 : return ret;
106 : : }
107 : :
108 : 64879 : static PyRef getClass( const Runtime & r , const char * name)
109 : : {
110 [ + - ]: 64879 : return PyRef( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), (char*) name ) );
111 : : }
112 : :
113 : 18949 : PyRef getTypeClass( const Runtime & r )
114 : : {
115 : 18949 : return getClass( r , "Type" );
116 : : }
117 : :
118 : 16915 : PyRef getEnumClass( const Runtime & r )
119 : : {
120 : 16915 : return getClass( r , "Enum" );
121 : : }
122 : :
123 : 4985 : PyRef getCharClass( const Runtime & r )
124 : : {
125 : 4985 : return getClass( r , "Char" );
126 : : }
127 : :
128 : 19045 : PyRef getByteSequenceClass( const Runtime & r )
129 : : {
130 : 19045 : return getClass( r , "ByteSequence" );
131 : : }
132 : :
133 : 4985 : PyRef getAnyClass( const Runtime & r )
134 : : {
135 : 4985 : return getClass( r , "Any" );
136 : : }
137 : :
138 : :
139 : 0 : sal_Unicode PyChar2Unicode( PyObject *obj ) throw ( RuntimeException )
140 : : {
141 [ # # ]: 0 : PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE );
142 [ # # ]: 0 : if( ! PyUnicode_Check( value.get() ) )
143 : : {
144 : : throw RuntimeException(
145 : : USTR_ASCII( "attribute value of uno.Char is not a unicode string" ),
146 [ # # ][ # # ]: 0 : Reference< XInterface > () );
147 : : }
148 : :
149 [ # # ][ # # ]: 0 : if( PyUnicode_GetSize( value.get() ) < 1 )
150 : : {
151 : : throw RuntimeException(
152 : : USTR_ASCII( "uno.Char contains an empty unicode string" ),
153 [ # # ][ # # ]: 0 : Reference< XInterface > () );
154 : : }
155 : :
156 [ # # ]: 0 : sal_Unicode c = (sal_Unicode)PyUnicode_AsUnicode( value.get() )[0];
157 [ # # ]: 0 : return c;
158 : : }
159 : :
160 : 5175 : Any PyEnum2Enum( PyObject *obj ) throw ( RuntimeException )
161 : : {
162 : 5175 : Any ret;
163 [ + - ]: 5175 : PyRef typeName( PyObject_GetAttrString( obj,"typeName" ), SAL_NO_ACQUIRE);
164 [ + - ]: 5175 : PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE);
165 [ - + ][ - + ]: 5175 : if( !PyString_Check( typeName.get() ) || ! PyString_Check( value.get() ) )
[ + - ]
166 : : {
167 : : throw RuntimeException(
168 : : USTR_ASCII( "attributes typeName and/or value of uno.Enum are not strings" ),
169 [ # # ][ # # ]: 0 : Reference< XInterface > () );
170 : : }
171 : :
172 [ + - ]: 5175 : OUString strTypeName( OUString::createFromAscii( PyString_AsString( typeName.get() ) ) );
173 [ + - ]: 5175 : char *stringValue = PyString_AsString( value.get() );
174 : :
175 : 5175 : TypeDescription desc( strTypeName );
176 [ + + ]: 5175 : if( desc.is() )
177 : : {
178 [ - + ]: 3942 : if(desc.get()->eTypeClass != typelib_TypeClass_ENUM )
179 : : {
180 : 0 : OUStringBuffer buf;
181 [ # # ][ # # ]: 0 : buf.appendAscii( "pyuno.checkEnum: " ).append(strTypeName).appendAscii( "is a " );
[ # # ]
182 : : buf.appendAscii(
183 [ # # ]: 0 : typeClassToString( (com::sun::star::uno::TypeClass) desc.get()->eTypeClass));
184 [ # # ]: 0 : buf.appendAscii( ", expected ENUM" );
185 [ # # ][ # # ]: 0 : throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface> () );
186 : : }
187 : :
188 : 3942 : desc.makeComplete();
189 : :
190 : 3942 : typelib_EnumTypeDescription *pEnumDesc = (typelib_EnumTypeDescription*) desc.get();
191 : 3942 : int i = 0;
192 [ + - ]: 83610 : for( i = 0; i < pEnumDesc->nEnumValues ; i ++ )
193 : : {
194 [ + + ]: 83610 : if( (*((OUString *)&pEnumDesc->ppEnumNames[i])).compareToAscii( stringValue ) == 0 )
195 : : {
196 : 3942 : break;
197 : : }
198 : : }
199 [ - + ]: 3942 : if( i == pEnumDesc->nEnumValues )
200 : : {
201 : 0 : OUStringBuffer buf;
202 [ # # ][ # # ]: 0 : buf.appendAscii( "value " ).appendAscii( stringValue ).appendAscii( "is unknown in enum " );
[ # # ]
203 [ # # ][ # # ]: 0 : buf.appendAscii( PyString_AsString( typeName.get() ) );
204 [ # # ][ # # ]: 0 : throw RuntimeException( buf.makeStringAndClear(), Reference<XInterface> () );
205 : : }
206 : 3942 : ret = Any( &pEnumDesc->pEnumValues[i], desc.get()->pWeakRef );
207 : : }
208 : : else
209 : : {
210 : 1233 : OUStringBuffer buf;
211 [ + - ][ + - ]: 1233 : buf.appendAscii( "enum " ).appendAscii( PyString_AsString(typeName.get()) ).appendAscii( " is unknown" );
[ + - ][ + - ]
212 [ + - ][ + - ]: 1233 : throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface> () );
213 : : }
214 [ + - ][ + - ]: 5175 : return ret;
215 : : }
216 : :
217 : :
218 : 2820 : Type PyType2Type( PyObject * o ) throw(RuntimeException )
219 : : {
220 [ + - ]: 2820 : PyRef pyName( PyObject_GetAttrString( o, "typeName" ), SAL_NO_ACQUIRE);
221 [ - + ]: 2820 : if( !PyString_Check( pyName.get() ) )
222 : : {
223 : : throw RuntimeException(
224 : : USTR_ASCII( "type object does not have typeName property" ),
225 [ # # ][ # # ]: 0 : Reference< XInterface > () );
226 : : }
227 : :
228 [ + - ]: 2820 : PyRef pyTC( PyObject_GetAttrString( o, "typeClass" ), SAL_NO_ACQUIRE );
229 [ + - ]: 2820 : Any enumValue = PyEnum2Enum( pyTC.get() );
230 : :
231 [ + - ]: 2820 : OUString name( OUString::createFromAscii( PyString_AsString( pyName.get() ) ) );
232 : 2820 : TypeDescription desc( name );
233 [ - + ]: 2820 : if( ! desc.is() )
234 : : {
235 : 0 : OUStringBuffer buf;
236 [ # # ][ # # ]: 0 : buf.appendAscii( "type " ).append(name).appendAscii( " is unknown" );
[ # # ]
237 : : throw RuntimeException(
238 [ # # ][ # # ]: 0 : buf.makeStringAndClear(), Reference< XInterface > () );
239 : : }
240 [ - + ]: 2820 : if( desc.get()->eTypeClass != (typelib_TypeClass) *(sal_Int32*)enumValue.getValue() )
241 : : {
242 : 0 : OUStringBuffer buf;
243 [ # # ][ # # ]: 0 : buf.appendAscii( "pyuno.checkType: " ).append(name).appendAscii( " is a " );
[ # # ]
244 [ # # ]: 0 : buf.appendAscii( typeClassToString( (TypeClass) desc.get()->eTypeClass) );
245 [ # # ]: 0 : buf.appendAscii( ", but type got construct with typeclass " );
246 [ # # ]: 0 : buf.appendAscii( typeClassToString( (TypeClass) *(sal_Int32*)enumValue.getValue() ) );
247 : : throw RuntimeException(
248 [ # # ][ # # ]: 0 : buf.makeStringAndClear(), Reference< XInterface > () );
249 : : }
250 [ + - ][ + - ]: 2820 : return desc.get()->pWeakRef;
251 : : }
252 : :
253 : 1784 : static PyObject* callCtor( const Runtime &r , const char * clazz, const PyRef & args )
254 : : {
255 [ + - ][ + - ]: 1784 : PyRef code( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), (char*)clazz ) );
[ + - ]
256 [ - + ]: 1784 : if( ! code.is() )
257 : : {
258 : 0 : OStringBuffer buf;
259 [ # # ]: 0 : buf.append( "couldn't access uno." );
260 [ # # ]: 0 : buf.append( clazz );
261 [ # # ]: 0 : PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
262 : 0 : return NULL;
263 : : }
264 [ + - ]: 1784 : PyRef instance( PyObject_CallObject( code.get(), args.get() ), SAL_NO_ACQUIRE);
265 [ + - ]: 1784 : Py_XINCREF( instance.get() );
266 [ + - ][ + - ]: 1784 : return instance.get();
267 : :
268 : : }
269 : :
270 : :
271 : 786 : PyObject *PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r )
272 : : {
273 [ + - ]: 786 : PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE );
274 [ + - ][ + - ]: 786 : PyTuple_SetItem( args.get() , 0 , PyString_FromString( enumBase ) );
275 [ + - ][ + - ]: 786 : PyTuple_SetItem( args.get() , 1 , PyString_FromString( enumValue ) );
276 : :
277 [ + - ][ + - ]: 786 : return callCtor( r, "Enum" , args );
278 : : }
279 : :
280 : :
281 : 786 : PyObject* PyUNO_Type_new (const char *typeName , TypeClass t , const Runtime &r )
282 : : {
283 : : // retrieve type object
284 [ + - ]: 786 : PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE );
285 : :
286 [ + - ][ + - ]: 786 : PyTuple_SetItem( args.get() , 0 , PyString_FromString( typeName ) );
287 [ + - ]: 786 : PyObject *typeClass = PyUNO_Enum_new( "com.sun.star.uno.TypeClass" , typeClassToString(t), r );
288 [ - + ]: 786 : if( ! typeClass )
289 : 0 : return NULL;
290 [ + - ]: 786 : PyTuple_SetItem( args.get() , 1 , typeClass);
291 : :
292 [ + - ][ + - ]: 786 : return callCtor( r, "Type" , args );
293 : : }
294 : :
295 : 0 : PyObject* PyUNO_char_new ( sal_Unicode val , const Runtime &r )
296 : : {
297 : : // retrieve type object
298 [ # # ]: 0 : PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
299 : :
300 : : Py_UNICODE u[2];
301 : 0 : u[0] = val;
302 : 0 : u[1] = 0;
303 [ # # ][ # # ]: 0 : PyTuple_SetItem( args.get() , 0 , PyUnicode_FromUnicode( u ,1) );
304 : :
305 [ # # ][ # # ]: 0 : return callCtor( r, "Char" , args );
306 : : }
307 : :
308 : 212 : PyObject *PyUNO_ByteSequence_new(
309 : : const com::sun::star::uno::Sequence< sal_Int8 > &byteSequence, const Runtime &r )
310 : : {
311 : : PyRef str(
312 : 212 : PyString_FromStringAndSize( (char*)byteSequence.getConstArray(), byteSequence.getLength()),
313 [ + - ]: 212 : SAL_NO_ACQUIRE );
314 [ + - ]: 212 : PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
315 [ + - ]: 212 : PyTuple_SetItem( args.get() , 0 , str.getAcquired() );
316 [ + - ][ + - ]: 212 : return callCtor( r, "ByteSequence" , args );
[ + - ]
317 : :
318 : : }
319 : : }
320 : :
321 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|