Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9604 for code/branches


Ignore:
Timestamp:
Mar 30, 2013, 3:38:02 PM (12 years ago)
Author:
landauf
Message:

added some checks and a size() function to object list

Location:
code/branches/core6/src/libraries/core/object
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core6/src/libraries/core/object/ObjectList.h

    r9596 r9604  
    6868            typedef ObjectListIterator<T> iterator;
    6969
     70            /// Returns the size of the list
     71            inline static size_t size()
     72            {
     73                return ClassIdentifier<T>::getIdentifier()->getObjects()->size();
     74            }
     75
    7076            /// Returns an Iterator to the first element in the list.
    7177            inline static ObjectListElement<T>* begin()
  • code/branches/core6/src/libraries/core/object/ObjectListBase.cc

    r9599 r9604  
    4343    ObjectListBaseElement::~ObjectListBaseElement()
    4444    {
    45         this->list_->removeElement(this);
     45        if (this->list_)
     46            this->list_->removeElement(this);
    4647    }
    4748
     
    5354        this->first_ = 0;
    5455        this->last_ = 0;
     56        this->size_ = 0;
    5557    }
    5658
     
    6062    ObjectListBase::~ObjectListBase()
    6163    {
    62         ObjectListBaseElement* temp;
    6364        while (this->first_)
    64         {
    65             temp = this->first_->next_;
    6665            delete this->first_;
    67             this->first_ = temp;
    68         }
    6966    }
    7067
     
    8683    void ObjectListBase::addElement(ObjectListBaseElement* element)
    8784    {
     85        if (element->list_)
     86        {
     87            orxout(internal_error) << "Element is already registered in another list" << endl;
     88            return;
     89        }
     90
    8891        if (!this->last_)
    8992        {
    9093            // If the list is empty
    9194            this->last_ = element;
    92             this->first_ = this->last_; // There's only one object in the list now
     95            this->first_ = element; // There's only one object in the list now
    9396        }
    9497        else
     
    97100            ObjectListBaseElement* temp = this->last_;
    98101            this->last_ = element;
    99             this->last_->prev_ = temp;
    100             temp->next_ = this->last_;
     102            element->prev_ = temp;
     103            temp->next_ = element;
    101104        }
     105
     106        element->list_ = this;
     107        ++this->size_;
    102108    }
    103109
     
    107113    void ObjectListBase::removeElement(ObjectListBaseElement* element)
    108114    {
    109         orxout(verbose, context::object_list) << "Removing Object from " << element->objectBase_->getIdentifier()->getName() << "-list." << endl;
     115        if (element->list_ != this)
     116        {
     117            orxout(internal_error) << "Element is not registered in this list" << endl;
     118            return;
     119        }
     120
     121        if (element->objectBase_)
     122            orxout(verbose, context::object_list) << "Removing Object from " << element->objectBase_->getIdentifier()->getName() << "-list." << endl;
    110123        this->notifyRemovalListeners(element);
    111124
     
    119132        else
    120133            this->first_ = element->next_; // If there is no prev_, we deleted the first object and have to update the first_ pointer of the list
     134
     135        element->list_ = 0;
     136        element->next_ = 0;
     137        element->prev_ = 0;
     138        --this->size_;
    121139    }
    122140}
  • code/branches/core6/src/libraries/core/object/ObjectListBase.h

    r9599 r9604  
    5656                @param objectBase The object to store
    5757            */
    58             ObjectListBaseElement(Listable* object, ObjectListBase* list) : next_(0), prev_(0), objectBase_(object), list_(list) {}
     58            ObjectListBaseElement(Listable* object) : next_(0), prev_(0), objectBase_(object), list_(0) {}
    5959            ~ObjectListBaseElement();
    6060
     
    7474    {
    7575        public:
    76             ObjectListElement(T* object, ObjectListBase* list) : ObjectListBaseElement(static_cast<Listable*>(object), list), object_(object) {}
     76            ObjectListElement(T* object) : ObjectListBaseElement(static_cast<Listable*>(object)), object_(object) {}
    7777            T* object_;              //!< The object
    7878    };
     
    113113            inline ObjectListBaseElement* add(T* object)
    114114            {
    115                 ObjectListBaseElement* element = new ObjectListElement<T>(object, this);
     115                ObjectListBaseElement* element = new ObjectListElement<T>(object);
    116116                this->addElement(element);
    117117                return element;
     
    120120            void addElement(ObjectListBaseElement* element);
    121121            void removeElement(ObjectListBaseElement* element);
     122
     123            size_t size() const { return this->size_; }
    122124
    123125            /// Returns a pointer to the first element in the list. Works only with Iterator.
     
    148150            ObjectListBaseElement* first_;                              //!< The first element in the list
    149151            ObjectListBaseElement* last_;                               //!< The last element in the list
     152            size_t size_;                                               //!< The number of elements in the list
    150153            std::vector<ObjectListElementRemovalListener*> listeners_;  //!< A list of Iterators pointing on an element in this list
    151154    };
  • code/branches/core6/src/libraries/core/object/ObjectListIterator.h

    r9598 r9604  
    7070    class ObjectListIterator : public IteratorBase<T, ObjectListIterator<T> >
    7171    {
    72         template <class I>
    73         friend class Iterator;
    74 
    7572        public:
    7673            /**
Note: See TracChangeset for help on using the changeset viewer.