LibreOffice Module pyuno (master) 1
pyuno_type.cxx
Go to the documentation of this file.
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 <o3tl/any.hxx>
22
23#include <typelib/typedescription.hxx>
24
25
26using com::sun::star::uno::TypeClass;
27using com::sun::star::uno::Type;
28using com::sun::star::uno::RuntimeException;
29using com::sun::star::uno::Any;
30using com::sun::star::uno::TypeDescription;
31
32namespace pyuno
33{
34const char *typeClassToString( TypeClass t )
35{
36 const char * ret = nullptr;
37 switch (t)
38 {
39 case css::uno::TypeClass_VOID:
40 ret = "VOID"; break;
41 case css::uno::TypeClass_CHAR:
42 ret = "CHAR"; break;
43 case css::uno::TypeClass_BOOLEAN:
44 ret = "BOOLEAN"; break;
45 case css::uno::TypeClass_BYTE:
46 ret = "BYTE"; break;
47 case css::uno::TypeClass_SHORT:
48 ret = "SHORT"; break;
49 case css::uno::TypeClass_UNSIGNED_SHORT:
50 ret = "UNSIGNED_SHORT"; break;
51 case css::uno::TypeClass_LONG:
52 ret = "LONG"; break;
53 case css::uno::TypeClass_UNSIGNED_LONG:
54 ret = "UNSIGNED_LONG"; break;
55 case css::uno::TypeClass_HYPER:
56 ret = "HYPER"; break;
57 case css::uno::TypeClass_UNSIGNED_HYPER:
58 ret = "UNSIGNED_HYPER"; break;
59 case css::uno::TypeClass_FLOAT:
60 ret = "FLOAT"; break;
61 case css::uno::TypeClass_DOUBLE:
62 ret = "DOUBLE"; break;
63 case css::uno::TypeClass_STRING:
64 ret = "STRING"; break;
65 case css::uno::TypeClass_TYPE:
66 ret = "TYPE"; break;
67 case css::uno::TypeClass_ANY:
68 ret = "ANY";break;
69 case css::uno::TypeClass_ENUM:
70 ret = "ENUM";break;
71 case css::uno::TypeClass_STRUCT:
72 ret = "STRUCT"; break;
73 case css::uno::TypeClass_EXCEPTION:
74 ret = "EXCEPTION"; break;
75 case css::uno::TypeClass_SEQUENCE:
76 ret = "SEQUENCE"; break;
77 case css::uno::TypeClass_INTERFACE:
78 ret = "INTERFACE"; break;
79 case css::uno::TypeClass_TYPEDEF:
80 ret = "TYPEDEF"; break;
81 case css::uno::TypeClass_SERVICE:
82 ret = "SERVICE"; break;
83 case css::uno::TypeClass_MODULE:
84 ret = "MODULE"; break;
85 case css::uno::TypeClass_INTERFACE_METHOD:
86 ret = "INTERFACE_METHOD"; break;
87 case css::uno::TypeClass_INTERFACE_ATTRIBUTE:
88 ret = "INTERFACE_ATTRIBUTE"; break;
89 default:
90 ret = "UNKNOWN"; break;
91 }
92 return ret;
93}
94
95static PyRef getClass( const Runtime & r , const char * name)
96{
97 return PyRef( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), name ) );
98}
99
101{
102 return getClass( r , "Type" );
103}
104
106{
107 return getClass( r , "Enum" );
108}
109
111{
112 return getClass( r , "Char" );
113}
114
116{
117 return getClass( r , "ByteSequence" );
118}
119
121{
122 return getClass( r , "Any" );
123}
124
125
127{
128 PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE );
129 if( ! PyUnicode_Check( value.get() ) )
130 {
131 throw RuntimeException(
132 "attribute value of uno.Char is not a unicode string" );
133 }
134
135 if( PyUnicode_GetLength( value.get() ) < 1 )
136 {
137 throw RuntimeException(
138 "uno.Char contains an empty unicode string");
139 }
140
141 sal_Unicode c = static_cast<sal_Unicode>(PyUnicode_ReadChar( value.get(), 0));
142 return c;
143}
144
145Any PyEnum2Enum( PyObject *obj )
146{
147 Any ret;
148 PyRef typeName( PyObject_GetAttrString( obj,"typeName" ), SAL_NO_ACQUIRE);
149 PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE);
150 if( !PyUnicode_Check( typeName.get() ) || ! PyUnicode_Check( value.get() ) )
151 {
152 throw RuntimeException(
153 "attributes typeName and/or value of uno.Enum are not strings" );
154 }
155
156 OUString strTypeName( OUString::createFromAscii( PyUnicode_AsUTF8( typeName.get() ) ) );
157 char const *stringValue = PyUnicode_AsUTF8( value.get() );
158
159 TypeDescription desc( strTypeName );
160 if( !desc.is() )
161 {
162 throw RuntimeException( "enum " + OUString::createFromAscii( PyUnicode_AsUTF8(typeName.get()) ) + " is unknown" );
163 }
164
165 if(desc.get()->eTypeClass != typelib_TypeClass_ENUM )
166 {
167 throw RuntimeException( "pyuno.checkEnum: " + strTypeName + "is a " +
168 OUString::createFromAscii(typeClassToString( static_cast<css::uno::TypeClass>(desc.get()->eTypeClass))) +
169 ", expected ENUM" );
170 }
171
172 desc.makeComplete();
173
174 typelib_EnumTypeDescription *pEnumDesc = reinterpret_cast<typelib_EnumTypeDescription*>(desc.get());
175 int i = 0;
176 for( i = 0; i < pEnumDesc->nEnumValues ; i ++ )
177 {
178 if( OUString::unacquired(&pEnumDesc->ppEnumNames[i]).equalsAscii( stringValue ) )
179 {
180 break;
181 }
182 }
183 if( i == pEnumDesc->nEnumValues )
184 {
185 throw RuntimeException( "value " + OUString::createFromAscii( stringValue ) +
186 "is unknown in enum " +
187 OUString::createFromAscii( PyUnicode_AsUTF8( typeName.get() ) ) );
188 }
189 ret = Any( &pEnumDesc->pEnumValues[i], desc.get()->pWeakRef );
190
191 return ret;
192}
193
194
195Type PyType2Type( PyObject * o )
196{
197 PyRef pyName( PyObject_GetAttrString( o, "typeName" ), SAL_NO_ACQUIRE);
198 if( !PyUnicode_Check( pyName.get() ) )
199 {
200 throw RuntimeException(
201 "type object does not have typeName property" );
202 }
203
204 PyRef pyTC( PyObject_GetAttrString( o, "typeClass" ), SAL_NO_ACQUIRE );
205 Any enumValue = PyEnum2Enum( pyTC.get() );
206
207 OUString name( OUString::createFromAscii( PyUnicode_AsUTF8( pyName.get() ) ) );
208 TypeDescription desc( name );
209 if( ! desc.is() )
210 {
211 throw RuntimeException( "type " + name + " is unknown" );
212 }
213 css::uno::TypeClass tc = *o3tl::doAccess<css::uno::TypeClass>(enumValue);
214 if( static_cast<css::uno::TypeClass>(desc.get()->eTypeClass) != tc )
215 {
216 throw RuntimeException( "pyuno.checkType: " + name + " is a " +
217 OUString::createFromAscii( typeClassToString( static_cast<TypeClass>(desc.get()->eTypeClass)) ) +
218 ", but type got construct with typeclass " +
219 OUString::createFromAscii( typeClassToString( tc ) ) );
220 }
221 return desc.get()->pWeakRef;
222}
223
224static PyObject* callCtor( const Runtime &r , const char * clazz, const PyRef & args )
225{
226 PyRef code( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), clazz ) );
227 if( ! code.is() )
228 {
229 OString buf = OString::Concat("couldn't access uno.") + clazz;
230 PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
231 return nullptr;
232 }
233 PyRef instance( PyObject_CallObject( code.get(), args.get() ), SAL_NO_ACQUIRE);
234 Py_XINCREF( instance.get() );
235 return instance.get();
236
237}
238
239
240PyObject *PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r )
241{
242 PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE, NOT_NULL );
243 PyTuple_SetItem( args.get() , 0 , PyUnicode_FromString( enumBase ) );
244 PyTuple_SetItem( args.get() , 1 , PyUnicode_FromString( enumValue ) );
245
246 return callCtor( r, "Enum" , args );
247}
248
249
250PyObject* PyUNO_Type_new (const char *typeName , TypeClass t , const Runtime &r )
251{
252 // retrieve type object
253 PyRef args(PyTuple_New( 2 ), SAL_NO_ACQUIRE, NOT_NULL);
254
255 PyTuple_SetItem( args.get() , 0 , PyUnicode_FromString( typeName ) );
256 PyObject *typeClass = PyUNO_Enum_new( "com.sun.star.uno.TypeClass" , typeClassToString(t), r );
257 if( ! typeClass )
258 return nullptr;
259 PyTuple_SetItem( args.get() , 1 , typeClass);
260
261 return callCtor( r, "Type" , args );
262}
263
264PyObject* PyUNO_char_new ( sal_Unicode val , const Runtime &r )
265{
266 // retrieve type object
267 PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE, NOT_NULL );
268 static_assert(sizeof(sal_Unicode) == sizeof(Py_UCS2), "unexpected size");
269 PyTuple_SetItem( args.get() , 0 , PyUnicode_FromKindAndData( PyUnicode_2BYTE_KIND, &val ,1) );
270
271 return callCtor( r, "Char" , args );
272}
273
275 const css::uno::Sequence< sal_Int8 > &byteSequence, const Runtime &r )
276{
277 PyRef str(
278 PyBytes_FromStringAndSize( reinterpret_cast<char const *>(byteSequence.getConstArray()), byteSequence.getLength()),
279 SAL_NO_ACQUIRE );
280 PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE, NOT_NULL );
281 PyTuple_SetItem( args.get() , 0 , str.getAcquired() );
282 return callCtor( r, "ByteSequence" , args );
283
284}
285}
286
287/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
Helper class for keeping references to python objects.
Definition: pyuno.hxx:80
PyObject * get() const noexcept
Definition: pyuno.hxx:99
PyObject * getAcquired() const
Definition: pyuno.hxx:101
The pyuno::Runtime class keeps the internal state of the python UNO bridge for the currently in use p...
Definition: pyuno.hxx:164
RuntimeImpl * getImpl() const
Returns the internal handle.
Definition: pyuno.hxx:242
Any value
const char * name
Type
int i
args
Definition: pyuno.cxx:72
static PyObject * callCtor(const Runtime &r, const char *clazz, const PyRef &args)
Definition: pyuno_type.cxx:224
PyObject * PyUNO_Type_new(const char *typeName, css::uno::TypeClass t, const Runtime &r)
PyRef getCharClass(const Runtime &)
Definition: pyuno_type.cxx:110
PyObject * PyUNO_ByteSequence_new(const css::uno::Sequence< sal_Int8 > &, const Runtime &r)
Definition: pyuno_type.cxx:274
PyObject * PyUNO_char_new(sal_Unicode c, const Runtime &r)
Definition: pyuno_type.cxx:264
const char * typeClassToString(css::uno::TypeClass t)
sal_Unicode PyChar2Unicode(PyObject *o)
Definition: pyuno_type.cxx:126
PyObject * PyUNO_Enum_new(const char *enumBase, const char *enumValue, const Runtime &r)
Definition: pyuno_type.cxx:240
PyRef getTypeClass(const Runtime &)
Definition: pyuno_type.cxx:100
PyRef getEnumClass(const Runtime &)
Definition: pyuno_type.cxx:105
css::uno::Type PyType2Type(PyObject *o)
Definition: pyuno_type.cxx:195
PyRef getByteSequenceClass(const Runtime &)
Definition: pyuno_type.cxx:115
@ NOT_NULL
definition of a no acquire enum for ctors
Definition: pyuno.hxx:65
PyRef getClass(const OUString &name, const Runtime &runtime)
css::uno::Any PyEnum2Enum(PyObject *obj)
Definition: pyuno_type.cxx:145
PyRef getAnyClass(const Runtime &)
Definition: pyuno_type.cxx:120
sal_Unicode code
OUString typeName
PyRef const & getUnoModule()
PyObject_HEAD struct RuntimeCargo * cargo
Definition: pyuno_impl.hxx:238
sal_uInt16 sal_Unicode