www.pudn.com > DirectX source.zip > CObject.cpp
/*!
@file : CObject.cpp
Contains the implementation of class CObject.
@author Sarmad Kh. Abdulla
*/
//------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
// Header files
#include "stdafx.h"
/*!
* Initializes the link tree variables of this object
*
* @param none
*
* @return :
*/
CObject::CObject()
{
pFirstChild = NULL;
pLastChild = NULL;
pPrevSibling = NULL;
pNextSibling = NULL;
pOwner = NULL;
}
/*!
* Removes the object from the tree by detaching it from its parent and detaching all
* the childs of this object.
*
* @param none
*
* @return :
*/
CObject::~CObject()
{
// detaches all the childs
while( pFirstChild ) DetachChild( *pFirstChild );
// detaches this object from it's parent
if( pOwner )
pOwner->DetachChild( *this );
}
/*!
* Attaches a child object to this object.
*
* @param object : A reference to the child object to attach.
*
* @return void :
*/
void CObject::AttachChild( CObject & object )
{
if( object.pOwner )
object.pOwner->DetachChild( object );
object.pPrevSibling = pLastChild;
object.pNextSibling = NULL;
if( pLastChild )
pLastChild->pNextSibling = &object;
if( !pFirstChild )
pFirstChild = &object;
pLastChild = &object;
object.pOwner = this;
}
/*!
* Detaches a child object from this object.
*
* @param object : A reference to the child object to detach.
*
* @return void :
*/
void CObject::DetachChild( CObject & object )
{
if( object.pPrevSibling )
object.pPrevSibling->pNextSibling = object.pNextSibling;
if( object.pNextSibling )
object.pNextSibling->pPrevSibling = object.pPrevSibling;
if( !object.pPrevSibling )
pFirstChild = object.pNextSibling;
if( !object.pNextSibling )
pLastChild = object.pPrevSibling;
object.pPrevSibling = NULL;
object.pNextSibling = NULL;
object.pOwner = NULL;
}
/*!
* Delete all the childs attached to this object
*
* @param void :
*
* @return void :
*/
void CObject::DeleteChilds( void )
{
CObject * pobj = GetFirstChild();
while( pobj )
{
pobj->DeleteChilds();
delete pobj;
pobj = GetFirstChild();
}
}