Go to the documentation of this file.00001
00002
00003 #ifndef ACE_UNBOUNDED_QUEUE_CPP
00004 #define ACE_UNBOUNDED_QUEUE_CPP
00005
00006 #include "ace/Unbounded_Queue.h"
00007
00008 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00009 # pragma once
00010 #endif
00011
00012 #if !defined (__ACE_INLINE__)
00013 #include "ace/Unbounded_Queue.inl"
00014 #endif
00015
00016 #include "ace/Malloc_Base.h"
00017 #include "ace/Log_Msg.h"
00018 #include "ace/os_include/os_errno.h"
00019
00020 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00021
00022 ACE_ALLOC_HOOK_DEFINE(ACE_Unbounded_Queue)
00023
00024 template <class T>
00025 ACE_Unbounded_Queue<T>::ACE_Unbounded_Queue (ACE_Allocator *alloc)
00026 : head_ (0),
00027 cur_size_ (0),
00028 allocator_ (alloc)
00029 {
00030
00031
00032 if (this->allocator_ == 0)
00033 this->allocator_ = ACE_Allocator::instance ();
00034
00035 ACE_NEW_MALLOC (this->head_,
00036 (ACE_Node<T> *) this->allocator_->malloc (sizeof (ACE_Node<T>)),
00037 ACE_Node<T>);
00038
00039 this->head_->next_ = this->head_;
00040 }
00041
00042 template <class T>
00043 ACE_Unbounded_Queue<T>::ACE_Unbounded_Queue (const ACE_Unbounded_Queue<T> &us)
00044 : head_ (0),
00045 cur_size_ (0),
00046 allocator_ (us.allocator_)
00047 {
00048
00049
00050 if (this->allocator_ == 0)
00051 this->allocator_ = ACE_Allocator::instance ();
00052
00053 ACE_NEW_MALLOC (this->head_,
00054 (ACE_Node<T> *) this->allocator_->malloc (sizeof (ACE_Node<T>)),
00055 ACE_Node<T>);
00056 this->head_->next_ = this->head_;
00057 this->copy_nodes (us);
00058 }
00059
00060 template <class T> void
00061 ACE_Unbounded_Queue<T>::operator= (const ACE_Unbounded_Queue<T> &us)
00062 {
00063
00064
00065 if (this != &us)
00066 {
00067 this->delete_nodes ();
00068 this->copy_nodes (us);
00069 }
00070 }
00071
00072 template <class T> ACE_Unbounded_Queue_Iterator<T>
00073 ACE_Unbounded_Queue<T>::begin (void)
00074 {
00075
00076 return ACE_Unbounded_Queue_Iterator<T> (*this);
00077 }
00078
00079 template <class T> ACE_Unbounded_Queue_Iterator<T>
00080 ACE_Unbounded_Queue<T>::end (void)
00081 {
00082
00083 return ACE_Unbounded_Queue_Iterator<T> (*this, 1);
00084 }
00085
00086 template <class T> void
00087 ACE_Unbounded_Queue<T>::dump (void) const
00088 {
00089 #if defined (ACE_HAS_DUMP)
00090
00091
00092 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00093 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nhead_ = %u"), this->head_));
00094 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nhead_->next_ = %u"), this->head_->next_));
00095 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\ncur_size_ = %d\n"), this->cur_size_));
00096
00097 T *item = 0;
00098 #if !defined (ACE_NLOGGING)
00099 size_t count = 1;
00100 #endif
00101
00102 for (ACE_Unbounded_Queue_Iterator<T> iter (*(ACE_Unbounded_Queue<T> *) this);
00103 iter.next (item) != 0;
00104 iter.advance ())
00105 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("count = %d\n"), count++));
00106
00107 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00108 #endif
00109 }
00110
00111 template <class T> void
00112 ACE_Unbounded_Queue<T>::copy_nodes (const ACE_Unbounded_Queue<T> &us)
00113 {
00114 for (ACE_Node<T> *curr = us.head_->next_;
00115 curr != us.head_;
00116 curr = curr->next_)
00117 if (this->enqueue_tail (curr->item_) == -1)
00118
00119 this->delete_nodes ();
00120 }
00121
00122 template <class T> void
00123 ACE_Unbounded_Queue<T>::delete_nodes (void)
00124 {
00125 for (ACE_Node<T> *curr = this->head_->next_;
00126
00127 curr != this->head_;
00128 )
00129 {
00130 ACE_Node<T> *temp = curr;
00131 curr = curr->next_;
00132
00133 ACE_DES_FREE_TEMPLATE (temp,
00134 this->allocator_->free,
00135 ACE_Node,
00136 <T>);
00137 --this->cur_size_;
00138
00139
00140
00141 }
00142
00143
00144 this->head_->next_ = this->head_;
00145 }
00146
00147 template <class T>
00148 ACE_Unbounded_Queue<T>::~ACE_Unbounded_Queue (void)
00149 {
00150
00151
00152 this->delete_nodes ();
00153 ACE_DES_FREE_TEMPLATE (head_,
00154 this->allocator_->free,
00155 ACE_Node,
00156 <T>);
00157 }
00158
00159 template <class T> int
00160 ACE_Unbounded_Queue<T>::enqueue_head (const T &new_item)
00161 {
00162
00163
00164 ACE_Node<T> *temp = 0;
00165
00166
00167 ACE_NEW_MALLOC_RETURN (temp,
00168 static_cast<ACE_Node<T> *> (this->allocator_->malloc (sizeof (ACE_Node<T>))),
00169 ACE_Node<T> (new_item, this->head_->next_),
00170 -1);
00171
00172
00173
00174 this->head_->next_ = temp;
00175
00176 ++this->cur_size_;
00177 return 0;
00178 }
00179
00180 template <class T> int
00181 ACE_Unbounded_Queue<T>::enqueue_tail (const T &new_item)
00182 {
00183
00184
00185
00186
00187
00188 this->head_->item_ = new_item;
00189
00190 ACE_Node<T> *temp = 0;
00191
00192
00193 ACE_NEW_MALLOC_RETURN (temp,
00194 static_cast<ACE_Node<T> *> (this->allocator_->malloc (sizeof (ACE_Node<T>))),
00195 ACE_Node<T> (this->head_->next_),
00196 -1);
00197
00198 this->head_->next_ = temp;
00199
00200
00201 this->head_ = temp;
00202
00203 ++this->cur_size_;
00204 return 0;
00205 }
00206
00207 template <class T> int
00208 ACE_Unbounded_Queue<T>::dequeue_head (T &item)
00209 {
00210
00211
00212
00213 if (this->is_empty ())
00214 return -1;
00215
00216 ACE_Node<T> *temp = this->head_->next_;
00217
00218 item = temp->item_;
00219 this->head_->next_ = temp->next_;
00220 ACE_DES_FREE_TEMPLATE (temp,
00221 this->allocator_->free,
00222 ACE_Node,
00223 <T>);
00224 --this->cur_size_;
00225 return 0;
00226 }
00227
00228 template <class T> void
00229 ACE_Unbounded_Queue<T>::reset (void)
00230 {
00231 ACE_TRACE ("reset");
00232
00233 this->delete_nodes ();
00234 }
00235
00236 template <class T> int
00237 ACE_Unbounded_Queue<T>::get (T *&item, size_t slot) const
00238 {
00239
00240
00241 ACE_Node<T> *curr = this->head_->next_;
00242
00243 size_t i;
00244
00245 for (i = 0; i < this->cur_size_; i++)
00246 {
00247 if (i == slot)
00248 break;
00249
00250 curr = curr->next_;
00251 }
00252
00253 if (i < this->cur_size_)
00254 {
00255 item = &curr->item_;
00256 return 0;
00257 }
00258 else
00259 return -1;
00260 }
00261
00262 template <class T> int
00263 ACE_Unbounded_Queue<T>::set (const T &item,
00264 size_t slot)
00265 {
00266
00267
00268 ACE_Node<T> *curr = this->head_->next_;
00269
00270 size_t i;
00271
00272 for (i = 0;
00273 i < slot && i < this->cur_size_;
00274 ++i)
00275 curr = curr->next_;
00276
00277 if (i < this->cur_size_)
00278 {
00279
00280 curr->item_ = item;
00281 return 0;
00282 }
00283 else
00284 {
00285
00286
00287
00288
00289 if (i == slot)
00290 {
00291
00292 if (this->enqueue_tail (item) == -1)
00293 return -1;
00294 else
00295 return 0;
00296 }
00297 else
00298 {
00299 T const dummy = T ();
00300
00301
00302 for (; i < slot; ++i)
00303 {
00304
00305
00306
00307 curr = this->head_;
00308
00309
00310
00311 if (this->enqueue_tail (dummy) == -1)
00312 return -1;
00313 }
00314
00315 curr->item_ = item;
00316 return 0;
00317 }
00318 }
00319 }
00320
00321
00322
00323 template <class T> void
00324 ACE_Unbounded_Queue_Const_Iterator<T>::dump (void) const
00325 {
00326 #if defined (ACE_HAS_DUMP)
00327
00328 #endif
00329 }
00330
00331 template <class T>
00332 ACE_Unbounded_Queue_Const_Iterator<T>::ACE_Unbounded_Queue_Const_Iterator (const ACE_Unbounded_Queue<T> &q, int end)
00333 : current_ (end == 0 ? q.head_->next_ : q.head_ ),
00334 queue_ (q)
00335 {
00336
00337 }
00338
00339 template <class T> int
00340 ACE_Unbounded_Queue_Const_Iterator<T>::advance (void)
00341 {
00342
00343 this->current_ = this->current_->next_;
00344 return this->current_ != this->queue_.head_;
00345 }
00346
00347 template <class T> int
00348 ACE_Unbounded_Queue_Const_Iterator<T>::first (void)
00349 {
00350
00351 this->current_ = this->queue_.head_->next_;
00352 return this->current_ != this->queue_.head_;
00353 }
00354
00355 template <class T> int
00356 ACE_Unbounded_Queue_Const_Iterator<T>::done (void) const
00357 {
00358 ACE_TRACE ("ACE_Unbounded_Queue_Const_Iterator<T>::done");
00359
00360 return this->current_ == this->queue_.head_;
00361 }
00362
00363 template <class T> int
00364 ACE_Unbounded_Queue_Const_Iterator<T>::next (T *&item)
00365 {
00366
00367 if (this->current_ == this->queue_.head_)
00368 return 0;
00369 else
00370 {
00371 item = &this->current_->item_;
00372 return 1;
00373 }
00374 }
00375
00376
00377
00378 template <class T> void
00379 ACE_Unbounded_Queue_Iterator<T>::dump (void) const
00380 {
00381 #if defined (ACE_HAS_DUMP)
00382
00383 #endif
00384 }
00385
00386 template <class T>
00387 ACE_Unbounded_Queue_Iterator<T>::ACE_Unbounded_Queue_Iterator (ACE_Unbounded_Queue<T> &q, int end)
00388 : current_ (end == 0 ? q.head_->next_ : q.head_ ),
00389 queue_ (q)
00390 {
00391
00392 }
00393
00394 template <class T> int
00395 ACE_Unbounded_Queue_Iterator<T>::advance (void)
00396 {
00397
00398 this->current_ = this->current_->next_;
00399 return this->current_ != this->queue_.head_;
00400 }
00401
00402 template <class T> int
00403 ACE_Unbounded_Queue_Iterator<T>::first (void)
00404 {
00405
00406 this->current_ = this->queue_.head_->next_;
00407 return this->current_ != this->queue_.head_;
00408 }
00409
00410 template <class T> int
00411 ACE_Unbounded_Queue_Iterator<T>::done (void) const
00412 {
00413 ACE_TRACE ("ACE_Unbounded_Queue_Iterator<T>::done");
00414
00415 return this->current_ == this->queue_.head_;
00416 }
00417
00418 template <class T> int
00419 ACE_Unbounded_Queue_Iterator<T>::next (T *&item)
00420 {
00421
00422 if (this->current_ == this->queue_.head_)
00423 return 0;
00424 else
00425 {
00426 item = &this->current_->item_;
00427 return 1;
00428 }
00429 }
00430
00431 ACE_END_VERSIONED_NAMESPACE_DECL
00432
00433 #endif