Cantera  3.0.0
Loading...
Searching...
No Matches
PythonHandle.h
Go to the documentation of this file.
1//! @file PythonHandle.h
2
3#ifndef CT_PYTHONHANDLE_H
4#define CT_PYTHONHANDLE_H
5
6// This file is part of Cantera. See License.txt in the top-level directory or
7// at https://cantera.org/license.txt for license and copyright information.
8
9#include "Python.h"
11
12namespace Cantera
13{
14
15//! Class that holds an owned or weak (borrowed) reference to a Python object
17{
18public:
19 //! Create a handle to hold a Python object
20 //! @param obj The Python object to be held
21 //! @param weak `true` if this is a weak reference to the Python object and this
22 //! handle is not responsible for deleting the Python object, or `false` if this
23 //! handle should own a reference to the Python object
24 PythonHandle(PyObject* obj, bool weak) : m_obj(obj), m_weak(weak) {
25 if (!weak) {
26 Py_XINCREF(obj);
27 }
28 }
29 PythonHandle(const PythonHandle&) = delete;
30 PythonHandle& operator=(const PythonHandle&) = delete;
31
33 if (!m_weak) {
34 Py_XDECREF(m_obj);
35 }
36 }
37
38 void* get() override {
39 return m_obj;
40 }
41
42private:
43 PyObject* m_obj;
44 bool m_weak;
45};
46
47}
48
49#endif
A base class for managing the lifetime of an external object, such as a Python object used by a Deleg...
Class that holds an owned or weak (borrowed) reference to a Python object.
PythonHandle(PyObject *obj, bool weak)
Create a handle to hold a Python object.
void * get() override
Get the underlying external object.
Namespace for the Cantera kernel.
Definition AnyMap.cpp:564