You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
1.3 KiB
89 lines
1.3 KiB
namespace provsubsys{
|
|
|
|
template<class T>
|
|
class RefCountingTraits{
|
|
public:
|
|
void add_ref(T * ptr){ ptr->AddRef();};
|
|
void release(T * ptr){ ptr->Release();};
|
|
};
|
|
|
|
|
|
template<class T, class ref_traits = RefCountingTraits<T> >
|
|
class auto_ref: private ref_traits {
|
|
T* pointee_;
|
|
|
|
public:
|
|
auto_ref():pointee_(NULL)
|
|
{ };
|
|
|
|
explicit auto_ref(T * _P):pointee_(_P)
|
|
{
|
|
add_ref();
|
|
};
|
|
|
|
explicit auto_ref(const int):pointee_(NULL)
|
|
{ } ;
|
|
|
|
auto_ref(const auto_ref& _S):pointee_(_S.pointee_)
|
|
{
|
|
add_ref();
|
|
};
|
|
|
|
~auto_ref()
|
|
{
|
|
release();
|
|
};
|
|
|
|
bool operator==(const auto_ref& _R)
|
|
{
|
|
return pointee_ == _R.pointee_;
|
|
};
|
|
|
|
const auto_ref& operator=( const auto_ref& _R)
|
|
{
|
|
if(pointee_ == _R.pointee_)
|
|
return *this;
|
|
release();
|
|
pointee_ = _R.pointee_;
|
|
add_ref();
|
|
return *this;
|
|
};
|
|
|
|
|
|
T& operator*( ) const
|
|
{
|
|
return *pointee_;
|
|
|
|
};
|
|
|
|
T* operator->( ) const
|
|
{
|
|
return pointee_;
|
|
};
|
|
|
|
operator bool( ) const throw( )
|
|
{
|
|
return pointee_ != NULL;
|
|
};
|
|
|
|
void Dettach()
|
|
{
|
|
release();
|
|
pointee_ = NULL;
|
|
};
|
|
|
|
protected:
|
|
void add_ref()
|
|
{
|
|
if( pointee_ != NULL )
|
|
ref_traits::add_ref(pointee_);
|
|
};
|
|
|
|
void release()
|
|
{
|
|
if( pointee_ != NULL )
|
|
ref_traits::release(pointee_);
|
|
};
|
|
};
|
|
};
|
|
|