LibreOffice Module pyuno (master) 1
pyuno_impl.hxx
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#pragma once
20
21#if defined(_MSC_VER)
22// Workaround for some horrible hypot() mess
23#include <math.h>
24#endif
25
26#include <Python.h>
27
28//Python 3.0 and newer don't have these flags
29#ifndef Py_TPFLAGS_HAVE_ITER
30# define Py_TPFLAGS_HAVE_ITER 0
31#endif
32#ifndef Py_TPFLAGS_HAVE_RICHCOMPARE
33# define Py_TPFLAGS_HAVE_RICHCOMPARE 0
34#endif
35#ifndef Py_TPFLAGS_HAVE_SEQUENCE_IN
36# define Py_TPFLAGS_HAVE_SEQUENCE_IN 0
37#endif
38
39#include <pyuno.hxx>
40
41#include <string_view>
42#include <unordered_map>
43#include <unordered_set>
44
45#include <com/sun/star/container/XIndexAccess.hpp>
46#include <com/sun/star/lang/XUnoTunnel.hpp>
47#include <com/sun/star/script/XInvocation.hpp>
48
51
52#include <osl/module.hxx>
53
54namespace com::sun::star::beans { class XIntrospection; }
55namespace com::sun::star::container { class XEnumeration; }
56namespace com::sun::star::container { class XHierarchicalNameAccess; }
57namespace com::sun::star::lang { class XSingleServiceFactory; }
58namespace com::sun::star::reflection { class XIdlReflection; }
59namespace com::sun::star::script { class XInvocation2; }
60namespace com::sun::star::script { class XInvocationAdapterFactory2; }
61namespace com::sun::star::script { class XTypeConverter; }
62
63namespace pyuno
64{
65
66
67// Logging API - implementation can be found in pyuno_util
68
69struct RuntimeCargo;
70namespace LogLevel
71{
72// when you add a loglevel, extend the log function !
73const sal_Int32 NONE = 0;
74const sal_Int32 CALL = 1;
75const sal_Int32 ARGS = 2;
76}
77
78bool isLog( RuntimeCargo const *cargo, sal_Int32 loglevel );
79void log( RuntimeCargo *cargo, sal_Int32 level, std::u16string_view logString );
80void log( RuntimeCargo *cargo, sal_Int32 level, const char *str );
81void logCall( RuntimeCargo *cargo, const char *intro,
82 void * ptr, std::u16string_view aFunctionName,
83 const css::uno::Sequence< css::uno::Any > & args );
84void logReply( RuntimeCargo *cargo, const char *intro,
85 void * ptr, std::u16string_view aFunctionName,
86 const css::uno::Any &returnValue,
87 const css::uno::Sequence< css::uno::Any > & args );
88void logException( RuntimeCargo *cargo, const char *intro,
89 void * ptr, std::u16string_view aFunctionName,
90 const void * data, const css::uno::Type & type );
91const sal_Int32 VAL2STR_MODE_DEEP = 0;
92const sal_Int32 VAL2STR_MODE_SHALLOW = 1;
93OUString val2str( const void * pVal, typelib_TypeDescriptionReference * pTypeRef, sal_Int32 mode = VAL2STR_MODE_DEEP );
94
95
96typedef std::unordered_map
97<
98 PyRef,
99 css::uno::WeakReference< css::script::XInvocation >,
102
103
104typedef std::unordered_map
105<
106 OUString,
107 PyRef
109
110typedef std::unordered_map
111<
112 OUString,
113 css::uno::Sequence< sal_Int16 >
115
116typedef std::unordered_set< PyRef , PyRef::Hash > ClassSet;
117
118int PyUNO_initType();
120
122 const css::uno::Any & targetInterface,
123 const css::uno::Reference<css::lang::XSingleServiceFactory> & ssf );
124
126 const css::uno::Any &targetInterface,
127 const css::uno::Reference<css::lang::XSingleServiceFactory> &ssf );
128
130{
131 css::uno::Reference <css::script::XInvocation2> xInvocation;
132 css::uno::Any wrappedObject;
133};
134
135typedef struct
136{
137 PyObject_HEAD
139} PyUNO;
140
142 const css::uno::Reference<css::container::XEnumeration>& xEnumeration);
143
145{
146 css::uno::Reference <css::container::XEnumeration> xEnumeration;
147};
148
149typedef struct
150{
151 PyObject_HEAD
154
156 const css::uno::Reference<css::container::XIndexAccess> &xIndexAccess);
157
159{
160 css::uno::Reference <css::container::XIndexAccess> xIndexAccess;
161 int index;
162};
163
164typedef struct
165{
166 PyObject_HEAD
169
170PyRef ustring2PyUnicode( const OUString &source );
171PyRef ustring2PyString( std::u16string_view source );
172OUString pyString2ustring( PyObject *str );
173
177
179 const css::uno::Reference<css::script::XInvocation2> &xInv,
180 const OUString &methodName,
182
183PyObject* PyUNO_Type_new (const char *typeName , css::uno::TypeClass t , const Runtime &r );
184PyObject* PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r );
185PyObject* PyUNO_char_new (sal_Unicode c , const Runtime &r);
186PyObject *PyUNO_ByteSequence_new( const css::uno::Sequence< sal_Int8 > &, const Runtime &r );
187
188PyRef getTypeClass( const Runtime &);
189PyRef getEnumClass( const Runtime &);
190PyRef getCharClass( const Runtime &);
194PyRef getClass( const OUString & name , const Runtime & runtime );
195PyRef getAnyClass( const Runtime &);
196PyObject *PyUNO_invoke( PyObject *object, const char *name , PyObject *args );
197
199css::uno::Any PyEnum2Enum( PyObject *obj );
201sal_Unicode PyChar2Unicode( PyObject *o );
203css::uno::Type PyType2Type( PyObject * o );
204
205void raisePyExceptionWithAny( const css::uno::Any &a );
206const char *typeClassToString( css::uno::TypeClass t );
207
209PyRef getObjectFromUnoModule( const Runtime &runtime, const char * object );
210
211bool isInterfaceClass( const Runtime &, PyObject *obj );
212bool isInstanceOfStructOrException( PyObject *obj);
213
215{
216 css::uno::Reference< css::lang::XSingleServiceFactory > xInvocation;
217 css::uno::Reference< css::script::XTypeConverter> xTypeConverter;
218 css::uno::Reference< css::uno::XComponentContext > xContext;
219 css::uno::Reference< css::reflection::XIdlReflection > xCoreReflection;
220 css::uno::Reference< css::container::XHierarchicalNameAccess > xTdMgr;
221 css::uno::Reference< css::script::XInvocationAdapterFactory2 > xAdapterFactory;
222 css::uno::Reference< css::beans::XIntrospection > xIntrospection;
224 osl::Module testModule;
225 bool valid;
230 sal_Int32 logLevel;
231
232 PyRef const & getUnoModule();
233};
234
236{
237 PyObject_HEAD
239public:
240 static void del( PyObject *self );
241
243 static PyRef create(
244 const css::uno::Reference< css::uno::XComponentContext > & xContext );
245};
246
247
248class Adapter : public cppu::WeakImplHelper<
249 css::script::XInvocation, css::lang::XUnoTunnel >
250{
252 PyInterpreterState *mInterpreter; // interpreters don't seem to be refcounted !
253 css::uno::Sequence< css::uno::Type > mTypes;
255
256private:
257 css::uno::Sequence< sal_Int16 > getOutIndexes( const OUString & functionName );
258
259public:
260public:
261 Adapter( PyRef obj,
262 const css::uno::Sequence< css::uno::Type > & types );
263
264 static const css::uno::Sequence< sal_Int8 > & getUnoTunnelId();
265 const PyRef& getWrappedObject() const { return mWrappedObject; }
266 const css::uno::Sequence< css::uno::Type >& getWrappedTypes() const { return mTypes; }
267 virtual ~Adapter() override;
268
269 // XInvocation
270 virtual css::uno::Reference< css::beans::XIntrospectionAccess >
271 SAL_CALL getIntrospection( ) override;
272 virtual css::uno::Any SAL_CALL invoke(
273 const OUString& aFunctionName,
274 const css::uno::Sequence< css::uno::Any >& aParams,
275 css::uno::Sequence< sal_Int16 >& aOutParamIndex,
276 css::uno::Sequence< css::uno::Any >& aOutParam ) override;
277
278 virtual void SAL_CALL setValue(
279 const OUString& aPropertyName,
280 const css::uno::Any& aValue ) override;
281
282 virtual css::uno::Any SAL_CALL getValue( const OUString& aPropertyName ) override;
283 virtual sal_Bool SAL_CALL hasMethod( const OUString& aName ) override;
284 virtual sal_Bool SAL_CALL hasProperty( const OUString& aName ) override;
285
286 // XUnoTunnel
287 virtual sal_Int64 SAL_CALL getSomething(
288 const css::uno::Sequence< sal_Int8 >& aIdentifier ) override;
289};
290
291
298void decreaseRefCount( PyInterpreterState *interpreter, PyObject *object );
299
300}
301
302/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const PyRef & getWrappedObject() const
Definition: pyuno_impl.hxx:265
virtual css::uno::Reference< css::beans::XIntrospectionAccess > SAL_CALL getIntrospection() override
Adapter(PyRef obj, const css::uno::Sequence< css::uno::Type > &types)
MethodOutIndexMap m_methodOutIndexMap
Definition: pyuno_impl.hxx:254
virtual css::uno::Any SAL_CALL getValue(const OUString &aPropertyName) override
virtual ~Adapter() override
virtual void SAL_CALL setValue(const OUString &aPropertyName, const css::uno::Any &aValue) override
virtual sal_Bool SAL_CALL hasProperty(const OUString &aName) override
css::uno::Sequence< sal_Int16 > getOutIndexes(const OUString &functionName)
virtual css::uno::Any SAL_CALL invoke(const OUString &aFunctionName, const css::uno::Sequence< css::uno::Any > &aParams, css::uno::Sequence< sal_Int16 > &aOutParamIndex, css::uno::Sequence< css::uno::Any > &aOutParam) override
virtual sal_Bool SAL_CALL hasMethod(const OUString &aName) override
PyRef mWrappedObject
Definition: pyuno_impl.hxx:251
PyInterpreterState * mInterpreter
Definition: pyuno_impl.hxx:252
static const css::uno::Sequence< sal_Int8 > & getUnoTunnelId()
const css::uno::Sequence< css::uno::Type > & getWrappedTypes() const
Definition: pyuno_impl.hxx:266
css::uno::Sequence< css::uno::Type > mTypes
Definition: pyuno_impl.hxx:253
virtual sal_Int64 SAL_CALL getSomething(const css::uno::Sequence< sal_Int8 > &aIdentifier) override
Helper class for keeping references to python objects.
Definition: pyuno.hxx:80
The pyuno::Runtime class keeps the internal state of the python UNO bridge for the currently in use p...
Definition: pyuno.hxx:164
const sal_Int32 NONE
Definition: pyuno_impl.hxx:73
const sal_Int32 CALL
Definition: pyuno_impl.hxx:74
const sal_Int32 ARGS
Definition: pyuno_impl.hxx:75
Definition: pyuno.cxx:72
void log(RuntimeCargo *cargo, sal_Int32 level, std::u16string_view logString)
Definition: pyuno_util.cxx:98
void raiseInvocationTargetExceptionWhenNeeded(const Runtime &runtime)
PyObject * PyUNO_Type_new(const char *typeName, css::uno::TypeClass t, const Runtime &r)
bool isLog(RuntimeCargo const *cargo, sal_Int32 loglevel)
Definition: pyuno_util.cxx:93
bool isInstanceOfStructOrException(PyObject *obj)
void logCall(RuntimeCargo *cargo, const char *intro, void *ptr, std::u16string_view aFunctionName, const css::uno::Sequence< css::uno::Any > &args)
PyObject * PyUNO_list_iterator_new(const css::uno::Reference< css::container::XIndexAccess > &xIndexAccess)
std::unordered_map< PyRef, css::uno::WeakReference< css::script::XInvocation >, PyRef::Hash > PyRef2Adapter
Definition: pyuno_impl.hxx:101
PyRef PyUNO_new(const Any &targetInterface, const Reference< XSingleServiceFactory > &ssf)
Definition: pyuno.cxx:1684
const sal_Int32 VAL2STR_MODE_DEEP
Definition: pyuno_impl.hxx:91
ConversionMode
Definition: pyuno.hxx:153
@ REJECT_UNO_ANY
Definition: pyuno.hxx:153
PyRef getCharClass(const Runtime &)
Definition: pyuno_type.cxx:110
PyRef getPyUnoStructClass()
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
int PyUNOStruct_initType()
PyRef ustring2PyString(std::u16string_view source)
Definition: pyuno_util.cxx:51
const char * typeClassToString(css::uno::TypeClass t)
std::unordered_map< OUString, css::uno::Sequence< sal_Int16 > > MethodOutIndexMap
Definition: pyuno_impl.hxx:114
sal_Unicode PyChar2Unicode(PyObject *o)
Definition: pyuno_type.cxx:126
void decreaseRefCount(PyInterpreterState *interpreter, PyObject *object)
releases a refcount on the interpreter object and on another given python object.
Definition: pyuno_gc.cxx:104
PyObject * PyUNO_Enum_new(const char *enumBase, const char *enumValue, const Runtime &r)
Definition: pyuno_type.cxx:240
void raisePyExceptionWithAny(const css::uno::Any &anyExc)
PyRef getTypeClass(const Runtime &)
Definition: pyuno_type.cxx:100
PyObject * PyUNO_iterator_new(const css::uno::Reference< css::container::XEnumeration > &xEnumeration)
PyRef getEnumClass(const Runtime &)
Definition: pyuno_type.cxx:105
std::unordered_set< PyRef, PyRef::Hash > ClassSet
Definition: pyuno_impl.hxx:116
css::uno::Type PyType2Type(PyObject *o)
Definition: pyuno_type.cxx:195
bool isInterfaceClass(const Runtime &runtime, PyObject *obj)
OUString val2str(const void *pVal, typelib_TypeDescriptionReference *pTypeRef, sal_Int32 mode)
Definition: pyuno.cxx:87
PyRef getByteSequenceClass(const Runtime &)
Definition: pyuno_type.cxx:115
void logException(RuntimeCargo *cargo, const char *intro, void *ptr, std::u16string_view aFunctionName, const void *data, const css::uno::Type &type)
Definition: pyuno_util.cxx:145
int PyUNO_initType()
Definition: pyuno.cxx:1674
OUString pyString2ustring(PyObject *str)
Definition: pyuno_util.cxx:57
PyObject * PyUNO_invoke(PyObject *object, const char *name, PyObject *args)
Definition: pyuno.cxx:379
PyRef PyUNO_callable_new(const Reference< XInvocation2 > &my_inv, const OUString &methodName, enum ConversionMode mode)
std::unordered_map< OUString, PyRef > ExceptionClassMap
Definition: pyuno_impl.hxx:108
PyRef getObjectFromUnoModule(const Runtime &runtime, const char *object)
Definition: pyuno_util.cxx:79
PyRef ustring2PyUnicode(const OUString &source)
Definition: pyuno_util.cxx:34
PyRef getClass(const OUString &name, const Runtime &runtime)
void logReply(RuntimeCargo *cargo, const char *intro, void *ptr, std::u16string_view aFunctionName, const css::uno::Any &returnValue, const css::uno::Sequence< css::uno::Any > &args)
css::uno::Any PyEnum2Enum(PyObject *obj)
Definition: pyuno_type.cxx:145
PyRef getAnyClass(const Runtime &)
Definition: pyuno_type.cxx:120
PyRef PyUNOStruct_new(const css::uno::Any &targetInterface, const css::uno::Reference< css::lang::XSingleServiceFactory > &ssf)
PyRef getPyUnoClass()
Definition: pyuno.cxx:1679
const sal_Int32 VAL2STR_MODE_SHALLOW
Definition: pyuno_impl.hxx:92
ConversionMode mode
OUString methodName
css::uno::Any wrappedObject
Definition: pyuno_impl.hxx:132
css::uno::Reference< css::script::XInvocation2 > xInvocation
Definition: pyuno_impl.hxx:131
css::uno::Reference< css::container::XEnumeration > xEnumeration
Definition: pyuno_impl.hxx:146
PyObject_HEAD PyUNO_iterator_Internals * members
Definition: pyuno_impl.hxx:152
css::uno::Reference< css::container::XIndexAccess > xIndexAccess
Definition: pyuno_impl.hxx:160
PyObject_HEAD PyUNO_list_iterator_Internals * members
Definition: pyuno_impl.hxx:167
PyObject_HEAD PyUNOInternals * members
Definition: pyuno_impl.hxx:138
css::uno::Reference< css::script::XTypeConverter > xTypeConverter
Definition: pyuno_impl.hxx:217
osl::Module testModule
Definition: pyuno_impl.hxx:224
css::uno::Reference< css::reflection::XIdlReflection > xCoreReflection
Definition: pyuno_impl.hxx:219
css::uno::Reference< css::lang::XSingleServiceFactory > xInvocation
Definition: pyuno_impl.hxx:216
css::uno::Reference< css::container::XHierarchicalNameAccess > xTdMgr
Definition: pyuno_impl.hxx:220
PyRef const & getUnoModule()
PyRef2Adapter mappedObjects
Definition: pyuno_impl.hxx:228
css::uno::Reference< css::beans::XIntrospection > xIntrospection
Definition: pyuno_impl.hxx:222
css::uno::Reference< css::script::XInvocationAdapterFactory2 > xAdapterFactory
Definition: pyuno_impl.hxx:221
ExceptionClassMap exceptionMap
Definition: pyuno_impl.hxx:226
css::uno::Reference< css::uno::XComponentContext > xContext
Definition: pyuno_impl.hxx:218
static PyRef create(const css::uno::Reference< css::uno::XComponentContext > &xContext)
PyObject_HEAD struct RuntimeCargo * cargo
Definition: pyuno_impl.hxx:238
static void del(PyObject *self)
unsigned char sal_Bool
sal_uInt16 sal_Unicode