Loading [MathJax]/extensions/tex2jax.js
LSST Applications g0f08755f38+05b4b46b2c,g1653933729+a905cd61c3,g168dd56ebc+a905cd61c3,g1a2382251a+526767c3b0,g20f6ffc8e0+05b4b46b2c,g217e2c1bcf+f8df405040,g28da252d5a+e530e4405a,g2bbee38e9b+e060cc3e60,g2bc492864f+e060cc3e60,g32e5bea42b+7044b77928,g347aa1857d+e060cc3e60,g35bb328faa+a905cd61c3,g3a166c0a6a+e060cc3e60,g3bd4b5ce2c+9af3f3d415,g3e281a1b8c+2bff41ced5,g3e8969e208+a905cd61c3,g414038480c+882f223820,g41af890bb2+f72d0f2eea,g43bc871e57+ad86a2d9e2,g78460c75b0+4ae99bb757,g80478fca09+8d821d1b28,g82479be7b0+ec26a56c2d,g858d7b2824+05b4b46b2c,g9125e01d80+a905cd61c3,ga5288a1d22+64e5455051,gb58c049af0+84d1b6ec45,gc28159a63d+e060cc3e60,gc5452a3dca+b82ec7cc4c,gcab2d0539d+01da5adb7a,gcf0d15dbbd+56822d21ae,gda6a2b7d83+56822d21ae,gdaeeff99f8+686ef0dd99,ge79ae78c31+e060cc3e60,gef2f8181fd+f2c81e61ee,gf0baf85859+f9edac6842,gf1e97e5484+3a635bd7af,gfa517265be+05b4b46b2c,gfa999e8aa5+d85414070d,w.2025.01
LSST Data Management Base Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Public Types | Public Member Functions | List of all members
lsst::cpputils::python::PySharedPtr< T > Class Template Referencefinal

A shared pointer that tracks both a C++ object and its associated PyObject. More...

#include <PySharedPtr.h>

Public Types

using element_type = T
 

Public Member Functions

 PySharedPtr (T *const ptr)
 Create a pointer that counts as an extra reference in the Python environment.
 
 PySharedPtr (PySharedPtr const &) noexcept=default
 
 PySharedPtr (PySharedPtr &&) noexcept=default
 
PySharedPtroperator= (PySharedPtr const &) noexcept=default
 
PySharedPtroperator= (PySharedPtr &&) noexcept=default
 
 ~PySharedPtr () noexcept=default
 
 PySharedPtr (std::shared_ptr< T > r) noexcept
 
 operator std::shared_ptr< T > () noexcept
 
T * get () const noexcept
 

Detailed Description

template<typename T>
class lsst::cpputils::python::PySharedPtr< T >

A shared pointer that tracks both a C++ object and its associated PyObject.

Each group of PySharedPtr for a given object collectively counts as one reference to that object for the purpose of Python garbage collection.

A PySharedPtr is implicitly convertible to and from a std::shared_ptr to minimize API impact. Any shared_ptr created this way will (I think) keep the Python reference alive, as described above.

Definition at line 49 of file PySharedPtr.h.

Member Typedef Documentation

◆ element_type

template<typename T >
using lsst::cpputils::python::PySharedPtr< T >::element_type = T

Definition at line 51 of file PySharedPtr.h.

Constructor & Destructor Documentation

◆ PySharedPtr() [1/4]

template<typename T >
lsst::cpputils::python::PySharedPtr< T >::PySharedPtr ( T *const ptr)
inlineexplicit

Create a pointer that counts as an extra reference in the Python environment.

Parameters
ptra pointer to a pybind11-managed object.

Definition at line 59 of file PySharedPtr.h.

59 : _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 }
std::uint64_t * ptr
Definition RangeSet.cc:95

◆ PySharedPtr() [2/4]

template<typename T >
lsst::cpputils::python::PySharedPtr< T >::PySharedPtr ( PySharedPtr< T > const & )
defaultnoexcept

◆ PySharedPtr() [3/4]

template<typename T >
lsst::cpputils::python::PySharedPtr< T >::PySharedPtr ( PySharedPtr< T > && )
defaultnoexcept

◆ ~PySharedPtr()

template<typename T >
lsst::cpputils::python::PySharedPtr< T >::~PySharedPtr ( )
defaultnoexcept

◆ PySharedPtr() [4/4]

template<typename T >
lsst::cpputils::python::PySharedPtr< T >::PySharedPtr ( std::shared_ptr< T > r)
inlinenoexcept

Definition at line 78 of file PySharedPtr.h.

78: _impl(std::move(r)) {}
T move(T... args)

Member Function Documentation

◆ get()

template<typename T >
T * lsst::cpputils::python::PySharedPtr< T >::get ( ) const
inlinenoexcept

Definition at line 81 of file PySharedPtr.h.

81{ return _impl.get(); }

◆ operator std::shared_ptr< T >()

template<typename T >
lsst::cpputils::python::PySharedPtr< T >::operator std::shared_ptr< T > ( )
inlinenoexcept

Definition at line 79 of file PySharedPtr.h.

79{ return _impl; }

◆ operator=() [1/2]

template<typename T >
PySharedPtr & lsst::cpputils::python::PySharedPtr< T >::operator= ( PySharedPtr< T > && )
defaultnoexcept

◆ operator=() [2/2]

template<typename T >
PySharedPtr & lsst::cpputils::python::PySharedPtr< T >::operator= ( PySharedPtr< T > const & )
defaultnoexcept

The documentation for this class was generated from the following file: