#pragma once #define PY_SSIZE_T_CLEAN #include #include namespace NPyBind { template 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> { private: typedef TIntrusivePtr> TParent; typedef TPythonIntrusivePtrOps 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(); } }; }