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