/*
* =====================================================================================
*
* Filename: T_smartpointer.cpp
*
* Description: smartpointer of boost
*
* Version: 1.0
* Created: 2008年12月11日 13时25分54秒
* Revision: none
* Compiler: gcc
*
* Author: Li WeiJian (mn), lwj1396@163.com
* Company: hunan university
*
* =====================================================================================
*/
#include<boost/scoped_ptr.hpp>
#include<boost/shared_ptr.hpp>
#include<boost/utility.hpp>
#include<iostream>
//============================= scoped_ptr==============================================
//
struct Shoe{~Shoe(){std::cout<<"Buckle my shoe!"<<std::endl;}};
class Myclass
{
boost::scoped_ptr<int> ptr;
public:
Myclass():ptr(new int){*ptr = 0;}
~Myclass(){std::cout<<"destructor of Myclass/n";}
int add_one() {return ++*ptr;}
};
class example : private boost::noncopyable
{
private:
boost::scoped_ptr<Myclass> _imp;//hide the implement detail
public:
example():_imp(new Myclass){}
~example(){std::cout<<"destructor of example/n";}
int dothing(){return _imp->add_one();}
};
void Test_scoped_ptr()
{
boost::scoped_ptr<Shoe> x(new Shoe);
x.reset();//reset()的时候就删除了该对象
example e;
example b;
/* 出错,因为是noncopyable
example *p=&e;
*p=b;
or e=b
*/
std::cout<<e.dothing()<<"/n";
}
//end scoped_ptr
//===================================shared_ptr==============================
//
// interface programming
//x.h
class X
{
public:
virtual void f()=0;
virtual void g()=0;
protected:
~X(){}
};
//share_ptr<X> createX();
//x.cpp
class X_impl:public X
{
private:
X_impl(X_impl const &);
X_impl & operator=(X_impl const &);
~X_impl(){};//不能被delete,且不能声明static的X_impl全局对象了,即不能创建在栈上
public:
class deleter;
friend class deleter;
class deleter
{
public:
void operator()(X_impl* p)
{
std::cout<<"X_imple deleted by deleter/n";
delete p;
}
};
X_impl(){}
virtual void f()
{
std::cout<<"X_impl f()/n";
}
virtual void g()
{
std::cout<<"X_impl g()/n";
}
};
boost::shared_ptr<X> createX()
{
//自定义了删除器
boost::shared_ptr<X> px(new X_impl,X_impl::deleter());
return px;
}
//====包装 static的东西,引用计数为0时候,依然没有删除
class A
{
public:
void f(){std::cout<<"i am a A/n";}
};
struct null_deleter
{
void operator()(void const *) const
{}
};
static A x;
boost::shared_ptr<A> createX_impl()
{
boost::shared_ptr<A> px(&x,null_deleter());
return px;
}
//==================
//===================从一个raw pointer处获得share_ptr
void f(boost::shared_ptr<A> pa) //会隐式转换?
{}
//对于成员函数而言,可以在第一个参数处转换this
//void f(boost::shared_ptr<A> this_, int m)
//===================
//====================shared from this=====================
class impl:public boost::enable_shared_from_this<impl>
{
public:
virtual shared_ptr<impl> getImpl()
{
return boost::shared_from_this();
}
};
//====================
//===============shared_ptr<void> 相当于void*========
boost::shared<void> pa(new A);
//===============
void Test_share_ptr()
{
// boost::shared_ptr<X> p=createX();
//p.reset(); 可以reset,但是X_impl会被销毁,后边就不能再调用函数
// p->f();
//delete p.get(); 因为~X()是protected,所以此处会有错
boost::shared_ptr<A> i=createX_impl();
i->f();
}
int main()
{
//Test_scoped_ptr();
Test_share_ptr();
}
* =====================================================================================
*
* Filename: T_smartpointer.cpp
*
* Description: smartpointer of boost
*
* Version: 1.0
* Created: 2008年12月11日 13时25分54秒
* Revision: none
* Compiler: gcc
*
* Author: Li WeiJian (mn), lwj1396@163.com
* Company: hunan university
*
* =====================================================================================
*/
#include<boost/scoped_ptr.hpp>
#include<boost/shared_ptr.hpp>
#include<boost/utility.hpp>
#include<iostream>
//============================= scoped_ptr==============================================
//
struct Shoe{~Shoe(){std::cout<<"Buckle my shoe!"<<std::endl;}};
class Myclass
{
boost::scoped_ptr<int> ptr;
public:
Myclass():ptr(new int){*ptr = 0;}
~Myclass(){std::cout<<"destructor of Myclass/n";}
int add_one() {return ++*ptr;}
};
class example : private boost::noncopyable
{
private:
boost::scoped_ptr<Myclass> _imp;//hide the implement detail
public:
example():_imp(new Myclass){}
~example(){std::cout<<"destructor of example/n";}
int dothing(){return _imp->add_one();}
};
void Test_scoped_ptr()
{
boost::scoped_ptr<Shoe> x(new Shoe);
x.reset();//reset()的时候就删除了该对象
example e;
example b;
/* 出错,因为是noncopyable
example *p=&e;
*p=b;
or e=b
*/
std::cout<<e.dothing()<<"/n";
}
//end scoped_ptr
//===================================shared_ptr==============================
//
// interface programming
//x.h
class X
{
public:
virtual void f()=0;
virtual void g()=0;
protected:
~X(){}
};
//share_ptr<X> createX();
//x.cpp
class X_impl:public X
{
private:
X_impl(X_impl const &);
X_impl & operator=(X_impl const &);
~X_impl(){};//不能被delete,且不能声明static的X_impl全局对象了,即不能创建在栈上
public:
class deleter;
friend class deleter;
class deleter
{
public:
void operator()(X_impl* p)
{
std::cout<<"X_imple deleted by deleter/n";
delete p;
}
};
X_impl(){}
virtual void f()
{
std::cout<<"X_impl f()/n";
}
virtual void g()
{
std::cout<<"X_impl g()/n";
}
};
boost::shared_ptr<X> createX()
{
//自定义了删除器
boost::shared_ptr<X> px(new X_impl,X_impl::deleter());
return px;
}
//====包装 static的东西,引用计数为0时候,依然没有删除
class A
{
public:
void f(){std::cout<<"i am a A/n";}
};
struct null_deleter
{
void operator()(void const *) const
{}
};
static A x;
boost::shared_ptr<A> createX_impl()
{
boost::shared_ptr<A> px(&x,null_deleter());
return px;
}
//==================
//===================从一个raw pointer处获得share_ptr
void f(boost::shared_ptr<A> pa) //会隐式转换?
{}
//对于成员函数而言,可以在第一个参数处转换this
//void f(boost::shared_ptr<A> this_, int m)
//===================
//====================shared from this=====================
class impl:public boost::enable_shared_from_this<impl>
{
public:
virtual shared_ptr<impl> getImpl()
{
return boost::shared_from_this();
}
};
//====================
//===============shared_ptr<void> 相当于void*========
boost::shared<void> pa(new A);
//===============
void Test_share_ptr()
{
// boost::shared_ptr<X> p=createX();
//p.reset(); 可以reset,但是X_impl会被销毁,后边就不能再调用函数
// p->f();
//delete p.get(); 因为~X()是protected,所以此处会有错
boost::shared_ptr<A> i=createX_impl();
i->f();
}
int main()
{
//Test_scoped_ptr();
Test_share_ptr();
}