Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2012merge/src/orxonox/pickup/PickupIdentifier.cc @ 9314

Last change on this file since 9314 was 9314, checked in by landauf, 12 years ago

probably a bugfix: PickupIdentifier compared only the first parameter in the map.
also some performance enhancements

  • Property svn:eol-style set to native
File size: 5.1 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/**
30    @file PickupIdentifier.cc
31    @brief Implementation of the PickupIdentifier class.
32*/
33
34#include "PickupIdentifier.h"
35
36#include "core/CoreIncludes.h"
37#include "core/Identifier.h"
38
39#include "interfaces/Pickupable.h"
40
41namespace orxonox
42{
43
44    /**
45    @brief
46        Constructor. Registers the object and initializes member variables.
47    */
48    PickupIdentifier::PickupIdentifier(Pickupable* pickup)
49    {
50        RegisterRootObject(PickupIdentifier);
51
52        if(pickup == NULL)
53            orxout(internal_error, context::pickups) << "PickupIdentifier was created without a valid Pickupable." << endl;
54
55        this->pickup_ = pickup;
56    }
57
58    PickupIdentifier::~PickupIdentifier()
59    {
60
61    }
62
63    /**
64    @brief
65        Compares two PickupIdentifiers and returns 0 if a == b, <0 if a < b and >0 if a > b for a.compare(b).
66    @param identifier
67        Pointer to the second PickupIdentifier, b.
68    @return
69        Returns an integer. 0 if the two compared PickupIdentifiers are the same, <0 if a < b and >0 if a > b.
70    */
71    int PickupIdentifier::compare(const PickupIdentifier* identifier) const
72    {
73        assert(identifier);
74        assert(identifier->pickup_);
75        assert(this->pickup_);
76
77        // If the classIdentifiers are not the same (meaning the PickupIdentifiers identify different classes), then obviously the two Pickupables identified by the PickupIdentifiers cannot be the same. An ordering is established through the alphabetical ordering of the respective classnames.
78        if(identifier->pickup_->getIdentifier() != this->pickup_->getIdentifier())
79            return this->pickup_->getIdentifier()->getName().compare(identifier->pickup_->getIdentifier()->getName());
80
81        // If the class is the same for both PickupIdentifiers we go on to check the parameters of the class.
82        // If the two have a different number of parameters then obviously something is very wrong.
83        if(this->parameters_.size() != identifier->parameters_.size())
84        {
85            orxout(internal_error, context::pickups) << "Two PickupIdentifiers of the same Class have a different number of parameters. " << this->parameters_.size() << " vs. " << identifier->parameters_.size() << ". This indicates a bug in " << this->pickup_->getIdentifier()->getName() << "." << endl;
86            return this->parameters_.size()-identifier->parameters_.size();
87        }
88
89        // We iterate through all parameters and compare their values (which are strings). The ordering is once again established by the alphabetical comparison of the two value strings.
90        std::map<std::string, std::string>::const_iterator it1 = this->parameters_.begin();
91        std::map<std::string, std::string>::const_iterator it2 = identifier->parameters_.begin();
92        for( ; it1 != this->parameters_.end(); ++it1, ++it2)
93        {
94            // If a parameter present in one of the identifiers is not found in the other, once again, something is very wrong.
95            if(it1->first != it2->first)
96            {
97                orxout(internal_error, context::pickups) << this->pickup_->getIdentifier()->getName() <<  " Something went wrong in PickupIdentifier!" << endl;
98                return it1->first.compare(it2->first);
99            }
100
101            int result = it1->second.compare(it2->second);
102            if(result != 0)
103                return result;
104        }
105
106        return 0;
107    }
108
109    /**
110    @brief
111        Add a parameter to the PickupIdentifier.
112    @param name
113        The name of the parameter.
114    @param value
115        The value of the parameter.
116    @return
117        Returns false if the parameter already existed, true if not.
118    */
119    bool PickupIdentifier::addParameter(const std::string& name, const MultiType& value)
120    {
121        orxout(verbose, context::pickups) << "PickupIdentifier " << name << ", " << value << endl;
122
123        if(!(this->parameters_.find(name) == this->parameters_.end()))
124        {
125            orxout(verbose, context::pickups) << "Request for adding a parameter that already exists for the PickupIdentififer was denied. name: '" << name << "', value: '" << value << "'."<<  endl;
126            return false;
127        }
128
129        this->parameters_[name] = value.getString();
130
131        return true;
132    }
133
134}
Note: See TracBrowser for help on using the repository browser.