Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 25, 2013, 9:08:42 PM (11 years ago)
Author:
landauf
Message:

merged core6 back to trunk

Location:
code/trunk
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/libraries/core/object/ObjectListBase.h

    r9593 r9667  
    4242#include "core/CorePrereqs.h"
    4343#include <vector>
     44#include "Context.h"
    4445
    4546namespace orxonox
     
    5657                @param objectBase The object to store
    5758            */
    58             ObjectListBaseElement(Listable* objectBase) : next_(0), prev_(0), objectBase_(objectBase) {}
     59            ObjectListBaseElement(Listable* object) : next_(0), prev_(0), objectBase_(object), list_(0) {}
     60            virtual ~ObjectListBaseElement() { this->removeFromList(); }
     61
     62            virtual void changeContext(Context* oldContext, Context* newContext) = 0;
    5963
    6064            ObjectListBaseElement* next_;       //!< The next element in the list
    6165            ObjectListBaseElement* prev_;       //!< The previous element in the list
    62             Listable* objectBase_;
     66            Listable* objectBase_;              //!< The object
     67            ObjectListBase* list_;              //!< The list
     68
     69        protected:
     70            void removeFromList();
    6371    };
    6472
     
    7381        public:
    7482            ObjectListElement(T* object) : ObjectListBaseElement(static_cast<Listable*>(object)), object_(object) {}
     83
     84            virtual void changeContext(Context* oldContext, Context* newContext)
     85            {
     86                // add object to new context, but only if this element belongs exactly to the old context (and not to a sub-context to avoid re-adding objects
     87                // multiple times if they are in multiple contexts)
     88                if (oldContext->getObjectList<T>() == this->list_)
     89                    newContext->addObject(this->object_);
     90
     91                // remove from old list
     92                this->removeFromList();
     93            }
     94
    7595            T* object_;              //!< The object
    7696    };
    7797
     98
     99    // ########################################
     100    // ### ObjectListElementRemovalListener ###
     101    // ########################################
     102    /// Gets called by the object list if an element is removed
     103    class _CoreExport ObjectListElementRemovalListener
     104    {
     105        public:
     106            virtual ~ObjectListElementRemovalListener() {}
     107            virtual void removedElement(ObjectListBaseElement* element) = 0;
     108    };
    78109
    79110    // ###############################
     
    97128            ~ObjectListBase();
    98129
    99             template <class T>
    100             inline ObjectListBaseElement* add(T* object)
    101                 { return this->addElement(new ObjectListElement<T>(object)); }
    102 
    103             ObjectListBaseElement* addElement(ObjectListBaseElement* element);
     130            void addElement(ObjectListBaseElement* element);
    104131            void removeElement(ObjectListBaseElement* element);
    105132
    106             /// Helper struct, used to export an element and the list to an instance of Iterator.
    107             struct Export
    108             {
    109                 Export(ObjectListBase* list, ObjectListBaseElement* element) : list_(list), element_(element) {}
    110                 ObjectListBase* list_;
    111                 ObjectListBaseElement* element_;
    112             };
     133            size_t size() const { return this->size_; }
    113134
    114135            /// Returns a pointer to the first element in the list. Works only with Iterator.
    115             inline Export begin() { return ObjectListBase::Export(this, this->first_); }
     136            inline ObjectListBaseElement* begin() const { return this->first_; }
    116137            /// Returns a pointer to the element after the last element in the list. Works only with Iterator.
    117             inline Export end() { return ObjectListBase::Export(this, 0); }
     138            inline ObjectListBaseElement* end() const { return 0; }
    118139            /// Returns a pointer to the last element in the list. Works only with Iterator.
    119             inline Export rbegin() { return ObjectListBase::Export(this, this->last_); }
     140            inline ObjectListBaseElement* rbegin() const { return this->last_; }
    120141            /// Returns a pointer to the element in front of the first element in the list. Works only with Iterator.
    121             inline Export rend() { return ObjectListBase::Export(this, 0); }
     142            inline ObjectListBaseElement* rend() const { return 0; }
    122143
    123             inline void registerIterator(void* iterator) { this->iterators_.push_back(iterator); }
    124             inline void unregisterIterator(void* iterator)
     144            inline void registerRemovalListener(ObjectListElementRemovalListener* listener) { this->listeners_.push_back(listener); }
     145            inline void unregisterRemovalListener(ObjectListElementRemovalListener* listener)
    125146            {
    126                 for (unsigned int i = 0; i < this->iterators_.size(); ++i)
     147                for (unsigned int i = 0; i < this->listeners_.size(); ++i)
    127148                {
    128                     if (iterators_[i] == iterator)
     149                    if (listeners_[i] == listener)
    129150                    {
    130                         iterators_.erase(iterators_.begin() + i);
     151                        listeners_.erase(listeners_.begin() + i);
    131152                        break;
    132153                    }
    133154                }
    134155            }
    135             inline void registerObjectListIterator(void* iterator) { this->objectListIterators_.push_back(iterator); }
    136             inline void unregisterObjectListIterator(void* iterator)
    137             {
    138                 for (unsigned int i = 0; i < this->objectListIterators_.size(); ++i)
    139                 {
    140                     if (objectListIterators_[i] == iterator)
    141                     {
    142                         objectListIterators_.erase(objectListIterators_.begin() + i);
    143                         break;
    144                     }
    145                 }
    146             }
    147             void notifyIterators(Listable* object) const;
    148156
    149157        private:
    150             ObjectListBaseElement* first_;           //!< The first element in the list
    151             ObjectListBaseElement* last_;            //!< The last element in the list
    152             std::vector<void*> iterators_;           //!< A list of Iterators pointing on an element in this list
    153             std::vector<void*> objectListIterators_; //!< A list of ObjectListIterators pointing on an element in this list
     158            void notifyRemovalListeners(ObjectListBaseElement* element) const;
     159
     160            ObjectListBaseElement* first_;                              //!< The first element in the list
     161            ObjectListBaseElement* last_;                               //!< The last element in the list
     162            size_t size_;                                               //!< The number of elements in the list
     163            std::vector<ObjectListElementRemovalListener*> listeners_;  //!< A list of Iterators pointing on an element in this list
    154164    };
    155165}
Note: See TracChangeset for help on using the changeset viewer.