#include <iostream>
#include <memory>
using namespace std;
#include<iostream>
class sp_base{
public:
virtual void del(void* obj) = 0;
void inc_ref(){
ref_count_++;
}
void dec_ref(){
ref_count_--;
}
int ref_count()const{
return ref_count_;
}
private:
int ref_count_ = 1;
};
template<typename T, typename D>
class sp_impl : public sp_base{
public:
sp_impl(D d):d_(d){}
void del(void* obj){
d_((T*)obj);
}
private:
D d_;
};
template<typename T>
class default_del{
public:
void operator()(T* obj){
std::cout << "sizeof (T) = " << sizeof(T) << std::endl;
delete obj;
}
};
template<typename T>
class sh_ptr{
public:
template<class U> explicit sh_ptr(U* p){
obj = (T*)p;
sp = new sp_impl<U, default_del<U>>(default_del<U>());
}
template<class U, class D> sh_ptr(U* p, D del){
obj = (T*)p;
sp = new sp_impl<U, D>(del);
}
sh_ptr(const sh_ptr& x){
obj = x.obj;
sp = x.sp;
sp->inc_ref();
}
template <class U> sh_ptr(const sh_ptr<U>& x){
obj = (T*)x.obj;
sp = x.sp;
sp->inc_ref();
}
~sh_ptr(){
sp->dec_ref();
if(sp->ref_count() == 0){
sp->del(obj);
}
}
private:
T* obj;
sp_base* sp;
};
class Base{
public:
Base(int b):m(b){}
~Base(){
std::cout << "deconctructor b" << std::endl;
}
private:
int m;
};
void delBase(Base* b){
delete b;
}
class delFunctor{
public:
void operator()(Base* b){
delete b;
}
};
int main(){
{
sh_ptr<Base> si = sh_ptr<Base>(new Base(5), delBase);
sh_ptr<Base> si2 = sh_ptr<Base>(new Base(4), delFunctor());
sh_ptr<Base> si3 = sh_ptr<Base>(new Base(3), [](Base* b){delete b;});
}
return 0;
}