// 一般用途
template<class T>struct SP_traits {
static void Release(T* ptr) {
delete ptr;
}
};
// 特殊用途
template<> struct SP_traits<somlist>{
static void Release(T* ptr)
{
somlist::iterator iter;
for(iter= ptr->begin() ;iter!=ptr->end();iter++)
delete (*iter) ;
delete ptr;
}
}
template<class T>
class SP//simple smart pointer
{
public:
SP():ptr(NULL) {
}
SP(T*p):ptr(p) {
}
void Release()
{
if( ptr != NULL )
{
SP_traits<T>::Release(ptr);
}
ptr = NULL;
}
T* operator=(T*p){
Release();
ptr = p;
return ptr;
}
T** operator &() const {
return &ptr;
}
T** operator &() {
return &ptr;
}
T& operator *() {
return *ptr;
}
T& operator *() const {
return *ptr;
}
T* operator->() {
return ptr;
}
T* operator->() const {
return ptr;
}
operator T*() {
return ptr;
}
operator T*() const {
return ptr;
}
T& operator []( int i ) {
return ptr[ i ];
}
T& operator []( int i ) const {
return ptr[ i ];
}
bool operator == ( const T* p ){
return ptr == p;
}
bool operator != ( const T* p ){
return ptr != p;
}
bool operator == ( const T* p ) const {
return ptr == p;
}
bool operator != ( const T* p ) const {
return ptr != p;
}
~SP()
{
Release();
}
PM_BOOL IsValid() {
return ptr != NULL;
}
void Attach( T* ptr_new )
{
Release();
ptr = ptr_new;
}
void Detach( T* ptr_new )
{
ptr = NULL;
}
T* ptr;
};