Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 5, 2009, 9:22:22 PM (14 years ago)
Author:
rgrieder
Message:

Synchronised sandbox with current code trunk. There should be a few bug fixes.

Location:
sandbox
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sandbox

  • sandbox/src/libraries/core/Identifier.h

    r5782 r6038  
    2929/**
    3030    @file
    31     @brief Definition of the Identifier, ClassIdentifier and SubclassIdentifier classes, implementation of the ClassIdentifier and SubclassIdentifier classes.
     31    @brief Definition of the Identifier class, definition and implementation of the ClassIdentifier class.
    3232
    3333    The Identifier contains all needed information about the class it belongs to:
     
    4545
    4646    Every Identifier is in fact a ClassIdentifier, but they are derived from Identifier.
    47 
    48     SubclassIdentifier is a separated class, acting like an Identifier, but has a given class.
    49     You can only assign Identifiers of exactly the given class or of a derivative to a SubclassIdentifier.
    5047*/
    5148
     
    8986    class _CoreExport Identifier
    9087    {
    91         template <class T>
    92         friend class SubclassIdentifier;
    93 
    94         friend class Factory;
    95 
    9688        public:
     89            /** @brief Returns the name of the class the Identifier belongs to. @return The name */
     90            inline const std::string& getName() const { return this->name_; }
     91            void setName(const std::string& name);
     92
     93            /** @brief Returns the unique ID of the class */
     94            FORCEINLINE unsigned int getClassID() const { return this->classID_; }
     95
     96            /** @brief Returns the list of all existing objects of this class. @return The list */
     97            inline ObjectListBase* getObjects() const { return this->objects_; }
     98
    9799            /** @brief Sets the Factory. @param factory The factory to assign */
    98             inline void addFactory(BaseFactory* factory) { this->factory_ = factory; }
     100            inline void addFactory(Factory* factory) { this->factory_ = factory; }
     101            /** @brief Returns true if the Identifier has a Factory. */
     102            inline bool hasFactory() const { return (this->factory_ != 0); }
    99103
    100104            BaseObject* fabricate(BaseObject* creator);
     105
    101106            bool isA(const Identifier* identifier) const;
    102107            bool isExactlyA(const Identifier* identifier) const;
     
    106111            bool isDirectParentOf(const Identifier* identifier) const;
    107112
    108             /** @brief Returns the list of all existing objects of this class. @return The list */
    109             inline ObjectListBase* getObjects() const
    110                 { return this->objects_; }
    111 
    112             /** @brief Returns the name of the class the Identifier belongs to. @return The name */
    113             inline const std::string& getName() const { return this->name_; }
    114             void setName(const std::string& name);
    115 
    116             virtual void updateConfigValues(bool updateChildren = true) const = 0;
     113
     114            /////////////////////////////
     115            ////// Class Hierarchy //////
     116            /////////////////////////////
     117            static void createClassHierarchy();
     118
     119            /** @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 */
     120            inline static bool isCreatingHierarchy() { return (hierarchyCreatingCounter_s > 0); }
    117121
    118122            /** @brief Returns the parents of the class the Identifier belongs to. @return The list of all parents */
     
    124128
    125129            /** @brief Returns the children of the class the Identifier belongs to. @return The list of all children */
    126             inline const std::set<const Identifier*>& getChildren() const { return (*this->children_); }
     130            inline const std::set<const Identifier*>& getChildren() const { return this->children_; }
    127131            /** @brief Returns the begin-iterator of the children-list. @return The begin-iterator */
    128             inline std::set<const Identifier*>::const_iterator getChildrenBegin() const { return this->children_->begin(); }
     132            inline std::set<const Identifier*>::const_iterator getChildrenBegin() const { return this->children_.begin(); }
    129133            /** @brief Returns the end-iterator of the children-list. @return The end-iterator */
    130             inline std::set<const Identifier*>::const_iterator getChildrenEnd() const { return this->children_->end(); }
     134            inline std::set<const Identifier*>::const_iterator getChildrenEnd() const { return this->children_.end(); }
    131135
    132136            /** @brief Returns the direct parents of the class the Identifier belongs to. @return The list of all direct parents */
     
    138142
    139143            /** @brief Returns the direct children the class the Identifier belongs to. @return The list of all direct children */
    140             inline const std::set<const Identifier*>& getDirectChildren() const { return (*this->directChildren_); }
     144            inline const std::set<const Identifier*>& getDirectChildren() const { return this->directChildren_; }
    141145            /** @brief Returns the begin-iterator of the direct-children-list. @return The begin-iterator */
    142             inline std::set<const Identifier*>::const_iterator getDirectChildrenBegin() const { return this->directChildren_->begin(); }
     146            inline std::set<const Identifier*>::const_iterator getDirectChildrenBegin() const { return this->directChildren_.begin(); }
    143147            /** @brief Returns the end-iterator of the direct-children-list. @return The end-iterator */
    144             inline std::set<const Identifier*>::const_iterator getDirectChildrenEnd() const { return this->directChildren_->end(); }
    145 
    146 
    147             /** @brief Returns the map that stores all Identifiers. @return The map */
    148             static inline const std::map<std::string, Identifier*>& getIdentifierMap() { return Identifier::getIdentifierMapIntern(); }
    149             /** @brief Returns a const_iterator to the beginning of the map that stores all Identifiers. @return The const_iterator */
    150             static inline std::map<std::string, Identifier*>::const_iterator getIdentifierMapBegin() { return Identifier::getIdentifierMap().begin(); }
    151             /** @brief Returns a const_iterator to the end of the map that stores all Identifiers. @return The const_iterator */
    152             static inline std::map<std::string, Identifier*>::const_iterator getIdentifierMapEnd() { return Identifier::getIdentifierMap().end(); }
     148            inline std::set<const Identifier*>::const_iterator getDirectChildrenEnd() const { return this->directChildren_.end(); }
     149
     150
     151            //////////////////////////
     152            ///// Identifier Map /////
     153            //////////////////////////
     154            static void destroyAllIdentifiers();
     155
     156            static Identifier* getIdentifierByString(const std::string& name);
     157            static Identifier* getIdentifierByLowercaseString(const std::string& name);
     158            static Identifier* getIdentifierByID(uint32_t id);
     159
     160            static void clearNetworkIDs();
     161
     162            /** @brief Returns the map that stores all Identifiers with their names. @return The map */
     163            static inline const std::map<std::string, Identifier*>& getStringIdentifierMap() { return Identifier::getStringIdentifierMapIntern(); }
     164            /** @brief Returns a const_iterator to the beginning of the map that stores all Identifiers with their names. @return The const_iterator */
     165            static inline std::map<std::string, Identifier*>::const_iterator getStringIdentifierMapBegin() { return Identifier::getStringIdentifierMap().begin(); }
     166            /** @brief Returns a const_iterator to the end of the map that stores all Identifiers with their names. @return The const_iterator */
     167            static inline std::map<std::string, Identifier*>::const_iterator getStringIdentifierMapEnd() { return Identifier::getStringIdentifierMap().end(); }
    153168
    154169            /** @brief Returns the map that stores all Identifiers with their names in lowercase. @return The map */
    155             static inline const std::map<std::string, Identifier*>& getLowercaseIdentifierMap() { return Identifier::getLowercaseIdentifierMapIntern(); }
     170            static inline const std::map<std::string, Identifier*>& getLowercaseStringIdentifierMap() { return Identifier::getLowercaseStringIdentifierMapIntern(); }
    156171            /** @brief Returns a const_iterator to the beginning of the map that stores all Identifiers with their names in lowercase. @return The const_iterator */
    157             static inline std::map<std::string, Identifier*>::const_iterator getLowercaseIdentifierMapBegin() { return Identifier::getLowercaseIdentifierMap().begin(); }
     172            static inline std::map<std::string, Identifier*>::const_iterator getLowercaseStringIdentifierMapBegin() { return Identifier::getLowercaseStringIdentifierMap().begin(); }
    158173            /** @brief Returns a const_iterator to the end of the map that stores all Identifiers with their names in lowercase. @return The const_iterator */
    159             static inline std::map<std::string, Identifier*>::const_iterator getLowercaseIdentifierMapEnd() { return Identifier::getLowercaseIdentifierMap().end(); }
    160 
     174            static inline std::map<std::string, Identifier*>::const_iterator getLowercaseStringIdentifierMapEnd() { return Identifier::getLowercaseStringIdentifierMap().end(); }
     175
     176            /** @brief Returns the map that stores all Identifiers with their IDs. @return The map */
     177            static inline const std::map<uint32_t, Identifier*>& getIDIdentifierMap() { return Identifier::getIDIdentifierMapIntern(); }
     178            /** @brief Returns a const_iterator to the beginning of the map that stores all Identifiers with their IDs. @return The const_iterator */
     179            static inline std::map<uint32_t, Identifier*>::const_iterator getIDIdentifierMapBegin() { return Identifier::getIDIdentifierMap().begin(); }
     180            /** @brief Returns a const_iterator to the end of the map that stores all Identifiers with their IDs. @return The const_iterator */
     181            static inline std::map<uint32_t, Identifier*>::const_iterator getIDIdentifierMapEnd() { return Identifier::getIDIdentifierMap().end(); }
     182
     183
     184            /////////////////////////
     185            ///// Config Values /////
     186            /////////////////////////
     187            virtual void updateConfigValues(bool updateChildren = true) const = 0;
     188
     189            /** @brief Returns true if this class has at least one config value. @return True if this class has at least one config value */
     190            inline bool hasConfigValues() const { return this->bHasConfigValues_; }
    161191
    162192            /** @brief Returns the map that stores all config values. @return The const_iterator */
     
    174204            inline std::map<std::string, ConfigValueContainer*>::const_iterator getLowercaseConfigValueMapEnd() const { return this->configValues_LC_.end(); }
    175205
    176 
    177             /** @brief Returns true if this class has at least one config value. @return True if this class has at least one config value */
    178             inline bool hasConfigValues() const { return this->bHasConfigValues_; }
    179 
    180             /** @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 */
    181             inline static bool isCreatingHierarchy() { return (hierarchyCreatingCounter_s > 0); }
    182 
    183             /** @brief Returns the unique ID of the class */
    184             FORCEINLINE unsigned int getClassID() const { return this->classID_; }
    185 
    186206            void addConfigValueContainer(const std::string& varname, ConfigValueContainer* container);
    187207            ConfigValueContainer* getConfigValueContainer(const std::string& varname);
    188208            ConfigValueContainer* getLowercaseConfigValueContainer(const std::string& varname);
    189209
    190             void initializeClassHierarchy(std::set<const Identifier*>* parents, bool bRootClass);
    191 
    192             static void destroyAllIdentifiers();
    193210
    194211        protected:
     
    199216            static Identifier* getIdentifierSingleton(const std::string& name, Identifier* proposal);
    200217
    201             /** @brief Returns the map that stores all Identifiers. @return The map */
    202             static std::map<std::string, Identifier*>& getIdentifierMapIntern();
     218            void initializeClassHierarchy(std::set<const Identifier*>* parents, bool bRootClass);
     219
     220            /** @brief Returns the map that stores all Identifiers with their names. @return The map */
     221            static std::map<std::string, Identifier*>& getStringIdentifierMapIntern();
    203222            /** @brief Returns the map that stores all Identifiers with their names in lowercase. @return The map */
    204             static std::map<std::string, Identifier*>& getLowercaseIdentifierMapIntern();
     223            static std::map<std::string, Identifier*>& getLowercaseStringIdentifierMapIntern();
     224            /** @brief Returns the map that stores all Identifiers with their network IDs. @return The map */
     225            static std::map<uint32_t, Identifier*>& getIDIdentifierMapIntern();
    205226
    206227            /** @brief Returns the children of the class the Identifier belongs to. @return The list of all children */
    207             inline std::set<const Identifier*>& getChildrenIntern() const { return (*this->children_); }
     228            inline std::set<const Identifier*>& getChildrenIntern() const { return this->children_; }
    208229            /** @brief Returns the direct children of the class the Identifier belongs to. @return The list of all direct children */
    209             inline std::set<const Identifier*>& getDirectChildrenIntern() const { return (*this->directChildren_); }
     230            inline std::set<const Identifier*>& getDirectChildrenIntern() const { return this->directChildren_; }
    210231
    211232            ObjectListBase* objects_;                                      //!< The list of all objects of this class
    212233
    213234        private:
    214             /**
    215                 @brief Increases the hierarchyCreatingCounter_s variable, causing all new objects to store their parents.
    216             */
    217             inline static void startCreatingHierarchy()
    218             {
    219                 hierarchyCreatingCounter_s++;
    220                 COUT(4) << "*** Identifier: Increased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << std::endl;
    221             }
    222 
    223             /**
    224                 @brief Decreases the hierarchyCreatingCounter_s variable, causing the objects to stop storing their parents.
    225             */
    226             inline static void stopCreatingHierarchy()
    227             {
    228                 hierarchyCreatingCounter_s--;
    229                 COUT(4) << "*** Identifier: Decreased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << std::endl;
    230             }
     235            /** @brief Increases the hierarchyCreatingCounter_s variable, causing all new objects to store their parents. */
     236            inline static void startCreatingHierarchy() { hierarchyCreatingCounter_s++; }
     237            /** @brief Decreases the hierarchyCreatingCounter_s variable, causing the objects to stop storing their parents. */
     238            inline static void stopCreatingHierarchy()  { hierarchyCreatingCounter_s--; }
    231239
    232240            static std::map<std::string, Identifier*>& getTypeIDIdentifierMap();
     
    235243
    236244            std::set<const Identifier*> parents_;                          //!< The parents of the class the Identifier belongs to
    237             std::set<const Identifier*>* children_;                        //!< The children of the class the Identifier belongs to
     245            mutable std::set<const Identifier*> children_;                 //!< The children of the class the Identifier belongs to
    238246
    239247            std::set<const Identifier*> directParents_;                    //!< The direct parents of the class the Identifier belongs to
    240             std::set<const Identifier*>* directChildren_;                  //!< The direct children of the class the Identifier belongs to
     248            mutable std::set<const Identifier*> directChildren_;           //!< The direct children of the class the Identifier belongs to
    241249
    242250            bool bCreatedOneObject_;                                       //!< True if at least one object of the given type was created (used to determine the need of storing the parents)
    243251            bool bSetName_;                                                //!< True if the name is set
    244252            std::string name_;                                             //!< The name of the class the Identifier belongs to
    245             BaseFactory* factory_;                                         //!< The Factory, able to create new objects of the given class (if available)
     253            Factory* factory_;                                             //!< The Factory, able to create new objects of the given class (if available)
    246254            static int hierarchyCreatingCounter_s;                         //!< Bigger than zero if at least one Identifier stores its parents (its an int instead of a bool to avoid conflicts with multithreading)
    247255            const unsigned int classID_;                                   //!< Uniquely identifies a class (might not be the same as the networkID_)
     
    302310    inline ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier()
    303311    {
    304         // check if the static field has already been filled
    305         if (ClassIdentifier<T>::classIdentifier_s == 0)
     312        // check if the Identifier already exists
     313        if (!ClassIdentifier<T>::classIdentifier_s)
    306314            ClassIdentifier<T>::initialiseIdentifier();
    307315
     
    430438#endif
    431439    }
    432 
    433 
    434     // ###############################
    435     // ###   SubclassIdentifier    ###
    436     // ###############################
    437     //! The SubclassIdentifier acts almost like an Identifier, but has some prerequisites.
    438     /**
    439         You can only assign an Identifier that belongs to a class T (or derived) to a SubclassIdentifier<T>.
    440         If you assign something else, the program aborts.
    441         Because we know the minimum type, a dynamic_cast is done, which makes it easier to create a new object.
    442     */
    443     template <class T>
    444     class SubclassIdentifier
    445     {
    446         public:
    447             /**
    448                 @brief Constructor: Automaticaly assigns the Identifier of the given class.
    449             */
    450             SubclassIdentifier()
    451             {
    452                 this->identifier_ = ClassIdentifier<T>::getIdentifier();
    453             }
    454 
    455             /**
    456                 @brief Copyconstructor: Assigns the given Identifier.
    457                 @param identifier The Identifier
    458             */
    459             SubclassIdentifier(Identifier* identifier)
    460             {
    461                 this->operator=(identifier);
    462             }
    463 
    464             /**
    465                 @brief Overloading of the = operator: assigns the identifier and checks its type.
    466                 @param identifier The Identifier to assign
    467                 @return The SubclassIdentifier itself
    468             */
    469             SubclassIdentifier<T>& operator=(Identifier* identifier)
    470             {
    471                 if (!identifier || !identifier->isA(ClassIdentifier<T>::getIdentifier()))
    472                 {
    473                     COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl;
    474                     if (identifier)
    475                     {
    476                         COUT(1) << "Error: Class " << identifier->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << "!" << std::endl;
    477                         COUT(1) << "Error: SubclassIdentifier<" << ClassIdentifier<T>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden." << std::endl;
    478                     }
    479                     else
    480                     {
    481                         COUT(1) << "Error: Can't assign NULL identifier" << std::endl;
    482                     }
    483                 }
    484                 else
    485                 {
    486                     this->identifier_ = identifier;
    487                 }
    488                 return *this;
    489             }
    490 
    491             /**
    492                 @brief Overloading of the * operator: returns the assigned identifier.
    493             */
    494             inline Identifier* operator*() const
    495             {
    496                 return this->identifier_;
    497             }
    498 
    499             /**
    500                 @brief Overloading of the -> operator: returns the assigned identifier.
    501             */
    502             inline Identifier* operator->() const
    503             {
    504                 return this->identifier_;
    505             }
    506 
    507             /**
    508                 @brief Returns the assigned identifier. This allows you to assign a SubclassIdentifier to a normal Identifier*.
    509             */
    510             inline operator Identifier*() const
    511             {
    512                 return this->identifier_;
    513             }
    514 
    515             /**
    516                 @brief Creates a new object of the type of the assigned Identifier and dynamic_casts it to the minimal type given by T.
    517                 @return The new object
    518             */
    519             T* fabricate(BaseObject* creator) const
    520             {
    521                 BaseObject* newObject = this->identifier_->fabricate(creator);
    522 
    523                 // Check if the creation was successful
    524                 if (newObject)
    525                 {
    526                     return orxonox_cast<T*>(newObject);
    527                 }
    528                 else
    529                 {
    530                     // Something went terribly wrong
    531                     if (this->identifier_)
    532                     {
    533                         COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl;
    534                         COUT(1) << "Error: Class " << this->identifier_->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << "!" << std::endl;
    535                         COUT(1) << "Error: Couldn't fabricate a new Object." << std::endl;
    536                         COUT(1) << "Aborting..." << std::endl;
    537                     }
    538                     else
    539                     {
    540                         COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl;
    541                         COUT(1) << "Error: Couldn't fabricate a new Object - Identifier is undefined." << std::endl;
    542                         COUT(1) << "Aborting..." << std::endl;
    543                     }
    544 
    545                     assert(false);
    546                     return 0;
    547                 }
    548             }
    549 
    550             /** @brief Returns the assigned identifier. @return The identifier */
    551             inline Identifier* getIdentifier() const
    552                 { return this->identifier_; }
    553 
    554         private:
    555             Identifier* identifier_;            //!< The assigned identifier
    556     };
    557440}
    558441
Note: See TracChangeset for help on using the changeset viewer.