Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 9, 2008, 4:44:36 PM (16 years ago)
Author:
landauf
Message:

merged core branch to trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/orxonox/core/Identifier.cc

    r790 r871  
    2626 */
    2727
    28 /*!
     28/**
    2929    @file Identifier.cc
    3030    @brief Implementation of the Identifier class.
    3131*/
     32
     33#include <ostream>
    3234
    3335#include "Identifier.h"
     
    4951        this->factory_ = 0;
    5052
    51         this->children_ = new IdentifierList;
     53        this->children_ = new std::list<const Identifier*>();
     54        this->directChildren_ = new std::list<const Identifier*>();
    5255
    5356        // Use a static variable because the classID gets created before main() and that's why we should avoid static member variables
     
    5760
    5861    /**
    59         @brief Destructor: Deletes the IdentifierList containing the children.
     62        @brief Destructor: Deletes the list containing the children.
    6063    */
    6164    Identifier::~Identifier()
    6265    {
    6366        delete this->children_;
    64     }
    65 
    66     /**
    67         @brief Initializes the Identifier with an IdentifierList containing all parents of the class the Identifier belongs to.
    68         @param parents The IdentifierList containing all parents
    69     */
    70     void Identifier::initialize(const IdentifierList* parents)
    71     {
    72         COUT(4) << "*** Initialize " << this->name_ << "-Singleton." << std::endl;
     67        delete this->directChildren_;
     68    }
     69
     70    /**
     71        @brief Initializes the Identifier with a list containing all parents of the class the Identifier belongs to.
     72        @param parents A list containing all parents
     73    */
     74    void Identifier::initialize(std::list<const Identifier*>* parents)
     75    {
     76        COUT(4) << "*** Identifier: Initialize " << this->name_ << "-Singleton." << std::endl;
    7377        this->bCreatedOneObject_ = true;
    7478
    7579        if (parents)
    7680        {
    77             IdentifierListElement* temp1 = parents->first_;
    78             while (temp1)
     81            this->parents_ = (*parents);
     82            this->directParents_ = (*parents);
     83
     84            // Iterate through all parents
     85            for (std::list<const Identifier*>::iterator it = parents->begin(); it != parents->end(); ++it)
    7986            {
    80                 this->parents_.add(temp1->identifier_);
    81                 temp1->identifier_->getChildren().add(this); // We're a child of our parents
    82 
    83                 temp1 = temp1->next_;
     87                // Tell the parent we're one of it's children
     88                (*it)->getChildrenIntern().insert((*it)->getChildrenIntern().end(), this);
     89
     90                // Erase all parents of our parent from our direct-parent-list
     91                for (std::list<const Identifier*>::const_iterator it1 = (*it)->getParents().begin(); it1 != (*it)->getParents().end(); ++it1)
     92                {
     93                    // Search for the parent's parent in our direct-parent-list
     94                    for (std::list<const Identifier*>::iterator it2 = this->directParents_.begin(); it2 != this->directParents_.end(); ++it2)
     95                    {
     96                        if ((*it1) == (*it2))
     97                        {
     98                            // We've found a non-direct parent in our list: Erase it
     99                            this->directParents_.erase(it2);
     100                            break;
     101                        }
     102                    }
     103                }
     104            }
     105
     106            // Now iterate through all direct parents
     107            for (std::list<const Identifier*>::iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it)
     108            {
     109                // Tell the parent we're one of it's direct children
     110                (*it)->getDirectChildrenIntern().insert((*it)->getDirectChildrenIntern().end(), this);
    84111            }
    85112        }
     
    98125        else
    99126        {
    100             // Abstract classes don't have a factory and therefore can't create new objects
    101             COUT(1) << "Error: Cannot create an object of type '" << this->name_ << "'. Class is abstract." << std::endl;
     127            COUT(1) << "An error occurred in Identifier.cc:" << std::endl;
     128            COUT(1) << "Error: Cannot fabricate an object of type '" << this->name_ << "'. Class has no factory." << std::endl;
    102129            COUT(1) << "Aborting..." << std::endl;
    103130            abort();
     
    117144
    118145    /**
    119         @returns a reference to the Identifier map, containing all Identifiers.
    120     */
    121     std::map<std::string, Identifier*>& Identifier::getIdentifierMap()
    122     {
    123         static std::map<std::string, Identifier*> identifierMapStaticReference = std::map<std::string, Identifier*>();
    124         return identifierMapStaticReference;
    125     }
    126 
    127     /**
    128         @returns true, if the Identifier is at least of the given type.
     146        @brief Returns true, if the Identifier is at least of the given type.
    129147        @param identifier The identifier to compare with
    130148    */
    131149    bool Identifier::isA(const Identifier* identifier) const
    132150    {
    133         return (identifier == this || this->parents_.isInList(identifier));
    134     }
    135 
    136     /**
    137         @returns true, if the Identifier is exactly of the given type.
    138         @param identifier The identifier to compare with
    139     */
    140     bool Identifier::isDirectlyA(const Identifier* identifier) const
     151        return (identifier == this || this->identifierIsInList(identifier, this->parents_));
     152    }
     153
     154    /**
     155        @brief Returns true, if the Identifier is exactly of the given type.
     156        @param identifier The identifier to compare with
     157    */
     158    bool Identifier::isExactlyA(const Identifier* identifier) const
    141159    {
    142160        return (identifier == this);
     
    144162
    145163    /**
    146         @returns true, if the assigned identifier is a child of the given identifier.
     164        @brief Returns true, if the assigned identifier is a child of the given identifier.
    147165        @param identifier The identifier to compare with
    148166    */
    149167    bool Identifier::isChildOf(const Identifier* identifier) const
    150168    {
    151         return this->parents_.isInList(identifier);
    152     }
    153 
    154     /**
    155         @returns true, if the assigned identifier is a parent of the given identifier.
     169        return this->identifierIsInList(identifier, this->parents_);
     170    }
     171
     172    /**
     173        @brief Returns true, if the assigned identifier is a direct child of the given identifier.
     174        @param identifier The identifier to compare with
     175    */
     176    bool Identifier::isDirectChildOf(const Identifier* identifier) const
     177    {
     178        return this->identifierIsInList(identifier, this->directParents_);
     179    }
     180
     181    /**
     182        @brief Returns true, if the assigned identifier is a parent of the given identifier.
    156183        @param identifier The identifier to compare with
    157184    */
    158185    bool Identifier::isParentOf(const Identifier* identifier) const
    159186    {
    160         return this->children_->isInList(identifier);
     187        return this->identifierIsInList(identifier, *this->children_);
     188    }
     189
     190    /**
     191        @brief Returns true, if the assigned identifier is a direct parent of the given identifier.
     192        @param identifier The identifier to compare with
     193    */
     194    bool Identifier::isDirectParentOf(const Identifier* identifier) const
     195    {
     196        return this->identifierIsInList(identifier, *this->directChildren_);
     197    }
     198
     199    /**
     200        @brief Returns the ConfigValueContainer of a variable, given by the string of its name.
     201        @param varname The name of the variable
     202        @return The ConfigValueContainer
     203    */
     204    ConfigValueContainer* Identifier::getConfigValueContainer(const std::string& varname)
     205    {
     206        std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_.find(varname);
     207        if (it != configValues_.end())
     208            return ((*it).second);
     209        else
     210            return 0;
     211    }
     212
     213    /**
     214        @brief Adds the ConfigValueContainer of a variable, given by the string of its name.
     215        @param varname The name of the variablee
     216        @param container The container
     217    */
     218    void Identifier::addConfigValueContainer(const std::string& varname, ConfigValueContainer* container)
     219    {
     220        this->configValues_[varname] = container;
     221    }
     222
     223    /**
     224        @brief Searches for a given identifier in a list and returns whether the identifier is in the list or not.
     225        @param identifier The identifier to look for
     226        @param list The list
     227        @return True = the identifier is in the list
     228    */
     229    bool Identifier::identifierIsInList(const Identifier* identifier, const std::list<const Identifier*>& list)
     230    {
     231        for (std::list<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it)
     232            if (identifier == (*it))
     233                return true;
     234
     235        return false;
     236    }
     237
     238    std::ostream& operator<<(std::ostream& out, const std::list<const Identifier*>& list)
     239    {
     240        for (std::list<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it)
     241            out << (*it)->getName() << " ";
     242
     243        return out;
    161244    }
    162245}
Note: See TracChangeset for help on using the changeset viewer.