LibreOffice Module pyuno (master) 1
pyuno.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#pragma once
21
22#ifndef Py_PYTHON_H
23#include <Python.h>
24#endif // #ifdef Py_PYTHON_H
25
26#include <com/sun/star/uno/Any.hxx>
27
28namespace com::sun::star::uno { class XComponentContext; }
29namespace com::sun::star::uno { template <typename > class Reference; }
30
43#if defined LO_DLLIMPLEMENTATION_PYUNO
44#define LO_DLLPUBLIC_PYUNO SAL_DLLPUBLIC_EXPORT
45#else
46#define LO_DLLPUBLIC_PYUNO SAL_DLLPUBLIC_IMPORT
47#endif
48
56extern "C" LO_DLLPUBLIC_PYUNO PyObject* PyInit_pyuno();
57
58namespace pyuno
59{
60
62{
66};
67
79class PyRef
80{
81 PyObject *m;
82public:
83 PyRef () : m(nullptr) {}
84 PyRef( PyObject * p ) : m( p ) { Py_XINCREF( m ); }
85
86 PyRef( PyObject * p, __sal_NoAcquire ) : m( p ) {}
87
88 PyRef( PyObject * p, __sal_NoAcquire, NotNull ) : m( p )
89 {
90 if (!m)
91 throw std::bad_alloc();
92 }
93
94 PyRef(const PyRef &r) : m(r.get()) { Py_XINCREF(m); }
95 PyRef(PyRef &&r) noexcept : m(r.get()) { r.scratch(); }
96
97 ~PyRef() { Py_XDECREF( m ); }
98
99 PyObject *get() const noexcept { return m; }
100
101 PyObject * getAcquired() const
102 {
103 Py_XINCREF( m );
104 return m;
105 }
106
108 {
109 PyObject *tmp = m;
110 m = r.getAcquired();
111 Py_XDECREF(tmp);
112 return *this;
113 }
114
116 {
117 PyObject *tmp = m;
118 m = r.get();
119 r.scratch();
120 Py_XDECREF(tmp);
121 return *this;
122 }
123
124 bool operator == ( const PyRef & r ) const
125 {
126 return r.get() == m;
127 }
128
131 void scratch() noexcept
132 {
133 m = nullptr;
134 }
135
139 bool is() const
140 {
141 return m != nullptr;
142 }
143
144 struct Hash
145 {
146 sal_IntPtr operator () ( const PyRef &r) const { return reinterpret_cast<sal_IntPtr>( r.get() ); }
147 };
148};
149
150//struct stRuntimeImpl;
152
154
155
164{
166
171 bool pyIterUnpack( PyObject *const, css::uno::Any & ) const;
172public:
173 ~Runtime( );
174
186 Runtime();
187
188 Runtime( const Runtime & );
189 Runtime & operator = ( const Runtime & );
190
203 static void initialize(
204 const css::uno::Reference< css::uno::XComponentContext > & ctx );
205
210 static bool isInitialized();
211
222 PyRef any2PyObject (const css::uno::Any &source ) const;
223
232 css::uno::Any pyObject2Any (
233 const PyRef & source , enum ConversionMode mode = REJECT_UNO_ANY ) const;
234
237 css::uno::Any extractUnoException(
238 const PyRef & excType, const PyRef & excValue, const PyRef & excTraceback) const;
239
242 RuntimeImpl *getImpl() const { return impl; }
243};
244
245
272{
273 PyThreadState *tstate;
275 PyThreadAttach ( const PyThreadAttach & ) = delete;
276 PyThreadAttach & operator = ( const PyThreadAttach & ) = delete;
277public:
278
286 PyThreadAttach( PyInterpreterState *interp);
287
288
292};
293
300{
301 PyThreadState *tstate;
302 PyThreadDetach ( const PyThreadDetach & ) = delete;
304
305public:
317};
318
319}
320
321/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Helper class for keeping references to python objects.
Definition: pyuno.hxx:80
PyRef(PyRef &&r) noexcept
Definition: pyuno.hxx:95
void scratch() noexcept
clears the reference without decreasing the reference count only seldom needed !
Definition: pyuno.hxx:131
bool is() const
returns 1 when the reference points to a python object python object, otherwise 0.
Definition: pyuno.hxx:139
PyRef(PyObject *p, __sal_NoAcquire, NotNull)
Definition: pyuno.hxx:88
PyRef & operator=(PyRef &&r)
Definition: pyuno.hxx:115
PyRef(PyObject *p, __sal_NoAcquire)
Definition: pyuno.hxx:86
PyRef(PyObject *p)
Definition: pyuno.hxx:84
PyObject * get() const noexcept
Definition: pyuno.hxx:99
PyObject * m
Definition: pyuno.hxx:81
PyRef & operator=(const PyRef &r)
Definition: pyuno.hxx:107
bool operator==(const PyRef &r) const
Definition: pyuno.hxx:124
PyObject * getAcquired() const
Definition: pyuno.hxx:101
PyRef(const PyRef &r)
Definition: pyuno.hxx:94
helper class for attaching the current thread to the python runtime.
Definition: pyuno.hxx:272
PyThreadState * tstate
Definition: pyuno.hxx:273
PyThreadAttach(const PyThreadAttach &)=delete
helper class for detaching the current thread from the python runtime to do some blocking,...
Definition: pyuno.hxx:300
~PyThreadDetach()
Acquires the global interpreter lock again.
PyThreadDetach()
Releases the global interpreter lock.
PyThreadDetach(const PyThreadDetach &)=delete
PyThreadDetach & operator=(const PyThreadDetach &)=delete
PyThreadState * tstate
Definition: pyuno.hxx:301
The pyuno::Runtime class keeps the internal state of the python UNO bridge for the currently in use p...
Definition: pyuno.hxx:164
RuntimeImpl * getImpl() const
Returns the internal handle.
Definition: pyuno.hxx:242
RuntimeImpl * impl
Definition: pyuno.hxx:165
void * p
Reference
Definition: pyuno.cxx:72
ConversionMode
Definition: pyuno.hxx:153
@ REJECT_UNO_ANY
Definition: pyuno.hxx:153
@ ACCEPT_UNO_ANY
Definition: pyuno.hxx:153
NotNull
Definition: pyuno.hxx:62
@ NOT_NULL
definition of a no acquire enum for ctors
Definition: pyuno.hxx:65
#define LO_DLLPUBLIC_PYUNO
External interface of the Python UNO bridge.
Definition: pyuno.hxx:46
LO_DLLPUBLIC_PYUNO PyObject * PyInit_pyuno()
function called by the python runtime to initialize the pyuno module.
ConversionMode mode
sal_IntPtr operator()(const PyRef &r) const
Definition: pyuno.hxx:146