00001 // $Id: Intrusive_List.cpp 84273 2009-01-30 12:55:25Z johnnyw $ 00002 00003 #ifndef ACE_INTRUSIVE_LIST_CPP 00004 #define ACE_INTRUSIVE_LIST_CPP 00005 00006 #include "ace/Intrusive_List.h" 00007 00008 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00009 # pragma once 00010 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00011 00012 #if !defined (__ACE_INLINE__) 00013 #include "ace/Intrusive_List.inl" 00014 #endif /* __ACE_INLINE__ */ 00015 00016 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00017 00018 template <class T> 00019 ACE_Intrusive_List<T>::ACE_Intrusive_List (void) 00020 : head_ (0) 00021 , tail_ (0) 00022 { 00023 } 00024 00025 template<class T> 00026 ACE_Intrusive_List<T>::~ACE_Intrusive_List (void) 00027 { 00028 } 00029 00030 template<class T> void 00031 ACE_Intrusive_List<T>::push_back (T *node) 00032 { 00033 if (this->tail_ == 0) 00034 { 00035 this->tail_ = node; 00036 this->head_ = node; 00037 node->next (0); 00038 node->prev (0); 00039 } 00040 else 00041 { 00042 this->tail_->next (node); 00043 node->prev (this->tail_); 00044 node->next (0); 00045 this->tail_ = node; 00046 } 00047 } 00048 00049 template<class T> void 00050 ACE_Intrusive_List<T>::push_front (T *node) 00051 { 00052 if (this->head_ == 0) 00053 { 00054 this->tail_ = node; 00055 this->head_ = node; 00056 node->next (0); 00057 node->prev (0); 00058 } 00059 else 00060 { 00061 this->head_->prev (node); 00062 node->next (this->head_); 00063 node->prev (0); 00064 this->head_ = node; 00065 } 00066 } 00067 00068 template<class T> T * 00069 ACE_Intrusive_List<T>::pop_front (void) 00070 { 00071 T *node = this->head_; 00072 if (node != 0) 00073 { 00074 this->unsafe_remove (node); 00075 } 00076 return node; 00077 } 00078 00079 template<class T> T * 00080 ACE_Intrusive_List<T>::pop_back (void) 00081 { 00082 T *node = this->tail_; 00083 if (node != 0) 00084 { 00085 this->unsafe_remove (node); 00086 } 00087 return node; 00088 } 00089 00090 template<class T> void 00091 ACE_Intrusive_List<T>::remove (T *node) 00092 { 00093 for (T *i = this->head_; i != 0; i = i->next ()) 00094 { 00095 if (node == i) 00096 { 00097 this->unsafe_remove (node); 00098 return; 00099 } 00100 } 00101 } 00102 00103 template<class T> void 00104 ACE_Intrusive_List<T>::unsafe_remove (T *node) 00105 { 00106 if (node->prev () != 0) 00107 node->prev ()->next (node->next ()); 00108 else 00109 this->head_ = node->next (); 00110 00111 if (node->next () != 0) 00112 node->next ()->prev (node->prev ()); 00113 else 00114 this->tail_ = node->prev (); 00115 00116 node->next (0); 00117 node->prev (0); 00118 } 00119 00120 #if 0 00121 template<class T> void 00122 ACE_Intrusive_List_Node<T>::check_invariants (void) 00123 { 00124 ACE_ASSERT ((this->next () == 0) || (this->next ()->prev () == this)); 00125 ACE_ASSERT ((this->prev () == 0) || (this->prev ()->next () == this)); 00126 } 00127 00128 template<class T> void 00129 ACE_Intrusive_List<T>::check_invariants (void) 00130 { 00131 ACE_ASSERT ((this->tail_ == 0) || (this->tail_->next () == 0)); 00132 ACE_ASSERT ((this->head_ == 0) || (this->head_->prev () == 0)); 00133 ACE_ASSERT (!((this->head_ == 0) ^ (this->tail_ == 0))); 00134 00135 int found_tail = 0; 00136 for (T *i = this->head_; i != 0; i = i->next ()) 00137 { 00138 if (i == this->tail_) 00139 found_tail = 1; 00140 i->check_invariants (); 00141 } 00142 ACE_ASSERT (this->tail_ == 0 || found_tail == 1); 00143 00144 int found_head = 0; 00145 for (T *j = this->tail_; j != 0; j = j->prev ()) 00146 { 00147 if (j == this->head_) 00148 found_head = 1; 00149 j->check_invariants (); 00150 } 00151 ACE_ASSERT (this->head_ == 0 || found_head == 1); 00152 } 00153 #endif /* 0 */ 00154 00155 ACE_END_VERSIONED_NAMESPACE_DECL 00156 00157 #endif /* ACE_INTRUSIVE_LIST_CPP */