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