xref: /aoo41x/main/pyuno/source/module/pyuno_impl.hxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #ifndef _PYUNO_IMPL_
28*cdf0e10cSrcweir #define _PYUNO_IMPL_
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <pyuno/pyuno.hxx>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <hash_map>
33*cdf0e10cSrcweir #include <hash_set>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <com/sun/star/beans/XIntrospection.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/script/XTypeConverter.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/script/XInvocation2.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/script/XInvocationAdapterFactory2.hpp>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <com/sun/star/reflection/XIdlReflection.hpp>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include <com/sun/star/lang/XUnoTunnel.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
48*cdf0e10cSrcweir #include <cppuhelper/weakref.hxx>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir namespace pyuno
51*cdf0e10cSrcweir {
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir //--------------------------------------------------
54*cdf0e10cSrcweir // Logging API - implementation can be found in pyuno_util
55*cdf0e10cSrcweir //--------------------------------------------------
56*cdf0e10cSrcweir struct RuntimeCargo;
57*cdf0e10cSrcweir namespace LogLevel
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir // when you add a loglevel, extend the log function !
60*cdf0e10cSrcweir static const sal_Int32 NONE = 0;
61*cdf0e10cSrcweir static const sal_Int32 CALL = 1;
62*cdf0e10cSrcweir static const sal_Int32 ARGS = 2;
63*cdf0e10cSrcweir }
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir bool isLog( RuntimeCargo *cargo, sal_Int32 loglevel );
66*cdf0e10cSrcweir void log( RuntimeCargo *cargo, sal_Int32 level, const rtl::OUString &logString );
67*cdf0e10cSrcweir void log( RuntimeCargo *cargo, sal_Int32 level, const char *str );
68*cdf0e10cSrcweir void logCall( RuntimeCargo *cargo, const char *intro,
69*cdf0e10cSrcweir               void * ptr, const rtl::OUString & aFunctionName,
70*cdf0e10cSrcweir               const com::sun::star::uno::Sequence< com::sun::star::uno::Any > & args );
71*cdf0e10cSrcweir void logReply( RuntimeCargo *cargo, const char *intro,
72*cdf0e10cSrcweir               void * ptr, const rtl::OUString & aFunctionName,
73*cdf0e10cSrcweir               const com::sun::star::uno::Any &returnValue,
74*cdf0e10cSrcweir               const com::sun::star::uno::Sequence< com::sun::star::uno::Any > & args );
75*cdf0e10cSrcweir void logException( RuntimeCargo *cargo, const char *intro,
76*cdf0e10cSrcweir                    void * ptr, const rtl::OUString &aFunctionName,
77*cdf0e10cSrcweir                    const void * data, const com::sun::star::uno::Type & type );
78*cdf0e10cSrcweir static const sal_Int32 VAL2STR_MODE_DEEP = 0;
79*cdf0e10cSrcweir static const sal_Int32 VAL2STR_MODE_SHALLOW = 1;
80*cdf0e10cSrcweir rtl::OUString val2str( const void * pVal, typelib_TypeDescriptionReference * pTypeRef, sal_Int32 mode = VAL2STR_MODE_DEEP ) SAL_THROW( () );
81*cdf0e10cSrcweir //--------------------------------------------------
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir typedef ::std::hash_map
84*cdf0e10cSrcweir <
85*cdf0e10cSrcweir     PyRef,
86*cdf0e10cSrcweir     com::sun::star::uno::WeakReference< com::sun::star::script::XInvocation >,
87*cdf0e10cSrcweir     PyRef::Hash,
88*cdf0e10cSrcweir     std::equal_to< PyRef >
89*cdf0e10cSrcweir > PyRef2Adapter;
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir typedef ::std::hash_map
93*cdf0e10cSrcweir <
94*cdf0e10cSrcweir rtl::OUString,
95*cdf0e10cSrcweir PyRef,
96*cdf0e10cSrcweir rtl::OUStringHash,
97*cdf0e10cSrcweir std::equal_to<rtl::OUString>
98*cdf0e10cSrcweir > ExceptionClassMap;
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir typedef ::std::hash_map
101*cdf0e10cSrcweir <
102*cdf0e10cSrcweir     rtl::OUString,
103*cdf0e10cSrcweir     com::sun::star::uno::Sequence< sal_Int16 >,
104*cdf0e10cSrcweir     rtl::OUStringHash,
105*cdf0e10cSrcweir     std::equal_to< rtl::OUString >
106*cdf0e10cSrcweir > MethodOutIndexMap;
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir typedef ::std::hash_set< PyRef , PyRef::Hash , std::equal_to<PyRef> > ClassSet;
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir PyObject* PyUNO_new(
111*cdf0e10cSrcweir     const com::sun::star::uno::Any & targetInterface,
112*cdf0e10cSrcweir     const com::sun::star::uno::Reference<com::sun::star::lang::XSingleServiceFactory> & ssf);
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir PyObject* PyUNO_new_UNCHECKED (
115*cdf0e10cSrcweir     const com::sun::star::uno::Any & targetInterface,
116*cdf0e10cSrcweir     const com::sun::star::uno::Reference<com::sun::star::lang::XSingleServiceFactory> & ssf);
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir typedef struct
119*cdf0e10cSrcweir {
120*cdf0e10cSrcweir     com::sun::star::uno::Reference <com::sun::star::script::XInvocation2> xInvocation;
121*cdf0e10cSrcweir     com::sun::star::uno::Any wrappedObject;
122*cdf0e10cSrcweir } PyUNOInternals;
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir typedef struct
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir     PyObject_HEAD
127*cdf0e10cSrcweir     PyUNOInternals* members;
128*cdf0e10cSrcweir } PyUNO;
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir PyRef ustring2PyUnicode( const rtl::OUString &source );
131*cdf0e10cSrcweir PyRef ustring2PyString( const ::rtl::OUString & source );
132*cdf0e10cSrcweir rtl::OUString pyString2ustring( PyObject *str );
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir PyRef AnyToPyObject (const com::sun::star::uno::Any & a, const Runtime &r )
136*cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir com::sun::star::uno::Any PyObjectToAny (PyObject* o)
139*cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir void raiseInvocationTargetExceptionWhenNeeded( const Runtime &runtime )
142*cdf0e10cSrcweir     throw ( com::sun::star::reflection::InvocationTargetException );
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir // bool CheckPyObjectTypes (PyObject* o, Sequence<Type> types);
145*cdf0e10cSrcweir // bool CheckPyObjectType (PyObject* o, Type type); //Only check 1 object.
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir com::sun::star::uno::TypeClass StringToTypeClass (char* string);
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir PyRef PyUNO_callable_new (
150*cdf0e10cSrcweir     const com::sun::star::uno::Reference<com::sun::star::script::XInvocation2> &xInv,
151*cdf0e10cSrcweir     const rtl::OUString &methodName,
152*cdf0e10cSrcweir     const com::sun::star::uno::Reference<com::sun::star::lang::XSingleServiceFactory> &ssf,
153*cdf0e10cSrcweir     const com::sun::star::uno::Reference<com::sun::star::script::XTypeConverter> &tc,
154*cdf0e10cSrcweir     ConversionMode mode = REJECT_UNO_ANY );
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir PyObject* PyUNO_Type_new (const char *typeName , com::sun::star::uno::TypeClass t , const Runtime &r );
157*cdf0e10cSrcweir PyObject* PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r );
158*cdf0e10cSrcweir PyObject* PyUNO_char_new (sal_Unicode c , const Runtime &r);
159*cdf0e10cSrcweir PyObject *PyUNO_ByteSequence_new( const com::sun::star::uno::Sequence< sal_Int8 > &, const Runtime &r );
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir PyObject *importToGlobal( PyObject *typeName, PyObject *dict, PyObject *targetName );
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir PyRef getTypeClass( const Runtime &);
164*cdf0e10cSrcweir PyRef getEnumClass( const Runtime &);
165*cdf0e10cSrcweir PyRef getBoolClass( const Runtime &);
166*cdf0e10cSrcweir PyRef getCharClass( const Runtime &);
167*cdf0e10cSrcweir PyRef getByteSequenceClass( const Runtime & );
168*cdf0e10cSrcweir PyRef getPyUnoClass( const Runtime &);
169*cdf0e10cSrcweir PyRef getClass( const rtl::OUString & name , const Runtime & runtime );
170*cdf0e10cSrcweir PyRef getAnyClass( const Runtime &);
171*cdf0e10cSrcweir PyObject *PyUNO_invoke( PyObject *object, const char *name , PyObject *args );
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir com::sun::star::uno::Any PyEnum2Enum( PyObject *obj )
174*cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
175*cdf0e10cSrcweir sal_Bool PyBool2Bool( PyObject *o, const Runtime & r )
176*cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
177*cdf0e10cSrcweir sal_Unicode PyChar2Unicode( PyObject *o )
178*cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
179*cdf0e10cSrcweir com::sun::star::uno::Type PyType2Type( PyObject * o )
180*cdf0e10cSrcweir     throw( com::sun::star::uno::RuntimeException );
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir void raisePyExceptionWithAny( const com::sun::star::uno::Any &a );
183*cdf0e10cSrcweir const char *typeClassToString( com::sun::star::uno::TypeClass t );
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir PyRef getObjectFromUnoModule( const Runtime &runtime, const char * object )
186*cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException );
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir sal_Bool isInterfaceClass( const Runtime &, PyObject *obj );
189*cdf0e10cSrcweir bool isInstanceOfStructOrException( PyObject *obj);
190*cdf0e10cSrcweir com::sun::star::uno::Sequence<com::sun::star::uno::Type> implementsInterfaces(
191*cdf0e10cSrcweir     const Runtime & runtime, PyObject *obj );
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir struct RuntimeCargo
194*cdf0e10cSrcweir {
195*cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::lang::XSingleServiceFactory > xInvocation;
196*cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::script::XTypeConverter> xTypeConverter;
197*cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > xContext;
198*cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::reflection::XIdlReflection > xCoreReflection;
199*cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::container::XHierarchicalNameAccess > xTdMgr;
200*cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::script::XInvocationAdapterFactory2 > xAdapterFactory;
201*cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::beans::XIntrospection > xIntrospection;
202*cdf0e10cSrcweir     PyRef dictUnoModule;
203*cdf0e10cSrcweir     bool valid;
204*cdf0e10cSrcweir     ExceptionClassMap exceptionMap;
205*cdf0e10cSrcweir     ClassSet interfaceSet;
206*cdf0e10cSrcweir     PyRef2Adapter mappedObjects;
207*cdf0e10cSrcweir     FILE *logFile;
208*cdf0e10cSrcweir     sal_Int32 logLevel;
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir     PyRef getUnoModule();
211*cdf0e10cSrcweir };
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir struct stRuntimeImpl
214*cdf0e10cSrcweir {
215*cdf0e10cSrcweir     PyObject_HEAD
216*cdf0e10cSrcweir     struct RuntimeCargo *cargo;
217*cdf0e10cSrcweir public:
218*cdf0e10cSrcweir     static void del( PyObject *self );
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir     static PyRef create(
221*cdf0e10cSrcweir         const com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > & xContext )
222*cdf0e10cSrcweir         throw ( com::sun::star::uno::RuntimeException );
223*cdf0e10cSrcweir };
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir class Adapter : public cppu::WeakImplHelper2<
227*cdf0e10cSrcweir     com::sun::star::script::XInvocation, com::sun::star::lang::XUnoTunnel >
228*cdf0e10cSrcweir {
229*cdf0e10cSrcweir     PyRef mWrappedObject;
230*cdf0e10cSrcweir     PyInterpreterState *mInterpreter;  // interpreters don't seem to be refcounted !
231*cdf0e10cSrcweir     com::sun::star::uno::Sequence< com::sun::star::uno::Type > mTypes;
232*cdf0e10cSrcweir     MethodOutIndexMap m_methodOutIndexMap;
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir private:
235*cdf0e10cSrcweir     com::sun::star::uno::Sequence< sal_Int16 > getOutIndexes( const rtl::OUString & functionName );
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir public:
238*cdf0e10cSrcweir public:
239*cdf0e10cSrcweir     Adapter( const PyRef &obj,
240*cdf0e10cSrcweir              const com::sun::star::uno::Sequence< com::sun::star::uno::Type > & types );
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir     static com::sun::star::uno::Sequence< sal_Int8 > getUnoTunnelImplementationId();
243*cdf0e10cSrcweir     PyRef getWrappedObject() { return mWrappedObject; }
244*cdf0e10cSrcweir     com::sun::star::uno::Sequence< com::sun::star::uno::Type > getWrappedTypes() { return mTypes; }
245*cdf0e10cSrcweir     virtual ~Adapter();
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir     // XInvocation
248*cdf0e10cSrcweir     virtual com::sun::star::uno::Reference< ::com::sun::star::beans::XIntrospectionAccess >
249*cdf0e10cSrcweir            SAL_CALL getIntrospection(  ) throw (::com::sun::star::uno::RuntimeException);
250*cdf0e10cSrcweir     virtual ::com::sun::star::uno::Any SAL_CALL invoke(
251*cdf0e10cSrcweir         const ::rtl::OUString& aFunctionName,
252*cdf0e10cSrcweir         const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aParams,
253*cdf0e10cSrcweir         ::com::sun::star::uno::Sequence< sal_Int16 >& aOutParamIndex,
254*cdf0e10cSrcweir         ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aOutParam )
255*cdf0e10cSrcweir         throw (::com::sun::star::lang::IllegalArgumentException,
256*cdf0e10cSrcweir                ::com::sun::star::script::CannotConvertException,
257*cdf0e10cSrcweir                ::com::sun::star::reflection::InvocationTargetException,
258*cdf0e10cSrcweir                ::com::sun::star::uno::RuntimeException);
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir     virtual void SAL_CALL setValue(
261*cdf0e10cSrcweir         const ::rtl::OUString& aPropertyName,
262*cdf0e10cSrcweir         const ::com::sun::star::uno::Any& aValue )
263*cdf0e10cSrcweir         throw (::com::sun::star::beans::UnknownPropertyException,
264*cdf0e10cSrcweir                ::com::sun::star::script::CannotConvertException,
265*cdf0e10cSrcweir                ::com::sun::star::reflection::InvocationTargetException,
266*cdf0e10cSrcweir                ::com::sun::star::uno::RuntimeException);
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir     virtual ::com::sun::star::uno::Any SAL_CALL getValue( const ::rtl::OUString& aPropertyName )
269*cdf0e10cSrcweir         throw (::com::sun::star::beans::UnknownPropertyException,
270*cdf0e10cSrcweir                ::com::sun::star::uno::RuntimeException);
271*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasMethod( const ::rtl::OUString& aName )
272*cdf0e10cSrcweir         throw (::com::sun::star::uno::RuntimeException);
273*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasProperty( const ::rtl::OUString& aName )
274*cdf0e10cSrcweir         throw (::com::sun::star::uno::RuntimeException);
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir     // XUnoTunnel
277*cdf0e10cSrcweir     virtual sal_Int64 SAL_CALL getSomething(
278*cdf0e10cSrcweir         const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier )
279*cdf0e10cSrcweir         throw (::com::sun::star::uno::RuntimeException);
280*cdf0e10cSrcweir };
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir /** releases a refcount on the interpreter object and on another given python object.
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir    The function can be called from any thread regardless of whether the global
286*cdf0e10cSrcweir    interpreter lock is held.
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir  */
289*cdf0e10cSrcweir void decreaseRefCount( PyInterpreterState *interpreter, PyObject *object );
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir }
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir #endif
294