[Orxonox-commit 4619] r9290 - code/branches/presentation2012merge/src/modules/pickup

landauf at orxonox.net landauf at orxonox.net
Sun Jun 10 23:01:40 CEST 2012


Author: landauf
Date: 2012-06-10 23:01:40 +0200 (Sun, 10 Jun 2012)
New Revision: 9290

Modified:
   code/branches/presentation2012merge/src/modules/pickup/CollectiblePickup.cc
   code/branches/presentation2012merge/src/modules/pickup/CollectiblePickup.h
   code/branches/presentation2012merge/src/modules/pickup/PickupCollection.cc
   code/branches/presentation2012merge/src/modules/pickup/PickupCollection.h
   code/branches/presentation2012merge/src/modules/pickup/PickupCollectionIdentifier.cc
   code/branches/presentation2012merge/src/modules/pickup/PickupCollectionIdentifier.h
Log:
Fixed crash with MSVC if a PickupCollection was used
A depleted CollectiblePickup is now destroyed instead of being dropped
A destroyed CollectiblePickup removes itself from the PickupCollection
PickupCollection has to use a list instead of a vector because of this reason
Also PickupCollectionIdentifier needed to be changed because the number of pickups in a collection may now change
Probably also fixed a bug in PickupCollectionIdentifier::compare() because it2 was not incremented

not completely clean yet

Modified: code/branches/presentation2012merge/src/modules/pickup/CollectiblePickup.cc
===================================================================
--- code/branches/presentation2012merge/src/modules/pickup/CollectiblePickup.cc	2012-06-10 17:58:42 UTC (rev 9289)
+++ code/branches/presentation2012merge/src/modules/pickup/CollectiblePickup.cc	2012-06-10 21:01:40 UTC (rev 9290)
@@ -44,11 +44,9 @@
         Constructor.
         Registers the object and initializes variables.
     */
-    CollectiblePickup::CollectiblePickup() : isInCollection_(false)
+    CollectiblePickup::CollectiblePickup() : collection_(NULL)
     {
         RegisterObject(CollectiblePickup);
-
-        this->collection_ = NULL;
     }
 
     /**
@@ -57,43 +55,12 @@
     */
     CollectiblePickup::~CollectiblePickup()
     {
-
+        if (this->isInCollection())
+            this->collection_->removePickupable(this);
     }
 
     /**
     @brief
-        Is called by OrxonoxClass::destroy() before the object is actually destroyed.
-    */
-    void CollectiblePickup::preDestroy(void)
-    {
-        this->Pickupable::preDestroy();
-
-        // The PickupCollection has to be destroyed as well.
-        if(this->isInCollection())
-            this->collection_->Pickupable::destroy();
-    }
-
-    /**
-    @brief
-        Destroys a Pickupable.
-    */
-    void CollectiblePickup::destroyPickup(void)
-    {
-        if(!this->isInCollection()) // If the CollectiblePickup is not in a PickupCollection the destroyPickup method of Pickupable is called.
-            this->Pickupable::destroyPickup();
-        else // Else the ColectiblePickup is dropped and disabled,
-        {
-            this->drop(false);
-            if(this->isInCollection() && this->isEnabled()) // It is only disabled if it is enabled and still ina PickupCollection after having been dropped.
-            {
-                this->setDisabled();
-                this->collection_->pickupDisabled();
-            }
-        }
-    }
-
-    /**
-    @brief
         Is called by the PickupCarrier when it is being destroyed.
     */
     void CollectiblePickup::carrierDestroyed(void)
@@ -130,36 +97,21 @@
 
     /**
     @brief
-        Adds this CollectiblePickup to the input PickupCollection.
+        Notifies this CollectiblePickup that it was added to a PickupCollection.
     @param collection
         A pointer to the PickupCollection to which the CollectiblePickup should be added.
-    @return
-        Returns true if the CollectiblePickup was successfully added to the PickupCollection.
     */
-    bool CollectiblePickup::addToCollection(PickupCollection* collection)
+    void CollectiblePickup::wasAddedToCollection(PickupCollection* collection)
     {
-        if(this->isInCollection() || collection == NULL) //If the CollectiblePickup already is in a PickupCollection or if the input pointer is NULL.
-            return false;
-
-        this->isInCollection_ = true;
         this->collection_ = collection;
-        return true;
     }
 
     /**
     @brief
-        Removes this CollectiblePickup from its PickupCollection.
-    @return
-        Returns true if the CollectiblePickup was succcessfully removed.
+        Notifies this CollectiblePickup that it was removed from its PickupCollection.
     */
-    bool CollectiblePickup::removeFromCollection(void)
+    void CollectiblePickup::wasRemovedFromCollection(void)
     {
-        if(!this->isInCollection()) //If the CollectiblePickup is not in a PickupCollection.
-            return false;
-
-        this->isInCollection_ = false;
         this->collection_ = NULL;
-        return true;
     }
-
 }

Modified: code/branches/presentation2012merge/src/modules/pickup/CollectiblePickup.h
===================================================================
--- code/branches/presentation2012merge/src/modules/pickup/CollectiblePickup.h	2012-06-10 17:58:42 UTC (rev 9289)
+++ code/branches/presentation2012merge/src/modules/pickup/CollectiblePickup.h	2012-06-10 21:01:40 UTC (rev 9290)
@@ -54,6 +54,7 @@
     */
     class _PickupExport CollectiblePickup : public Pickupable
     {
+        friend class PickupCollection;
 
         public:
             CollectiblePickup(); //!< Constructor.
@@ -67,21 +68,15 @@
             @return Returns true if the CollectiblePickup is part of a PickupCollection.
             */
             bool isInCollection(void) const
-                { return this->isInCollection_; }
+                { return this->collection_ != NULL; }
 
-            bool addToCollection(PickupCollection* collection); //!< Adds this CollectiblePickup to the input PickupCollection.
-            bool removeFromCollection(void); //!< Removes this CollectiblePickup from its PickupCollection.
-
             void carrierDestroyed(void); //!< Is called by the PickupCarrier when it is being destroyed.
 
-        protected:
-            virtual void preDestroy(void); //!< Is called by OrxonoxClass::destroy() before the object is actually destroyed.
-            virtual void destroyPickup(void); //!< Destroys a Pickupable.
-
         private:
-            bool isInCollection_; //!< True if the CollectiblePickup is in a PickupCollection.
-            PickupCollection* collection_; //!< A pointer to the PickupCollection this CollectiblePickup is in.
+            void wasAddedToCollection(PickupCollection* collection);
+            void wasRemovedFromCollection(void);
 
+            PickupCollection* collection_; //!< A pointer to the PickupCollection this CollectiblePickup is in.
     };
 }
 

Modified: code/branches/presentation2012merge/src/modules/pickup/PickupCollection.cc
===================================================================
--- code/branches/presentation2012merge/src/modules/pickup/PickupCollection.cc	2012-06-10 17:58:42 UTC (rev 9289)
+++ code/branches/presentation2012merge/src/modules/pickup/PickupCollection.cc	2012-06-10 21:01:40 UTC (rev 9290)
@@ -69,13 +69,13 @@
     @brief
         Destructor. Iterates through all Pickupables this PickupCollection consists of and destroys them if they haven't been already.
     */
-    PickupCollection::~ PickupCollection()
+    PickupCollection::~PickupCollection()
     {
         // Destroy all Pickupables constructing this PickupCollection.
-        for(std::vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
+        for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it)
         {
-            (*it)->removeFromCollection();
-            (*it)->destroy();
+            (*it)->wasRemovedFromCollection();
+            (*it)->destroyPickup();
         }
         this->pickups_.clear();
 
@@ -92,24 +92,10 @@
         SUPER(PickupCollection, XMLPort, xmlelement, mode);
 
         XMLPortObject(PickupCollection, CollectiblePickup, "pickupables", addPickupable, getPickupable, xmlelement, mode);
-
-        this->initializeIdentifier();
     }
 
     /**
     @brief
-        Initializes the PickupIdentifier for this pickup.
-    */
-    void PickupCollection::initializeIdentifier(void)
-    {
-        for(std::vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
-        {
-            this->pickupCollectionIdentifier_->addPickup((*it)->getPickupIdentifier());
-        }
-    }
-
-    /**
-    @brief
         Is called when the pickup has transited from used to unused or the other way around.
         Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedUsed); to their changdeUsed method.
     */
@@ -119,7 +105,7 @@
 
         this->processingUsed_ = true;
         // Change used for all Pickupables this PickupCollection consists of.
-        for(std::vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
+        for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it)
             (*it)->setUsed(this->isUsed());
 
         this->processingUsed_ = false;
@@ -156,7 +142,7 @@
         SUPER(PickupCollection, changedCarrier);
 
         // Change the PickupCarrier for all Pickupables this PickupCollection consists of.
-        for(std::vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
+        for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it)
         {
             if(this->getCarrier() == NULL)
                 (*it)->setCarrier(NULL);
@@ -176,8 +162,8 @@
 
         this->processingPickedUp_ = true;
         // Change the pickedUp status for all Pickupables this PickupCollection consists of.
-        for(std::vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
-            (*it)->setPickedUp(this->isPickedUp());
+        for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); )
+            (*(it++))->setPickedUp(this->isPickedUp());
 
         this->processingPickedUp_ = false;
 
@@ -219,14 +205,12 @@
 
         PickupCollection* pickup = orxonox_cast<PickupCollection*>(item);
         // Clone all Pickupables this PickupCollection consist of.
-        for(std::vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
+        for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it)
         {
             Pickupable* newPickup = (*it)->clone();
             CollectiblePickup* collectible = static_cast<CollectiblePickup*>(newPickup);
             pickup->addPickupable(collectible);
         }
-
-        pickup->initializeIdentifier();
     }
 
     /**
@@ -239,7 +223,7 @@
     */
     bool PickupCollection::isTarget(const PickupCarrier* carrier) const
     {
-        for(std::vector<CollectiblePickup*>::const_iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
+        for(std::list<CollectiblePickup*>::const_iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it)
         {
             if(!carrier->isTarget(*it))
                 return false;
@@ -273,8 +257,8 @@
         if(pickup == NULL)
             return false;
 
-        pickup->addToCollection(this);
         this->pickups_.push_back(pickup);
+        pickup->wasAddedToCollection(this);
         return true;
     }
 
@@ -288,11 +272,41 @@
     */
     const Pickupable* PickupCollection::getPickupable(unsigned int index) const
     {
-        return this->pickups_[index];
+        unsigned int count = 0;
+        for (std::list<CollectiblePickup*>::const_iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it)
+        {
+            if (count == index)
+                return *it;
+            else
+                ++count;
+        }
+        return NULL;
     }
 
     /**
     @brief
+        Removes the Pickup from the Collection.
+    @param pickup
+        The Pickup to be removed.
+    @return
+        Returns true if the pickup was in the collection.
+    */
+    bool PickupCollection::removePickupable(CollectiblePickup* pickup)
+    {
+        for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it)
+        {
+            if (*it == pickup)
+            {
+                this->pickups_.erase(it);
+                pickup->wasRemovedFromCollection();
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+    @brief
         Informs the PickupCollection, that one of its pickups has changed its used status to the input value.
         This is used internally by the CollectiblePickup class.
     @param changed

Modified: code/branches/presentation2012merge/src/modules/pickup/PickupCollection.h
===================================================================
--- code/branches/presentation2012merge/src/modules/pickup/PickupCollection.h	2012-06-10 17:58:42 UTC (rev 9289)
+++ code/branches/presentation2012merge/src/modules/pickup/PickupCollection.h	2012-06-10 21:01:40 UTC (rev 9290)
@@ -88,14 +88,16 @@
 
             bool addPickupable(CollectiblePickup* pickup); //!< Add the input Pickupable to list of Pickupables combined by this PickupCollection.
             const Pickupable* getPickupable(unsigned int index) const; //!< Get the Pickupable at the given index.
+            bool removePickupable(CollectiblePickup* pickup); //!< Removes the input Pickupable from the list of Pickupables in this PickupCollection.
 
+            inline const std::list<CollectiblePickup*>& getPickups() const
+                { return this->pickups_; }
+
             void pickupChangedUsed(bool changed); //!< Informs the PickupCollection, that one of its pickups has changed its used status to the input value.
             void pickupChangedPickedUp(bool changed); //!< Informs the PickupCollection, that one of its pickups has changed its picked up status to the input value.
             void pickupDisabled(void); //!< Informs the PickupCollection, that one of its pickups has been disabled.
 
         protected:
-            void initializeIdentifier(void); //!< Initializes the PickupIdentifier for this pickup.
-
             virtual bool createSpawner(void); //!< Facilitates the creation of a PickupSpawner upon dropping of the Pickupable.
 
             PickupCollectionIdentifier* pickupCollectionIdentifier_; //!< The PickupCollectionIdentifier of this PickupCollection. Is used to distinguish different PickupCollections amongst themselves.
@@ -104,7 +106,7 @@
             void changedUsedAction(void); //!< Helper method.
             void changedPickedUpAction(void); //!< Helper method.
 
-            std::vector<CollectiblePickup*> pickups_; //!< The list of the pointers of all the Pickupables this PickupCollection consists of. They are weak pointers to facilitate testing, whether the pointers are still valid.
+            std::list<CollectiblePickup*> pickups_; //!< The list of the pointers of all the Pickupables this PickupCollection consists of. They are weak pointers to facilitate testing, whether the pointers are still valid.
 
             unsigned int usedCounter_; //!< Keeps track of the number of pickups of this PickupCollection, that are in use.
             unsigned int pickedUpCounter_; //!< Keeps track of the number of pickups of this PickupCollection, that are picked up.

Modified: code/branches/presentation2012merge/src/modules/pickup/PickupCollectionIdentifier.cc
===================================================================
--- code/branches/presentation2012merge/src/modules/pickup/PickupCollectionIdentifier.cc	2012-06-10 17:58:42 UTC (rev 9289)
+++ code/branches/presentation2012merge/src/modules/pickup/PickupCollectionIdentifier.cc	2012-06-10 21:01:40 UTC (rev 9290)
@@ -34,6 +34,7 @@
 #include "core/CoreIncludes.h"
 
 #include "PickupCollectionIdentifier.h"
+#include "PickupCollection.h"
 
 namespace orxonox
 {
@@ -42,9 +43,11 @@
     @brief
         Constructor. Registers the object.
     */
-    PickupCollectionIdentifier::PickupCollectionIdentifier(Pickupable* pickup) : PickupIdentifier(pickup)
+    PickupCollectionIdentifier::PickupCollectionIdentifier(PickupCollection* collection) : PickupIdentifier(collection)
     {
         RegisterObject(PickupCollectionIdentifier);
+
+        this->collection_ = collection;
     }
 
     /**
@@ -79,17 +82,20 @@
         }
 
         // If the number of Pickupables each of the two PickupCollectionIdentifiers contain differ, the one with less is considered smaller.
-        if(this->identifiers_.size() != collectionIdentifier->identifiers_.size())
-            return this->identifiers_.size()-collectionIdentifier->identifiers_.size();
+        if(this->collection_->getPickups().size() != collectionIdentifier->collection_->getPickups().size())
+            return this->collection_->getPickups().size()-collectionIdentifier->collection_->getPickups().size();
 
         // Compare the Pickupables of the two PickupCollectionIdentifiers one after the other. the one with the first 'smaller' one is considered smaller.
-        std::set<const PickupIdentifier*, PickupIdentifierCompare>::const_iterator it2 = collectionIdentifier->identifiers_.begin();
-        for(std::set<const PickupIdentifier*, PickupIdentifierCompare>::const_iterator it = this->identifiers_.begin(); it != this->identifiers_.end(); it++)
+        std::list<CollectiblePickup*>::const_iterator it1 = this->collection_->getPickups().begin();
+        std::list<CollectiblePickup*>::const_iterator it2 = collectionIdentifier->collection_->getPickups().begin();
+        for( ; it1 != this->collection_->getPickups().end(); ++it1, ++it2)
         {
+            const PickupIdentifier* id1 = (*it1)->getPickupIdentifier();
+            const PickupIdentifier* id2 = (*it2)->getPickupIdentifier();
 
-            if((*it)->compare(*it2) < 0)
+            if(id1->compare(id2) < 0)
                 return -1;
-            if((*it2)->compare(*it) < 0)
+            if(id2->compare(id1) < 0)
                 return 1;
         }
 
@@ -97,16 +103,5 @@
         return 0;
     }
 
-    /**
-    @brief
-        Add a Pickupable to the PickupCollectionIdentifier.
-    @param identifier
-        A pointer to the PickupIdentifier of the Pickupable to be added.
-    */
-    void PickupCollectionIdentifier::addPickup(const PickupIdentifier* identifier)
-    {
-        this->identifiers_.insert(identifier);
-    }
-
 }
 

Modified: code/branches/presentation2012merge/src/modules/pickup/PickupCollectionIdentifier.h
===================================================================
--- code/branches/presentation2012merge/src/modules/pickup/PickupCollectionIdentifier.h	2012-06-10 17:58:42 UTC (rev 9289)
+++ code/branches/presentation2012merge/src/modules/pickup/PickupCollectionIdentifier.h	2012-06-10 21:01:40 UTC (rev 9290)
@@ -60,15 +60,13 @@
     {
 
         public:
-            PickupCollectionIdentifier(Pickupable* pickup); //!< Constructor.
+            PickupCollectionIdentifier(PickupCollection* pickup); //!< Constructor.
             ~PickupCollectionIdentifier(); //!< Destructor.
 
             virtual int compare(const PickupIdentifier* identifier) const; //!< Compares a PickupCollectionIdentifier with a PickupIdentifier.
 
-            void addPickup(const PickupIdentifier* identifier); //!< Add a @ref orxonox::Pickupable "Pickupable" to the PickupCollectionIdentifier.
-
         private:
-            std::set<const PickupIdentifier*, PickupIdentifierCompare> identifiers_; //!< The set of PickupIdentifiers of the @ref orxonox::Pickupable "Pickupables", the @ref orxonox::PickupCollection "PickupCollection" with this PickupCollectionIdentifier consists of, ordered by the rule set by @ref orxonox::PickupIdentifierCompare "PickupIdentifierCompare".
+            PickupCollection* collection_;
 
     };
 




More information about the Orxonox-commit mailing list