Skip to content

Commit 7b74b17

Browse files
Add a method to get an owned reference from a given reference.
1 parent 970ff1f commit 7b74b17

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

dynd/include/utility_functions.hpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,21 @@ class py_ref_t {
384384
}
385385
}
386386

387-
py_ref_t<false, not_null> borrow() noexcept { return py_ref<false, not_null>(o, false); }
387+
// Return a wrapped borrowed reference to the wrapped reference.
388+
py_ref_t<false, not_null> borrow() noexcept
389+
{
390+
// Assert that the wrapped reference is actually valid if it isn't null.
391+
PYDYND_ASSERT_IF(o != nullptr, Py_REFCNT(o) > 0);
392+
return py_ref_t<false, not_null>(o, false);
393+
}
394+
395+
// Return a wrapped owned reference referring to the currently wrapped reference.
396+
py_ref_t<true, not_null> borrow() noexcept
397+
{
398+
// Assert that the wrapped reference is actually valid if it isn't null.
399+
PYDYND_ASSERT_IF(o != nullptr, Py_REFCNT(o) > 0);
400+
return py_ref_t<true, not_null>(o, false);
401+
}
388402

389403
// Return a reference to the encapsulated PyObject as a raw pointer.
390404
// Set the encapsulated pointer to NULL.

0 commit comments

Comments
 (0)