1 #pragma once 2 3 #include <torch/csrc/Export.h> 4 #include <torch/csrc/python_headers.h> 5 #include <utility> 6 7 template <class T> 8 class TORCH_PYTHON_API THPPointer { 9 public: THPPointer()10 THPPointer() : ptr(nullptr){}; THPPointer(T * ptr)11 explicit THPPointer(T* ptr) noexcept : ptr(ptr){}; THPPointer(THPPointer && p)12 THPPointer(THPPointer&& p) noexcept : ptr(std::exchange(p.ptr, nullptr)) {} 13 ~THPPointer()14 ~THPPointer() { 15 free(); 16 }; get()17 T* get() { 18 return ptr; 19 } get()20 const T* get() const { 21 return ptr; 22 } release()23 T* release() { 24 T* tmp = ptr; 25 ptr = nullptr; 26 return tmp; 27 } 28 operator T*() { 29 return ptr; 30 } 31 THPPointer& operator=(T* new_ptr) noexcept { 32 free(); 33 ptr = new_ptr; 34 return *this; 35 } 36 THPPointer& operator=(THPPointer&& p) noexcept { 37 free(); 38 ptr = p.ptr; 39 p.ptr = nullptr; 40 return *this; 41 } 42 T* operator->() { 43 return ptr; 44 } 45 explicit operator bool() const { 46 return ptr != nullptr; 47 } 48 49 private: 50 void free(); 51 T* ptr = nullptr; 52 }; 53 54 /** 55 * An RAII-style, owning pointer to a PyObject. You must protect 56 * destruction of this object with the GIL. 57 * 58 * WARNING: Think twice before putting this as a field in a C++ 59 * struct. This class does NOT take out the GIL on destruction, 60 * so if you will need to ensure that the destructor of your struct 61 * is either (a) always invoked when the GIL is taken or (b) takes 62 * out the GIL itself. Easiest way to avoid this problem is to 63 * not use THPPointer in this situation. 64 */ 65 using THPObjectPtr = THPPointer<PyObject>; 66 using THPCodeObjectPtr = THPPointer<PyCodeObject>; 67 using THPFrameObjectPtr = THPPointer<PyFrameObject>; 68