1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#ifndef INCLUDED_PYUNO_SOURCE_MODULE_PYUNO_IMPL_HXX
#define INCLUDED_PYUNO_SOURCE_MODULE_PYUNO_IMPL_HXX
#if defined(_MSC_VER) && (_MSC_VER >= 1600)
// Workaround for some horrible hypot() mess
#include <math.h>
#endif
#include <Python.h>
//Must define PyVarObject_HEAD_INIT for Python 2.5 or older
#ifndef PyVarObject_HEAD_INIT
#define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
#endif
#include <pyuno/pyuno.hxx>
#include <boost/unordered_map.hpp>
#include <boost/unordered_set.hpp>
#include <com/sun/star/beans/XIntrospection.hpp>
#include <com/sun/star/script/XTypeConverter.hpp>
#include <com/sun/star/script/XInvocation2.hpp>
#include <com/sun/star/script/XInvocationAdapterFactory2.hpp>
#include <com/sun/star/reflection/XIdlReflection.hpp>
#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
#include <com/sun/star/lang/XUnoTunnel.hpp>
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
#include <cppuhelper/implbase2.hxx>
#include <cppuhelper/weakref.hxx>
#include <osl/module.hxx>
// In Python 3, the PyString_* functions have been replaced by PyBytes_*
// and PyUnicode_* functions.
#if PY_MAJOR_VERSION >= 3
// compatibility wrappers for Python "str" type (PyUnicode in 3, PyString in 2)
inline PyObject* PyStr_FromString(const char *string)
{
return PyUnicode_FromString(string);
}
inline char * PyStr_AsString(PyObject *object)
{
return PyUnicode_AsUTF8(object);
}
inline bool PyStr_Check(PyObject *object)
{
return PyUnicode_Check(object);
}
// compatibility wrappers for Python non-Unicode string/buffer type
// (PyBytes in 3, PyString in 2)
inline bool PyStrBytes_Check(PyObject *object)
{
return PyBytes_Check(object);
}
inline char* PyStrBytes_AsString(PyObject *object)
{
return PyBytes_AsString(object);
}
inline Py_ssize_t PyStrBytes_Size(PyObject *object)
{
return PyBytes_Size(object);
}
inline PyObject* PyStrBytes_FromStringAndSize(const char *string, Py_ssize_t len)
{
return PyBytes_FromStringAndSize(string, len);
}
#else
inline char * PyStr_AsString(PyObject *object)
{
return PyString_AsString(object);
}
inline PyObject* PyStr_FromString(const char *string)
{
return PyString_FromString(string);
}
inline bool PyStr_Check(PyObject *object)
{
return PyString_Check(object);
}
inline bool PyStrBytes_Check(PyObject *object)
{
return PyString_Check(object);
}
inline char* PyStrBytes_AsString(PyObject *object)
{
return PyString_AsString(object);
}
inline Py_ssize_t PyStrBytes_Size(PyObject *object)
{
return PyString_Size(object);
}
inline PyObject* PyStrBytes_FromStringAndSize(const char *string, Py_ssize_t len)
{
return PyString_FromStringAndSize(string, len);
}
#endif /* PY_MAJOR_VERSION >= 3 */
namespace pyuno
{
// Logging API - implementation can be found in pyuno_util
struct RuntimeCargo;
namespace LogLevel
{
// when you add a loglevel, extend the log function !
static const sal_Int32 NONE = 0;
static const sal_Int32 CALL = 1;
static const sal_Int32 ARGS = 2;
}
bool isLog( RuntimeCargo *cargo, sal_Int32 loglevel );
void log( RuntimeCargo *cargo, sal_Int32 level, const OUString &logString );
void log( RuntimeCargo *cargo, sal_Int32 level, const char *str );
void logCall( RuntimeCargo *cargo, const char *intro,
void * ptr, const OUString & aFunctionName,
const com::sun::star::uno::Sequence< com::sun::star::uno::Any > & args );
void logReply( RuntimeCargo *cargo, const char *intro,
void * ptr, const OUString & aFunctionName,
const com::sun::star::uno::Any &returnValue,
const com::sun::star::uno::Sequence< com::sun::star::uno::Any > & args );
void logException( RuntimeCargo *cargo, const char *intro,
void * ptr, const OUString &aFunctionName,
const void * data, const com::sun::star::uno::Type & type );
static const sal_Int32 VAL2STR_MODE_DEEP = 0;
static const sal_Int32 VAL2STR_MODE_SHALLOW = 1;
OUString val2str( const void * pVal, typelib_TypeDescriptionReference * pTypeRef, sal_Int32 mode = VAL2STR_MODE_DEEP ) SAL_THROW(());
typedef ::boost::unordered_map
<
PyRef,
com::sun::star::uno::WeakReference< com::sun::star::script::XInvocation >,
PyRef::Hash,
std::equal_to< PyRef >
> PyRef2Adapter;
typedef ::boost::unordered_map
<
OUString,
PyRef,
OUStringHash,
std::equal_to<OUString>
> ExceptionClassMap;
typedef ::boost::unordered_map
<
OUString,
com::sun::star::uno::Sequence< sal_Int16 >,
OUStringHash,
std::equal_to< OUString >
> MethodOutIndexMap;
typedef ::boost::unordered_set< PyRef , PyRef::Hash , std::equal_to<PyRef> > ClassSet;
int PyUNO_initType();
PyObject* PyUNO_new(
const com::sun::star::uno::Any & targetInterface,
const com::sun::star::uno::Reference<com::sun::star::lang::XSingleServiceFactory> & ssf);
PyObject* PyUNO_new_UNCHECKED (
const com::sun::star::uno::Any & targetInterface,
const com::sun::star::uno::Reference<com::sun::star::lang::XSingleServiceFactory> & ssf);
typedef struct
{
com::sun::star::uno::Reference <com::sun::star::script::XInvocation2> xInvocation;
com::sun::star::uno::Any wrappedObject;
} PyUNOInternals;
typedef struct
{
PyObject_HEAD
PyUNOInternals* members;
} PyUNO;
PyRef ustring2PyUnicode( const OUString &source );
PyRef ustring2PyString( const OUString & source );
OUString pyString2ustring( PyObject *str );
PyRef AnyToPyObject (const com::sun::star::uno::Any & a, const Runtime &r )
throw ( com::sun::star::uno::RuntimeException );
com::sun::star::uno::Any PyObjectToAny (PyObject* o)
throw ( com::sun::star::uno::RuntimeException );
void raiseInvocationTargetExceptionWhenNeeded( const Runtime &runtime )
throw ( com::sun::star::reflection::InvocationTargetException );
com::sun::star::uno::TypeClass StringToTypeClass (char* string);
PyRef PyUNO_callable_new (
const com::sun::star::uno::Reference<com::sun::star::script::XInvocation2> &xInv,
const OUString &methodName,
ConversionMode mode = REJECT_UNO_ANY );
PyObject* PyUNO_Type_new (const char *typeName , com::sun::star::uno::TypeClass t , const Runtime &r );
PyObject* PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r );
PyObject* PyUNO_char_new (sal_Unicode c , const Runtime &r);
PyObject *PyUNO_ByteSequence_new( const com::sun::star::uno::Sequence< sal_Int8 > &, const Runtime &r );
PyRef getTypeClass( const Runtime &);
PyRef getEnumClass( const Runtime &);
PyRef getBoolClass( const Runtime &);
PyRef getCharClass( const Runtime &);
PyRef getByteSequenceClass( const Runtime & );
PyRef getPyUnoClass();
PyRef getClass( const OUString & name , const Runtime & runtime );
PyRef getAnyClass( const Runtime &);
PyObject *PyUNO_invoke( PyObject *object, const char *name , PyObject *args );
com::sun::star::uno::Any PyEnum2Enum( PyObject *obj )
throw ( com::sun::star::uno::RuntimeException );
bool PyBool2Bool( PyObject *o, const Runtime & r )
throw ( com::sun::star::uno::RuntimeException );
sal_Unicode PyChar2Unicode( PyObject *o )
throw ( com::sun::star::uno::RuntimeException );
com::sun::star::uno::Type PyType2Type( PyObject * o )
throw( com::sun::star::uno::RuntimeException );
void raisePyExceptionWithAny( const com::sun::star::uno::Any &a );
const char *typeClassToString( com::sun::star::uno::TypeClass t );
PyRef getObjectFromUnoModule( const Runtime &runtime, const char * object )
throw ( com::sun::star::uno::RuntimeException );
bool isInterfaceClass( const Runtime &, PyObject *obj );
bool isInstanceOfStructOrException( PyObject *obj);
com::sun::star::uno::Sequence<com::sun::star::uno::Type> implementsInterfaces(
const Runtime & runtime, PyObject *obj );
struct RuntimeCargo
{
com::sun::star::uno::Reference< com::sun::star::lang::XSingleServiceFactory > xInvocation;
com::sun::star::uno::Reference< com::sun::star::script::XTypeConverter> xTypeConverter;
com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > xContext;
com::sun::star::uno::Reference< com::sun::star::reflection::XIdlReflection > xCoreReflection;
com::sun::star::uno::Reference< com::sun::star::container::XHierarchicalNameAccess > xTdMgr;
com::sun::star::uno::Reference< com::sun::star::script::XInvocationAdapterFactory2 > xAdapterFactory;
com::sun::star::uno::Reference< com::sun::star::beans::XIntrospection > xIntrospection;
PyRef dictUnoModule;
osl::Module testModule;
bool valid;
ExceptionClassMap exceptionMap;
ClassSet interfaceSet;
PyRef2Adapter mappedObjects;
FILE *logFile;
sal_Int32 logLevel;
PyRef getUnoModule();
};
struct stRuntimeImpl
{
PyObject_HEAD
struct RuntimeCargo *cargo;
public:
static void del( PyObject *self );
static PyRef create(
const com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > & xContext )
throw ( com::sun::star::uno::RuntimeException );
};
class Adapter : public cppu::WeakImplHelper2<
com::sun::star::script::XInvocation, com::sun::star::lang::XUnoTunnel >
{
PyRef mWrappedObject;
PyInterpreterState *mInterpreter; // interpreters don't seem to be refcounted !
com::sun::star::uno::Sequence< com::sun::star::uno::Type > mTypes;
MethodOutIndexMap m_methodOutIndexMap;
private:
com::sun::star::uno::Sequence< sal_Int16 > getOutIndexes( const OUString & functionName );
public:
public:
Adapter( const PyRef &obj,
const com::sun::star::uno::Sequence< com::sun::star::uno::Type > & types );
static com::sun::star::uno::Sequence< sal_Int8 > getUnoTunnelImplementationId();
PyRef getWrappedObject() { return mWrappedObject; }
com::sun::star::uno::Sequence< com::sun::star::uno::Type > getWrappedTypes() { return mTypes; }
virtual ~Adapter();
// XInvocation
virtual com::sun::star::uno::Reference< ::com::sun::star::beans::XIntrospectionAccess >
SAL_CALL getIntrospection( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
virtual ::com::sun::star::uno::Any SAL_CALL invoke(
const OUString& aFunctionName,
const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aParams,
::com::sun::star::uno::Sequence< sal_Int16 >& aOutParamIndex,
::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aOutParam )
throw (::com::sun::star::lang::IllegalArgumentException,
::com::sun::star::script::CannotConvertException,
::com::sun::star::reflection::InvocationTargetException,
::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
virtual void SAL_CALL setValue(
const OUString& aPropertyName,
const ::com::sun::star::uno::Any& aValue )
throw (::com::sun::star::beans::UnknownPropertyException,
::com::sun::star::script::CannotConvertException,
::com::sun::star::reflection::InvocationTargetException,
::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
virtual ::com::sun::star::uno::Any SAL_CALL getValue( const OUString& aPropertyName )
throw (::com::sun::star::beans::UnknownPropertyException,
::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
virtual sal_Bool SAL_CALL hasMethod( const OUString& aName )
throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
virtual sal_Bool SAL_CALL hasProperty( const OUString& aName )
throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
// XUnoTunnel
virtual sal_Int64 SAL_CALL getSomething(
const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier )
throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
};
/** releases a refcount on the interpreter object and on another given python object.
The function can be called from any thread regardless of whether the global
interpreter lock is held.
*/
void decreaseRefCount( PyInterpreterState *interpreter, PyObject *object );
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|