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 <o3tl/any.hxx>
23 #include <rtl/strbuf.hxx>
25 #include <typelib/typedescription.hxx>
28 using com::sun::star::uno::TypeClass
;
29 using com::sun::star::uno::Type
;
30 using com::sun::star::uno::RuntimeException
;
31 using com::sun::star::uno::Any
;
32 using com::sun::star::uno::TypeDescription
;
36 const char *typeClassToString( TypeClass t
)
38 const char * ret
= nullptr;
41 case css::uno::TypeClass_VOID
:
43 case css::uno::TypeClass_CHAR
:
45 case css::uno::TypeClass_BOOLEAN
:
46 ret
= "BOOLEAN"; break;
47 case css::uno::TypeClass_BYTE
:
49 case css::uno::TypeClass_SHORT
:
51 case css::uno::TypeClass_UNSIGNED_SHORT
:
52 ret
= "UNSIGNED_SHORT"; break;
53 case css::uno::TypeClass_LONG
:
55 case css::uno::TypeClass_UNSIGNED_LONG
:
56 ret
= "UNSIGNED_LONG"; break;
57 case css::uno::TypeClass_HYPER
:
59 case css::uno::TypeClass_UNSIGNED_HYPER
:
60 ret
= "UNSIGNED_HYPER"; break;
61 case css::uno::TypeClass_FLOAT
:
63 case css::uno::TypeClass_DOUBLE
:
64 ret
= "DOUBLE"; break;
65 case css::uno::TypeClass_STRING
:
66 ret
= "STRING"; break;
67 case css::uno::TypeClass_TYPE
:
69 case css::uno::TypeClass_ANY
:
71 case css::uno::TypeClass_ENUM
:
73 case css::uno::TypeClass_STRUCT
:
74 ret
= "STRUCT"; break;
75 case css::uno::TypeClass_EXCEPTION
:
76 ret
= "EXCEPTION"; break;
77 case css::uno::TypeClass_SEQUENCE
:
78 ret
= "SEQUENCE"; break;
79 case css::uno::TypeClass_INTERFACE
:
80 ret
= "INTERFACE"; break;
81 case css::uno::TypeClass_TYPEDEF
:
82 ret
= "TYPEDEF"; break;
83 case css::uno::TypeClass_SERVICE
:
84 ret
= "SERVICE"; break;
85 case css::uno::TypeClass_MODULE
:
86 ret
= "MODULE"; break;
87 case css::uno::TypeClass_INTERFACE_METHOD
:
88 ret
= "INTERFACE_METHOD"; break;
89 case css::uno::TypeClass_INTERFACE_ATTRIBUTE
:
90 ret
= "INTERFACE_ATTRIBUTE"; break;
92 ret
= "UNKNOWN"; break;
97 static PyRef
getClass( const Runtime
& r
, const char * name
)
99 return PyRef( PyDict_GetItemString( r
.getImpl()->cargo
->getUnoModule().get(), name
) );
102 PyRef
getTypeClass( const Runtime
& r
)
104 return getClass( r
, "Type" );
107 PyRef
getEnumClass( const Runtime
& r
)
109 return getClass( r
, "Enum" );
112 PyRef
getCharClass( const Runtime
& r
)
114 return getClass( r
, "Char" );
117 PyRef
getByteSequenceClass( const Runtime
& r
)
119 return getClass( r
, "ByteSequence" );
122 PyRef
getAnyClass( const Runtime
& r
)
124 return getClass( r
, "Any" );
128 sal_Unicode
PyChar2Unicode( PyObject
*obj
)
130 PyRef
value( PyObject_GetAttrString( obj
, "value" ), SAL_NO_ACQUIRE
);
131 if( ! PyUnicode_Check( value
.get() ) )
133 throw RuntimeException(
134 "attribute value of uno.Char is not a unicode string" );
137 #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 3
138 if( PyUnicode_GetLength( value
.get() ) < 1 )
140 if( PyUnicode_GetSize( value
.get() ) < 1 )
143 throw RuntimeException(
144 "uno.Char contains an empty unicode string");
147 sal_Unicode c
= static_cast<sal_Unicode
>(PyUnicode_AsUnicode( value
.get() )[0]);
151 Any
PyEnum2Enum( PyObject
*obj
)
154 PyRef
typeName( PyObject_GetAttrString( obj
,"typeName" ), SAL_NO_ACQUIRE
);
155 PyRef
value( PyObject_GetAttrString( obj
, "value" ), SAL_NO_ACQUIRE
);
156 if( !PyStr_Check( typeName
.get() ) || ! PyStr_Check( value
.get() ) )
158 throw RuntimeException(
159 "attributes typeName and/or value of uno.Enum are not strings" );
162 OUString
strTypeName( OUString::createFromAscii( PyStr_AsString( typeName
.get() ) ) );
163 char const *stringValue
= PyStr_AsString( value
.get() );
165 TypeDescription
desc( strTypeName
);
168 throw RuntimeException( "enum " + OUString::createFromAscii( PyStr_AsString(typeName
.get()) ) + " is unknown" );
171 if(desc
.get()->eTypeClass
!= typelib_TypeClass_ENUM
)
173 throw RuntimeException( "pyuno.checkEnum: " + strTypeName
+ "is a " +
174 OUString::createFromAscii(typeClassToString( static_cast<css::uno::TypeClass
>(desc
.get()->eTypeClass
))) +
180 typelib_EnumTypeDescription
*pEnumDesc
= reinterpret_cast<typelib_EnumTypeDescription
*>(desc
.get());
182 for( i
= 0; i
< pEnumDesc
->nEnumValues
; i
++ )
184 if( OUString::unacquired(&pEnumDesc
->ppEnumNames
[i
]).equalsAscii( stringValue
) )
189 if( i
== pEnumDesc
->nEnumValues
)
191 throw RuntimeException( "value " + OUString::createFromAscii( stringValue
) +
192 "is unknown in enum " +
193 OUString::createFromAscii( PyStr_AsString( typeName
.get() ) ) );
195 ret
= Any( &pEnumDesc
->pEnumValues
[i
], desc
.get()->pWeakRef
);
201 Type
PyType2Type( PyObject
* o
)
203 PyRef
pyName( PyObject_GetAttrString( o
, "typeName" ), SAL_NO_ACQUIRE
);
204 if( !PyStr_Check( pyName
.get() ) )
206 throw RuntimeException(
207 "type object does not have typeName property" );
210 PyRef
pyTC( PyObject_GetAttrString( o
, "typeClass" ), SAL_NO_ACQUIRE
);
211 Any enumValue
= PyEnum2Enum( pyTC
.get() );
213 OUString
name( OUString::createFromAscii( PyStr_AsString( pyName
.get() ) ) );
214 TypeDescription
desc( name
);
217 throw RuntimeException( "type " + name
+ " is unknown" );
219 css::uno::TypeClass tc
= *o3tl::doAccess
<css::uno::TypeClass
>(enumValue
);
220 if( static_cast<css::uno::TypeClass
>(desc
.get()->eTypeClass
) != tc
)
222 throw RuntimeException( "pyuno.checkType: " + name
+ " is a " +
223 OUString::createFromAscii( typeClassToString( static_cast<TypeClass
>(desc
.get()->eTypeClass
)) ) +
224 ", but type got construct with typeclass " +
225 OUString::createFromAscii( typeClassToString( tc
) ) );
227 return desc
.get()->pWeakRef
;
230 static PyObject
* callCtor( const Runtime
&r
, const char * clazz
, const PyRef
& args
)
232 PyRef
code( PyDict_GetItemString( r
.getImpl()->cargo
->getUnoModule().get(), clazz
) );
235 OString buf
= OStringLiteral("couldn't access uno.") + clazz
;
236 PyErr_SetString( PyExc_RuntimeError
, buf
.getStr() );
239 PyRef
instance( PyObject_CallObject( code
.get(), args
.get() ), SAL_NO_ACQUIRE
);
240 Py_XINCREF( instance
.get() );
241 return instance
.get();
246 PyObject
*PyUNO_Enum_new( const char *enumBase
, const char *enumValue
, const Runtime
&r
)
248 PyRef
args( PyTuple_New( 2 ), SAL_NO_ACQUIRE
, NOT_NULL
);
249 PyTuple_SetItem( args
.get() , 0 , PyStr_FromString( enumBase
) );
250 PyTuple_SetItem( args
.get() , 1 , PyStr_FromString( enumValue
) );
252 return callCtor( r
, "Enum" , args
);
256 PyObject
* PyUNO_Type_new (const char *typeName
, TypeClass t
, const Runtime
&r
)
258 // retrieve type object
259 PyRef
args(PyTuple_New( 2 ), SAL_NO_ACQUIRE
, NOT_NULL
);
261 PyTuple_SetItem( args
.get() , 0 , PyStr_FromString( typeName
) );
262 PyObject
*typeClass
= PyUNO_Enum_new( "com.sun.star.uno.TypeClass" , typeClassToString(t
), r
);
265 PyTuple_SetItem( args
.get() , 1 , typeClass
);
267 return callCtor( r
, "Type" , args
);
270 PyObject
* PyUNO_char_new ( sal_Unicode val
, const Runtime
&r
)
272 // retrieve type object
273 PyRef
args( PyTuple_New( 1 ), SAL_NO_ACQUIRE
, NOT_NULL
);
278 PyTuple_SetItem( args
.get() , 0 , PyUnicode_FromUnicode( u
,1) );
280 return callCtor( r
, "Char" , args
);
283 PyObject
*PyUNO_ByteSequence_new(
284 const css::uno::Sequence
< sal_Int8
> &byteSequence
, const Runtime
&r
)
287 PyStrBytes_FromStringAndSize( reinterpret_cast<char const *>(byteSequence
.getConstArray()), byteSequence
.getLength()),
289 PyRef
args( PyTuple_New( 1 ), SAL_NO_ACQUIRE
, NOT_NULL
);
290 PyTuple_SetItem( args
.get() , 0 , str
.getAcquired() );
291 return callCtor( r
, "ByteSequence" , args
);
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */