Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / pyuno / source / module / pyuno_type.cxx
blobde4a212e22bb52ed3834067ba92c640a7712dfe0
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 <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;
34 namespace pyuno
36 const char *typeClassToString( TypeClass t )
38 const char * ret = nullptr;
39 switch (t)
41 case css::uno::TypeClass_VOID:
42 ret = "VOID"; break;
43 case css::uno::TypeClass_CHAR:
44 ret = "CHAR"; break;
45 case css::uno::TypeClass_BOOLEAN:
46 ret = "BOOLEAN"; break;
47 case css::uno::TypeClass_BYTE:
48 ret = "BYTE"; break;
49 case css::uno::TypeClass_SHORT:
50 ret = "SHORT"; break;
51 case css::uno::TypeClass_UNSIGNED_SHORT:
52 ret = "UNSIGNED_SHORT"; break;
53 case css::uno::TypeClass_LONG:
54 ret = "LONG"; break;
55 case css::uno::TypeClass_UNSIGNED_LONG:
56 ret = "UNSIGNED_LONG"; break;
57 case css::uno::TypeClass_HYPER:
58 ret = "HYPER"; break;
59 case css::uno::TypeClass_UNSIGNED_HYPER:
60 ret = "UNSIGNED_HYPER"; break;
61 case css::uno::TypeClass_FLOAT:
62 ret = "FLOAT"; break;
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:
68 ret = "TYPE"; break;
69 case css::uno::TypeClass_ANY:
70 ret = "ANY";break;
71 case css::uno::TypeClass_ENUM:
72 ret = "ENUM";break;
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;
91 default:
92 ret = "UNKNOWN"; break;
94 return ret;
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 )
139 #else
140 if( PyUnicode_GetSize( value.get() ) < 1 )
141 #endif
143 throw RuntimeException(
144 "uno.Char contains an empty unicode string");
147 sal_Unicode c = static_cast<sal_Unicode>(PyUnicode_AsUnicode( value.get() )[0]);
148 return c;
151 Any PyEnum2Enum( PyObject *obj )
153 Any ret;
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 );
166 if( !desc.is() )
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))) +
175 ", expected ENUM" );
178 desc.makeComplete();
180 typelib_EnumTypeDescription *pEnumDesc = reinterpret_cast<typelib_EnumTypeDescription*>(desc.get());
181 int i = 0;
182 for( i = 0; i < pEnumDesc->nEnumValues ; i ++ )
184 if( OUString::unacquired(&pEnumDesc->ppEnumNames[i]).equalsAscii( stringValue ) )
186 break;
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 );
197 return ret;
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 );
215 if( ! desc.is() )
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 ) );
233 if( ! code.is() )
235 OString buf = OStringLiteral("couldn't access uno.") + clazz;
236 PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
237 return nullptr;
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 );
263 if( ! typeClass )
264 return nullptr;
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 );
275 Py_UNICODE u[2];
276 u[0] = val;
277 u[1] = 0;
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 )
286 PyRef str(
287 PyStrBytes_FromStringAndSize( reinterpret_cast<char const *>(byteSequence.getConstArray()), byteSequence.getLength()),
288 SAL_NO_ACQUIRE );
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: */