LSST Applications g0f08755f38+9522ef2f0f,g1653933729+a905cd61c3,g168dd56ebc+a905cd61c3,g1a2382251a+910d683904,g20f6ffc8e0+9522ef2f0f,g217e2c1bcf+f4af07de8a,g28da252d5a+26a25b978d,g2bbee38e9b+cc7bbd92cc,g2bc492864f+cc7bbd92cc,g32e5bea42b+de24d92311,g347aa1857d+cc7bbd92cc,g35bb328faa+a905cd61c3,g3a166c0a6a+cc7bbd92cc,g3bd4b5ce2c+02735527dc,g3e281a1b8c+2bff41ced5,g414038480c+4de324692b,g41af890bb2+4fc8c6ef01,g43bc871e57+d0d7cc457a,g78460c75b0+4ae99bb757,g80478fca09+615987a4d7,g82479be7b0+970d1d03ea,g8365541083+a905cd61c3,g858d7b2824+9522ef2f0f,g9125e01d80+a905cd61c3,ga5288a1d22+9ad990292e,gb58c049af0+84d1b6ec45,gc28159a63d+cc7bbd92cc,gc5452a3dca+b82ec7cc4c,gcab2d0539d+475d436cbd,gcf0d15dbbd+d816b8a730,gda6a2b7d83+d816b8a730,gdaeeff99f8+686ef0dd99,ge79ae78c31+cc7bbd92cc,gef2f8181fd+c1889b0e42,gf0baf85859+f9edac6842,gf1e97e5484+a55c27affc,gfa517265be+9522ef2f0f,gfa999e8aa5+d85414070d,w.2025.01
LSST Data Management Base Package
Loading...
Searching...
No Matches
PySharedPtr.h
Go to the documentation of this file.
1// -*- lsst-c++ -*-
2/*
3 * LSST Data Management System
4 * See COPYRIGHT file at the top of the source tree.
5 *
6 * This product includes software developed by the
7 * LSST Project (http://www.lsst.org/).
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the LSST License Statement and
20 * the GNU General Public License along with this program. If not,
21 * see <http://www.lsstcorp.org/LegalNotices/>.
22 */
23
24#ifndef LSST_CPPUTILS_PYTHON_PYSHAREDPTR_H
25#define LSST_CPPUTILS_PYTHON_PYSHAREDPTR_H
26
27#include "pybind11/pybind11.h"
28
29#include <memory>
30
31namespace lsst {
32namespace cpputils {
33namespace python {
34
35/* Workaround for C++ objects outliving Python objects
36 * by Xfel and florianwechsung, https://github.com/pybind/pybind11/issues/1389
37 */
48template <typename T>
49class PySharedPtr final {
50public:
51 using element_type = T;
52
59 explicit PySharedPtr(T* const ptr) : _impl() {
60 if (ptr != nullptr) {
61 // `cast` returns new PyObject only if `*ptr` not yet associated with one
62 PyObject* pyObj = pybind11::cast(ptr).ptr();
63 Py_INCREF(pyObj);
64 // if any operation with shared_ptr fails, pyObj will get decremented correctly
65 std::shared_ptr<PyObject> manager(pyObj, [](PyObject* const obj) noexcept {
66 if (obj != nullptr) Py_DECREF(obj);
67 });
68 _impl = std::shared_ptr<T>(manager, ptr);
69 }
70 }
71
72 PySharedPtr(PySharedPtr const&) noexcept = default;
73 PySharedPtr(PySharedPtr&&) noexcept = default;
74 PySharedPtr& operator=(PySharedPtr const&) noexcept = default;
75 PySharedPtr& operator=(PySharedPtr&&) noexcept = default;
76 ~PySharedPtr() noexcept = default;
77
78 PySharedPtr(std::shared_ptr<T> r) noexcept : _impl(std::move(r)) {}
79 operator std::shared_ptr<T>() noexcept { return _impl; }
80
81 T* get() const noexcept { return _impl.get(); }
82
83private:
85};
86
87} // namespace python
88} // namespace cpputils
89} // namespace lsst
90
91// Macro must be called in the global namespace
93
94#endif
PYBIND11_DECLARE_HOLDER_TYPE(T, lsst::cpputils::python::PySharedPtr< T >)
std::uint64_t * ptr
Definition RangeSet.cc:95
A shared pointer that tracks both a C++ object and its associated PyObject.
Definition PySharedPtr.h:49
PySharedPtr(PySharedPtr &&) noexcept=default
PySharedPtr(T *const ptr)
Create a pointer that counts as an extra reference in the Python environment.
Definition PySharedPtr.h:59
PySharedPtr(PySharedPtr const &) noexcept=default
STL namespace.