xref: /aoo42x/main/pyuno/source/module/pyuno_impl.hxx (revision 24c56ab9)
1db6edb48SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3db6edb48SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4db6edb48SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5db6edb48SAndrew Rist  * distributed with this work for additional information
6db6edb48SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7db6edb48SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8db6edb48SAndrew Rist  * "License"); you may not use this file except in compliance
9db6edb48SAndrew Rist  * with the License.  You may obtain a copy of the License at
10db6edb48SAndrew Rist  *
11db6edb48SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12db6edb48SAndrew Rist  *
13db6edb48SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14db6edb48SAndrew Rist  * software distributed under the License is distributed on an
15db6edb48SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16db6edb48SAndrew Rist  * KIND, either express or implied.  See the License for the
17db6edb48SAndrew Rist  * specific language governing permissions and limitations
18db6edb48SAndrew Rist  * under the License.
19db6edb48SAndrew Rist  *
20db6edb48SAndrew Rist  *************************************************************/
21db6edb48SAndrew Rist 
22db6edb48SAndrew Rist 
23cdf0e10cSrcweir #ifndef _PYUNO_IMPL_
24cdf0e10cSrcweir #define _PYUNO_IMPL_
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <pyuno/pyuno.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <hash_map>
29cdf0e10cSrcweir #include <hash_set>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <com/sun/star/beans/XIntrospection.hpp>
32cdf0e10cSrcweir #include <com/sun/star/script/XTypeConverter.hpp>
33cdf0e10cSrcweir #include <com/sun/star/script/XInvocation2.hpp>
34cdf0e10cSrcweir #include <com/sun/star/script/XInvocationAdapterFactory2.hpp>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <com/sun/star/reflection/XIdlReflection.hpp>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <com/sun/star/lang/XUnoTunnel.hpp>
41cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
44cdf0e10cSrcweir #include <cppuhelper/weakref.hxx>
45cdf0e10cSrcweir 
4677dc4149SPedro Giffuni //
4777dc4149SPedro Giffuni // Local workarounds for compatibility issues
4877dc4149SPedro Giffuni //
4977dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
5077dc4149SPedro Giffuni     #define PYSTR_FROMSTR               PyUnicode_FromString
5177dc4149SPedro Giffuni     #define USTR_TO_PYSTR               ustring2PyUnicode
5277dc4149SPedro Giffuni     #define PYSTR_CHECK                 PyUnicode_Check
5377dc4149SPedro Giffuni #else
5477dc4149SPedro Giffuni     #define PYSTR_FROMSTR               PyBytes_FromString
5577dc4149SPedro Giffuni     #define USTR_TO_PYSTR               ustring2PyString
5677dc4149SPedro Giffuni     #define PYSTR_CHECK                 PyBytes_Check
5777dc4149SPedro Giffuni #endif
5877dc4149SPedro Giffuni 
59*24c56ab9SHerbert Dürr #include <rtl/string.hxx>
PyErr_SetString(PyObject * pyObj,const rtl::OString & rName)60*24c56ab9SHerbert Dürr inline void PyErr_SetString( PyObject* pyObj, const rtl::OString& rName) { PyErr_SetString( pyObj, rName.getStr());}
61*24c56ab9SHerbert Dürr 
62cdf0e10cSrcweir namespace pyuno
63cdf0e10cSrcweir {
64cdf0e10cSrcweir 
65cdf0e10cSrcweir //--------------------------------------------------
66cdf0e10cSrcweir // Logging API - implementation can be found in pyuno_util
67cdf0e10cSrcweir //--------------------------------------------------
68cdf0e10cSrcweir struct RuntimeCargo;
69cdf0e10cSrcweir namespace LogLevel
70cdf0e10cSrcweir {
71cdf0e10cSrcweir // when you add a loglevel, extend the log function !
72cdf0e10cSrcweir static const sal_Int32 NONE = 0;
73cdf0e10cSrcweir static const sal_Int32 CALL = 1;
74cdf0e10cSrcweir static const sal_Int32 ARGS = 2;
75cdf0e10cSrcweir }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir bool isLog( RuntimeCargo *cargo, sal_Int32 loglevel );
78cdf0e10cSrcweir void log( RuntimeCargo *cargo, sal_Int32 level, const rtl::OUString &logString );
79cdf0e10cSrcweir void log( RuntimeCargo *cargo, sal_Int32 level, const char *str );
80cdf0e10cSrcweir void logCall( RuntimeCargo *cargo, const char *intro,
81cdf0e10cSrcweir               void * ptr, const rtl::OUString & aFunctionName,
82cdf0e10cSrcweir               const com::sun::star::uno::Sequence< com::sun::star::uno::Any > & args );
83cdf0e10cSrcweir void logReply( RuntimeCargo *cargo, const char *intro,
84cdf0e10cSrcweir               void * ptr, const rtl::OUString & aFunctionName,
85cdf0e10cSrcweir               const com::sun::star::uno::Any &returnValue,
86cdf0e10cSrcweir               const com::sun::star::uno::Sequence< com::sun::star::uno::Any > & args );
87cdf0e10cSrcweir void logException( RuntimeCargo *cargo, const char *intro,
88cdf0e10cSrcweir                    void * ptr, const rtl::OUString &aFunctionName,
89cdf0e10cSrcweir                    const void * data, const com::sun::star::uno::Type & type );
90cdf0e10cSrcweir static const sal_Int32 VAL2STR_MODE_DEEP = 0;
91cdf0e10cSrcweir static const sal_Int32 VAL2STR_MODE_SHALLOW = 1;
92cdf0e10cSrcweir rtl::OUString val2str( const void * pVal, typelib_TypeDescriptionReference * pTypeRef, sal_Int32 mode = VAL2STR_MODE_DEEP ) SAL_THROW( () );
93cdf0e10cSrcweir //--------------------------------------------------
94cdf0e10cSrcweir 
95cdf0e10cSrcweir typedef ::std::hash_map
96cdf0e10cSrcweir <
97cdf0e10cSrcweir     PyRef,
98cdf0e10cSrcweir     com::sun::star::uno::WeakReference< com::sun::star::script::XInvocation >,
99cdf0e10cSrcweir     PyRef::Hash,
100cdf0e10cSrcweir     std::equal_to< PyRef >
101cdf0e10cSrcweir > PyRef2Adapter;
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 
104cdf0e10cSrcweir typedef ::std::hash_map
105cdf0e10cSrcweir <
106cdf0e10cSrcweir rtl::OUString,
107cdf0e10cSrcweir PyRef,
108cdf0e10cSrcweir rtl::OUStringHash,
109cdf0e10cSrcweir std::equal_to<rtl::OUString>
110cdf0e10cSrcweir > ExceptionClassMap;
111cdf0e10cSrcweir 
112cdf0e10cSrcweir typedef ::std::hash_map
113cdf0e10cSrcweir <
114cdf0e10cSrcweir     rtl::OUString,
115cdf0e10cSrcweir     com::sun::star::uno::Sequence< sal_Int16 >,
116cdf0e10cSrcweir     rtl::OUStringHash,
117cdf0e10cSrcweir     std::equal_to< rtl::OUString >
118cdf0e10cSrcweir > MethodOutIndexMap;
119cdf0e10cSrcweir 
120cdf0e10cSrcweir typedef ::std::hash_set< PyRef , PyRef::Hash , std::equal_to<PyRef> > ClassSet;
121cdf0e10cSrcweir 
122cdf0e10cSrcweir PyObject* PyUNO_new(
123cdf0e10cSrcweir     const com::sun::star::uno::Any & targetInterface,
124cdf0e10cSrcweir     const com::sun::star::uno::Reference<com::sun::star::lang::XSingleServiceFactory> & ssf);
125cdf0e10cSrcweir 
126cdf0e10cSrcweir PyObject* PyUNO_new_UNCHECKED (
127cdf0e10cSrcweir     const com::sun::star::uno::Any & targetInterface,
128cdf0e10cSrcweir     const com::sun::star::uno::Reference<com::sun::star::lang::XSingleServiceFactory> & ssf);
129cdf0e10cSrcweir 
130cdf0e10cSrcweir typedef struct
131cdf0e10cSrcweir {
132cdf0e10cSrcweir     com::sun::star::uno::Reference <com::sun::star::script::XInvocation2> xInvocation;
133cdf0e10cSrcweir     com::sun::star::uno::Any wrappedObject;
134cdf0e10cSrcweir } PyUNOInternals;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir typedef struct
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     PyObject_HEAD
139cdf0e10cSrcweir     PyUNOInternals* members;
140cdf0e10cSrcweir } PyUNO;
141cdf0e10cSrcweir 
142cdf0e10cSrcweir PyRef ustring2PyUnicode( const rtl::OUString &source );
143cdf0e10cSrcweir PyRef ustring2PyString( const ::rtl::OUString & source );
144cdf0e10cSrcweir rtl::OUString pyString2ustring( PyObject *str );
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 
147cdf0e10cSrcweir PyRef AnyToPyObject (const com::sun::star::uno::Any & a, const Runtime &r )
148cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
149cdf0e10cSrcweir 
150cdf0e10cSrcweir com::sun::star::uno::Any PyObjectToAny (PyObject* o)
151cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
152cdf0e10cSrcweir 
153cdf0e10cSrcweir void raiseInvocationTargetExceptionWhenNeeded( const Runtime &runtime )
154cdf0e10cSrcweir     throw ( com::sun::star::reflection::InvocationTargetException );
155cdf0e10cSrcweir 
156cdf0e10cSrcweir // bool CheckPyObjectTypes (PyObject* o, Sequence<Type> types);
157cdf0e10cSrcweir // bool CheckPyObjectType (PyObject* o, Type type); //Only check 1 object.
158cdf0e10cSrcweir 
159cdf0e10cSrcweir com::sun::star::uno::TypeClass StringToTypeClass (char* string);
160cdf0e10cSrcweir 
161cdf0e10cSrcweir PyRef PyUNO_callable_new (
162cdf0e10cSrcweir     const com::sun::star::uno::Reference<com::sun::star::script::XInvocation2> &xInv,
163cdf0e10cSrcweir     const rtl::OUString &methodName,
164cdf0e10cSrcweir     ConversionMode mode = REJECT_UNO_ANY );
165cdf0e10cSrcweir 
166cdf0e10cSrcweir PyObject* PyUNO_Type_new (const char *typeName , com::sun::star::uno::TypeClass t , const Runtime &r );
167cdf0e10cSrcweir PyObject* PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r );
168cdf0e10cSrcweir PyObject* PyUNO_char_new (sal_Unicode c , const Runtime &r);
169cdf0e10cSrcweir PyObject *PyUNO_ByteSequence_new( const com::sun::star::uno::Sequence< sal_Int8 > &, const Runtime &r );
170cdf0e10cSrcweir 
171cdf0e10cSrcweir PyObject *importToGlobal( PyObject *typeName, PyObject *dict, PyObject *targetName );
172cdf0e10cSrcweir 
173cdf0e10cSrcweir PyRef getTypeClass( const Runtime &);
174cdf0e10cSrcweir PyRef getEnumClass( const Runtime &);
175cdf0e10cSrcweir PyRef getBoolClass( const Runtime &);
176cdf0e10cSrcweir PyRef getCharClass( const Runtime &);
177cdf0e10cSrcweir PyRef getByteSequenceClass( const Runtime & );
17877dc4149SPedro Giffuni PyRef getPyUnoClass();
179cdf0e10cSrcweir PyRef getClass( const rtl::OUString & name , const Runtime & runtime );
180cdf0e10cSrcweir PyRef getAnyClass( const Runtime &);
181cdf0e10cSrcweir PyObject *PyUNO_invoke( PyObject *object, const char *name , PyObject *args );
182cdf0e10cSrcweir 
183cdf0e10cSrcweir com::sun::star::uno::Any PyEnum2Enum( PyObject *obj )
184cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
185cdf0e10cSrcweir sal_Bool PyBool2Bool( PyObject *o, const Runtime & r )
186cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
187cdf0e10cSrcweir sal_Unicode PyChar2Unicode( PyObject *o )
188cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
189cdf0e10cSrcweir com::sun::star::uno::Type PyType2Type( PyObject * o )
190cdf0e10cSrcweir     throw( com::sun::star::uno::RuntimeException );
191cdf0e10cSrcweir 
192cdf0e10cSrcweir void raisePyExceptionWithAny( const com::sun::star::uno::Any &a );
193cdf0e10cSrcweir const char *typeClassToString( com::sun::star::uno::TypeClass t );
194cdf0e10cSrcweir 
195cdf0e10cSrcweir PyRef getObjectFromUnoModule( const Runtime &runtime, const char * object )
196cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
197cdf0e10cSrcweir 
198cdf0e10cSrcweir sal_Bool isInterfaceClass( const Runtime &, PyObject *obj );
199cdf0e10cSrcweir bool isInstanceOfStructOrException( PyObject *obj);
200cdf0e10cSrcweir com::sun::star::uno::Sequence<com::sun::star::uno::Type> implementsInterfaces(
201cdf0e10cSrcweir     const Runtime & runtime, PyObject *obj );
202cdf0e10cSrcweir 
203cdf0e10cSrcweir struct RuntimeCargo
204cdf0e10cSrcweir {
205cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::lang::XSingleServiceFactory > xInvocation;
206cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::script::XTypeConverter> xTypeConverter;
207cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > xContext;
208cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::reflection::XIdlReflection > xCoreReflection;
209cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::container::XHierarchicalNameAccess > xTdMgr;
210cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::script::XInvocationAdapterFactory2 > xAdapterFactory;
211cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::beans::XIntrospection > xIntrospection;
212cdf0e10cSrcweir     PyRef dictUnoModule;
213cdf0e10cSrcweir     bool valid;
214cdf0e10cSrcweir     ExceptionClassMap exceptionMap;
215cdf0e10cSrcweir     ClassSet interfaceSet;
216cdf0e10cSrcweir     PyRef2Adapter mappedObjects;
217cdf0e10cSrcweir     FILE *logFile;
218cdf0e10cSrcweir     sal_Int32 logLevel;
219cdf0e10cSrcweir 
220cdf0e10cSrcweir     PyRef getUnoModule();
221cdf0e10cSrcweir };
222cdf0e10cSrcweir 
223cdf0e10cSrcweir struct stRuntimeImpl
224cdf0e10cSrcweir {
225cdf0e10cSrcweir     PyObject_HEAD
226cdf0e10cSrcweir     struct RuntimeCargo *cargo;
227cdf0e10cSrcweir public:
228cdf0e10cSrcweir     static void del( PyObject *self );
229cdf0e10cSrcweir 
230cdf0e10cSrcweir     static PyRef create(
231cdf0e10cSrcweir         const com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > & xContext )
232cdf0e10cSrcweir         throw ( com::sun::star::uno::RuntimeException );
233cdf0e10cSrcweir };
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 
236cdf0e10cSrcweir class Adapter : public cppu::WeakImplHelper2<
237cdf0e10cSrcweir     com::sun::star::script::XInvocation, com::sun::star::lang::XUnoTunnel >
238cdf0e10cSrcweir {
239cdf0e10cSrcweir     PyRef mWrappedObject;
240cdf0e10cSrcweir     PyInterpreterState *mInterpreter;  // interpreters don't seem to be refcounted !
241cdf0e10cSrcweir     com::sun::star::uno::Sequence< com::sun::star::uno::Type > mTypes;
242cdf0e10cSrcweir     MethodOutIndexMap m_methodOutIndexMap;
243cdf0e10cSrcweir 
244cdf0e10cSrcweir private:
245cdf0e10cSrcweir     com::sun::star::uno::Sequence< sal_Int16 > getOutIndexes( const rtl::OUString & functionName );
246cdf0e10cSrcweir 
247cdf0e10cSrcweir public:
248cdf0e10cSrcweir public:
249cdf0e10cSrcweir     Adapter( const PyRef &obj,
250cdf0e10cSrcweir              const com::sun::star::uno::Sequence< com::sun::star::uno::Type > & types );
251cdf0e10cSrcweir 
252cdf0e10cSrcweir     static com::sun::star::uno::Sequence< sal_Int8 > getUnoTunnelImplementationId();
getWrappedObject()253cdf0e10cSrcweir     PyRef getWrappedObject() { return mWrappedObject; }
getWrappedTypes()254cdf0e10cSrcweir     com::sun::star::uno::Sequence< com::sun::star::uno::Type > getWrappedTypes() { return mTypes; }
255cdf0e10cSrcweir     virtual ~Adapter();
256cdf0e10cSrcweir 
257cdf0e10cSrcweir     // XInvocation
258cdf0e10cSrcweir     virtual com::sun::star::uno::Reference< ::com::sun::star::beans::XIntrospectionAccess >
259cdf0e10cSrcweir            SAL_CALL getIntrospection(  ) throw (::com::sun::star::uno::RuntimeException);
260cdf0e10cSrcweir     virtual ::com::sun::star::uno::Any SAL_CALL invoke(
261cdf0e10cSrcweir         const ::rtl::OUString& aFunctionName,
262cdf0e10cSrcweir         const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aParams,
263cdf0e10cSrcweir         ::com::sun::star::uno::Sequence< sal_Int16 >& aOutParamIndex,
264cdf0e10cSrcweir         ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aOutParam )
265cdf0e10cSrcweir         throw (::com::sun::star::lang::IllegalArgumentException,
266cdf0e10cSrcweir                ::com::sun::star::script::CannotConvertException,
267cdf0e10cSrcweir                ::com::sun::star::reflection::InvocationTargetException,
268cdf0e10cSrcweir                ::com::sun::star::uno::RuntimeException);
269cdf0e10cSrcweir 
270cdf0e10cSrcweir     virtual void SAL_CALL setValue(
271cdf0e10cSrcweir         const ::rtl::OUString& aPropertyName,
272cdf0e10cSrcweir         const ::com::sun::star::uno::Any& aValue )
273cdf0e10cSrcweir         throw (::com::sun::star::beans::UnknownPropertyException,
274cdf0e10cSrcweir                ::com::sun::star::script::CannotConvertException,
275cdf0e10cSrcweir                ::com::sun::star::reflection::InvocationTargetException,
276cdf0e10cSrcweir                ::com::sun::star::uno::RuntimeException);
277cdf0e10cSrcweir 
278cdf0e10cSrcweir     virtual ::com::sun::star::uno::Any SAL_CALL getValue( const ::rtl::OUString& aPropertyName )
279cdf0e10cSrcweir         throw (::com::sun::star::beans::UnknownPropertyException,
280cdf0e10cSrcweir                ::com::sun::star::uno::RuntimeException);
281cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasMethod( const ::rtl::OUString& aName )
282cdf0e10cSrcweir         throw (::com::sun::star::uno::RuntimeException);
283cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasProperty( const ::rtl::OUString& aName )
284cdf0e10cSrcweir         throw (::com::sun::star::uno::RuntimeException);
285cdf0e10cSrcweir 
286cdf0e10cSrcweir     // XUnoTunnel
287cdf0e10cSrcweir     virtual sal_Int64 SAL_CALL getSomething(
288cdf0e10cSrcweir         const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier )
289cdf0e10cSrcweir         throw (::com::sun::star::uno::RuntimeException);
290cdf0e10cSrcweir };
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 
293cdf0e10cSrcweir /** releases a refcount on the interpreter object and on another given python object.
294cdf0e10cSrcweir 
295cdf0e10cSrcweir    The function can be called from any thread regardless of whether the global
296cdf0e10cSrcweir    interpreter lock is held.
297cdf0e10cSrcweir 
298cdf0e10cSrcweir  */
299cdf0e10cSrcweir void decreaseRefCount( PyInterpreterState *interpreter, PyObject *object );
300cdf0e10cSrcweir 
301cdf0e10cSrcweir }
302cdf0e10cSrcweir 
303cdf0e10cSrcweir #endif
304