1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
37 const char *typeClassToString( TypeClass t
)
42 case com::sun::star::uno::TypeClass_VOID
:
44 case com::sun::star::uno::TypeClass_CHAR
:
46 case com::sun::star::uno::TypeClass_BOOLEAN
:
47 ret
= "BOOLEAN"; break;
48 case com::sun::star::uno::TypeClass_BYTE
:
50 case com::sun::star::uno::TypeClass_SHORT
:
52 case com::sun::star::uno::TypeClass_UNSIGNED_SHORT
:
53 ret
= "UNSIGNED_SHORT"; break;
54 case com::sun::star::uno::TypeClass_LONG
:
56 case com::sun::star::uno::TypeClass_UNSIGNED_LONG
:
57 ret
= "UNSIGNED_LONG"; break;
58 case com::sun::star::uno::TypeClass_HYPER
:
60 case com::sun::star::uno::TypeClass_UNSIGNED_HYPER
:
61 ret
= "UNSIGNED_HYPER"; break;
62 case com::sun::star::uno::TypeClass_FLOAT
:
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
:
70 case com::sun::star::uno::TypeClass_ANY
:
72 case com::sun::star::uno::TypeClass_ENUM
:
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
:
86 case com::sun::star::uno::TypeClass_ARRAY
:
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;
97 ret
= "UNKNOWN"; break;
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];
154 Any
PyEnum2Enum( PyObject
*obj
) throw ( RuntimeException
)
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
);
172 if(desc
.get()->eTypeClass
!= typelib_TypeClass_ENUM
)
175 buf
.appendAscii( "pyuno.checkEnum: " ).append(strTypeName
).appendAscii( "is a " );
177 typeClassToString( (com::sun::star::uno::TypeClass
) desc
.get()->eTypeClass
));
178 buf
.appendAscii( ", expected ENUM" );
179 throw RuntimeException( buf
.makeStringAndClear(), Reference
< XInterface
> () );
184 typelib_EnumTypeDescription
*pEnumDesc
= (typelib_EnumTypeDescription
*) desc
.get();
186 for( i
= 0; i
< pEnumDesc
->nEnumValues
; i
++ )
188 if( (*((OUString
*)&pEnumDesc
->ppEnumNames
[i
])).equalsAscii( stringValue
) )
193 if( i
== pEnumDesc
->nEnumValues
)
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
);
205 buf
.appendAscii( "enum " ).appendAscii( PyStr_AsString(typeName
.get()) ).appendAscii( " is unknown" );
206 throw RuntimeException( buf
.makeStringAndClear(), Reference
< XInterface
> () );
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
);
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() )
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
) );
253 buf
.append( "couldn't access uno." );
255 PyErr_SetString( PyExc_RuntimeError
, buf
.getStr() );
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
);
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
);
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
)
306 PyStrBytes_FromStringAndSize( (char*)byteSequence
.getConstArray(), byteSequence
.getLength()),
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: */