xref: /trunk/main/pyuno/source/module/pyuno_impl.hxx (revision 60fcfbce1cf55d512c6ebbf7aee11464c2706d80)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _PYUNO_IMPL_
24 #define _PYUNO_IMPL_
25 
26 #include <pyuno/pyuno.hxx>
27 
28 #include <unordered_map>
29 #include <unordered_set>
30 
31 #include <com/sun/star/beans/XIntrospection.hpp>
32 #include <com/sun/star/script/XTypeConverter.hpp>
33 #include <com/sun/star/script/XInvocation2.hpp>
34 #include <com/sun/star/script/XInvocationAdapterFactory2.hpp>
35 
36 #include <com/sun/star/reflection/XIdlReflection.hpp>
37 #include <com/sun/star/reflection/XIdlClass.hpp>
38 #include <com/sun/star/reflection/XIdlMethod.hpp>
39 
40 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
41 
42 #include <com/sun/star/lang/XUnoTunnel.hpp>
43 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
44 
45 #include <cppuhelper/implbase2.hxx>
46 #include <cppuhelper/weakref.hxx>
47 
48 //
49 // Local workarounds for compatibility issues
50 //
51     #define PYSTR_FROMSTR               PyUnicode_FromString
52     #define USTR_TO_PYSTR               ustring2PyUnicode
53     #define PYSTR_CHECK                 PyUnicode_Check
54 
55 #include <rtl/string.hxx>
PyErr_SetString(PyObject * pyObj,const rtl::OString & rName)56 inline void PyErr_SetString( PyObject* pyObj, const rtl::OString& rName) { PyErr_SetString( pyObj, rName.getStr());}
57 
58 namespace pyuno
59 {
60 
61 //--------------------------------------------------
62 // Logging API - implementation can be found in pyuno_util
63 //--------------------------------------------------
64 struct RuntimeCargo;
65 namespace LogLevel
66 {
67 // when you add a loglevel, extend the log function !
68 static const sal_Int32 NONE = 0;
69 static const sal_Int32 CALL = 1;
70 static const sal_Int32 ARGS = 2;
71 }
72 
73 bool isLog( RuntimeCargo *cargo, sal_Int32 loglevel );
74 void log( RuntimeCargo *cargo, sal_Int32 level, const rtl::OUString &logString );
75 void log( RuntimeCargo *cargo, sal_Int32 level, const char *str );
76 void logCall( RuntimeCargo *cargo, const char *intro,
77               void * ptr, const rtl::OUString & aFunctionName,
78               const com::sun::star::uno::Sequence< com::sun::star::uno::Any > & args );
79 void logReply( RuntimeCargo *cargo, const char *intro,
80               void * ptr, const rtl::OUString & aFunctionName,
81               const com::sun::star::uno::Any &returnValue,
82               const com::sun::star::uno::Sequence< com::sun::star::uno::Any > & args );
83 void logException( RuntimeCargo *cargo, const char *intro,
84                    void * ptr, const rtl::OUString &aFunctionName,
85                    const void * data, const com::sun::star::uno::Type & type );
86 static const sal_Int32 VAL2STR_MODE_DEEP = 0;
87 static const sal_Int32 VAL2STR_MODE_SHALLOW = 1;
88 rtl::OUString val2str( const void * pVal, typelib_TypeDescriptionReference * pTypeRef, sal_Int32 mode = VAL2STR_MODE_DEEP ) SAL_THROW( () );
89 //--------------------------------------------------
90 
91 typedef ::std::unordered_map
92 <
93     PyRef,
94     com::sun::star::uno::WeakReference< com::sun::star::script::XInvocation >,
95     PyRef::Hash,
96     std::equal_to< PyRef >
97 > PyRef2Adapter;
98 
99 
100 typedef ::std::unordered_map
101 <
102 rtl::OUString,
103 PyRef,
104 rtl::OUStringHash,
105 std::equal_to<rtl::OUString>
106 > ExceptionClassMap;
107 
108 typedef ::std::unordered_map
109 <
110     rtl::OUString,
111     com::sun::star::uno::Sequence< sal_Int16 >,
112     rtl::OUStringHash,
113     std::equal_to< rtl::OUString >
114 > MethodOutIndexMap;
115 
116 typedef ::std::unordered_set< PyRef , PyRef::Hash , std::equal_to<PyRef> > ClassSet;
117 
118 PyObject* PyUNO_new(
119     const com::sun::star::uno::Any & targetInterface,
120     const com::sun::star::uno::Reference<com::sun::star::lang::XSingleServiceFactory> & ssf);
121 
122 PyObject* PyUNO_new_UNCHECKED (
123     const com::sun::star::uno::Any & targetInterface,
124     const com::sun::star::uno::Reference<com::sun::star::lang::XSingleServiceFactory> & ssf);
125 
126 typedef struct
127 {
128     com::sun::star::uno::Reference <com::sun::star::script::XInvocation> xInvocation;
129     com::sun::star::uno::Any wrappedObject;
130 } PyUNOInternals;
131 
132 typedef struct
133 {
134     PyObject_HEAD
135     PyUNOInternals* members;
136 } PyUNO;
137 
138 PyRef ustring2PyUnicode( const rtl::OUString &source );
139 rtl::OUString pyString2ustring( PyObject *str );
140 
141 
142 PyRef AnyToPyObject (const com::sun::star::uno::Any & a, const Runtime &r );
143 
144 com::sun::star::uno::Any PyObjectToAny (PyObject* o);
145 
146 void raiseInvocationTargetExceptionWhenNeeded( const Runtime &runtime );
147 
148 // bool CheckPyObjectTypes (PyObject* o, Sequence<Type> types);
149 // bool CheckPyObjectType (PyObject* o, Type type); //Only check 1 object.
150 
151 com::sun::star::uno::TypeClass StringToTypeClass (char* string);
152 
153 PyRef PyUNO_callable_new (
154     const com::sun::star::uno::Reference<com::sun::star::script::XInvocation> &xInv,
155     const rtl::OUString &methodName,
156     ConversionMode mode = REJECT_UNO_ANY,
157     const com::sun::star::uno::Reference<com::sun::star::reflection::XIdlMethod> &xMethod
158         = com::sun::star::uno::Reference<com::sun::star::reflection::XIdlMethod>(),
159     const com::sun::star::uno::Any &target = com::sun::star::uno::Any() );
160 
161 PyObject* PyUNO_Type_new (const char *typeName , com::sun::star::uno::TypeClass t , const Runtime &r );
162 PyObject* PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r );
163 PyObject* PyUNO_char_new (sal_Unicode c , const Runtime &r);
164 PyObject *PyUNO_ByteSequence_new( const com::sun::star::uno::Sequence< sal_Int8 > &, const Runtime &r );
165 
166 PyObject *importToGlobal( PyObject *typeName, PyObject *dict, PyObject *targetName );
167 
168 PyRef getTypeClass( const Runtime &);
169 PyRef getEnumClass( const Runtime &);
170 PyRef getBoolClass( const Runtime &);
171 PyRef getCharClass( const Runtime &);
172 PyRef getByteSequenceClass( const Runtime & );
173 PyRef getPyUnoClass();
174 PyRef getClass( const rtl::OUString & name , const Runtime & runtime );
175 PyRef getAnyClass( const Runtime &);
176 PyObject *PyUNO_invoke( PyObject *object, const char *name , PyObject *args );
177 
178 com::sun::star::uno::Any PyEnum2Enum( PyObject *obj );
179 sal_Bool PyBool2Bool( PyObject *o, const Runtime & r );
180 sal_Unicode PyChar2Unicode( PyObject *o );
181 com::sun::star::uno::Type PyType2Type( PyObject * o );
182 
183 void raisePyExceptionWithAny( const com::sun::star::uno::Any &a );
184 const char *typeClassToString( com::sun::star::uno::TypeClass t );
185 
186 PyRef getObjectFromUnoModule( const Runtime &runtime, const char * object );
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 };
223 
224 
225 class Adapter : public cppu::WeakImplHelper2<
226     com::sun::star::script::XInvocation, com::sun::star::lang::XUnoTunnel >
227 {
228     PyRef mWrappedObject;
229     PyInterpreterState *mInterpreter;  // interpreters don't seem to be refcounted !
230     com::sun::star::uno::Sequence< com::sun::star::uno::Type > mTypes;
231     MethodOutIndexMap m_methodOutIndexMap;
232 
233 private:
234     com::sun::star::uno::Sequence< sal_Int16 > getOutIndexes( const rtl::OUString & functionName );
235 
236 public:
237 public:
238     Adapter( const PyRef &obj,
239              const com::sun::star::uno::Sequence< com::sun::star::uno::Type > & types );
240 
241     static com::sun::star::uno::Sequence< sal_Int8 > getUnoTunnelImplementationId();
getWrappedObject()242     PyRef getWrappedObject() { return mWrappedObject; }
getWrappedTypes()243     com::sun::star::uno::Sequence< com::sun::star::uno::Type > getWrappedTypes() { return mTypes; }
244     virtual ~Adapter();
245 
246     // XInvocation
247     virtual com::sun::star::uno::Reference< ::com::sun::star::beans::XIntrospectionAccess >
248            SAL_CALL getIntrospection(  );
249     virtual ::com::sun::star::uno::Any SAL_CALL invoke(
250         const ::rtl::OUString& aFunctionName,
251         const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aParams,
252         ::com::sun::star::uno::Sequence< sal_Int16 >& aOutParamIndex,
253         ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aOutParam );
254 
255     virtual void SAL_CALL setValue(
256         const ::rtl::OUString& aPropertyName,
257         const ::com::sun::star::uno::Any& aValue );
258 
259     virtual ::com::sun::star::uno::Any SAL_CALL getValue( const ::rtl::OUString& aPropertyName );
260     virtual sal_Bool SAL_CALL hasMethod( const ::rtl::OUString& aName );
261     virtual sal_Bool SAL_CALL hasProperty( const ::rtl::OUString& aName );
262 
263     // XUnoTunnel
264     virtual sal_Int64 SAL_CALL getSomething(
265         const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier );
266 };
267 
268 
269 /** releases a refcount on the interpreter object and on another given python object.
270 
271    The function can be called from any thread regardless of whether the global
272    interpreter lock is held.
273 
274  */
275 void decreaseRefCount( PyInterpreterState *interpreter, PyObject *object );
276 
277 }
278 
279 #endif
280