xref: /aoo41x/main/pyuno/source/module/pyuno_impl.hxx (revision 77dc4149)
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 
46*77dc4149SPedro Giffuni //
47*77dc4149SPedro Giffuni // Local workarounds for compatibility issues
48*77dc4149SPedro Giffuni //
49*77dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
50*77dc4149SPedro Giffuni     #define PYSTR_FROMSTR               PyUnicode_FromString
51*77dc4149SPedro Giffuni     #define USTR_TO_PYSTR               ustring2PyUnicode
52*77dc4149SPedro Giffuni     #define PYSTR_CHECK                 PyUnicode_Check
53*77dc4149SPedro Giffuni #else
54*77dc4149SPedro Giffuni     #define PYSTR_FROMSTR               PyBytes_FromString
55*77dc4149SPedro Giffuni     #define USTR_TO_PYSTR               ustring2PyString
56*77dc4149SPedro Giffuni     #define PYSTR_CHECK                 PyBytes_Check
57*77dc4149SPedro Giffuni #endif
58*77dc4149SPedro Giffuni 
59cdf0e10cSrcweir namespace pyuno
60cdf0e10cSrcweir {
61cdf0e10cSrcweir 
62cdf0e10cSrcweir //--------------------------------------------------
63cdf0e10cSrcweir // Logging API - implementation can be found in pyuno_util
64cdf0e10cSrcweir //--------------------------------------------------
65cdf0e10cSrcweir struct RuntimeCargo;
66cdf0e10cSrcweir namespace LogLevel
67cdf0e10cSrcweir {
68cdf0e10cSrcweir // when you add a loglevel, extend the log function !
69cdf0e10cSrcweir static const sal_Int32 NONE = 0;
70cdf0e10cSrcweir static const sal_Int32 CALL = 1;
71cdf0e10cSrcweir static const sal_Int32 ARGS = 2;
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir bool isLog( RuntimeCargo *cargo, sal_Int32 loglevel );
75cdf0e10cSrcweir void log( RuntimeCargo *cargo, sal_Int32 level, const rtl::OUString &logString );
76cdf0e10cSrcweir void log( RuntimeCargo *cargo, sal_Int32 level, const char *str );
77cdf0e10cSrcweir void logCall( RuntimeCargo *cargo, const char *intro,
78cdf0e10cSrcweir               void * ptr, const rtl::OUString & aFunctionName,
79cdf0e10cSrcweir               const com::sun::star::uno::Sequence< com::sun::star::uno::Any > & args );
80cdf0e10cSrcweir void logReply( RuntimeCargo *cargo, const char *intro,
81cdf0e10cSrcweir               void * ptr, const rtl::OUString & aFunctionName,
82cdf0e10cSrcweir               const com::sun::star::uno::Any &returnValue,
83cdf0e10cSrcweir               const com::sun::star::uno::Sequence< com::sun::star::uno::Any > & args );
84cdf0e10cSrcweir void logException( RuntimeCargo *cargo, const char *intro,
85cdf0e10cSrcweir                    void * ptr, const rtl::OUString &aFunctionName,
86cdf0e10cSrcweir                    const void * data, const com::sun::star::uno::Type & type );
87cdf0e10cSrcweir static const sal_Int32 VAL2STR_MODE_DEEP = 0;
88cdf0e10cSrcweir static const sal_Int32 VAL2STR_MODE_SHALLOW = 1;
89cdf0e10cSrcweir rtl::OUString val2str( const void * pVal, typelib_TypeDescriptionReference * pTypeRef, sal_Int32 mode = VAL2STR_MODE_DEEP ) SAL_THROW( () );
90cdf0e10cSrcweir //--------------------------------------------------
91cdf0e10cSrcweir 
92cdf0e10cSrcweir typedef ::std::hash_map
93cdf0e10cSrcweir <
94cdf0e10cSrcweir     PyRef,
95cdf0e10cSrcweir     com::sun::star::uno::WeakReference< com::sun::star::script::XInvocation >,
96cdf0e10cSrcweir     PyRef::Hash,
97cdf0e10cSrcweir     std::equal_to< PyRef >
98cdf0e10cSrcweir > PyRef2Adapter;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 
101cdf0e10cSrcweir typedef ::std::hash_map
102cdf0e10cSrcweir <
103cdf0e10cSrcweir rtl::OUString,
104cdf0e10cSrcweir PyRef,
105cdf0e10cSrcweir rtl::OUStringHash,
106cdf0e10cSrcweir std::equal_to<rtl::OUString>
107cdf0e10cSrcweir > ExceptionClassMap;
108cdf0e10cSrcweir 
109cdf0e10cSrcweir typedef ::std::hash_map
110cdf0e10cSrcweir <
111cdf0e10cSrcweir     rtl::OUString,
112cdf0e10cSrcweir     com::sun::star::uno::Sequence< sal_Int16 >,
113cdf0e10cSrcweir     rtl::OUStringHash,
114cdf0e10cSrcweir     std::equal_to< rtl::OUString >
115cdf0e10cSrcweir > MethodOutIndexMap;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir typedef ::std::hash_set< PyRef , PyRef::Hash , std::equal_to<PyRef> > ClassSet;
118cdf0e10cSrcweir 
119cdf0e10cSrcweir PyObject* PyUNO_new(
120cdf0e10cSrcweir     const com::sun::star::uno::Any & targetInterface,
121cdf0e10cSrcweir     const com::sun::star::uno::Reference<com::sun::star::lang::XSingleServiceFactory> & ssf);
122cdf0e10cSrcweir 
123cdf0e10cSrcweir PyObject* PyUNO_new_UNCHECKED (
124cdf0e10cSrcweir     const com::sun::star::uno::Any & targetInterface,
125cdf0e10cSrcweir     const com::sun::star::uno::Reference<com::sun::star::lang::XSingleServiceFactory> & ssf);
126cdf0e10cSrcweir 
127cdf0e10cSrcweir typedef struct
128cdf0e10cSrcweir {
129cdf0e10cSrcweir     com::sun::star::uno::Reference <com::sun::star::script::XInvocation2> xInvocation;
130cdf0e10cSrcweir     com::sun::star::uno::Any wrappedObject;
131cdf0e10cSrcweir } PyUNOInternals;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir typedef struct
134cdf0e10cSrcweir {
135cdf0e10cSrcweir     PyObject_HEAD
136cdf0e10cSrcweir     PyUNOInternals* members;
137cdf0e10cSrcweir } PyUNO;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir PyRef ustring2PyUnicode( const rtl::OUString &source );
140cdf0e10cSrcweir PyRef ustring2PyString( const ::rtl::OUString & source );
141cdf0e10cSrcweir rtl::OUString pyString2ustring( PyObject *str );
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 
144cdf0e10cSrcweir PyRef AnyToPyObject (const com::sun::star::uno::Any & a, const Runtime &r )
145cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
146cdf0e10cSrcweir 
147cdf0e10cSrcweir com::sun::star::uno::Any PyObjectToAny (PyObject* o)
148cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
149cdf0e10cSrcweir 
150cdf0e10cSrcweir void raiseInvocationTargetExceptionWhenNeeded( const Runtime &runtime )
151cdf0e10cSrcweir     throw ( com::sun::star::reflection::InvocationTargetException );
152cdf0e10cSrcweir 
153cdf0e10cSrcweir // bool CheckPyObjectTypes (PyObject* o, Sequence<Type> types);
154cdf0e10cSrcweir // bool CheckPyObjectType (PyObject* o, Type type); //Only check 1 object.
155cdf0e10cSrcweir 
156cdf0e10cSrcweir com::sun::star::uno::TypeClass StringToTypeClass (char* string);
157cdf0e10cSrcweir 
158cdf0e10cSrcweir PyRef PyUNO_callable_new (
159cdf0e10cSrcweir     const com::sun::star::uno::Reference<com::sun::star::script::XInvocation2> &xInv,
160cdf0e10cSrcweir     const rtl::OUString &methodName,
161cdf0e10cSrcweir     const com::sun::star::uno::Reference<com::sun::star::lang::XSingleServiceFactory> &ssf,
162cdf0e10cSrcweir     const com::sun::star::uno::Reference<com::sun::star::script::XTypeConverter> &tc,
163cdf0e10cSrcweir     ConversionMode mode = REJECT_UNO_ANY );
164cdf0e10cSrcweir 
165cdf0e10cSrcweir PyObject* PyUNO_Type_new (const char *typeName , com::sun::star::uno::TypeClass t , const Runtime &r );
166cdf0e10cSrcweir PyObject* PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r );
167cdf0e10cSrcweir PyObject* PyUNO_char_new (sal_Unicode c , const Runtime &r);
168cdf0e10cSrcweir PyObject *PyUNO_ByteSequence_new( const com::sun::star::uno::Sequence< sal_Int8 > &, const Runtime &r );
169cdf0e10cSrcweir 
170cdf0e10cSrcweir PyObject *importToGlobal( PyObject *typeName, PyObject *dict, PyObject *targetName );
171cdf0e10cSrcweir 
172cdf0e10cSrcweir PyRef getTypeClass( const Runtime &);
173cdf0e10cSrcweir PyRef getEnumClass( const Runtime &);
174cdf0e10cSrcweir PyRef getBoolClass( const Runtime &);
175cdf0e10cSrcweir PyRef getCharClass( const Runtime &);
176cdf0e10cSrcweir PyRef getByteSequenceClass( const Runtime & );
177*77dc4149SPedro Giffuni PyRef getPyUnoClass();
178cdf0e10cSrcweir PyRef getClass( const rtl::OUString & name , const Runtime & runtime );
179cdf0e10cSrcweir PyRef getAnyClass( const Runtime &);
180cdf0e10cSrcweir PyObject *PyUNO_invoke( PyObject *object, const char *name , PyObject *args );
181cdf0e10cSrcweir 
182cdf0e10cSrcweir com::sun::star::uno::Any PyEnum2Enum( PyObject *obj )
183cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
184cdf0e10cSrcweir sal_Bool PyBool2Bool( PyObject *o, const Runtime & r )
185cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
186cdf0e10cSrcweir sal_Unicode PyChar2Unicode( PyObject *o )
187cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
188cdf0e10cSrcweir com::sun::star::uno::Type PyType2Type( PyObject * o )
189cdf0e10cSrcweir     throw( com::sun::star::uno::RuntimeException );
190cdf0e10cSrcweir 
191cdf0e10cSrcweir void raisePyExceptionWithAny( const com::sun::star::uno::Any &a );
192cdf0e10cSrcweir const char *typeClassToString( com::sun::star::uno::TypeClass t );
193cdf0e10cSrcweir 
194cdf0e10cSrcweir PyRef getObjectFromUnoModule( const Runtime &runtime, const char * object )
195cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
196cdf0e10cSrcweir 
197cdf0e10cSrcweir sal_Bool isInterfaceClass( const Runtime &, PyObject *obj );
198cdf0e10cSrcweir bool isInstanceOfStructOrException( PyObject *obj);
199cdf0e10cSrcweir com::sun::star::uno::Sequence<com::sun::star::uno::Type> implementsInterfaces(
200cdf0e10cSrcweir     const Runtime & runtime, PyObject *obj );
201cdf0e10cSrcweir 
202cdf0e10cSrcweir struct RuntimeCargo
203cdf0e10cSrcweir {
204cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::lang::XSingleServiceFactory > xInvocation;
205cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::script::XTypeConverter> xTypeConverter;
206cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > xContext;
207cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::reflection::XIdlReflection > xCoreReflection;
208cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::container::XHierarchicalNameAccess > xTdMgr;
209cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::script::XInvocationAdapterFactory2 > xAdapterFactory;
210cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::beans::XIntrospection > xIntrospection;
211cdf0e10cSrcweir     PyRef dictUnoModule;
212cdf0e10cSrcweir     bool valid;
213cdf0e10cSrcweir     ExceptionClassMap exceptionMap;
214cdf0e10cSrcweir     ClassSet interfaceSet;
215cdf0e10cSrcweir     PyRef2Adapter mappedObjects;
216cdf0e10cSrcweir     FILE *logFile;
217cdf0e10cSrcweir     sal_Int32 logLevel;
218cdf0e10cSrcweir 
219cdf0e10cSrcweir     PyRef getUnoModule();
220cdf0e10cSrcweir };
221cdf0e10cSrcweir 
222cdf0e10cSrcweir struct stRuntimeImpl
223cdf0e10cSrcweir {
224cdf0e10cSrcweir     PyObject_HEAD
225cdf0e10cSrcweir     struct RuntimeCargo *cargo;
226cdf0e10cSrcweir public:
227cdf0e10cSrcweir     static void del( PyObject *self );
228cdf0e10cSrcweir 
229cdf0e10cSrcweir     static PyRef create(
230cdf0e10cSrcweir         const com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > & xContext )
231cdf0e10cSrcweir         throw ( com::sun::star::uno::RuntimeException );
232cdf0e10cSrcweir };
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 
235cdf0e10cSrcweir class Adapter : public cppu::WeakImplHelper2<
236cdf0e10cSrcweir     com::sun::star::script::XInvocation, com::sun::star::lang::XUnoTunnel >
237cdf0e10cSrcweir {
238cdf0e10cSrcweir     PyRef mWrappedObject;
239cdf0e10cSrcweir     PyInterpreterState *mInterpreter;  // interpreters don't seem to be refcounted !
240cdf0e10cSrcweir     com::sun::star::uno::Sequence< com::sun::star::uno::Type > mTypes;
241cdf0e10cSrcweir     MethodOutIndexMap m_methodOutIndexMap;
242cdf0e10cSrcweir 
243cdf0e10cSrcweir private:
244cdf0e10cSrcweir     com::sun::star::uno::Sequence< sal_Int16 > getOutIndexes( const rtl::OUString & functionName );
245cdf0e10cSrcweir 
246cdf0e10cSrcweir public:
247cdf0e10cSrcweir public:
248cdf0e10cSrcweir     Adapter( const PyRef &obj,
249cdf0e10cSrcweir              const com::sun::star::uno::Sequence< com::sun::star::uno::Type > & types );
250cdf0e10cSrcweir 
251cdf0e10cSrcweir     static com::sun::star::uno::Sequence< sal_Int8 > getUnoTunnelImplementationId();
252cdf0e10cSrcweir     PyRef getWrappedObject() { return mWrappedObject; }
253cdf0e10cSrcweir     com::sun::star::uno::Sequence< com::sun::star::uno::Type > getWrappedTypes() { return mTypes; }
254cdf0e10cSrcweir     virtual ~Adapter();
255cdf0e10cSrcweir 
256cdf0e10cSrcweir     // XInvocation
257cdf0e10cSrcweir     virtual com::sun::star::uno::Reference< ::com::sun::star::beans::XIntrospectionAccess >
258cdf0e10cSrcweir            SAL_CALL getIntrospection(  ) throw (::com::sun::star::uno::RuntimeException);
259cdf0e10cSrcweir     virtual ::com::sun::star::uno::Any SAL_CALL invoke(
260cdf0e10cSrcweir         const ::rtl::OUString& aFunctionName,
261cdf0e10cSrcweir         const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aParams,
262cdf0e10cSrcweir         ::com::sun::star::uno::Sequence< sal_Int16 >& aOutParamIndex,
263cdf0e10cSrcweir         ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aOutParam )
264cdf0e10cSrcweir         throw (::com::sun::star::lang::IllegalArgumentException,
265cdf0e10cSrcweir                ::com::sun::star::script::CannotConvertException,
266cdf0e10cSrcweir                ::com::sun::star::reflection::InvocationTargetException,
267cdf0e10cSrcweir                ::com::sun::star::uno::RuntimeException);
268cdf0e10cSrcweir 
269cdf0e10cSrcweir     virtual void SAL_CALL setValue(
270cdf0e10cSrcweir         const ::rtl::OUString& aPropertyName,
271cdf0e10cSrcweir         const ::com::sun::star::uno::Any& aValue )
272cdf0e10cSrcweir         throw (::com::sun::star::beans::UnknownPropertyException,
273cdf0e10cSrcweir                ::com::sun::star::script::CannotConvertException,
274cdf0e10cSrcweir                ::com::sun::star::reflection::InvocationTargetException,
275cdf0e10cSrcweir                ::com::sun::star::uno::RuntimeException);
276cdf0e10cSrcweir 
277cdf0e10cSrcweir     virtual ::com::sun::star::uno::Any SAL_CALL getValue( const ::rtl::OUString& aPropertyName )
278cdf0e10cSrcweir         throw (::com::sun::star::beans::UnknownPropertyException,
279cdf0e10cSrcweir                ::com::sun::star::uno::RuntimeException);
280cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasMethod( const ::rtl::OUString& aName )
281cdf0e10cSrcweir         throw (::com::sun::star::uno::RuntimeException);
282cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasProperty( const ::rtl::OUString& aName )
283cdf0e10cSrcweir         throw (::com::sun::star::uno::RuntimeException);
284cdf0e10cSrcweir 
285cdf0e10cSrcweir     // XUnoTunnel
286cdf0e10cSrcweir     virtual sal_Int64 SAL_CALL getSomething(
287cdf0e10cSrcweir         const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier )
288cdf0e10cSrcweir         throw (::com::sun::star::uno::RuntimeException);
289cdf0e10cSrcweir };
290cdf0e10cSrcweir 
291cdf0e10cSrcweir 
292cdf0e10cSrcweir /** releases a refcount on the interpreter object and on another given python object.
293cdf0e10cSrcweir 
294cdf0e10cSrcweir    The function can be called from any thread regardless of whether the global
295cdf0e10cSrcweir    interpreter lock is held.
296cdf0e10cSrcweir 
297cdf0e10cSrcweir  */
298cdf0e10cSrcweir void decreaseRefCount( PyInterpreterState *interpreter, PyObject *object );
299cdf0e10cSrcweir 
300cdf0e10cSrcweir }
301cdf0e10cSrcweir 
302cdf0e10cSrcweir #endif
303