Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / pyuno / source / module / pyuno_type.cxx
blobc23d1e7c29b5bc5b4aab0f1a7b06a4f1e1f6a82e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include "pyuno_impl.hxx"
21 #include <rtl/ustrbuf.hxx>
22 #include <rtl/strbuf.hxx>
24 #include <typelib/typedescription.hxx>
27 using com::sun::star::uno::TypeClass;
28 using com::sun::star::uno::Type;
29 using com::sun::star::uno::RuntimeException;
30 using com::sun::star::uno::Any;
31 using com::sun::star::uno::XInterface;
32 using com::sun::star::uno::Reference;
33 using com::sun::star::uno::TypeDescription;
35 namespace pyuno
37 const char *typeClassToString( TypeClass t )
39 const char * ret = 0;
40 switch (t)
42 case com::sun::star::uno::TypeClass_VOID:
43 ret = "VOID"; break;
44 case com::sun::star::uno::TypeClass_CHAR:
45 ret = "CHAR"; break;
46 case com::sun::star::uno::TypeClass_BOOLEAN:
47 ret = "BOOLEAN"; break;
48 case com::sun::star::uno::TypeClass_BYTE:
49 ret = "BYTE"; break;
50 case com::sun::star::uno::TypeClass_SHORT:
51 ret = "SHORT"; break;
52 case com::sun::star::uno::TypeClass_UNSIGNED_SHORT:
53 ret = "UNSIGNED_SHORT"; break;
54 case com::sun::star::uno::TypeClass_LONG:
55 ret = "LONG"; break;
56 case com::sun::star::uno::TypeClass_UNSIGNED_LONG:
57 ret = "UNSIGNED_LONG"; break;
58 case com::sun::star::uno::TypeClass_HYPER:
59 ret = "HYPER"; break;
60 case com::sun::star::uno::TypeClass_UNSIGNED_HYPER:
61 ret = "UNSIGNED_HYPER"; break;
62 case com::sun::star::uno::TypeClass_FLOAT:
63 ret = "FLOAT"; break;
64 case com::sun::star::uno::TypeClass_DOUBLE:
65 ret = "DOUBLE"; break;
66 case com::sun::star::uno::TypeClass_STRING:
67 ret = "STRING"; break;
68 case com::sun::star::uno::TypeClass_TYPE:
69 ret = "TYPE"; break;
70 case com::sun::star::uno::TypeClass_ANY:
71 ret = "ANY";break;
72 case com::sun::star::uno::TypeClass_ENUM:
73 ret = "ENUM";break;
74 case com::sun::star::uno::TypeClass_STRUCT:
75 ret = "STRUCT"; break;
76 case com::sun::star::uno::TypeClass_EXCEPTION:
77 ret = "EXCEPTION"; break;
78 case com::sun::star::uno::TypeClass_SEQUENCE:
79 ret = "SEQUENCE"; break;
80 case com::sun::star::uno::TypeClass_INTERFACE:
81 ret = "INTERFACE"; break;
82 case com::sun::star::uno::TypeClass_TYPEDEF:
83 ret = "TYPEDEF"; break;
84 case com::sun::star::uno::TypeClass_UNION:
85 ret = "UNION"; break;
86 case com::sun::star::uno::TypeClass_ARRAY:
87 ret = "ARRAY"; break;
88 case com::sun::star::uno::TypeClass_SERVICE:
89 ret = "SERVICE"; break;
90 case com::sun::star::uno::TypeClass_MODULE:
91 ret = "MODULE"; break;
92 case com::sun::star::uno::TypeClass_INTERFACE_METHOD:
93 ret = "INTERFACE_METHOD"; break;
94 case com::sun::star::uno::TypeClass_INTERFACE_ATTRIBUTE:
95 ret = "INTERFACE_ATTRIBUTE"; break;
96 default:
97 ret = "UNKNOWN"; break;
99 return ret;
102 static PyRef getClass( const Runtime & r , const char * name)
104 return PyRef( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), (char*) name ) );
107 PyRef getTypeClass( const Runtime & r )
109 return getClass( r , "Type" );
112 PyRef getEnumClass( const Runtime & r )
114 return getClass( r , "Enum" );
117 PyRef getCharClass( const Runtime & r )
119 return getClass( r , "Char" );
122 PyRef getByteSequenceClass( const Runtime & r )
124 return getClass( r , "ByteSequence" );
127 PyRef getAnyClass( const Runtime & r )
129 return getClass( r , "Any" );
133 sal_Unicode PyChar2Unicode( PyObject *obj ) throw ( RuntimeException )
135 PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE );
136 if( ! PyUnicode_Check( value.get() ) )
138 throw RuntimeException(
139 "attribute value of uno.Char is not a unicode string",
140 Reference< XInterface > () );
143 if( PyUnicode_GetSize( value.get() ) < 1 )
145 throw RuntimeException(
146 "uno.Char contains an empty unicode string",
147 Reference< XInterface > () );
150 sal_Unicode c = (sal_Unicode)PyUnicode_AsUnicode( value.get() )[0];
151 return c;
154 Any PyEnum2Enum( PyObject *obj ) throw ( RuntimeException )
156 Any ret;
157 PyRef typeName( PyObject_GetAttrString( obj,"typeName" ), SAL_NO_ACQUIRE);
158 PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE);
159 if( !PyStr_Check( typeName.get() ) || ! PyStr_Check( value.get() ) )
161 throw RuntimeException(
162 "attributes typeName and/or value of uno.Enum are not strings",
163 Reference< XInterface > () );
166 OUString strTypeName( OUString::createFromAscii( PyStr_AsString( typeName.get() ) ) );
167 char *stringValue = PyStr_AsString( value.get() );
169 TypeDescription desc( strTypeName );
170 if( desc.is() )
172 if(desc.get()->eTypeClass != typelib_TypeClass_ENUM )
174 OUStringBuffer buf;
175 buf.appendAscii( "pyuno.checkEnum: " ).append(strTypeName).appendAscii( "is a " );
176 buf.appendAscii(
177 typeClassToString( (com::sun::star::uno::TypeClass) desc.get()->eTypeClass));
178 buf.appendAscii( ", expected ENUM" );
179 throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface> () );
182 desc.makeComplete();
184 typelib_EnumTypeDescription *pEnumDesc = (typelib_EnumTypeDescription*) desc.get();
185 int i = 0;
186 for( i = 0; i < pEnumDesc->nEnumValues ; i ++ )
188 if( (*((OUString *)&pEnumDesc->ppEnumNames[i])).equalsAscii( stringValue ) )
190 break;
193 if( i == pEnumDesc->nEnumValues )
195 OUStringBuffer buf;
196 buf.appendAscii( "value " ).appendAscii( stringValue ).appendAscii( "is unknown in enum " );
197 buf.appendAscii( PyStr_AsString( typeName.get() ) );
198 throw RuntimeException( buf.makeStringAndClear(), Reference<XInterface> () );
200 ret = Any( &pEnumDesc->pEnumValues[i], desc.get()->pWeakRef );
202 else
204 OUStringBuffer buf;
205 buf.appendAscii( "enum " ).appendAscii( PyStr_AsString(typeName.get()) ).appendAscii( " is unknown" );
206 throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface> () );
208 return ret;
212 Type PyType2Type( PyObject * o ) throw(RuntimeException )
214 PyRef pyName( PyObject_GetAttrString( o, "typeName" ), SAL_NO_ACQUIRE);
215 if( !PyStr_Check( pyName.get() ) )
217 throw RuntimeException(
218 "type object does not have typeName property",
219 Reference< XInterface > () );
222 PyRef pyTC( PyObject_GetAttrString( o, "typeClass" ), SAL_NO_ACQUIRE );
223 Any enumValue = PyEnum2Enum( pyTC.get() );
225 OUString name( OUString::createFromAscii( PyStr_AsString( pyName.get() ) ) );
226 TypeDescription desc( name );
227 if( ! desc.is() )
229 OUStringBuffer buf;
230 buf.appendAscii( "type " ).append(name).appendAscii( " is unknown" );
231 throw RuntimeException(
232 buf.makeStringAndClear(), Reference< XInterface > () );
234 if( desc.get()->eTypeClass != (typelib_TypeClass) *(sal_Int32*)enumValue.getValue() )
236 OUStringBuffer buf;
237 buf.appendAscii( "pyuno.checkType: " ).append(name).appendAscii( " is a " );
238 buf.appendAscii( typeClassToString( (TypeClass) desc.get()->eTypeClass) );
239 buf.appendAscii( ", but type got construct with typeclass " );
240 buf.appendAscii( typeClassToString( (TypeClass) *(sal_Int32*)enumValue.getValue() ) );
241 throw RuntimeException(
242 buf.makeStringAndClear(), Reference< XInterface > () );
244 return desc.get()->pWeakRef;
247 static PyObject* callCtor( const Runtime &r , const char * clazz, const PyRef & args )
249 PyRef code( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), (char*)clazz ) );
250 if( ! code.is() )
252 OStringBuffer buf;
253 buf.append( "couldn't access uno." );
254 buf.append( clazz );
255 PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
256 return NULL;
258 PyRef instance( PyObject_CallObject( code.get(), args.get() ), SAL_NO_ACQUIRE);
259 Py_XINCREF( instance.get() );
260 return instance.get();
265 PyObject *PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r )
267 PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE );
268 PyTuple_SetItem( args.get() , 0 , PyStr_FromString( enumBase ) );
269 PyTuple_SetItem( args.get() , 1 , PyStr_FromString( enumValue ) );
271 return callCtor( r, "Enum" , args );
275 PyObject* PyUNO_Type_new (const char *typeName , TypeClass t , const Runtime &r )
277 // retrieve type object
278 PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE );
280 PyTuple_SetItem( args.get() , 0 , PyStr_FromString( typeName ) );
281 PyObject *typeClass = PyUNO_Enum_new( "com.sun.star.uno.TypeClass" , typeClassToString(t), r );
282 if( ! typeClass )
283 return NULL;
284 PyTuple_SetItem( args.get() , 1 , typeClass);
286 return callCtor( r, "Type" , args );
289 PyObject* PyUNO_char_new ( sal_Unicode val , const Runtime &r )
291 // retrieve type object
292 PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
294 Py_UNICODE u[2];
295 u[0] = val;
296 u[1] = 0;
297 PyTuple_SetItem( args.get() , 0 , PyUnicode_FromUnicode( u ,1) );
299 return callCtor( r, "Char" , args );
302 PyObject *PyUNO_ByteSequence_new(
303 const com::sun::star::uno::Sequence< sal_Int8 > &byteSequence, const Runtime &r )
305 PyRef str(
306 PyStrBytes_FromStringAndSize( (char*)byteSequence.getConstArray(), byteSequence.getLength()),
307 SAL_NO_ACQUIRE );
308 PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
309 PyTuple_SetItem( args.get() , 0 , str.getAcquired() );
310 return callCtor( r, "ByteSequence" , args );
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */