Smart pointer system (original, PDF, link)

Source: Internet
Author: User

Special thanks to the FTP space provided by renwind

Please visit the following link (in PDF format, use Acrobat 5 or later)

Http://www.zsws.org/renwind/smartPtr.pdf

 

Code:

 

/** Note ************************************ ********************************
Filename: refobject. h
Creator: noslopforever
More Author: noslopforever

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!
 
This document is an integral part of the gamerclass project. gamerclass and the author of this document noslopforever (death in heaven) [Email: noslopforever@yahoo.com.cn] declares here:
 
You are free and free to use this document, as permitted by law. However, you cannot change the nature of the freedom and sharing of this document, or privatize the document and its content.
This note must be retained without modification under any circumstances, even if you have modified this document during use.
In this note, only the more author part can be modified, but the original information of this part must be retained.
 
Let us jointly maintain a fair, equitable, legal, and reasonable code sharing environment. A good environment will benefit each other. Thank you.
 
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!

Purpose: tsafeptr null protects smart pointers
Tshareptr refcount smart pointer (must be used with the derived class of crefobject)
Tshareptrautovalid: The refcount smart pointer of the Self-deleted nature (must be used with the derived class of crefobject)

**************************************** ************************************/
# Ifndef _ refobject_h _
# DEFINE _ refobject_h _

# Include "nf3dtypes. H"

Startnamespace (nf3d)

 

/** Write this sentence in the class structure that requires invalid define, and pass in the class name of this class for CLS */
# Define def_invalid (CLS) Static CLS # * invalid;

/** Write this sentence in the corresponding CPP, pass in the class name of this class for CLS, and pass in the address of the corresponding illegal class for value */
# Define impl_invalid (CLS, value) CLS ## * CLS ##:: Invalid ### value ##;
/** Or, pass in the class name of this class for CLS, and pass in the corresponding illegal Class Name For inv_cls */
# Define impl_cls_invalid (CLS, inv_cls )/
Inv_cls g _ # inv_cls ;/
Impl_invalid (CLS, & G _ # inv_cls )/

 

Template <class T>
Class tshareptr;

//------------------------------------------------------------------------
/**
*/Brief
*/
Class nf3d_api crefobject
{
Public:

Crefobject (): m_un32refcount (0)
{}

Virtual ~ Crefobject ()
{
# Ifdef _ debug
If (m_un32refcount! = 0)
{Assert (false );}
# Endif
}
Def_invalid (crefobject)

PRIVATE:

Template <class T> friend class tshareptr;
Template <class T> friend class tshareptrautovalid;

// Reference count
Uint32 m_un32refcount;

PRIVATE:

Inline void addref ()
{++ M_un32refcount ;}

Inline void release ()
{
If (-- m_un32refcount = 0)
{Delete this ;}
}

Public:

Inline uint32 getrefcount ()
{Return m_un32refcount ;}

Template <class T>
Static void addref (T * t)
{
If (t)
{
T-> addref ();
}
}

Template <class T>
Static void release (T * t)
{
If (t)
{
T-> release ();
}
}

};
//------------------------------------------------------------------------

//------------------------------------------------------------------------
/**
* @ Brief
*/
Template <class T>
Class tshareptr
{
PRIVATE:

/** Internal interface */
T * m_pinner;

Inline T * _ returninner () const
{
If (! M_pinner)
{
// @ Todo throw or log error
Return T: invalid;
}

Return m_pinner;
}

Public:

Inline T * _ real_ptr () const
{Return m_pinner ;}

Inline operator bool () const
{Return m_pinner! = NULL ;}

Public:

/** Cast is an internal interface */
Inline T * PTR () const
{Return _ returninner ();}
Inline const T * cptr () const
{Return _ returninner ();}

Inline const T * operator-> () const
{Return _ returninner ();}
Inline T * operator-> ()
{Return _ returninner ();}

Public:

/** Default constructor */
Tshareptr ()
{
M_pinner = NULL;
}

/** Construct through internal interfaces */
Tshareptr (const T * pinner)
{
M_pinner = const_cast <t *> (pinner );
If (m_pinner)
{M_pinner-> addref ();}
}

/** Copy the constructor */
Template <Class U>
Tshareptr (tshareptr <u> const & RHs)
{
M_pinner = static_cast <t *> (RHS. _ real_ptr ());
If (m_pinner) {m_pinner-> addref ();}
}
Tshareptr (tshareptr <t> const & RHs)
{
M_pinner = RHS. _ real_ptr ();
If (m_pinner)
{M_pinner-> addref ();}
}

/** Destructor */
~ Tshareptr ()
{
If (m_pinner)
{
Static_cast <crefobject *> (m_pinner)-> release ();
M_pinner = NULL;
}
}

/** Value assignment function */
Template <Class U>
Inline tshareptr <t> & operator = (tshareptr <u> const & RHs)
{
Saferelner (m_pinner );
M_pinner = static_cast <t *> (RHS. _ real_ptr ());
If (m_pinner)
{M_pinner-> addref ();}

Return * this;
}
Inline tshareptr <t> & operator = (tshareptr <t> const & RHs)
{
Saferelner (m_pinner );
M_pinner = RHS. _ real_ptr ();
If (m_pinner)
{M_pinner-> addref ();}

Return * this;
}

/** Comparison function */
Template <Class U>
Inline bool operator = (tshareptr <u> const & RHs) const
{Return m_pinner = RHS. _ real_ptr ();}
Template <Class U>
Inline bool Operator! = (Tshareptr <u> const & RHs) const
{Return m_pinner! = RHS. _ real_ptr ();}

Template <Class U>
Inline bool operator = (u * RHs) const
{Return m_pinner = RHS ;}
Template <Class U>
Inline bool Operator! = (U * RHs) const
{Return m_pinner! = RHS ;}

Inline bool operator = (T * RHs) const
{Return m_pinner = RHS ;}
Inline bool Operator! = (T * RHs) const
{Return m_pinner! = RHS ;}

Static const tshareptr <t> & nullptr ()
{
Static tshareptr <t> null_handle;
Return null_handle;
}

};
//------------------------------------------------------------------------

 

//------------------------------------------------------------------------
/**
* @ Brief
*/
Template <class T>
Class tsafeptr
{
PRIVATE:

/** Internal interface */
T * m_pinner;

Inline T * _ returninner () const
{
If (! M_pinner)
{
// @ Todo throw or log error
Return T: invalid;
}

Return m_pinner;
}

Public:

Inline T * _ real_ptr () const
{Return m_pinner ;}

/** Cast is an internal interface */
Inline T * PTR () const
{
Return _ returninner ();
}
Inline const T * cptr () const
{Return _ returninner ();}
Inline operator bool () const
{Return m_pinner! = NULL ;}

Inline const T * operator-> () const
{
Return _ returninner ();
}
Inline T * operator-> ()
{
Return _ returninner ();
}

Public:

/** Default constructor */
Tsafeptr ()
{
M_pinner = NULL;
}

/** Construct through internal interfaces */
Tsafeptr (const T * pinner)
{
M_pinner = const_cast <t *> (pinner );
}

/** Copy the constructor */
Template <Class U>
Tsafeptr (tsafeptr <u> const & RHs)
{
M_pinner = static_cast <t *> (RHS. _ real_ptr ());
}
Tsafeptr (tsafeptr <t> const & RHs)
{
M_pinner = RHS. _ real_ptr ();
}

/** Destructor */
~ Tsafeptr ()
{
}

/** Value assignment function */
Template <Class U>
Inline tsafeptr <t> & operator = (tsafeptr <u> const & RHs)
{
M_pinner = static_cast <t *> (RHS. _ real_ptr ());
Return * this;
}
Inline tsafeptr <t> & operator = (tsafeptr <t> const & RHs)
{
M_pinner = RHS. _ real_ptr ();
Return * this;
}

/** Comparison function */
Template <Class U>
Inline bool operator = (tsafeptr <u> const & RHs) const
{Return m_pinner = RHS. _ real_ptr ();}
Template <Class U>
Inline bool Operator! = (Tsafeptr <u> const & RHs) const
{Return m_pinner! = RHS. _ real_ptr ();}

Template <Class U>
Inline bool operator = (u * RHs) const
{Return m_pinner = RHS ;}
Template <Class U>
Inline bool Operator! = (U * RHs) const
{Return m_pinner! = RHS ;}

Inline bool operator = (T * RHs) const
{Return m_pinner = RHS ;}
Inline bool Operator! = (T * RHs) const
{Return m_pinner! = RHS ;}

Static const tsafeptr <t> & nullptr ()
{
Static tsafeptr <t> null_handle;
Return null_handle;
}

};
//------------------------------------------------------------------------

 

//------------------------------------------------------------------------
/**
* @ Brief
*/
Template <class T>
Class tshareptrautovalid
{
PRIVATE:

/** Internal interface */
T * m_pinner;

Inline void _ validate () const
{
If (m_pinner)
{
If (m_pinner-> isdestroyed () {saferelease (const_cast <tshareptrautovalid <t> *> (this)-> m_pinner );}
}
}
Inline T * _ returninner () const
{
If (! M_pinner)
{
// Assert (false );
Return T: invalid;
}

Return m_pinner;
}

Public:

Inline T * _ real_ptr () const
{
_ Validate ();
Return m_pinner;
}
Inline operator bool () const
{Return m_pinner! = NULL ;}

Public:

/** Cast is an internal interface */
Inline T * PTR () const
{_ Validate (); Return _ returninner ();}
Inline const T * cptr () const
{_ Validate (); Return _ returninner ();}

Inline const T * operator-> () const
{_ Validate (); Return _ returninner ();}
Inline T * operator-> ()
{_ Validate (); Return _ returninner ();}

Public:

/** Default constructor */
Tshareptrautovalid ()
{
M_pinner = NULL;
}

/** Construct through internal interfaces */
Tshareptrautovalid (const T * pinner)
{
M_pinner = const_cast <t *> (pinner );
If (m_pinner)
{M_pinner-> addref ();}
}

/** Copy the constructor */
Template <Class U>
Tshareptrautovalid (tshareptrautovalid <u> const & RHs)
{
M_pinner = static_cast <t *> (RHS. _ real_ptr ());
If (m_pinner) {m_pinner-> addref ();}
}
Tshareptrautovalid (tshareptrautovalid <t> const & RHs)
{
M_pinner = RHS. _ real_ptr ();
If (m_pinner)
{M_pinner-> addref ();}
}

/** Destructor */
~ Tshareptrautovalid ()
{
If (m_pinner)
{
Static_cast <crefobject *> (m_pinner)-> release ();
M_pinner = NULL;
}
}

/** Value assignment function */
Template <Class U>
Inline tshareptrautovalid <t> & operator = (tshareptrautovalid <u> const & RHs)
{
Saferelner (m_pinner );
M_pinner = static_cast <t *> (RHS. _ real_ptr ());
If (m_pinner)
{M_pinner-> addref ();}

Return * this;
}
Inline tshareptrautovalid <t> & operator = (tshareptrautovalid <t> const & RHs)
{
Saferelner (m_pinner );
M_pinner = RHS. _ real_ptr ();
If (m_pinner)
{M_pinner-> addref ();}

Return * this;
}

/** Comparison function */
Template <Class U>
Inline bool operator = (tshareptrautovalid <u> const & RHs) const
{_ Validate (); Return m_pinner = RHS. _ real_ptr ();}
Template <Class U>
Inline bool Operator! = (Tshareptrautovalid <u> const & RHs) const
{_ Validate (); Return m_pinner! = RHS. _ real_ptr ();}

Template <Class U>
Inline bool operator = (u * RHs) const
{_ Validate (); Return m_pinner = RHS ;}
Template <Class U>
Inline bool Operator! = (U * RHs) const
{_ Validate (); Return m_pinner! = RHS ;}

Inline bool operator = (T * RHs) const
{_ Validate (); Return m_pinner = RHS ;}
Inline bool Operator! = (T * RHs) const
{_ Validate (); Return m_pinner! = RHS ;}

Static const tshareptrautovalid <t> & nullptr ()
{
Static tshareptrautovalid <t> null_handle;
Return null_handle;
}

};
//------------------------------------------------------------------------

 

//------------------------------------------------------------------------
Typedef tshareptr <crefobject> refobj_ptr;
//------------------------------------------------------------------------

 

Endnamespace

# Endif // _ refobject_h _

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.