diff options
author | qrort <qrort@yandex-team.com> | 2022-11-30 23:47:12 +0300 |
---|---|---|
committer | qrort <qrort@yandex-team.com> | 2022-11-30 23:47:12 +0300 |
commit | 22f8ae0e3f5d68b92aecccdf96c1d841a0334311 (patch) | |
tree | bffa27765faf54126ad44bcafa89fadecb7a73d7 /library/cpp/pybind | |
parent | 332b99e2173f0425444abb759eebcb2fafaa9209 (diff) | |
download | ydb-22f8ae0e3f5d68b92aecccdf96c1d841a0334311.tar.gz |
validate canons without yatest_common
Diffstat (limited to 'library/cpp/pybind')
-rw-r--r-- | library/cpp/pybind/ptr.h | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/library/cpp/pybind/ptr.h b/library/cpp/pybind/ptr.h new file mode 100644 index 0000000000..e136736690 --- /dev/null +++ b/library/cpp/pybind/ptr.h @@ -0,0 +1,51 @@ +#pragma once + +#define PY_SSIZE_T_CLEAN +#include <Python.h> +#include <util/generic/ptr.h> + +namespace NPyBind { + template <class T> + class TPythonIntrusivePtrOps { + public: + static inline void Ref(T* t) noexcept { + Py_XINCREF(t); + } + + static inline void UnRef(T* t) noexcept { + Py_XDECREF(t); + } + + static inline void DecRef(T* t) noexcept { + Py_XDECREF(t); + } + }; + + class TPyObjectPtr: public TIntrusivePtr<PyObject, TPythonIntrusivePtrOps<PyObject>> { + private: + typedef TIntrusivePtr<PyObject, TPythonIntrusivePtrOps<PyObject>> TParent; + typedef TPythonIntrusivePtrOps<PyObject> TOps; + + public: + inline TPyObjectPtr() noexcept { + } + + inline explicit TPyObjectPtr(PyObject* obj) noexcept + : TParent(obj) + { + } + + inline TPyObjectPtr(PyObject* obj, bool unref) noexcept + : TParent(obj) + { + if (unref) + TOps::UnRef(TParent::Get()); + } + + inline PyObject* RefGet() { + TOps::Ref(TParent::Get()); + return TParent::Get(); + } + }; + +} |