NodeList.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:1k
源码类别:

模拟服务器

开发平台:

C/C++

  1. #include "stdafx.h"
  2. #include "NodeList.h"
  3. #include "Macro.h"
  4. /*
  5.  * namespace OnlineGameLib
  6.  */
  7. namespace OnlineGameLib {
  8. /*
  9.  * CNodeList
  10.  */
  11. CNodeList::CNodeList()
  12. : m_pHead( 0 ),
  13. m_numNodes( 0 ) 
  14. {
  15. }
  16. void CNodeList::PushNode( Node *pNode )
  17. {
  18. ASSERT( pNode );
  19. pNode->AddToList( this );
  20. pNode->Next( m_pHead );
  21. m_pHead = pNode;
  22. ++ m_numNodes;
  23. }
  24. CNodeList::Node *CNodeList::PopNode()
  25. {
  26. Node *pNode = m_pHead;
  27. if ( pNode )
  28. {
  29. RemoveNode( pNode );
  30. }
  31. return pNode;
  32. }
  33. void CNodeList::RemoveNode( Node *pNode )
  34. {
  35. ASSERT( pNode );
  36. if ( pNode == m_pHead )
  37. {
  38. m_pHead = pNode->Next();
  39. }
  40. pNode->Unlink();
  41. -- m_numNodes;
  42. }
  43. /*
  44.  * CNodeList::Node
  45.  */
  46. CNodeList::Node::Node() 
  47. : m_pNext( 0 ),
  48. m_pPrev( 0 ),
  49. m_pList( 0 ) 
  50. {
  51. }
  52. CNodeList::Node::~Node() 
  53. {
  54. try
  55. {
  56. RemoveFromList();   
  57. }
  58. catch( ... )
  59. {
  60. TRACE( "CNodeList::Node::~Node() exception!" );
  61. }
  62. m_pNext = 0;
  63. m_pPrev = 0;
  64. m_pList = 0;
  65. }
  66. void CNodeList::Node::AddToList( CNodeList *pList )
  67. {
  68. m_pList = pList;
  69. }
  70. void CNodeList::Node::RemoveFromList()
  71. {
  72. if ( m_pList )
  73. {
  74. m_pList->RemoveNode( this );
  75. }
  76. }
  77. void CNodeList::Node::Unlink()
  78. {
  79. if ( m_pPrev )
  80. {
  81. m_pPrev->m_pNext = m_pNext;
  82. }
  83. if ( m_pNext )
  84. {
  85. m_pNext->m_pPrev = m_pPrev;
  86. }
  87.    
  88. m_pNext = 0;
  89. m_pPrev = 0;
  90. m_pList = 0;
  91. }
  92. } // End of namespace OnlineGameLib