WebSocket_CPP_98/CC_SDK_VS/TL/AutoDestruct.h
2025-12-29 09:55:17 +08:00

66 lines
1.7 KiB
C++

#ifndef SDK_TL_AUTODESTRUCT_H
#define SDK_TL_AUTODESTRUCT_H
#include "iostream"
namespace CTL{
template<typename T>
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