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::TypeDescription
;
35 const char *typeClassToString( TypeClass t
)
37 const char * ret
= nullptr;
40 case css::uno::TypeClass_VOID
:
42 case css::uno::TypeClass_CHAR
:
44 case css::uno::TypeClass_BOOLEAN
:
45 ret
= "BOOLEAN"; break;
46 case css::uno::TypeClass_BYTE
:
48 case css::uno::TypeClass_SHORT
:
50 case css::uno::TypeClass_UNSIGNED_SHORT
:
51 ret
= "UNSIGNED_SHORT"; break;
52 case css::uno::TypeClass_LONG
:
54 case css::uno::TypeClass_UNSIGNED_LONG
:
55 ret
= "UNSIGNED_LONG"; break;
56 case css::uno::TypeClass_HYPER
:
58 case css::uno::TypeClass_UNSIGNED_HYPER
:
59 ret
= "UNSIGNED_HYPER"; break;
60 case css::uno::TypeClass_FLOAT
:
62 case css::uno::TypeClass_DOUBLE
:
63 ret
= "DOUBLE"; break;
64 case css::uno::TypeClass_STRING
:
65 ret
= "STRING"; break;
66 case css::uno::TypeClass_TYPE
:
68 case css::uno::TypeClass_ANY
:
70 case css::uno::TypeClass_ENUM
:
72 case css::uno::TypeClass_STRUCT
:
73 ret
= "STRUCT"; break;
74 case css::uno::TypeClass_EXCEPTION
:
75 ret
= "EXCEPTION"; break;
76 case css::uno::TypeClass_SEQUENCE
:
77 ret
= "SEQUENCE"; break;
78 case css::uno::TypeClass_INTERFACE
:
79 ret
= "INTERFACE"; break;
80 case css::uno::TypeClass_TYPEDEF
:
81 ret
= "TYPEDEF"; break;
82 case css::uno::TypeClass_SERVICE
:
83 ret
= "SERVICE"; break;
84 case css::uno::TypeClass_MODULE
:
85 ret
= "MODULE"; break;
86 case css::uno::TypeClass_INTERFACE_METHOD
:
87 ret
= "INTERFACE_METHOD"; break;
88 case css::uno::TypeClass_INTERFACE_ATTRIBUTE
:
89 ret
= "INTERFACE_ATTRIBUTE"; break;
91 ret
= "UNKNOWN"; break;
96 static PyRef
getClass( const Runtime
& r
, const char * name
)
98 return PyRef( PyDict_GetItemString( r
.getImpl()->cargo
->getUnoModule().get(), name
) );
101 PyRef
getTypeClass( const Runtime
& r
)
103 return getClass( r
, "Type" );
106 PyRef
getEnumClass( const Runtime
& r
)
108 return getClass( r
, "Enum" );
111 PyRef
getCharClass( const Runtime
& r
)
113 return getClass( r
, "Char" );
116 PyRef
getByteSequenceClass( const Runtime
& r
)
118 return getClass( r
, "ByteSequence" );
121 PyRef
getAnyClass( const Runtime
& r
)
123 return getClass( r
, "Any" );
127 sal_Unicode
PyChar2Unicode( PyObject
*obj
) throw ( RuntimeException
)
129 PyRef
value( PyObject_GetAttrString( obj
, "value" ), SAL_NO_ACQUIRE
);
130 if( ! PyUnicode_Check( value
.get() ) )
132 throw RuntimeException(
133 "attribute value of uno.Char is not a unicode string" );
136 if( PyUnicode_GetSize( value
.get() ) < 1 )
138 throw RuntimeException(
139 "uno.Char contains an empty unicode string");
142 sal_Unicode c
= (sal_Unicode
)PyUnicode_AsUnicode( value
.get() )[0];
146 Any
PyEnum2Enum( PyObject
*obj
) throw ( RuntimeException
)
149 PyRef
typeName( PyObject_GetAttrString( obj
,"typeName" ), SAL_NO_ACQUIRE
);
150 PyRef
value( PyObject_GetAttrString( obj
, "value" ), SAL_NO_ACQUIRE
);
151 if( !PyStr_Check( typeName
.get() ) || ! PyStr_Check( value
.get() ) )
153 throw RuntimeException(
154 "attributes typeName and/or value of uno.Enum are not strings" );
157 OUString
strTypeName( OUString::createFromAscii( PyStr_AsString( typeName
.get() ) ) );
158 char *stringValue
= PyStr_AsString( value
.get() );
160 TypeDescription
desc( strTypeName
);
163 if(desc
.get()->eTypeClass
!= typelib_TypeClass_ENUM
)
166 buf
.append( "pyuno.checkEnum: " ).append(strTypeName
).append( "is a " );
168 typeClassToString( (css::uno::TypeClass
) desc
.get()->eTypeClass
));
169 buf
.append( ", expected ENUM" );
170 throw RuntimeException( buf
.makeStringAndClear() );
175 typelib_EnumTypeDescription
*pEnumDesc
= reinterpret_cast<typelib_EnumTypeDescription
*>(desc
.get());
177 for( i
= 0; i
< pEnumDesc
->nEnumValues
; i
++ )
179 if( OUString::unacquired(&pEnumDesc
->ppEnumNames
[i
]).equalsAscii( stringValue
) )
184 if( i
== pEnumDesc
->nEnumValues
)
187 buf
.append( "value " ).appendAscii( stringValue
).append( "is unknown in enum " );
188 buf
.appendAscii( PyStr_AsString( typeName
.get() ) );
189 throw RuntimeException( buf
.makeStringAndClear() );
191 ret
= Any( &pEnumDesc
->pEnumValues
[i
], desc
.get()->pWeakRef
);
196 buf
.append( "enum " ).appendAscii( PyStr_AsString(typeName
.get()) ).append( " is unknown" );
197 throw RuntimeException( buf
.makeStringAndClear() );
203 Type
PyType2Type( PyObject
* o
) throw(RuntimeException
)
205 PyRef
pyName( PyObject_GetAttrString( o
, "typeName" ), SAL_NO_ACQUIRE
);
206 if( !PyStr_Check( pyName
.get() ) )
208 throw RuntimeException(
209 "type object does not have typeName property" );
212 PyRef
pyTC( PyObject_GetAttrString( o
, "typeClass" ), SAL_NO_ACQUIRE
);
213 Any enumValue
= PyEnum2Enum( pyTC
.get() );
215 OUString
name( OUString::createFromAscii( PyStr_AsString( pyName
.get() ) ) );
216 TypeDescription
desc( name
);
220 buf
.append( "type " ).append(name
).append( " is unknown" );
221 throw RuntimeException( buf
.makeStringAndClear() );
223 if( desc
.get()->eTypeClass
!= (typelib_TypeClass
) *static_cast<sal_Int32
const *>(enumValue
.getValue()) )
226 buf
.append( "pyuno.checkType: " ).append(name
).append( " is a " );
227 buf
.appendAscii( typeClassToString( (TypeClass
) desc
.get()->eTypeClass
) );
228 buf
.append( ", but type got construct with typeclass " );
229 buf
.appendAscii( typeClassToString( (TypeClass
) *static_cast<sal_Int32
const *>(enumValue
.getValue()) ) );
230 throw RuntimeException( buf
.makeStringAndClear() );
232 return desc
.get()->pWeakRef
;
235 static PyObject
* callCtor( const Runtime
&r
, const char * clazz
, const PyRef
& args
)
237 PyRef
code( PyDict_GetItemString( r
.getImpl()->cargo
->getUnoModule().get(), clazz
) );
241 buf
.append( "couldn't access uno." );
243 PyErr_SetString( PyExc_RuntimeError
, buf
.getStr() );
246 PyRef
instance( PyObject_CallObject( code
.get(), args
.get() ), SAL_NO_ACQUIRE
);
247 Py_XINCREF( instance
.get() );
248 return instance
.get();
253 PyObject
*PyUNO_Enum_new( const char *enumBase
, const char *enumValue
, const Runtime
&r
)
255 PyRef
args( PyTuple_New( 2 ), SAL_NO_ACQUIRE
, NOT_NULL
);
256 PyTuple_SetItem( args
.get() , 0 , PyStr_FromString( enumBase
) );
257 PyTuple_SetItem( args
.get() , 1 , PyStr_FromString( enumValue
) );
259 return callCtor( r
, "Enum" , args
);
263 PyObject
* PyUNO_Type_new (const char *typeName
, TypeClass t
, const Runtime
&r
)
265 // retrieve type object
266 PyRef
args(PyTuple_New( 2 ), SAL_NO_ACQUIRE
, NOT_NULL
);
268 PyTuple_SetItem( args
.get() , 0 , PyStr_FromString( typeName
) );
269 PyObject
*typeClass
= PyUNO_Enum_new( "com.sun.star.uno.TypeClass" , typeClassToString(t
), r
);
272 PyTuple_SetItem( args
.get() , 1 , typeClass
);
274 return callCtor( r
, "Type" , args
);
277 PyObject
* PyUNO_char_new ( sal_Unicode val
, const Runtime
&r
)
279 // retrieve type object
280 PyRef
args( PyTuple_New( 1 ), SAL_NO_ACQUIRE
, NOT_NULL
);
285 PyTuple_SetItem( args
.get() , 0 , PyUnicode_FromUnicode( u
,1) );
287 return callCtor( r
, "Char" , args
);
290 PyObject
*PyUNO_ByteSequence_new(
291 const css::uno::Sequence
< sal_Int8
> &byteSequence
, const Runtime
&r
)
294 PyStrBytes_FromStringAndSize( reinterpret_cast<char const *>(byteSequence
.getConstArray()), byteSequence
.getLength()),
296 PyRef
args( PyTuple_New( 1 ), SAL_NO_ACQUIRE
, NOT_NULL
);
297 PyTuple_SetItem( args
.get() , 0 , str
.getAcquired() );
298 return callCtor( r
, "ByteSequence" , args
);
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */