1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #if defined(_MSC_VER) && (_MSC_VER >= 1600)
23 // Workaround for some horrible hypot() mess
29 //Must define PyVarObject_HEAD_INIT for Python 2.5 or older
30 #ifndef PyVarObject_HEAD_INIT
31 #define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
34 #include <pyuno/pyuno.hxx>
36 #include <boost/unordered_map.hpp>
37 #include <boost/unordered_set.hpp>
39 #include <com/sun/star/beans/XIntrospection.hpp>
40 #include <com/sun/star/script/XTypeConverter.hpp>
41 #include <com/sun/star/script/XInvocation2.hpp>
42 #include <com/sun/star/script/XInvocationAdapterFactory2.hpp>
44 #include <com/sun/star/reflection/XIdlReflection.hpp>
46 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
48 #include <com/sun/star/lang/XUnoTunnel.hpp>
49 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
51 #include <cppuhelper/implbase2.hxx>
52 #include <cppuhelper/weakref.hxx>
54 // In Python 3, the PyString_* functions have been replaced by PyBytes_*
55 // and PyUnicode_* functions.
56 #if PY_MAJOR_VERSION >= 3
58 // compatibility wrappers for Python "str" type (PyUnicode in 3, PyString in 2)
59 inline PyObject
* PyStr_FromString(const char *string
)
61 return PyUnicode_FromString(string
);
64 inline char * PyStr_AsString(PyObject
*object
)
66 return PyUnicode_AsUTF8(object
);
69 inline int PyStr_Check(PyObject
*object
)
71 return PyUnicode_Check(object
);
74 // compatibility wrappers for Python non-Unicode string/buffer type
75 // (PyBytes in 3, PyString in 2)
76 inline int PyStrBytes_Check(PyObject
*object
)
78 return PyBytes_Check(object
);
81 inline char* PyStrBytes_AsString(PyObject
*object
)
83 return PyBytes_AsString(object
);
86 inline Py_ssize_t
PyStrBytes_Size(PyObject
*object
)
88 return PyBytes_Size(object
);
91 inline PyObject
* PyStrBytes_FromStringAndSize(const char *string
, Py_ssize_t len
)
93 return PyBytes_FromStringAndSize(string
, len
);
96 inline char * PyStr_AsString(PyObject
*object
)
98 return PyString_AsString(object
);
101 inline PyObject
* PyStr_FromString(const char *string
)
103 return PyString_FromString(string
);
106 inline int PyStr_Check(PyObject
*object
)
108 return PyString_Check(object
);
110 inline int PyStrBytes_Check(PyObject
*object
)
112 return PyString_Check(object
);
115 inline char* PyStrBytes_AsString(PyObject
*object
)
117 return PyString_AsString(object
);
120 inline Py_ssize_t
PyStrBytes_Size(PyObject
*object
)
122 return PyString_Size(object
);
125 inline PyObject
* PyStrBytes_FromStringAndSize(const char *string
, Py_ssize_t len
)
127 return PyString_FromStringAndSize(string
, len
);
129 #endif /* PY_MAJOR_VERSION >= 3 */
134 //--------------------------------------------------
135 // Logging API - implementation can be found in pyuno_util
136 //--------------------------------------------------
140 // when you add a loglevel, extend the log function !
141 static const sal_Int32 NONE
= 0;
142 static const sal_Int32 CALL
= 1;
143 static const sal_Int32 ARGS
= 2;
146 bool isLog( RuntimeCargo
*cargo
, sal_Int32 loglevel
);
147 void log( RuntimeCargo
*cargo
, sal_Int32 level
, const OUString
&logString
);
148 void log( RuntimeCargo
*cargo
, sal_Int32 level
, const char *str
);
149 void logCall( RuntimeCargo
*cargo
, const char *intro
,
150 void * ptr
, const OUString
& aFunctionName
,
151 const com::sun::star::uno::Sequence
< com::sun::star::uno::Any
> & args
);
152 void logReply( RuntimeCargo
*cargo
, const char *intro
,
153 void * ptr
, const OUString
& aFunctionName
,
154 const com::sun::star::uno::Any
&returnValue
,
155 const com::sun::star::uno::Sequence
< com::sun::star::uno::Any
> & args
);
156 void logException( RuntimeCargo
*cargo
, const char *intro
,
157 void * ptr
, const OUString
&aFunctionName
,
158 const void * data
, const com::sun::star::uno::Type
& type
);
159 static const sal_Int32 VAL2STR_MODE_DEEP
= 0;
160 static const sal_Int32 VAL2STR_MODE_SHALLOW
= 1;
161 OUString
val2str( const void * pVal
, typelib_TypeDescriptionReference
* pTypeRef
, sal_Int32 mode
= VAL2STR_MODE_DEEP
) SAL_THROW(());
162 //--------------------------------------------------
164 typedef ::boost::unordered_map
167 com::sun::star::uno::WeakReference
< com::sun::star::script::XInvocation
>,
169 std::equal_to
< PyRef
>
173 typedef ::boost::unordered_map
178 std::equal_to
<OUString
>
181 typedef ::boost::unordered_map
184 com::sun::star::uno::Sequence
< sal_Int16
>,
186 std::equal_to
< OUString
>
189 typedef ::boost::unordered_set
< PyRef
, PyRef::Hash
, std::equal_to
<PyRef
> > ClassSet
;
191 int PyUNO_initType();
194 const com::sun::star::uno::Any
& targetInterface
,
195 const com::sun::star::uno::Reference
<com::sun::star::lang::XSingleServiceFactory
> & ssf
);
197 PyObject
* PyUNO_new_UNCHECKED (
198 const com::sun::star::uno::Any
& targetInterface
,
199 const com::sun::star::uno::Reference
<com::sun::star::lang::XSingleServiceFactory
> & ssf
);
203 com::sun::star::uno::Reference
<com::sun::star::script::XInvocation2
> xInvocation
;
204 com::sun::star::uno::Any wrappedObject
;
210 PyUNOInternals
* members
;
213 PyRef
ustring2PyUnicode( const OUString
&source
);
214 PyRef
ustring2PyString( const OUString
& source
);
215 OUString
pyString2ustring( PyObject
*str
);
218 PyRef
AnyToPyObject (const com::sun::star::uno::Any
& a
, const Runtime
&r
)
219 throw ( com::sun::star::uno::RuntimeException
);
221 com::sun::star::uno::Any
PyObjectToAny (PyObject
* o
)
222 throw ( com::sun::star::uno::RuntimeException
);
224 void raiseInvocationTargetExceptionWhenNeeded( const Runtime
&runtime
)
225 throw ( com::sun::star::reflection::InvocationTargetException
);
227 com::sun::star::uno::TypeClass
StringToTypeClass (char* string
);
229 PyRef
PyUNO_callable_new (
230 const com::sun::star::uno::Reference
<com::sun::star::script::XInvocation2
> &xInv
,
231 const OUString
&methodName
,
232 ConversionMode mode
= REJECT_UNO_ANY
);
234 PyObject
* PyUNO_Type_new (const char *typeName
, com::sun::star::uno::TypeClass t
, const Runtime
&r
);
235 PyObject
* PyUNO_Enum_new( const char *enumBase
, const char *enumValue
, const Runtime
&r
);
236 PyObject
* PyUNO_char_new (sal_Unicode c
, const Runtime
&r
);
237 PyObject
*PyUNO_ByteSequence_new( const com::sun::star::uno::Sequence
< sal_Int8
> &, const Runtime
&r
);
239 PyRef
getTypeClass( const Runtime
&);
240 PyRef
getEnumClass( const Runtime
&);
241 PyRef
getBoolClass( const Runtime
&);
242 PyRef
getCharClass( const Runtime
&);
243 PyRef
getByteSequenceClass( const Runtime
& );
244 PyRef
getPyUnoClass();
245 PyRef
getClass( const OUString
& name
, const Runtime
& runtime
);
246 PyRef
getAnyClass( const Runtime
&);
247 PyObject
*PyUNO_invoke( PyObject
*object
, const char *name
, PyObject
*args
);
249 com::sun::star::uno::Any
PyEnum2Enum( PyObject
*obj
)
250 throw ( com::sun::star::uno::RuntimeException
);
251 sal_Bool
PyBool2Bool( PyObject
*o
, const Runtime
& r
)
252 throw ( com::sun::star::uno::RuntimeException
);
253 sal_Unicode
PyChar2Unicode( PyObject
*o
)
254 throw ( com::sun::star::uno::RuntimeException
);
255 com::sun::star::uno::Type
PyType2Type( PyObject
* o
)
256 throw( com::sun::star::uno::RuntimeException
);
258 void raisePyExceptionWithAny( const com::sun::star::uno::Any
&a
);
259 const char *typeClassToString( com::sun::star::uno::TypeClass t
);
261 PyRef
getObjectFromUnoModule( const Runtime
&runtime
, const char * object
)
262 throw ( com::sun::star::uno::RuntimeException
);
264 sal_Bool
isInterfaceClass( const Runtime
&, PyObject
*obj
);
265 bool isInstanceOfStructOrException( PyObject
*obj
);
266 com::sun::star::uno::Sequence
<com::sun::star::uno::Type
> implementsInterfaces(
267 const Runtime
& runtime
, PyObject
*obj
);
271 com::sun::star::uno::Reference
< com::sun::star::lang::XSingleServiceFactory
> xInvocation
;
272 com::sun::star::uno::Reference
< com::sun::star::script::XTypeConverter
> xTypeConverter
;
273 com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
> xContext
;
274 com::sun::star::uno::Reference
< com::sun::star::reflection::XIdlReflection
> xCoreReflection
;
275 com::sun::star::uno::Reference
< com::sun::star::container::XHierarchicalNameAccess
> xTdMgr
;
276 com::sun::star::uno::Reference
< com::sun::star::script::XInvocationAdapterFactory2
> xAdapterFactory
;
277 com::sun::star::uno::Reference
< com::sun::star::beans::XIntrospection
> xIntrospection
;
280 ExceptionClassMap exceptionMap
;
281 ClassSet interfaceSet
;
282 PyRef2Adapter mappedObjects
;
286 PyRef
getUnoModule();
292 struct RuntimeCargo
*cargo
;
294 static void del( PyObject
*self
);
297 const com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
> & xContext
)
298 throw ( com::sun::star::uno::RuntimeException
);
302 class Adapter
: public cppu::WeakImplHelper2
<
303 com::sun::star::script::XInvocation
, com::sun::star::lang::XUnoTunnel
>
305 PyRef mWrappedObject
;
306 PyInterpreterState
*mInterpreter
; // interpreters don't seem to be refcounted !
307 com::sun::star::uno::Sequence
< com::sun::star::uno::Type
> mTypes
;
308 MethodOutIndexMap m_methodOutIndexMap
;
311 com::sun::star::uno::Sequence
< sal_Int16
> getOutIndexes( const OUString
& functionName
);
315 Adapter( const PyRef
&obj
,
316 const com::sun::star::uno::Sequence
< com::sun::star::uno::Type
> & types
);
318 static com::sun::star::uno::Sequence
< sal_Int8
> getUnoTunnelImplementationId();
319 PyRef
getWrappedObject() { return mWrappedObject
; }
320 com::sun::star::uno::Sequence
< com::sun::star::uno::Type
> getWrappedTypes() { return mTypes
; }
324 virtual com::sun::star::uno::Reference
< ::com::sun::star::beans::XIntrospectionAccess
>
325 SAL_CALL
getIntrospection( ) throw (::com::sun::star::uno::RuntimeException
);
326 virtual ::com::sun::star::uno::Any SAL_CALL
invoke(
327 const OUString
& aFunctionName
,
328 const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& aParams
,
329 ::com::sun::star::uno::Sequence
< sal_Int16
>& aOutParamIndex
,
330 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& aOutParam
)
331 throw (::com::sun::star::lang::IllegalArgumentException
,
332 ::com::sun::star::script::CannotConvertException
,
333 ::com::sun::star::reflection::InvocationTargetException
,
334 ::com::sun::star::uno::RuntimeException
);
336 virtual void SAL_CALL
setValue(
337 const OUString
& aPropertyName
,
338 const ::com::sun::star::uno::Any
& aValue
)
339 throw (::com::sun::star::beans::UnknownPropertyException
,
340 ::com::sun::star::script::CannotConvertException
,
341 ::com::sun::star::reflection::InvocationTargetException
,
342 ::com::sun::star::uno::RuntimeException
);
344 virtual ::com::sun::star::uno::Any SAL_CALL
getValue( const OUString
& aPropertyName
)
345 throw (::com::sun::star::beans::UnknownPropertyException
,
346 ::com::sun::star::uno::RuntimeException
);
347 virtual sal_Bool SAL_CALL
hasMethod( const OUString
& aName
)
348 throw (::com::sun::star::uno::RuntimeException
);
349 virtual sal_Bool SAL_CALL
hasProperty( const OUString
& aName
)
350 throw (::com::sun::star::uno::RuntimeException
);
353 virtual sal_Int64 SAL_CALL
getSomething(
354 const ::com::sun::star::uno::Sequence
< sal_Int8
>& aIdentifier
)
355 throw (::com::sun::star::uno::RuntimeException
);
359 /** releases a refcount on the interpreter object and on another given python object.
361 The function can be called from any thread regardless of whether the global
362 interpreter lock is held.
365 void decreaseRefCount( PyInterpreterState
*interpreter
, PyObject
*object
);
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */