update emoji autocorrect entries from po-files
[LibreOffice.git] / pyuno / source / module / pyuno_callable.cxx
blob075e0615876b972d972b8359f95a6a7df663d226
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 <osl/diagnose.h>
22 #include <osl/thread.h>
23 #include <rtl/ustrbuf.hxx>
25 using com::sun::star::uno::Sequence;
26 using com::sun::star::uno::Reference;
27 using com::sun::star::uno::XInterface;
28 using com::sun::star::uno::Any;
29 using com::sun::star::uno::Type;
30 using com::sun::star::uno::TypeClass;
31 using com::sun::star::uno::RuntimeException;
32 using com::sun::star::uno::XComponentContext;
33 using com::sun::star::lang::XSingleServiceFactory;
34 using com::sun::star::script::XTypeConverter;
35 using com::sun::star::script::XInvocation2;
37 namespace pyuno
39 typedef struct
41 Reference<XInvocation2> xInvocation;
42 OUString methodName;
43 ConversionMode mode;
44 } PyUNO_callable_Internals;
46 typedef struct
48 PyObject_HEAD
49 PyUNO_callable_Internals* members;
50 } PyUNO_callable;
52 void PyUNO_callable_del (PyObject* self)
54 PyUNO_callable* me;
56 me = reinterpret_cast<PyUNO_callable*>(self);
57 delete me->members;
58 PyObject_Del (self);
60 return;
63 PyObject* PyUNO_callable_call(
64 PyObject* self, PyObject* args, SAL_UNUSED_PARAMETER PyObject*)
66 PyUNO_callable* me;
68 Sequence<short> aOutParamIndex;
69 Sequence<Any> aOutParam;
70 Sequence<Any> aParams;
71 Any any_params;
72 Any ret_value;
73 RuntimeCargo *cargo = 0;
74 me = reinterpret_cast<PyUNO_callable*>(self);
76 PyRef ret;
77 try
79 Runtime runtime;
80 cargo = runtime.getImpl()->cargo;
81 any_params = runtime.pyObject2Any (args, me->members->mode);
83 if (any_params.getValueTypeClass () == com::sun::star::uno::TypeClass_SEQUENCE)
85 any_params >>= aParams;
87 else
89 aParams.realloc (1);
90 aParams [0] <<= any_params;
94 PyThreadDetach antiguard; //pyhton free zone
96 // do some logging if desired ...
97 if( isLog( cargo, LogLevel::CALL ) )
99 logCall( cargo, "try py->uno[0x", me->members->xInvocation.get(),
100 me->members->methodName, aParams );
103 // do the call
104 ret_value = me->members->xInvocation->invoke (
105 me->members->methodName, aParams, aOutParamIndex, aOutParam);
107 // log the reply, if desired
108 if( isLog( cargo, LogLevel::CALL ) )
110 logReply( cargo, "success py->uno[0x", me->members->xInvocation.get(),
111 me->members->methodName, ret_value, aOutParam);
116 PyRef temp = runtime.any2PyObject (ret_value);
117 if( aOutParam.getLength() )
119 PyRef return_list( PyTuple_New (1+aOutParam.getLength()), SAL_NO_ACQUIRE, NOT_NULL );
120 PyTuple_SetItem (return_list.get(), 0, temp.getAcquired());
122 // initialize with defaults in case of exceptions
123 int i;
124 for( i = 1 ; i < 1+aOutParam.getLength() ; i ++ )
126 Py_INCREF( Py_None );
127 PyTuple_SetItem( return_list.get() , i , Py_None );
130 for( i = 0 ; i < aOutParam.getLength() ; i ++ )
132 PyRef ref = runtime.any2PyObject( aOutParam[i] );
133 PyTuple_SetItem (return_list.get(), 1+i, ref.getAcquired());
135 ret = return_list;
137 else
139 ret = temp;
142 catch( const com::sun::star::reflection::InvocationTargetException & e )
145 if( isLog( cargo, LogLevel::CALL ) )
147 logException( cargo, "except py->uno[0x", me->members->xInvocation.get() ,
148 me->members->methodName, e.TargetException.getValue(), e.TargetException.getValueTypeRef());
150 raisePyExceptionWithAny( e.TargetException );
152 catch( const com::sun::star::script::CannotConvertException &e )
154 if( isLog( cargo, LogLevel::CALL ) )
156 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
157 me->members->methodName, &e, cppu::UnoType<decltype(e)>::get().getTypeLibType());
159 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
161 catch( const com::sun::star::lang::IllegalArgumentException &e )
163 if( isLog( cargo, LogLevel::CALL ) )
165 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
166 me->members->methodName, &e, cppu::UnoType<decltype(e)>::get().getTypeLibType());
168 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
170 catch (const ::com::sun::star::uno::RuntimeException &e)
172 if( cargo && isLog( cargo, LogLevel::CALL ) )
174 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
175 me->members->methodName, &e, cppu::UnoType<decltype(e)>::get().getTypeLibType());
177 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
180 return ret.getAcquired();
184 static PyTypeObject PyUNO_callable_Type =
186 PyVarObject_HEAD_INIT( &PyType_Type, 0 )
187 "PyUNO_callable",
188 sizeof (PyUNO_callable),
190 (destructor) ::pyuno::PyUNO_callable_del,
191 (printfunc) 0,
192 (getattrfunc) 0,
193 (setattrfunc) 0,
195 (reprfunc) 0,
199 (hashfunc) 0,
200 (ternaryfunc) ::pyuno::PyUNO_callable_call,
201 (reprfunc) 0,
202 (getattrofunc)0,
203 (setattrofunc)0,
204 NULL,
206 NULL,
207 (traverseproc)0,
208 (inquiry)0,
209 (richcmpfunc)0,
211 (getiterfunc)0,
212 (iternextfunc)0,
213 NULL,
214 NULL,
215 NULL,
216 NULL,
217 NULL,
218 (descrgetfunc)0,
219 (descrsetfunc)0,
221 (initproc)0,
222 (allocfunc)0,
223 (newfunc)0,
224 (freefunc)0,
225 (inquiry)0,
226 NULL,
227 NULL,
228 NULL,
229 NULL,
230 NULL,
231 (destructor)0
232 #if PY_VERSION_HEX >= 0x02060000
234 #endif
235 #if PY_VERSION_HEX >= 0x03040000
237 #endif
240 PyRef PyUNO_callable_new (
241 const Reference<XInvocation2> &my_inv,
242 const OUString & methodName,
243 enum ConversionMode mode )
245 PyUNO_callable* self;
247 OSL_ENSURE (my_inv.is(), "XInvocation must be valid");
249 self = PyObject_New (PyUNO_callable, &PyUNO_callable_Type);
250 if (self == NULL)
251 return NULL; //NULL == Error!
253 self->members = new PyUNO_callable_Internals;
254 self->members->xInvocation = my_inv;
255 self->members->methodName = methodName;
256 self->members->mode = mode;
258 return PyRef( reinterpret_cast<PyObject*>(self), SAL_NO_ACQUIRE );
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */