#ifndef SDK_TL_AUTODESTRUCT_H #define SDK_TL_AUTODESTRUCT_H #include "iostream" namespace CTL{ template class AutoDestruct{ T* m_ptr; bool m_is_owner; public: //-------------------------------------------------------------------------------------------------------------- AutoDestruct(T* ptr) { this->m_ptr = ptr; m_is_owner = true; } AutoDestruct(AutoDestruct&& other) { this->m_ptr = other.m_ptr; other.m_ptr = NULL; } ~AutoDestruct(){ if (m_ptr && m_is_owner) { delete m_ptr; } m_ptr = NULL; } //-------------------------------------------------------------------------------------------------------------- T* Get(){ return m_ptr; } const T* Get() const{ return m_ptr; } T* get(){ return m_ptr; } const T* get() const{ return m_ptr; } operator bool() const { return m_ptr != NULL; } T& operator*() const { return *m_ptr; // 假设 ptr 是内部 Term* } T* operator->() const { return m_ptr; // 假设 ptr 是内部 Term* } AutoDestruct& operator=(T* ptr){ delete m_ptr; m_ptr = ptr; return *this; } void reset(T* ptr = NULL){ delete m_ptr; m_ptr = ptr; } void release() { m_is_owner = false; } //-------------------------------------------------------------------------------------------------------------- }; } #endif