#pragma once #include // PyObject #include namespace NPython { template class TPyPtrOps { public: static inline void Ref(T* t) { Y_ASSERT(t); Py_INCREF(t); } static inline void UnRef(T* t) { Y_ASSERT(t); Py_DECREF(t); } static inline ui32 RefCount(const T* t) { Y_ASSERT(t); return t->ob_refcnt; } }; class TPyObjectPtr: public NYql::NUdf::TRefCountedPtr> { using TSelf = NYql::NUdf::TRefCountedPtr>; public: inline TPyObjectPtr() { } inline TPyObjectPtr(PyObject* p) : TSelf(p, STEAL_REF) // do not increment refcounter by default { } inline TPyObjectPtr(PyObject* p, AddRef) : TSelf(p) { } inline void ResetSteal(PyObject* p) { TSelf::Reset(p, STEAL_REF); } inline void ResetAddRef(PyObject* p) { TSelf::Reset(p); } inline void Reset() { TSelf::Reset(); } template inline T* GetAs() const { return reinterpret_cast(Get()); } void Reset(PyObject* p) = delete; }; } // namspace NPython