1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: pyuno_impl.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 #include <pyuno/pyuno.hxx>
38 #include <com/sun/star/beans/XIntrospection.hpp>
39 #include <com/sun/star/script/XTypeConverter.hpp>
40 #include <com/sun/star/script/XInvocation2.hpp>
41 #include <com/sun/star/script/XInvocationAdapterFactory2.hpp>
43 #include <com/sun/star/reflection/XIdlReflection.hpp>
45 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
47 #include <com/sun/star/lang/XUnoTunnel.hpp>
48 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
50 #include <cppuhelper/implbase2.hxx>
51 #include <cppuhelper/weakref.hxx>
56 //--------------------------------------------------
57 // Logging API - implementation can be found in pyuno_util
58 //--------------------------------------------------
62 // when you add a loglevel, extend the log function !
63 static const sal_Int32 NONE
= 0;
64 static const sal_Int32 CALL
= 1;
65 static const sal_Int32 ARGS
= 2;
68 bool isLog( RuntimeCargo
*cargo
, sal_Int32 loglevel
);
69 void log( RuntimeCargo
*cargo
, sal_Int32 level
, const rtl::OUString
&logString
);
70 void log( RuntimeCargo
*cargo
, sal_Int32 level
, const char *str
);
71 void logCall( RuntimeCargo
*cargo
, const char *intro
,
72 void * ptr
, const rtl::OUString
& aFunctionName
,
73 const com::sun::star::uno::Sequence
< com::sun::star::uno::Any
> & args
);
74 void logReply( RuntimeCargo
*cargo
, const char *intro
,
75 void * ptr
, const rtl::OUString
& aFunctionName
,
76 const com::sun::star::uno::Any
&returnValue
,
77 const com::sun::star::uno::Sequence
< com::sun::star::uno::Any
> & args
);
78 void logException( RuntimeCargo
*cargo
, const char *intro
,
79 void * ptr
, const rtl::OUString
&aFunctionName
,
80 const void * data
, const com::sun::star::uno::Type
& type
);
81 static const sal_Int32 VAL2STR_MODE_DEEP
= 0;
82 static const sal_Int32 VAL2STR_MODE_SHALLOW
= 1;
83 rtl::OUString
val2str( const void * pVal
, typelib_TypeDescriptionReference
* pTypeRef
, sal_Int32 mode
= VAL2STR_MODE_DEEP
) SAL_THROW( () );
84 //--------------------------------------------------
86 typedef ::std::hash_map
89 com::sun::star::uno::WeakReference
< com::sun::star::script::XInvocation
>,
91 std::equal_to
< PyRef
>
95 typedef ::std::hash_map
100 std::equal_to
<rtl::OUString
>
103 typedef ::std::hash_map
106 com::sun::star::uno::Sequence
< sal_Int16
>,
108 std::equal_to
< rtl::OUString
>
111 typedef ::std::hash_set
< PyRef
, PyRef::Hash
, std::equal_to
<PyRef
> > ClassSet
;
114 const com::sun::star::uno::Any
& targetInterface
,
115 const com::sun::star::uno::Reference
<com::sun::star::lang::XSingleServiceFactory
> & ssf
);
117 PyObject
* PyUNO_new_UNCHECKED (
118 const com::sun::star::uno::Any
& targetInterface
,
119 const com::sun::star::uno::Reference
<com::sun::star::lang::XSingleServiceFactory
> & ssf
);
123 com::sun::star::uno::Reference
<com::sun::star::script::XInvocation2
> xInvocation
;
124 com::sun::star::uno::Any wrappedObject
;
130 PyUNOInternals
* members
;
133 PyRef
ustring2PyUnicode( const rtl::OUString
&source
);
134 PyRef
ustring2PyString( const ::rtl::OUString
& source
);
135 rtl::OUString
pyString2ustring( PyObject
*str
);
138 PyRef
AnyToPyObject (const com::sun::star::uno::Any
& a
, const Runtime
&r
)
139 throw ( com::sun::star::uno::RuntimeException
);
141 com::sun::star::uno::Any
PyObjectToAny (PyObject
* o
)
142 throw ( com::sun::star::uno::RuntimeException
);
144 void raiseInvocationTargetExceptionWhenNeeded( const Runtime
&runtime
)
145 throw ( com::sun::star::reflection::InvocationTargetException
);
147 // bool CheckPyObjectTypes (PyObject* o, Sequence<Type> types);
148 // bool CheckPyObjectType (PyObject* o, Type type); //Only check 1 object.
150 com::sun::star::uno::TypeClass
StringToTypeClass (char* string
);
152 PyRef
PyUNO_callable_new (
153 const com::sun::star::uno::Reference
<com::sun::star::script::XInvocation2
> &xInv
,
154 const rtl::OUString
&methodName
,
155 const com::sun::star::uno::Reference
<com::sun::star::lang::XSingleServiceFactory
> &ssf
,
156 const com::sun::star::uno::Reference
<com::sun::star::script::XTypeConverter
> &tc
,
157 ConversionMode mode
= REJECT_UNO_ANY
);
159 PyObject
* PyUNO_Type_new (const char *typeName
, com::sun::star::uno::TypeClass t
, const Runtime
&r
);
160 PyObject
* PyUNO_Enum_new( const char *enumBase
, const char *enumValue
, const Runtime
&r
);
161 PyObject
* PyUNO_char_new (sal_Unicode c
, const Runtime
&r
);
162 PyObject
*PyUNO_ByteSequence_new( const com::sun::star::uno::Sequence
< sal_Int8
> &, const Runtime
&r
);
164 PyObject
*importToGlobal( PyObject
*typeName
, PyObject
*dict
, PyObject
*targetName
);
166 PyRef
getTypeClass( const Runtime
&);
167 PyRef
getEnumClass( const Runtime
&);
168 PyRef
getBoolClass( const Runtime
&);
169 PyRef
getCharClass( const Runtime
&);
170 PyRef
getByteSequenceClass( const Runtime
& );
171 PyRef
getPyUnoClass( const Runtime
&);
172 PyRef
getClass( const rtl::OUString
& name
, const Runtime
& runtime
);
173 PyRef
getAnyClass( const Runtime
&);
174 PyObject
*PyUNO_invoke( PyObject
*object
, const char *name
, PyObject
*args
);
176 com::sun::star::uno::Any
PyEnum2Enum( PyObject
*obj
)
177 throw ( com::sun::star::uno::RuntimeException
);
178 sal_Bool
PyBool2Bool( PyObject
*o
, const Runtime
& r
)
179 throw ( com::sun::star::uno::RuntimeException
);
180 sal_Unicode
PyChar2Unicode( PyObject
*o
)
181 throw ( com::sun::star::uno::RuntimeException
);
182 com::sun::star::uno::Type
PyType2Type( PyObject
* o
)
183 throw( com::sun::star::uno::RuntimeException
);
185 void raisePyExceptionWithAny( const com::sun::star::uno::Any
&a
);
186 const char *typeClassToString( com::sun::star::uno::TypeClass t
);
188 PyRef
getObjectFromUnoModule( const Runtime
&runtime
, const char * object
)
189 throw ( com::sun::star::uno::RuntimeException
);
191 sal_Bool
isInterfaceClass( const Runtime
&, PyObject
*obj
);
192 bool isInstanceOfStructOrException( PyObject
*obj
);
193 com::sun::star::uno::Sequence
<com::sun::star::uno::Type
> implementsInterfaces(
194 const Runtime
& runtime
, PyObject
*obj
);
198 com::sun::star::uno::Reference
< com::sun::star::lang::XSingleServiceFactory
> xInvocation
;
199 com::sun::star::uno::Reference
< com::sun::star::script::XTypeConverter
> xTypeConverter
;
200 com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
> xContext
;
201 com::sun::star::uno::Reference
< com::sun::star::reflection::XIdlReflection
> xCoreReflection
;
202 com::sun::star::uno::Reference
< com::sun::star::container::XHierarchicalNameAccess
> xTdMgr
;
203 com::sun::star::uno::Reference
< com::sun::star::script::XInvocationAdapterFactory2
> xAdapterFactory
;
204 com::sun::star::uno::Reference
< com::sun::star::beans::XIntrospection
> xIntrospection
;
207 ExceptionClassMap exceptionMap
;
208 ClassSet interfaceSet
;
209 PyRef2Adapter mappedObjects
;
213 PyRef
getUnoModule();
219 struct RuntimeCargo
*cargo
;
221 static void del( PyObject
*self
);
224 const com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
> & xContext
)
225 throw ( com::sun::star::uno::RuntimeException
);
229 class Adapter
: public cppu::WeakImplHelper2
<
230 com::sun::star::script::XInvocation
, com::sun::star::lang::XUnoTunnel
>
232 PyRef mWrappedObject
;
233 PyInterpreterState
*mInterpreter
; // interpreters don't seem to be refcounted !
234 com::sun::star::uno::Sequence
< com::sun::star::uno::Type
> mTypes
;
235 MethodOutIndexMap m_methodOutIndexMap
;
238 com::sun::star::uno::Sequence
< sal_Int16
> getOutIndexes( const rtl::OUString
& functionName
);
242 Adapter( const PyRef
&obj
,
243 const com::sun::star::uno::Sequence
< com::sun::star::uno::Type
> & types
);
245 static com::sun::star::uno::Sequence
< sal_Int8
> getUnoTunnelImplementationId();
246 PyRef
getWrappedObject() { return mWrappedObject
; }
247 com::sun::star::uno::Sequence
< com::sun::star::uno::Type
> getWrappedTypes() { return mTypes
; }
251 virtual com::sun::star::uno::Reference
< ::com::sun::star::beans::XIntrospectionAccess
>
252 SAL_CALL
getIntrospection( ) throw (::com::sun::star::uno::RuntimeException
);
253 virtual ::com::sun::star::uno::Any SAL_CALL
invoke(
254 const ::rtl::OUString
& aFunctionName
,
255 const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& aParams
,
256 ::com::sun::star::uno::Sequence
< sal_Int16
>& aOutParamIndex
,
257 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& aOutParam
)
258 throw (::com::sun::star::lang::IllegalArgumentException
,
259 ::com::sun::star::script::CannotConvertException
,
260 ::com::sun::star::reflection::InvocationTargetException
,
261 ::com::sun::star::uno::RuntimeException
);
263 virtual void SAL_CALL
setValue(
264 const ::rtl::OUString
& aPropertyName
,
265 const ::com::sun::star::uno::Any
& aValue
)
266 throw (::com::sun::star::beans::UnknownPropertyException
,
267 ::com::sun::star::script::CannotConvertException
,
268 ::com::sun::star::reflection::InvocationTargetException
,
269 ::com::sun::star::uno::RuntimeException
);
271 virtual ::com::sun::star::uno::Any SAL_CALL
getValue( const ::rtl::OUString
& aPropertyName
)
272 throw (::com::sun::star::beans::UnknownPropertyException
,
273 ::com::sun::star::uno::RuntimeException
);
274 virtual sal_Bool SAL_CALL
hasMethod( const ::rtl::OUString
& aName
)
275 throw (::com::sun::star::uno::RuntimeException
);
276 virtual sal_Bool SAL_CALL
hasProperty( const ::rtl::OUString
& aName
)
277 throw (::com::sun::star::uno::RuntimeException
);
280 virtual sal_Int64 SAL_CALL
getSomething(
281 const ::com::sun::star::uno::Sequence
< sal_Int8
>& aIdentifier
)
282 throw (::com::sun::star::uno::RuntimeException
);
286 /** releases a refcount on the interpreter object and on another given python object.
288 The function can be called from any thread regardless of whether the global
289 interpreter lock is held.
292 void decreaseRefCount( PyInterpreterState
*interpreter
, PyObject
*object
);