tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / pyuno / source / module / pyuno_impl.hxx
blob878367d3764afdf34fc8f2ce28f71d3bb6c9c72b
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 #pragma once
21 #if defined(_MSC_VER)
22 // Workaround for some horrible hypot() mess
23 #include <math.h>
24 #endif
26 #include <Python.h>
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
39 #include <pyuno.hxx>
41 #include <string_view>
42 #include <unordered_map>
43 #include <unordered_set>
45 #include <com/sun/star/container/XIndexAccess.hpp>
46 #include <com/sun/star/lang/XUnoTunnel.hpp>
47 #include <com/sun/star/script/XInvocation.hpp>
49 #include <cppuhelper/implbase.hxx>
50 #include <cppuhelper/weakref.hxx>
52 #include <osl/module.hxx>
54 namespace com::sun::star::beans { class XIntrospection; }
55 namespace com::sun::star::container { class XEnumeration; }
56 namespace com::sun::star::container { class XHierarchicalNameAccess; }
57 namespace com::sun::star::lang { class XSingleServiceFactory; }
58 namespace com::sun::star::reflection { class XIdlReflection; }
59 namespace com::sun::star::script { class XInvocation2; }
60 namespace com::sun::star::script { class XInvocationAdapterFactory2; }
61 namespace com::sun::star::script { class XTypeConverter; }
63 namespace pyuno
67 // Logging API - implementation can be found in pyuno_util
69 struct RuntimeCargo;
70 namespace LogLevel
72 // when you add a loglevel, extend the log function !
73 const sal_Int32 NONE = 0;
74 const sal_Int32 CALL = 1;
75 const sal_Int32 ARGS = 2;
78 bool isLog( RuntimeCargo const *cargo, sal_Int32 loglevel );
79 void log( RuntimeCargo *cargo, sal_Int32 level, std::u16string_view logString );
80 void log( RuntimeCargo *cargo, sal_Int32 level, const char *str );
81 void logCall( RuntimeCargo *cargo, const char *intro,
82 void * ptr, std::u16string_view aFunctionName,
83 const css::uno::Sequence< css::uno::Any > & args );
84 void 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 );
88 void logException( RuntimeCargo *cargo, const char *intro,
89 void * ptr, std::u16string_view aFunctionName,
90 const void * data, const css::uno::Type & type );
91 const sal_Int32 VAL2STR_MODE_DEEP = 0;
92 const sal_Int32 VAL2STR_MODE_SHALLOW = 1;
93 OUString val2str( const void * pVal, typelib_TypeDescriptionReference * pTypeRef, sal_Int32 mode = VAL2STR_MODE_DEEP );
96 typedef std::unordered_map
98 PyRef,
99 css::uno::WeakReference< css::script::XInvocation >,
100 PyRef::Hash
101 > PyRef2Adapter;
104 typedef std::unordered_map
106 OUString,
107 PyRef
108 > ExceptionClassMap;
110 typedef std::unordered_map
112 OUString,
113 css::uno::Sequence< sal_Int16 >
114 > MethodOutIndexMap;
116 typedef std::unordered_set< PyRef , PyRef::Hash > ClassSet;
118 int PyUNO_initType();
119 int PyUNOStruct_initType();
121 PyRef PyUNO_new (
122 const css::uno::Any & targetInterface,
123 const css::uno::Reference<css::lang::XSingleServiceFactory> & ssf );
125 PyRef PyUNOStruct_new (
126 const css::uno::Any &targetInterface,
127 const css::uno::Reference<css::lang::XSingleServiceFactory> &ssf );
129 struct PyUNOInternals
131 css::uno::Reference <css::script::XInvocation2> xInvocation;
132 css::uno::Any wrappedObject;
135 typedef struct
137 PyObject_HEAD
138 PyUNOInternals* members;
139 } PyUNO;
141 PyObject* PyUNO_iterator_new (
142 const css::uno::Reference<css::container::XEnumeration>& xEnumeration);
144 struct PyUNO_iterator_Internals
146 css::uno::Reference <css::container::XEnumeration> xEnumeration;
149 typedef struct
151 PyObject_HEAD
152 PyUNO_iterator_Internals* members;
153 } PyUNO_iterator;
155 PyObject* PyUNO_list_iterator_new (
156 const css::uno::Reference<css::container::XIndexAccess> &xIndexAccess);
158 struct PyUNO_list_iterator_Internals
160 css::uno::Reference <css::container::XIndexAccess> xIndexAccess;
161 int index;
164 typedef struct
166 PyObject_HEAD
167 PyUNO_list_iterator_Internals* members;
168 } PyUNO_list_iterator;
170 PyRef ustring2PyUnicode( const OUString &source );
171 PyRef ustring2PyString( std::u16string_view source );
172 OUString pyString2ustring( PyObject *str );
174 /// @throws css::reflection::InvocationTargetException
175 /// @throws css::uno::RuntimeException
176 void raiseInvocationTargetExceptionWhenNeeded( const Runtime &runtime );
178 PyRef PyUNO_callable_new (
179 const css::uno::Reference<css::script::XInvocation2> &xInv,
180 const OUString &methodName,
181 ConversionMode mode = REJECT_UNO_ANY );
183 PyObject* PyUNO_Type_new (const char *typeName , css::uno::TypeClass t , const Runtime &r );
184 PyObject* PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r );
185 PyObject* PyUNO_char_new (sal_Unicode c , const Runtime &r);
186 PyObject *PyUNO_ByteSequence_new( const css::uno::Sequence< sal_Int8 > &, const Runtime &r );
188 PyRef getTypeClass( const Runtime &);
189 PyRef getEnumClass( const Runtime &);
190 PyRef getCharClass( const Runtime &);
191 PyRef getByteSequenceClass( const Runtime & );
192 PyRef getPyUnoClass();
193 PyRef getPyUnoStructClass();
194 PyRef getClass( const OUString & name , const Runtime & runtime );
195 PyRef getAnyClass( const Runtime &);
196 PyObject *PyUNO_invoke( PyObject *object, const char *name , PyObject *args );
198 /// @throws css::uno::RuntimeException
199 css::uno::Any PyEnum2Enum( PyObject *obj );
200 /// @throws css::uno::RuntimeException
201 sal_Unicode PyChar2Unicode( PyObject *o );
202 /// @throws css::uno::RuntimeException
203 css::uno::Type PyType2Type( PyObject * o );
205 void raisePyExceptionWithAny( const css::uno::Any &a );
206 const char *typeClassToString( css::uno::TypeClass t );
208 /// @throws css::uno::RuntimeException
209 PyRef getObjectFromUnoModule( const Runtime &runtime, const char * object );
211 bool isInterfaceClass( const Runtime &, PyObject *obj );
212 bool isInstanceOfStructOrException( PyObject *obj);
214 struct RuntimeCargo
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;
223 PyRef dictUnoModule;
224 osl::Module testModule;
225 osl::Module unoTestModule;
226 bool valid;
227 ExceptionClassMap exceptionMap;
228 ClassSet interfaceSet;
229 PyRef2Adapter mappedObjects;
230 FILE *logFile;
231 sal_Int32 logLevel;
233 PyRef const & getUnoModule();
236 struct stRuntimeImpl
238 PyObject_HEAD
239 struct RuntimeCargo *cargo;
240 public:
241 static void del( PyObject *self );
243 /// @throws css::uno::RuntimeException
244 static PyRef create(
245 const css::uno::Reference< css::uno::XComponentContext > & xContext );
249 class Adapter : public cppu::WeakImplHelper<
250 css::script::XInvocation, css::lang::XUnoTunnel >
252 PyRef mWrappedObject;
253 PyInterpreterState *mInterpreter; // interpreters don't seem to be refcounted !
254 css::uno::Sequence< css::uno::Type > mTypes;
255 MethodOutIndexMap m_methodOutIndexMap;
257 private:
258 css::uno::Sequence< sal_Int16 > getOutIndexes( const OUString & functionName );
260 public:
261 public:
262 Adapter( PyRef obj,
263 const css::uno::Sequence< css::uno::Type > & types );
265 static const css::uno::Sequence< sal_Int8 > & getUnoTunnelId();
266 const PyRef& getWrappedObject() const { return mWrappedObject; }
267 const css::uno::Sequence< css::uno::Type >& getWrappedTypes() const { return mTypes; }
268 virtual ~Adapter() override;
270 // XInvocation
271 virtual css::uno::Reference< css::beans::XIntrospectionAccess >
272 SAL_CALL getIntrospection( ) override;
273 virtual css::uno::Any SAL_CALL invoke(
274 const OUString& aFunctionName,
275 const css::uno::Sequence< css::uno::Any >& aParams,
276 css::uno::Sequence< sal_Int16 >& aOutParamIndex,
277 css::uno::Sequence< css::uno::Any >& aOutParam ) override;
279 virtual void SAL_CALL setValue(
280 const OUString& aPropertyName,
281 const css::uno::Any& aValue ) override;
283 virtual css::uno::Any SAL_CALL getValue( const OUString& aPropertyName ) override;
284 virtual sal_Bool SAL_CALL hasMethod( const OUString& aName ) override;
285 virtual sal_Bool SAL_CALL hasProperty( const OUString& aName ) override;
287 // XUnoTunnel
288 virtual sal_Int64 SAL_CALL getSomething(
289 const css::uno::Sequence< sal_Int8 >& aIdentifier ) override;
293 /** releases a refcount on the interpreter object and on another given python object.
295 The function can be called from any thread regardless of whether the global
296 interpreter lock is held.
299 void decreaseRefCount( PyInterpreterState *interpreter, PyObject *object );
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */