//============================================================================= /** * @file Naming_Service_Container.cpp * * @author Bruce Trask */ //============================================================================= #ifndef NS_CONTAINER_CPP #define NS_CONTAINER_CPP #include "orbsvcs/Log_Macros.h" #include "orbsvcs/Log_Macros.h" #include "ace/Malloc_Base.h" #if !defined (ACE_LACKS_PRAGMA_ONCE) # pragma once #endif /* ACE_LACKS_PRAGMA_ONCE */ #include "orbsvcs/Naming/Naming_Service_Container.h" TAO_BEGIN_VERSIONED_NAMESPACE_DECL ACE_ALLOC_HOOK_DEFINE(ACE_NS_Node) template ACE_NS_Node::~ACE_NS_Node (void) { } template ACE_NS_Node::ACE_NS_Node (const T &i, ACE_NS_Node *n) : next_ (n), item_ (i) { // ACE_TRACE ("ACE_NS_Node::ACE_NS_Node"); } template ACE_NS_Node::ACE_NS_Node (ACE_NS_Node *n, int) : next_ (n) { // ACE_TRACE ("ACE_NS_Node::ACE_NS_Node"); } template ACE_NS_Node::ACE_NS_Node (const ACE_NS_Node &s) : next_ (s.next_), item_ (s.item_) { // ACE_TRACE ("ACE_NS_Node::ACE_NS_Node"); } ACE_ALLOC_HOOK_DEFINE(ACE_Unbounded_List) template size_t ACE_Unbounded_List::size () const { // ACE_TRACE ("ACE_Unbounded_List::size"); return this->cur_size_; } template int ACE_Unbounded_List::insert_tail (const T &item) { ACE_NS_Node *temp; // Insert into the old dummy node location. this->head_->item_ = item; // Create a new dummy node. ACE_NEW_MALLOC_RETURN (temp, (ACE_NS_Node*) this->allocator_->malloc (sizeof (ACE_NS_Node)), ACE_NS_Node (this->head_->next_), -1); // Link this pointer into the list. this->head_->next_ = temp; // Point the head to the new dummy node. this->head_ = temp; this->cur_size_++; return 0; } template void ACE_Unbounded_List::reset (void) { ACE_TRACE ("reset"); this->delete_nodes (); } template void ACE_Unbounded_List::dump () const { ACE_TRACE ("ACE_Unbounded_List::dump"); ORBSVCS_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this)); ORBSVCS_DEBUG ((LM_DEBUG, ACE_TEXT ("\nhead_ = %u"), this->head_)); ORBSVCS_DEBUG ((LM_DEBUG, ACE_TEXT ("\nhead_->next_ = %u"), this->head_->next_)); ORBSVCS_DEBUG ((LM_DEBUG, ACE_TEXT ("\ncur_size_ = %d\n"), this->cur_size_)); T *item = 0; #if !defined (ACE_NLOGGING) size_t count = 1; #endif /* ! ACE_NLOGGING */ for (ACE_Unbounded_List_Iterator iter (*(ACE_Unbounded_List *) this); iter.next (item) != 0; iter.advance ()) ORBSVCS_DEBUG ((LM_DEBUG, ACE_TEXT ("count = %d\n"), count++)); ORBSVCS_DEBUG ((LM_DEBUG, ACE_END_DUMP)); } template void ACE_Unbounded_List::copy_nodes (const ACE_Unbounded_List &us) { for (ACE_NS_Node *curr = us.head_->next_; curr != us.head_; curr = curr->next_) this->insert_tail (curr->item_); } template void ACE_Unbounded_List::delete_nodes (void) { ACE_NS_Node *curr = this->head_->next_; // Keep looking until we've hit the dummy node. while (curr != this->head_) { ACE_NS_Node *temp = curr; curr = curr->next_; ACE_DES_FREE_TEMPLATE (temp, this->allocator_->free, ACE_NS_Node, ); this->cur_size_--; } // Reset the list to be a circular list with just a dummy node. this->head_->next_ = this->head_; } template ACE_Unbounded_List::~ACE_Unbounded_List (void) { // ACE_TRACE ("ACE_Unbounded_List::~ACE_Unbounded_List"); this->delete_nodes (); // Delete the dummy node. ACE_DES_FREE_TEMPLATE (head_, this->allocator_->free, ACE_NS_Node, ); this->head_ = 0; } template ACE_Unbounded_List::ACE_Unbounded_List (ACE_Allocator *alloc) : head_ (0), cur_size_ (0), allocator_ (alloc) { // ACE_TRACE ("ACE_Unbounded_List::ACE_Unbounded_List"); if (this->allocator_ == 0) this->allocator_ = ACE_Allocator::instance (); ACE_NEW_MALLOC (this->head_, (ACE_NS_Node*) this->allocator_->malloc (sizeof (ACE_NS_Node)), ACE_NS_Node); // Make the list circular by pointing it back to itself. this->head_->next_ = this->head_; } template ACE_Unbounded_List::ACE_Unbounded_List (const ACE_Unbounded_List &us) : head_ (0), cur_size_ (0), allocator_ (us.allocator_) { ACE_TRACE ("ACE_Unbounded_List::ACE_Unbounded_List"); if (this->allocator_ == 0) this->allocator_ = ACE_Allocator::instance (); ACE_NEW_MALLOC (this->head_, (ACE_NS_Node*) this->allocator_->malloc (sizeof (ACE_NS_Node)), ACE_NS_Node); this->head_->next_ = this->head_; this->copy_nodes (us); } template void ACE_Unbounded_List::operator= (const ACE_Unbounded_List &us) { ACE_TRACE ("ACE_Unbounded_List::operator="); if (this != &us) { this->delete_nodes (); this->copy_nodes (us); } } template int ACE_Unbounded_List::insert (const T &item) { ACE_TRACE ("ACE_Unbounded_List::insert"); return this->insert_tail (item); } template int ACE_Unbounded_List::remove (const T &item) { // ACE_TRACE ("ACE_Unbounded_List::remove"); // Insert the item to be founded into the dummy node. this->head_->item_ = item; ACE_NS_Node *curr = this->head_; while (!(curr->next_->item_ == item)) curr = curr->next_; if (curr->next_ == this->head_) return -1; // Item was not found. else { ACE_NS_Node *temp = curr->next_; // Skip over the node that we're deleting. curr->next_ = temp->next_; this->cur_size_--; ACE_DES_FREE_TEMPLATE (temp, this->allocator_->free, ACE_NS_Node, ); return 0; } } template ACE_Unbounded_List_Iterator ACE_Unbounded_List::begin (void) { // ACE_TRACE ("ACE_Unbounded_List::begin"); return ACE_Unbounded_List_Iterator (*this); } template ACE_Unbounded_List_Iterator ACE_Unbounded_List::end (void) { // ACE_TRACE ("ACE_Unbounded_List::end"); return ACE_Unbounded_List_Iterator (*this, 1); } ACE_ALLOC_HOOK_DEFINE(ACE_Unbounded_List_Iterator) template void ACE_Unbounded_List_Iterator::dump () const { // ACE_TRACE ("ACE_Unbounded_List_Iterator::dump"); } template ACE_Unbounded_List_Iterator::ACE_Unbounded_List_Iterator (ACE_Unbounded_List &s, int end) : current_ (end == 0 ? s.head_->next_ : s.head_ ), set_ (&s) { // ACE_TRACE ("ACE_Unbounded_List_Iterator::ACE_Unbounded_List_Iterator"); } template int ACE_Unbounded_List_Iterator::advance (void) { // ACE_TRACE ("ACE_Unbounded_List_Iterator::advance"); this->current_ = this->current_->next_; return this->current_ != this->set_->head_; } template int ACE_Unbounded_List_Iterator::first (void) { // ACE_TRACE ("ACE_Unbounded_List_Iterator::first"); this->current_ = this->set_->head_->next_; return this->current_ != this->set_->head_; } template int ACE_Unbounded_List_Iterator::done () const { ACE_TRACE ("ACE_Unbounded_List_Iterator::done"); return this->current_ == this->set_->head_; } template int ACE_Unbounded_List_Iterator::next (T *&item) { // ACE_TRACE ("ACE_Unbounded_List_Iterator::next"); if (this->current_ == this->set_->head_) return 0; else { item = &this->current_->item_; return 1; } } template ACE_Unbounded_List_Iterator ACE_Unbounded_List_Iterator::operator++ (int) { //ACE_TRACE ("ACE_Unbounded_List_Iterator::operator++ (int)"); ACE_Unbounded_List_Iterator retv (*this); // postfix operator this->advance (); return retv; } template ACE_Unbounded_List_Iterator& ACE_Unbounded_List_Iterator::operator++ (void) { // ACE_TRACE ("ACE_Unbounded_List_Iterator::operator++ (void)"); // prefix operator this->advance (); return *this; } template T& ACE_Unbounded_List_Iterator::operator* (void) { //ACE_TRACE ("ACE_Unbounded_List_Iterator::operator*"); T *retv = 0; int result = this->next (retv); ACE_ASSERT (result != 0); ACE_UNUSED_ARG (result); return *retv; } template bool ACE_Unbounded_List_Iterator::operator== (const ACE_Unbounded_List_Iterator &rhs) const { //ACE_TRACE ("ACE_Unbounded_List_Iterator::operator=="); return (this->set_ == rhs.set_ && this->current_ == rhs.current_); } template bool ACE_Unbounded_List_Iterator::operator!= (const ACE_Unbounded_List_Iterator &rhs) const { //ACE_TRACE ("ACE_Unbounded_List_Iterator::operator!="); return (this->set_ != rhs.set_ || this->current_ != rhs.current_); } // --- template int ACE_Unbounded_List::is_empty () const { ACE_TRACE ("ACE_Unbounded_List::is_empty"); return this->head_ == this->head_->next_; } template int ACE_Unbounded_List::is_full () const { ACE_TRACE ("ACE_Unbounded_List::is_full"); return 0; // We should implement a "node of last resort for this..." } TAO_END_VERSIONED_NAMESPACE_DECL #endif /* NS_CONTAINERS_T_CPP */