Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Some documenting and cleaning up/re-organization in pickups module.

  • Property svn:eol-style set to native
File size: 4.7 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"
[7494]37#include "core/Identifier.h"
38
[6480]39#include "interfaces/Pickupable.h"
[6474]40
41namespace orxonox
42{
[7163]43
[6475]44    /**
45    @brief
46        Constructor. Registers the object and initializes member variables.
47    */
[6480]48    PickupIdentifier::PickupIdentifier(Pickupable* pickup)
[6474]49    {
50        RegisterRootObject(PickupIdentifier);
[7163]51
[6725]52        if(pickup == NULL)
53            COUT(1) << "Error, PickupIdentifier was created without a valid Pickupable." << std::endl;
[7163]54
[6480]55        this->pickup_ = pickup;
[6474]56    }
[7163]57
[6474]58    PickupIdentifier::~PickupIdentifier()
59    {
[6725]60
[6474]61    }
[7163]62
[6474]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
[6475]69        Returns an integer. 0 if the two compared PickupIdentifiers are the same, <0 if a < b and >0 if a > b.
[6474]70    */
[6475]71    int PickupIdentifier::compare(const PickupIdentifier* identifier) const
[6474]72    {
[7494]73        assert(identifier);
74        assert(identifier->pickup_);
75        assert(this->pickup_);
[7163]76
[7494]77        // 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]78        if(!identifier->pickup_->getIdentifier()->isExactlyA(this->pickup_->getIdentifier()))
79            return this->pickup_->getIdentifier()->getName().compare(identifier->pickup_->getIdentifier()->getName());
[7163]80
[7494]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.
[6475]83        if(!(this->parameters_.size() == identifier->parameters_.size()))
[6474]84        {
85            COUT(1) << "Something went wrong in PickupIdentifier!" << std::endl;
[6475]86            return this->parameters_.size()-identifier->parameters_.size();
[6474]87        }
[7163]88
[7494]89        // 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]90        for(std::map<std::string, std::string>::const_iterator it = this->parameters_.begin(); it != this->parameters_.end(); it++)
91        {
[7494]92            // If a parameter present in one of the identifiers is not found in the other, once again, something is very wrong.
[6475]93            if(identifier->parameters_.find(it->first) == identifier->parameters_.end())
[6474]94            {
95                COUT(1) << "Something went wrong in PickupIdentifier!" << std::endl;
96                return -1;
97            }
[6475]98            if(identifier->parameters_.find(it->first)->second != it->second)
99                return it->second.compare(identifier->parameters_.find(it->first)->second);
[6474]100        }
[7163]101
[6725]102        return 0;
[6474]103    }
[7163]104
[6475]105    /**
106    @brief
107        Add a parameter to the PickupIdentifier.
108    @param name
109        The name of the parameter.
110    @param value
111        The value of the parameter.
112    @return
113        Returns false if the parameter already existed, true if not.
114    */
[6474]115    bool PickupIdentifier::addParameter(std::string & name, std::string & value)
116    {
[6484]117        COUT(4) << "PickupIdentifier " << name << ", " << value << std::endl;
[7163]118
[6474]119        if(!(this->parameters_.find(name) == this->parameters_.end()))
120        {
[6499]121            COUT(4) << "Request for adding a parameter that already exists for the PickupIdentififer was denied. name: '" << name << "', value: '" << value << "'."<<  std::endl;
[6474]122            return false;
123        }
[7163]124
[6474]125        this->parameters_[name] = value;
[7163]126
[6474]127        return true;
128    }
[7163]129
[6474]130}
Note: See TracBrowser for help on using the repository browser.