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.h

    r790 r871  
    2626 */
    2727
    28 /*!
     28/**
    2929    @file Identifier.h
    3030    @brief Definition of the Identifier, ClassIdentifier and SubclassIdentifier classes, implementation of the ClassIdentifier and SubclassIdentifier classes.
     
    3939
    4040    Every object has a pointer to the Identifier of its class. This allows the use isA(...),
    41     isDirectlyA(...), isChildOf(...) and isParentOf(...).
     41    isExactlyA(...), isChildOf(...) and isParentOf(...).
    4242
    4343    To create the class-hierarchy, the Identifier has some intern functions and variables.
     
    5252#define _Identifier_H__
    5353
     54#include <list>
    5455#include <map>
    5556#include <string>
     
    5960
    6061#include "ObjectList.h"
    61 #include "IdentifierList.h"
    6262#include "Debug.h"
    6363#include "Iterator.h"
     
    8282
    8383        Every object has a pointer to the Identifier of its class. This allows the use isA(...),
    84         isDirectlyA(...), isChildOf(...) and isParentOf(...).
     84        isExactlyA(...), isChildOf(...) and isParentOf(...).
    8585
    8686        You can't directly create an Identifier, it's just the base-class for ClassIdentifier.
     
    8989    {
    9090        template <class T>
    91         friend class ClassIdentifier; // Forward declaration
     91        friend class ClassIdentifier;
    9292
    9393        template <class T>
    94         friend class SubclassIdentifier; // Forward declaration
    95 
    96         friend class Factory; // Forward declaration
     94        friend class SubclassIdentifier;
     95
     96        friend class Factory;
    9797
    9898        public:
     
    102102            BaseObject* fabricate();
    103103            bool isA(const Identifier* identifier) const;
    104             bool isDirectlyA(const Identifier* identifier) const;
     104            bool isExactlyA(const Identifier* identifier) const;
    105105            bool isChildOf(const Identifier* identifier) const;
     106            bool isDirectChildOf(const Identifier* identifier) const;
    106107            bool isParentOf(const Identifier* identifier) const;
    107 
    108             static std::map<std::string, Identifier*>& getIdentifierMap();
     108            bool isDirectParentOf(const Identifier* identifier) const;
    109109
    110110            /** @brief Removes all objects of the corresponding class. */
    111111            virtual void removeObjects() const = 0;
    112112
    113             /** @returns the name of the class the Identifier belongs to. */
     113            /** @brief Returns the name of the class the Identifier belongs to. @return The name */
    114114            inline const std::string& getName() const { return this->name_; }
    115115
    116             /** @returns the parents of the class the Identifier belongs to. */
    117             inline const IdentifierList& getParents() const { return this->parents_; }
    118 
    119             /** @returns the children of the class the Identifier belongs to. */
    120             inline IdentifierList& getChildren() const { return *this->children_; }
    121 
    122             /** @returns true, if a branch of the class-hierarchy is being created, causing all new objects to store their parents. */
     116            /** @brief Returns the parents of the class the Identifier belongs to. @return The list of all parents */
     117            inline const std::list<const Identifier*>& getParents() const { return this->parents_; }
     118            /** @brief Returns the begin-iterator of the parents-list. @return The begin-iterator */
     119            inline std::list<const Identifier*>::const_iterator getParentsBegin() const { return this->parents_.begin(); }
     120            /** @brief Returns the end-iterator of the parents-list. @return The end-iterator */
     121            inline std::list<const Identifier*>::const_iterator getParentsEnd() const { return this->parents_.end(); }
     122
     123            /** @brief Returns the children of the class the Identifier belongs to. @return The list of all children */
     124            inline const std::list<const Identifier*>& getChildren() const { return (*this->children_); }
     125            /** @brief Returns the begin-iterator of the children-list. @return The begin-iterator */
     126            inline std::list<const Identifier*>::const_iterator getChildrenBegin() const { return this->children_->begin(); }
     127            /** @brief Returns the end-iterator of the children-list. @return The end-iterator */
     128            inline std::list<const Identifier*>::const_iterator getChildrenEnd() const { return this->children_->end(); }
     129
     130            /** @brief Returns the direct parents of the class the Identifier belongs to. @return The list of all direct parents */
     131            inline const std::list<const Identifier*>& getDirectParents() const { return this->directParents_; }
     132            /** @brief Returns the begin-iterator of the direct-parents-list. @return The begin-iterator */
     133            inline std::list<const Identifier*>::const_iterator getDirectParentsBegin() const { return this->directParents_.begin(); }
     134            /** @brief Returns the end-iterator of the direct-parents-list. @return The end-iterator */
     135            inline std::list<const Identifier*>::const_iterator getDirectParentsEnd() const { return this->directParents_.end(); }
     136
     137            /** @brief Returns the direct children the class the Identifier belongs to. @return The list of all direct children */
     138            inline const std::list<const Identifier*>& getDirectChildren() const { return (*this->directChildren_); }
     139            /** @brief Returns the begin-iterator of the direct-children-list. @return The begin-iterator */
     140            inline std::list<const Identifier*>::const_iterator getDirectChildrenBegin() const { return this->directChildren_->begin(); }
     141            /** @brief Returns the end-iterator of the direct-children-list. @return The end-iterator */
     142            inline std::list<const Identifier*>::const_iterator getDirectChildrenEnd() const { return this->directChildren_->end(); }
     143
     144            /** @brief Returns true, if a branch of the class-hierarchy is being created, causing all new objects to store their parents. @return The status of the class-hierarchy creation */
    123145            inline static bool isCreatingHierarchy() { return (hierarchyCreatingCounter_s > 0); }
    124146
    125             /** @returns the network ID to identify a class through the network. */
     147            /** @brief Returns the network ID to identify a class through the network. @return the network ID */
    126148            inline const unsigned int getNetworkID() const { return this->classID_; }
    127149
     
    129151            void setNetworkID(unsigned int id);
    130152
    131             /** @returns the ConfigValueContainer of a variable, given by the string of its name. @param varname The name of the variable */
    132             inline ConfigValueContainer* getConfigValueContainer(const std::string& varname)
    133                 { return this->configValues_[varname]; }
    134 
    135             /** @brief Sets the ConfigValueContainer of a variable, given by the string of its name. @param varname The name of the variablee @param container The container */
    136             inline void setConfigValueContainer(const std::string& varname, ConfigValueContainer* container)
    137                 { this->configValues_[varname] = container; }
     153            ConfigValueContainer* getConfigValueContainer(const std::string& varname);
     154            void addConfigValueContainer(const std::string& varname, ConfigValueContainer* container);
     155
     156            virtual XMLPortParamContainer* getXMLPortParamContainer(const std::string& paramname) = 0;
     157            virtual void addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container) = 0;
     158
     159            virtual XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname) = 0;
     160            virtual void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container) = 0;
     161
     162            static bool identifierIsInList(const Identifier* identifier, const std::list<const Identifier*>& list);
    138163
    139164        private:
     
    141166            Identifier(const Identifier& identifier) {} // don't copy
    142167            virtual ~Identifier();
    143             void initialize(const IdentifierList* parents);
     168            void initialize(std::list<const Identifier*>* parents);
     169
     170            /** @brief Returns the children of the class the Identifier belongs to. @return The list of all children */
     171            inline std::list<const Identifier*>& getChildrenIntern() const { return (*this->children_); }
     172            /** @brief Returns the direct children of the class the Identifier belongs to. @return The list of all direct children */
     173            inline std::list<const Identifier*>& getDirectChildrenIntern() const { return (*this->directChildren_); }
    144174
    145175            /**
     
    149179            {
    150180                hierarchyCreatingCounter_s++;
    151                 COUT(4) << "*** Increased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << std::endl;
     181                COUT(4) << "*** Identifier: Increased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << std::endl;
    152182            }
    153183
     
    158188            {
    159189                hierarchyCreatingCounter_s--;
    160                 COUT(4) << "*** Decreased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << std::endl;
    161             }
    162 
    163             IdentifierList parents_;                                    //!< The Parents of the class the Identifier belongs to
    164             IdentifierList* children_;                                  //!< The Children of the class the Identifier belongs to
     190                COUT(4) << "*** Identifier: Decreased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << std::endl;
     191            }
     192
     193            std::list<const Identifier*> parents_;                      //!< The parents of the class the Identifier belongs to
     194            std::list<const Identifier*>* children_;                    //!< The children of the class the Identifier belongs to
     195
     196            std::list<const Identifier*> directParents_;                //!< The direct parents of the class the Identifier belongs to
     197            std::list<const Identifier*>* directChildren_;              //!< The direct children of the class the Identifier belongs to
    165198
    166199            std::string name_;                                          //!< The name of the class the Identifier belongs to
     
    173206    };
    174207
     208    std::ostream& operator<<(std::ostream& out, const std::list<const Identifier*>& list);
     209
    175210
    176211    // ###############################
     
    182217        This makes it possible to store informations about a class, sharing them with all
    183218        objects of that class without defining static variables in every class.
     219
     220        To be really sure that not more than exactly one object exists (even with libraries),
     221        ClassIdentifiers are only created by IdentifierDistributor.
     222
     223        You can access the ClassIdentifiers created by IdentifierDistributor through the
     224        ClassManager singletons.
    184225    */
    185226    template <class T>
    186227    class ClassIdentifier : public Identifier
    187228    {
     229        template <class TT>
     230        friend class ClassManager;
     231
    188232        public:
    189             static ClassIdentifier<T>* registerClass(const IdentifierList* parents, const std::string& name, bool bRootClass);
    190             static void addObject(T* object);
    191             static ClassIdentifier<T>* getIdentifier();
     233            ClassIdentifier<T>* registerClass(std::list<const Identifier*>* parents, const std::string& name, bool bRootClass);
     234            void addObject(T* object);
    192235            void removeObjects() const;
    193236            void setName(const std::string& name);
     237            inline const ObjectList<T>* getObjects() const { return this->objects_; }
     238
     239            XMLPortParamContainer* getXMLPortParamContainer(const std::string& paramname);
     240            void addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container);
     241
     242            XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname);
     243            void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container);
    194244
    195245        private:
     
    200250            ObjectList<T>* objects_;    //!< The ObjectList, containing all objects of type T
    201251            bool bSetName_;             //!< True if the name is set
     252            std::map<std::string, XMLPortClassParamContainer<T>*> xmlportParamContainers_;
     253            std::map<std::string, XMLPortClassObjectContainer<T, class O>*> xmlportObjectContainers_;
    202254    };
    203255
     
    208260    ClassIdentifier<T>::ClassIdentifier()
    209261    {
    210         this->objects_ = ObjectList<T>::getList();
     262//        this->objects_ = ObjectList<T>::getList();
     263        this->objects_ = new ObjectList<T>();
    211264        this->bSetName_ = false;
    212265    }
     
    214267    /**
    215268        @brief Registers a class, which means that the name and the parents get stored.
    216         @param parents An IdentifierList, containing the Identifiers of all parents of the class
     269        @param parents A list, containing the Identifiers of all parents of the class
    217270        @param name A string, containing exactly the name of the class
    218271        @param bRootClass True if the class is either an Interface or the BaseObject itself
     
    220273    */
    221274    template <class T>
    222     ClassIdentifier<T>* ClassIdentifier<T>::registerClass(const IdentifierList* parents, const std::string& name, bool bRootClass)
    223     {
    224         COUT(4) << "*** Register Class in " << name << "-Singleton." << std::endl;
     275    ClassIdentifier<T>* ClassIdentifier<T>::registerClass(std::list<const Identifier*>* parents, const std::string& name, bool bRootClass)
     276    {
     277        COUT(5) << "*** ClassIdentifier: Register Class in " << name << "-Singleton." << std::endl;
    225278
    226279        // Check if at least one object of the given type was created
    227         if (!getIdentifier()->bCreatedOneObject_)
     280        if (!this->bCreatedOneObject_)
    228281        {
    229282            // If no: We have to store the informations and initialize the Identifier
    230             getIdentifier()->setName(name);
    231 
    232             COUT(4) << "*** Register Class in " << name << "-Singleton -> Initialize Singleton." << std::endl;
     283            this->setName(name);
     284
     285            COUT(4) << "*** ClassIdentifier: Register Class in " << name << "-Singleton -> Initialize Singleton." << std::endl;
    233286            if (bRootClass)
    234                 getIdentifier()->initialize(NULL); // If a class is derived from two interfaces, the second interface might think it's derived from the first because of the order of constructor-calls. Thats why we set parents to zero in that case.
     287                this->initialize(NULL); // If a class is derived from two interfaces, the second interface might think it's derived from the first because of the order of constructor-calls. Thats why we set parents to zero in that case.
    235288            else
    236                 getIdentifier()->initialize(parents);
     289                this->initialize(parents);
    237290        }
    238291
    239         return getIdentifier();
    240     }
    241 
    242     /**
    243         @brief Creates the only instance of this class for the template class T.
    244         @return The Identifier itself
    245     */
    246     template <class T>
    247     ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier()
    248     {
    249         static ClassIdentifier<T> theOneAndOnlyInstance = ClassIdentifier<T>();
    250         static bool bIdentifierCreated = false;
    251 
    252         if (!bIdentifierCreated)
    253         {
    254             COUT(4) << "*** Create Identifier Singleton." << std::endl;
    255             bIdentifierCreated = true;
    256         }
    257 
    258         return &theOneAndOnlyInstance;
     292        return this;
    259293    }
    260294
     
    266300    void ClassIdentifier<T>::setName(const std::string& name)
    267301    {
    268         // Make sure we didn't already set the name, to avoid duplicate entries in the Identifier map
    269302        if (!this->bSetName_)
    270303        {
    271304            this->name_ = name;
    272             this->getIdentifierMap().insert(std::pair<std::string, Identifier*>(name, this));
    273305            this->bSetName_ = true;
    274306        }
     
    282314    void ClassIdentifier<T>::addObject(T* object)
    283315    {
    284         COUT(4) << "*** Added object to " << ClassIdentifier<T>::getIdentifier()->getName() << "-list." << std::endl;
    285         object->getMetaList().add(ClassIdentifier<T>::getIdentifier()->objects_, ClassIdentifier<T>::getIdentifier()->objects_->add(object));
     316        COUT(5) << "*** ClassIdentifier: Added object to " << this->getName() << "-list." << std::endl;
     317        object->getMetaList().add(this->objects_, this->objects_->add(object));
    286318    }
    287319
     
    292324    void ClassIdentifier<T>::removeObjects() const
    293325    {
    294         for (Iterator<T> it = ObjectList<T>::start(); it;)
     326        for (Iterator<T> it = this->objects_->start(); it;)
    295327            delete *(it++);
    296328    }
     329
     330    template <class T>
     331    XMLPortParamContainer* ClassIdentifier<T>::getXMLPortParamContainer(const std::string& paramname)
     332    {
     333        typename std::map<std::string, XMLPortClassParamContainer<T>*>::const_iterator it = xmlportParamContainers_.find(paramname);
     334        if (it != xmlportParamContainers_.end())
     335            return (XMLPortParamContainer*)((*it).second);
     336        else
     337            return 0;
     338    }
     339
     340    template <class T>
     341    void ClassIdentifier<T>::addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container)
     342    {
     343        this->xmlportParamContainers_[paramname] = (XMLPortClassParamContainer<T>*)container;
     344    }
     345
     346    template <class T>
     347    XMLPortObjectContainer* ClassIdentifier<T>::getXMLPortObjectContainer(const std::string& sectionname)
     348    {
     349        typename std::map<std::string, XMLPortClassObjectContainer<T, class O>*>::const_iterator it = xmlportObjectContainers_.find(sectionname);
     350        if (it != xmlportObjectContainers_.end())
     351            return (XMLPortObjectContainer*)((*it).second);
     352        else
     353            return 0;
     354    }
     355
     356    template <class T>
     357    void ClassIdentifier<T>::addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container)
     358    {
     359        this->xmlportObjectContainers_[sectionname] = (XMLPortClassObjectContainer<T, class O>*)container;
     360    }
     361
    297362
    298363    // ###############################
     
    314379            SubclassIdentifier()
    315380            {
    316                 this->identifier_ = ClassIdentifier<T>::getIdentifier();
     381                this->identifier_ = ClassManager<T>::getIdentifier();
     382            }
     383
     384            /**
     385                @brief Copyconstructor: Assigns the given Identifier.
     386                @param identifier The Identifier
     387            */
     388            SubclassIdentifier(Identifier* identifier)
     389            {
     390                this->identifier_ = identifier;
    317391            }
    318392
     
    324398            SubclassIdentifier<T>& operator=(Identifier* identifier)
    325399            {
    326                 if (!identifier->isA(ClassIdentifier<T>::getIdentifier()))
     400                if (!identifier->isA(ClassManager<T>::getIdentifier()))
    327401                {
    328                     COUT(1) << "Error: Class " << identifier->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << "!" << std::endl;
    329                     COUT(1) << "Error: SubclassIdentifier<" << ClassIdentifier<T>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden." << std::endl;
     402                    COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl;
     403                    COUT(1) << "Error: Class " << identifier->getName() << " is not a " << ClassManager<T>::getIdentifier()->getName() << "!" << std::endl;
     404                    COUT(1) << "Error: SubclassIdentifier<" << ClassManager<T>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden." << std::endl;
    330405                    COUT(1) << "Aborting..." << std::endl;
    331406                    abort();
     
    372447                    if (this->identifier_)
    373448                    {
    374                         COUT(1) << "Error: Class " << this->identifier_->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << "!" << std::endl;
     449                        COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl;
     450                        COUT(1) << "Error: Class " << this->identifier_->getName() << " is not a " << ClassManager<T>::getIdentifier()->getName() << "!" << std::endl;
    375451                        COUT(1) << "Error: Couldn't fabricate a new Object." << std::endl;
    376452                        COUT(1) << "Aborting..." << std::endl;
     
    378454                    else
    379455                    {
     456                        COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl;
    380457                        COUT(1) << "Error: Couldn't fabricate a new Object - Identifier is undefined." << std::endl;
    381458                        COUT(1) << "Aborting..." << std::endl;
     
    386463            }
    387464
    388             /** @returns the assigned identifier. */
     465            /** @brief Returns the assigned identifier. @return The identifier */
    389466            inline const Identifier* getIdentifier() const
    390467                { return this->identifier_; }
    391468
    392             /** @returns true, if the assigned identifier is at least of the given type. @param identifier The identifier to compare with */
     469            /** @brief Returns true, if the assigned identifier is at least of the given type. @param identifier The identifier to compare with */
    393470            inline bool isA(const Identifier* identifier) const
    394471                { return this->identifier_->isA(identifier); }
    395472
    396             /** @returns true, if the assigned identifier is exactly of the given type. @param identifier The identifier to compare with */
    397             inline bool isDirectlyA(const Identifier* identifier) const
    398                 { return this->identifier_->isDirectlyA(identifier); }
    399 
    400             /** @returns true, if the assigned identifier is a child of the given identifier. @param identifier The identifier to compare with */
     473            /** @brief Returns true, if the assigned identifier is exactly of the given type. @param identifier The identifier to compare with */
     474            inline bool isExactlyA(const Identifier* identifier) const
     475                { return this->identifier_->isExactlyA(identifier); }
     476
     477            /** @brief Returns true, if the assigned identifier is a child of the given identifier. @param identifier The identifier to compare with */
    401478            inline bool isChildOf(const Identifier* identifier) const
    402479                { return this->identifier_->isChildOf(identifier); }
    403480
    404             /** @returns true, if the assigned identifier is a parent of the given identifier. @param identifier The identifier to compare with */
     481            /** @brief Returns true, if the assigned identifier is a direct child of the given identifier. @param identifier The identifier to compare with */
     482            inline bool isDirectChildOf(const Identifier* identifier) const
     483                { return this->identifier_->isDirectChildOf(identifier); }
     484
     485            /** @brief Returns true, if the assigned identifier is a parent of the given identifier. @param identifier The identifier to compare with */
    405486            inline bool isParentOf(const Identifier* identifier) const
    406487                { return this->identifier_->isParentOf(identifier); }
    407488
     489            /** @brief Returns true, if the assigned identifier is a direct parent of the given identifier. @param identifier The identifier to compare with */
     490            inline bool isDirectParentOf(const Identifier* identifier) const
     491                { return this->identifier_->isDirectParentOf(identifier); }
     492
    408493        private:
    409             Identifier* identifier_;        //!< The assigned identifier
     494            Identifier* identifier_;            //!< The assigned identifier
    410495    };
    411496}
Note: See TracChangeset for help on using the changeset viewer.