nss: upgrade to release 3.73
[LibreOffice.git] / pyuno / source / module / pyuno_impl.hxx
blob6517884a01ef95f7eddcd08268af88e3c4a3cfd2
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 #ifndef INCLUDED_PYUNO_SOURCE_MODULE_PYUNO_IMPL_HXX
20 #define INCLUDED_PYUNO_SOURCE_MODULE_PYUNO_IMPL_HXX
22 #if defined(_MSC_VER)
23 // Workaround for some horrible hypot() mess
24 #include <math.h>
25 #endif
27 #include <Python.h>
29 //Python 3.0 and newer don't have these flags
30 #ifndef Py_TPFLAGS_HAVE_ITER
31 # define Py_TPFLAGS_HAVE_ITER 0
32 #endif
33 #ifndef Py_TPFLAGS_HAVE_RICHCOMPARE
34 # define Py_TPFLAGS_HAVE_RICHCOMPARE 0
35 #endif
36 #ifndef Py_TPFLAGS_HAVE_SEQUENCE_IN
37 # define Py_TPFLAGS_HAVE_SEQUENCE_IN 0
38 #endif
40 #include <pyuno.hxx>
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, const OUString &logString );
80 void log( RuntimeCargo *cargo, sal_Int32 level, const char *str );
81 void logCall( RuntimeCargo *cargo, const char *intro,
82 void * ptr, const OUString & aFunctionName,
83 const css::uno::Sequence< css::uno::Any > & args );
84 void logReply( RuntimeCargo *cargo, const char *intro,
85 void * ptr, const OUString & 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, const OUString &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( const OUString & 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 bool valid;
226 ExceptionClassMap exceptionMap;
227 ClassSet interfaceSet;
228 PyRef2Adapter mappedObjects;
229 FILE *logFile;
230 sal_Int32 logLevel;
232 PyRef const & getUnoModule();
235 struct stRuntimeImpl
237 PyObject_HEAD
238 struct RuntimeCargo *cargo;
239 public:
240 static void del( PyObject *self );
242 /// @throws css::uno::RuntimeException
243 static PyRef create(
244 const css::uno::Reference< css::uno::XComponentContext > & xContext );
248 class Adapter : public cppu::WeakImplHelper<
249 css::script::XInvocation, css::lang::XUnoTunnel >
251 PyRef mWrappedObject;
252 PyInterpreterState *mInterpreter; // interpreters don't seem to be refcounted !
253 css::uno::Sequence< css::uno::Type > mTypes;
254 MethodOutIndexMap m_methodOutIndexMap;
256 private:
257 css::uno::Sequence< sal_Int16 > getOutIndexes( const OUString & functionName );
259 public:
260 public:
261 Adapter( const PyRef &obj,
262 const css::uno::Sequence< css::uno::Type > & types );
264 static 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;
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;
278 virtual void SAL_CALL setValue(
279 const OUString& aPropertyName,
280 const css::uno::Any& aValue ) override;
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;
286 // XUnoTunnel
287 virtual sal_Int64 SAL_CALL getSomething(
288 const css::uno::Sequence< sal_Int8 >& aIdentifier ) override;
292 /** releases a refcount on the interpreter object and on another given python object.
294 The function can be called from any thread regardless of whether the global
295 interpreter lock is held.
298 void decreaseRefCount( PyInterpreterState *interpreter, PyObject *object );
302 #endif
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */