Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/pickup/PickupIdentifier.cc @ 7361

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

Merged presentation3 branch into trunk.

  • Property svn:eol-style set to native
File size: 5.4 KB
RevLine 
[6474]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
[6540]29/**
30    @file PickupIdentifier.cc
31    @brief Implementation of the PickupIdentifier class.
32*/
33
[6474]34#include "PickupIdentifier.h"
35
36#include "core/CoreIncludes.h"
[6480]37#include "interfaces/Pickupable.h"
[6474]38
39namespace orxonox
40{
[7163]41
[6475]42    /**
43    @brief
44        Constructor. Registers the object and initializes member variables.
45    */
[6480]46    PickupIdentifier::PickupIdentifier(Pickupable* pickup)
[6474]47    {
48        RegisterRootObject(PickupIdentifier);
[7163]49
[6725]50        if(pickup == NULL)
51            COUT(1) << "Error, PickupIdentifier was created without a valid Pickupable." << std::endl;
[7163]52
[6480]53        this->pickup_ = pickup;
[6474]54    }
[7163]55
[6474]56    PickupIdentifier::~PickupIdentifier()
57    {
[6725]58
[6474]59    }
[7163]60
[6474]61    /**
62    @brief
63        Compares two PickupIdentifiers and returns 0 if a == b, <0 if a < b and >0 if a > b for a.compare(b).
64    @param identifier
65        Pointer to the second PickupIdentifier, b.
66    @return
[6475]67        Returns an integer. 0 if the two compared PickupIdentifiers are the same, <0 if a < b and >0 if a > b.
[6474]68    */
[6475]69    int PickupIdentifier::compare(const PickupIdentifier* identifier) const
[6474]70    {
[6725]71        if(identifier == NULL)
72        {
73            return 1;
74            COUT(1) << "Error in PickupIdentifier::compare: Input Identifier is NULL." << std::endl;
75        }
[7163]76
[6725]77        if(identifier->pickup_ == NULL && this->pickup_ == NULL)
78        {
79            return 0;
80            COUT(1) << "Error in PickupIdentifier::compare: Pickup stored by Identifier is NULL." << std::endl;
81        }
[7163]82
[6725]83        if(identifier->pickup_ == NULL)
84        {
85            return 1;
86            COUT(1) << "Error in PickupIdentifier::compare: Pickup stored by Identifier is NULL." << std::endl;
87        }
[7163]88
[6725]89        if(this->pickup_ == NULL)
90        {
91            return -1;
92            COUT(1) << "Error in PickupIdentifier::compare: Pickup stored by Identifier is NULL." << std::endl;
93        }
[7163]94
[6475]95        //! 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.
[6480]96        if(!identifier->pickup_->getIdentifier()->isExactlyA(this->pickup_->getIdentifier()))
97            return this->pickup_->getIdentifier()->getName().compare(identifier->pickup_->getIdentifier()->getName());
[7163]98
[6475]99        //! If the class is the same for both PickupIdentifiers we go on to check the parameters of the class.
100        //! If the two have a different number of parameters then obviusly something is very wrong.
101        if(!(this->parameters_.size() == identifier->parameters_.size()))
[6474]102        {
103            COUT(1) << "Something went wrong in PickupIdentifier!" << std::endl;
[6475]104            return this->parameters_.size()-identifier->parameters_.size();
[6474]105        }
[7163]106
107        //! We iterate through all parameters and compare 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.
[6474]108        for(std::map<std::string, std::string>::const_iterator it = this->parameters_.begin(); it != this->parameters_.end(); it++)
109        {
[6475]110            //!< If a parameter present in one of the identifiers is not found in the other, once again, something is very wrong.
111            if(identifier->parameters_.find(it->first) == identifier->parameters_.end())
[6474]112            {
113                COUT(1) << "Something went wrong in PickupIdentifier!" << std::endl;
114                return -1;
115            }
[6475]116            if(identifier->parameters_.find(it->first)->second != it->second)
117                return it->second.compare(identifier->parameters_.find(it->first)->second);
[6474]118        }
[7163]119
[6725]120        return 0;
[6474]121    }
[7163]122
[6475]123    /**
124    @brief
125        Add a parameter to the PickupIdentifier.
126    @param name
127        The name of the parameter.
128    @param value
129        The value of the parameter.
130    @return
131        Returns false if the parameter already existed, true if not.
132    */
[6474]133    bool PickupIdentifier::addParameter(std::string & name, std::string & value)
134    {
[6484]135        COUT(4) << "PickupIdentifier " << name << ", " << value << std::endl;
[7163]136
[6474]137        if(!(this->parameters_.find(name) == this->parameters_.end()))
138        {
[6499]139            COUT(4) << "Request for adding a parameter that already exists for the PickupIdentififer was denied. name: '" << name << "', value: '" << value << "'."<<  std::endl;
[6474]140            return false;
141        }
[7163]142
[6474]143        this->parameters_[name] = value;
[7163]144
[6474]145        return true;
146    }
[7163]147
[6474]148}
Note: See TracBrowser for help on using the repository browser.