Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup3/src/orxonox/pickup/PickupIdentifier.cc @ 6475

Last change on this file since 6475 was 6475, checked in by dafrick, 14 years ago

Additional documentation, code niceifying and potential bug fixing. Also: Renamed DroppedItem to DroppedPickup.

File size: 4.6 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Damian 'Mozork' Frick
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "PickupIdentifier.h"
30
31#include "core/CoreIncludes.h"
32
33namespace orxonox
34{
35   
36    /**
37    @brief
38        Constructor. Registers the object and initializes member variables.
39    */
40    PickupIdentifier::PickupIdentifier()
41    {
42        RegisterRootObject(PickupIdentifier);
43       
44        this->classIdentifier_ = NULL;
45    }
46   
47    PickupIdentifier::~PickupIdentifier()
48    {
49       
50    }
51   
52    /**
53    @brief
54        Compares two PickupIdentifiers and returns 0 if a == b, <0 if a < b and >0 if a > b for a.compare(b).
55    @param identifier
56        Pointer to the second PickupIdentifier, b.
57    @return
58        Returns an integer. 0 if the two compared PickupIdentifiers are the same, <0 if a < b and >0 if a > b.
59    */
60    int PickupIdentifier::compare(const PickupIdentifier* identifier) const
61    {
62        //! If the classIdentifiers are not the same (meaning the PickupIdentifiers identify different classes), the obviously the two Pickupables identified by the PickupIdentifiers cannot be the same. An ordering is established through the alphabetical ordering of the respective classnames.
63        if(!identifier->classIdentifier_->isExactlyA(this->classIdentifier_))
64            return this->classIdentifier_->getName().compare(identifier->classIdentifier_->getName());
65       
66        //! If the class is the same for both PickupIdentifiers we go on to check the parameters of the class.
67        //! If the two have a different number of parameters then obviusly something is very wrong.
68        if(!(this->parameters_.size() == identifier->parameters_.size()))
69        {
70            COUT(1) << "Something went wrong in PickupIdentifier!" << std::endl;
71            return this->parameters_.size()-identifier->parameters_.size();
72        }
73       
74        //! We iterate through all parameters and compar their values (which are strings). The first parameter is the most significant. The ordering is once again established by the alphabetical comparison of the two value strings.
75        for(std::map<std::string, std::string>::const_iterator it = this->parameters_.begin(); it != this->parameters_.end(); it++)
76        {
77            //!< If a parameter present in one of the identifiers is not found in the other, once again, something is very wrong.
78            if(identifier->parameters_.find(it->first) == identifier->parameters_.end())
79            {
80                COUT(1) << "Something went wrong in PickupIdentifier!" << std::endl;
81                return -1;
82            }
83            if(identifier->parameters_.find(it->first)->second != it->second)
84                return it->second.compare(identifier->parameters_.find(it->first)->second);
85        }
86           
87        return false;
88    }
89   
90    /**
91    @brief
92        Add the class of the Pickupable to its PickupIdentifier.
93    @param identifier
94        A pointer to the Identifier of the class.
95    */
96    void PickupIdentifier::addClass(Identifier* identifier)
97    {
98        this->classIdentifier_ = identifier;
99    }
100   
101    /**
102    @brief
103        Add a parameter to the PickupIdentifier.
104    @param name
105        The name of the parameter.
106    @param value
107        The value of the parameter.
108    @return
109        Returns false if the parameter already existed, true if not.
110    */
111    bool PickupIdentifier::addParameter(std::string & name, std::string & value)
112    {
113        if(!(this->parameters_.find(name) == this->parameters_.end()))
114        {
115            COUT(2) << "Request for adding a parameter that already exists for the PickupIdentififer was denied." << std::endl;
116            return false;
117        }
118       
119        this->parameters_[name] = value;
120       
121        return true;
122    }
123   
124}
Note: See TracBrowser for help on using the repository browser.