update emoji autocorrect entries from po-files
[LibreOffice.git] / pyuno / source / module / pyuno_impl.hxx
blobe066b7327b12151e0f4a839b825a92d98d4491b2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
19 #ifndef INCLUDED_PYUNO_SOURCE_MODULE_PYUNO_IMPL_HXX
20 #define INCLUDED_PYUNO_SOURCE_MODULE_PYUNO_IMPL_HXX
22 #if defined(_MSC_VER)
23 // Workaround for some horrible hypot() mess
24 #include <math.h>
25 #endif
27 #include <Python.h>
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,
32 #endif
34 #include <pyuno/pyuno.hxx>
36 #include <unordered_map>
37 #include <unordered_set>
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 #include <osl/module.hxx>
56 // In Python 3, the PyString_* functions have been replaced by PyBytes_*
57 // and PyUnicode_* functions.
58 #if PY_MAJOR_VERSION >= 3
60 // compatibility wrappers for Python "str" type (PyUnicode in 3, PyString in 2)
61 inline PyObject* PyStr_FromString(const char *string)
63 return PyUnicode_FromString(string);
66 inline char * PyStr_AsString(PyObject *object)
68 return PyUnicode_AsUTF8(object);
71 inline bool PyStr_Check(PyObject *object)
73 return PyUnicode_Check(object);
76 // compatibility wrappers for Python non-Unicode string/buffer type
77 // (PyBytes in 3, PyString in 2)
78 inline bool PyStrBytes_Check(PyObject *object)
80 return PyBytes_Check(object);
83 inline char* PyStrBytes_AsString(PyObject *object)
85 return PyBytes_AsString(object);
88 inline Py_ssize_t PyStrBytes_Size(PyObject *object)
90 return PyBytes_Size(object);
93 inline PyObject* PyStrBytes_FromStringAndSize(const char *string, Py_ssize_t len)
95 return PyBytes_FromStringAndSize(string, len);
97 #else
98 inline char * PyStr_AsString(PyObject *object)
100 return PyString_AsString(object);
103 inline PyObject* PyStr_FromString(const char *string)
105 return PyString_FromString(string);
108 inline bool PyStr_Check(PyObject *object)
110 return PyString_Check(object);
112 inline bool PyStrBytes_Check(PyObject *object)
114 return PyString_Check(object);
117 inline char* PyStrBytes_AsString(PyObject *object)
119 return PyString_AsString(object);
122 inline Py_ssize_t PyStrBytes_Size(PyObject *object)
124 return PyString_Size(object);
127 inline PyObject* PyStrBytes_FromStringAndSize(const char *string, Py_ssize_t len)
129 return PyString_FromStringAndSize(string, len);
131 #endif /* PY_MAJOR_VERSION >= 3 */
133 namespace pyuno
137 // Logging API - implementation can be found in pyuno_util
139 struct RuntimeCargo;
140 namespace LogLevel
142 // when you add a loglevel, extend the log function !
143 static const sal_Int32 NONE = 0;
144 static const sal_Int32 CALL = 1;
145 static const sal_Int32 ARGS = 2;
148 bool isLog( RuntimeCargo *cargo, sal_Int32 loglevel );
149 void log( RuntimeCargo *cargo, sal_Int32 level, const OUString &logString );
150 void log( RuntimeCargo *cargo, sal_Int32 level, const char *str );
151 void logCall( RuntimeCargo *cargo, const char *intro,
152 void * ptr, const OUString & aFunctionName,
153 const com::sun::star::uno::Sequence< com::sun::star::uno::Any > & args );
154 void logReply( RuntimeCargo *cargo, const char *intro,
155 void * ptr, const OUString & aFunctionName,
156 const com::sun::star::uno::Any &returnValue,
157 const com::sun::star::uno::Sequence< com::sun::star::uno::Any > & args );
158 void logException( RuntimeCargo *cargo, const char *intro,
159 void * ptr, const OUString &aFunctionName,
160 const void * data, const com::sun::star::uno::Type & type );
161 static const sal_Int32 VAL2STR_MODE_DEEP = 0;
162 static const sal_Int32 VAL2STR_MODE_SHALLOW = 1;
163 OUString val2str( const void * pVal, typelib_TypeDescriptionReference * pTypeRef, sal_Int32 mode = VAL2STR_MODE_DEEP );
166 typedef std::unordered_map
168 PyRef,
169 com::sun::star::uno::WeakReference< com::sun::star::script::XInvocation >,
170 PyRef::Hash,
171 std::equal_to< PyRef >
172 > PyRef2Adapter;
175 typedef std::unordered_map
177 OUString,
178 PyRef,
179 OUStringHash,
180 std::equal_to<OUString>
181 > ExceptionClassMap;
183 typedef std::unordered_map
185 OUString,
186 com::sun::star::uno::Sequence< sal_Int16 >,
187 OUStringHash,
188 std::equal_to< OUString >
189 > MethodOutIndexMap;
191 typedef std::unordered_set< PyRef , PyRef::Hash , std::equal_to<PyRef> > ClassSet;
193 int PyUNO_initType();
195 PyObject* PyUNO_new(
196 const com::sun::star::uno::Any & targetInterface,
197 const com::sun::star::uno::Reference<com::sun::star::lang::XSingleServiceFactory> & ssf);
199 PyObject* PyUNO_new_UNCHECKED (
200 const com::sun::star::uno::Any & targetInterface,
201 const com::sun::star::uno::Reference<com::sun::star::lang::XSingleServiceFactory> & ssf);
203 typedef struct
205 com::sun::star::uno::Reference <com::sun::star::script::XInvocation2> xInvocation;
206 com::sun::star::uno::Any wrappedObject;
207 } PyUNOInternals;
209 typedef struct
211 PyObject_HEAD
212 PyUNOInternals* members;
213 } PyUNO;
215 PyRef ustring2PyUnicode( const OUString &source );
216 PyRef ustring2PyString( const OUString & source );
217 OUString pyString2ustring( PyObject *str );
220 PyRef AnyToPyObject (const com::sun::star::uno::Any & a, const Runtime &r )
221 throw ( com::sun::star::uno::RuntimeException );
223 com::sun::star::uno::Any PyObjectToAny (PyObject* o)
224 throw ( com::sun::star::uno::RuntimeException );
226 void raiseInvocationTargetExceptionWhenNeeded( const Runtime &runtime )
227 throw ( com::sun::star::reflection::InvocationTargetException );
229 com::sun::star::uno::TypeClass StringToTypeClass (char* string);
231 PyRef PyUNO_callable_new (
232 const com::sun::star::uno::Reference<com::sun::star::script::XInvocation2> &xInv,
233 const OUString &methodName,
234 ConversionMode mode = REJECT_UNO_ANY );
236 PyObject* PyUNO_Type_new (const char *typeName , com::sun::star::uno::TypeClass t , const Runtime &r );
237 PyObject* PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r );
238 PyObject* PyUNO_char_new (sal_Unicode c , const Runtime &r);
239 PyObject *PyUNO_ByteSequence_new( const com::sun::star::uno::Sequence< sal_Int8 > &, const Runtime &r );
241 PyRef getTypeClass( const Runtime &);
242 PyRef getEnumClass( const Runtime &);
243 PyRef getBoolClass( const Runtime &);
244 PyRef getCharClass( const Runtime &);
245 PyRef getByteSequenceClass( const Runtime & );
246 PyRef getPyUnoClass();
247 PyRef getClass( const OUString & name , const Runtime & runtime );
248 PyRef getAnyClass( const Runtime &);
249 PyObject *PyUNO_invoke( PyObject *object, const char *name , PyObject *args );
251 com::sun::star::uno::Any PyEnum2Enum( PyObject *obj )
252 throw ( com::sun::star::uno::RuntimeException );
253 bool PyBool2Bool( PyObject *o, const Runtime & r )
254 throw ( com::sun::star::uno::RuntimeException );
255 sal_Unicode PyChar2Unicode( PyObject *o )
256 throw ( com::sun::star::uno::RuntimeException );
257 com::sun::star::uno::Type PyType2Type( PyObject * o )
258 throw( com::sun::star::uno::RuntimeException );
260 void raisePyExceptionWithAny( const com::sun::star::uno::Any &a );
261 const char *typeClassToString( com::sun::star::uno::TypeClass t );
263 PyRef getObjectFromUnoModule( const Runtime &runtime, const char * object )
264 throw ( com::sun::star::uno::RuntimeException );
266 bool isInterfaceClass( const Runtime &, PyObject *obj );
267 bool isInstanceOfStructOrException( PyObject *obj);
268 com::sun::star::uno::Sequence<com::sun::star::uno::Type> implementsInterfaces(
269 const Runtime & runtime, PyObject *obj );
271 struct RuntimeCargo
273 com::sun::star::uno::Reference< com::sun::star::lang::XSingleServiceFactory > xInvocation;
274 com::sun::star::uno::Reference< com::sun::star::script::XTypeConverter> xTypeConverter;
275 com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > xContext;
276 com::sun::star::uno::Reference< com::sun::star::reflection::XIdlReflection > xCoreReflection;
277 com::sun::star::uno::Reference< com::sun::star::container::XHierarchicalNameAccess > xTdMgr;
278 com::sun::star::uno::Reference< com::sun::star::script::XInvocationAdapterFactory2 > xAdapterFactory;
279 com::sun::star::uno::Reference< com::sun::star::beans::XIntrospection > xIntrospection;
280 PyRef dictUnoModule;
281 osl::Module testModule;
282 bool valid;
283 ExceptionClassMap exceptionMap;
284 ClassSet interfaceSet;
285 PyRef2Adapter mappedObjects;
286 FILE *logFile;
287 sal_Int32 logLevel;
289 PyRef getUnoModule();
292 struct stRuntimeImpl
294 PyObject_HEAD
295 struct RuntimeCargo *cargo;
296 public:
297 static void del( PyObject *self );
299 static PyRef create(
300 const com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > & xContext )
301 throw ( com::sun::star::uno::RuntimeException );
305 class Adapter : public cppu::WeakImplHelper2<
306 com::sun::star::script::XInvocation, com::sun::star::lang::XUnoTunnel >
308 PyRef mWrappedObject;
309 PyInterpreterState *mInterpreter; // interpreters don't seem to be refcounted !
310 com::sun::star::uno::Sequence< com::sun::star::uno::Type > mTypes;
311 MethodOutIndexMap m_methodOutIndexMap;
313 private:
314 com::sun::star::uno::Sequence< sal_Int16 > getOutIndexes( const OUString & functionName );
316 public:
317 public:
318 Adapter( const PyRef &obj,
319 const com::sun::star::uno::Sequence< com::sun::star::uno::Type > & types );
321 static com::sun::star::uno::Sequence< sal_Int8 > getUnoTunnelImplementationId();
322 PyRef getWrappedObject() { return mWrappedObject; }
323 com::sun::star::uno::Sequence< com::sun::star::uno::Type > getWrappedTypes() { return mTypes; }
324 virtual ~Adapter();
326 // XInvocation
327 virtual com::sun::star::uno::Reference< ::com::sun::star::beans::XIntrospectionAccess >
328 SAL_CALL getIntrospection( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
329 virtual ::com::sun::star::uno::Any SAL_CALL invoke(
330 const OUString& aFunctionName,
331 const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aParams,
332 ::com::sun::star::uno::Sequence< sal_Int16 >& aOutParamIndex,
333 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aOutParam )
334 throw (::com::sun::star::lang::IllegalArgumentException,
335 ::com::sun::star::script::CannotConvertException,
336 ::com::sun::star::reflection::InvocationTargetException,
337 ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
339 virtual void SAL_CALL setValue(
340 const OUString& aPropertyName,
341 const ::com::sun::star::uno::Any& aValue )
342 throw (::com::sun::star::beans::UnknownPropertyException,
343 ::com::sun::star::script::CannotConvertException,
344 ::com::sun::star::reflection::InvocationTargetException,
345 ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
347 virtual ::com::sun::star::uno::Any SAL_CALL getValue( const OUString& aPropertyName )
348 throw (::com::sun::star::beans::UnknownPropertyException,
349 ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
350 virtual sal_Bool SAL_CALL hasMethod( const OUString& aName )
351 throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
352 virtual sal_Bool SAL_CALL hasProperty( const OUString& aName )
353 throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
355 // XUnoTunnel
356 virtual sal_Int64 SAL_CALL getSomething(
357 const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier )
358 throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
362 /** releases a refcount on the interpreter object and on another given python object.
364 The function can be called from any thread regardless of whether the global
365 interpreter lock is held.
368 void decreaseRefCount( PyInterpreterState *interpreter, PyObject *object );
372 #endif
374 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */