Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 26, 2015, 3:10:58 PM (10 years ago)
Author:
landauf
Message:

use typeid(T) instead of typeid(T).name() to identify a class. this avoids ambiguity if two classes in different anonymous namespaces use the same name.

Location:
code/branches/core7/src/libraries/core/class
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core7/src/libraries/core/class/Identifier.cc

    r10396 r10399  
    197197        {
    198198            // only Identifiable is allowed to have no parents (even tough it's currently not abstract)
    199             orxout(internal_error) << "Identifier " << this->getName() << " / " << this->getTypeidName() << " is marked as abstract but has no direct parents defined" << endl;
     199            orxout(internal_error) << "Identifier " << this->getName() << " / " << this->getTypeInfo().name() << " is marked as abstract but has no direct parents defined" << endl;
    200200            orxout(internal_error) << "  If this class is not abstract, use RegisterClass(ThisClass);" << endl;
    201201            orxout(internal_error) << "  If this class is abstract, use RegisterAbstractClass(ThisClass).inheritsFrom(Class(BaseClass));" << endl;
  • code/branches/core7/src/libraries/core/class/Identifier.h

    r10395 r10399  
    117117            inline const std::string& getName() const { return this->name_; }
    118118
    119             /// Returns the name of the class as it is returned by typeid(T).name()
    120             virtual const std::string& getTypeidName() = 0;
     119            /// Returns the type_info of the class as it is returned by typeid(T)
     120            virtual const std::type_info& getTypeInfo() = 0;
    121121
    122122            /// Returns the network ID to identify a class through the network.
     
    265265                ClassIdentifier<T>::classIdentifier_s = this;
    266266
    267                 this->typeidName_ = typeid(T).name();
    268267                SuperFunctionInitialization<0, T>::initialize(this);
    269268            }
     
    283282            virtual void updateConfigValues(bool updateChildren = true) const;
    284283
    285             virtual const std::string& getTypeidName()
    286                 { return this->typeidName_; }
     284            virtual const std::type_info& getTypeInfo()
     285                { return typeid(T); }
    287286
    288287            virtual bool canDynamicCastObjectToIdentifierClass(Identifiable* object) const
     
    297296            void updateConfigValues(bool updateChildren, Identifiable*) const;
    298297
    299             std::string typeidName_;
    300298            static WeakPtr<ClassIdentifier<T> > classIdentifier_s;
    301299    };
     
    312310    {
    313311        if (ClassIdentifier<T>::classIdentifier_s == NULL)
    314             ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*) IdentifierManager::getInstance().getIdentifierByTypeidName(typeid(T).name());
     312            ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*) IdentifierManager::getInstance().getIdentifierByTypeInfo(typeid(T));
    315313
    316314        OrxVerify(ClassIdentifier<T>::classIdentifier_s != NULL, "Assertion failed in ClassIdentifier of type " << typeid(T).name());
  • code/branches/core7/src/libraries/core/class/IdentifierManager.cc

    r10395 r10399  
    6262    void IdentifierManager::addIdentifier(Identifier* identifier)
    6363    {
    64         orxout(verbose, context::identifier) << "Adding identifier for " << identifier->getName() << " / " << identifier->getTypeidName() << endl;
    65 
    66         this->identifierByTypeidName_[identifier->getTypeidName()] = identifier;
     64        orxout(verbose, context::identifier) << "Adding identifier for " << identifier->getName() << " / " << identifier->getTypeInfo().name() << endl;
     65
     66        this->identifiers_.insert(identifier);
    6767        this->identifierByString_[identifier->getName()] = identifier;
    6868        this->identifierByLowercaseString_[getLowercase(identifier->getName())] = identifier;
     
    8787        {
    8888            Context temporaryContext(NULL);
    89             for (std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeidName_.begin(); it != this->identifierByTypeidName_.end(); ++it)
     89            for (std::set<Identifier*>::const_iterator it = this->identifiers_.begin(); it != this->identifiers_.end(); ++it)
    9090            {
    91                 orxout(verbose, context::identifier) << "Initialize ClassIdentifier<" << it->second->getName() << ">-Singleton." << endl;
     91                Identifier* identifier = (*it);
     92
     93                orxout(verbose, context::identifier) << "Initialize ClassIdentifier<" << identifier->getName() << ">-Singleton." << endl;
    9294                // To initialize the identifier, we create a new object and delete it afterwards.
    93                 if (it->second->hasFactory())
     95                if (identifier->hasFactory())
    9496                {
    9597                    this->identifierTraceOfNewObject_.clear();
    96                     this->recordTraceForIdentifier_ = it->second;
    97 
    98                     Identifiable* temp = it->second->fabricate(&temporaryContext);
     98                    this->recordTraceForIdentifier_ = identifier;
     99
     100                    Identifiable* temp = identifier->fabricate(&temporaryContext);
    99101
    100102                    this->recordTraceForIdentifier_ = NULL;
    101103
    102                     if (temp->getIdentifier() != it->second)
    103                         orxout(internal_error) << "Newly created object of type " << it->second->getName() << " has unexpected identifier. Did you forget to use RegisterObject(classname)?" << endl;
    104 
    105                     it->second->initializeParents(this->identifierTraceOfNewObject_[temp]);
     104                    if (temp->getIdentifier() != identifier)
     105                        orxout(internal_error) << "Newly created object of type " << identifier->getName() << " has unexpected identifier. Did you forget to use RegisterObject(classname)?" << endl;
     106
     107                    identifier->initializeParents(this->identifierTraceOfNewObject_[temp]);
    106108
    107109                    delete temp;
    108110                }
    109111
    110                 initializedIdentifiers.insert(it->second);
     112                initializedIdentifiers.insert(identifier);
    111113            }
    112114
     
    117119
    118120        // finish the initialization of all identifiers
    119         for (std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeidName_.begin(); it != this->identifierByTypeidName_.end(); ++it)
    120         {
    121             if (initializedIdentifiers.find(it->second) != initializedIdentifiers.end())
    122                 it->second->finishInitialization();
     121        for (std::set<Identifier*>::const_iterator it = this->identifiers_.begin(); it != this->identifiers_.end(); ++it)
     122        {
     123            Identifier* identifier = (*it);
     124
     125            if (initializedIdentifiers.find(identifier) != initializedIdentifiers.end())
     126                identifier->finishInitialization();
    123127            else
    124                 orxout(internal_error) << "Identifier was registered late and is not initialized: " << it->second->getName() << " / " << it->second->getTypeidName() << endl;
     128                orxout(internal_error) << "Identifier was registered late and is not initialized: " << identifier->getName() << " / " << identifier->getTypeInfo().name() << endl;
    125129        }
    126130
     
    139143    {
    140144        Context temporaryContext(NULL);
    141         for (std::map<std::string, Identifier*>::const_iterator it1 = this->identifierByTypeidName_.begin(); it1 != this->identifierByTypeidName_.end(); ++it1)
    142         {
    143             if (!it1->second->hasFactory())
     145        for (std::set<Identifier*>::const_iterator it1 = this->identifiers_.begin(); it1 != this->identifiers_.end(); ++it1)
     146        {
     147            if (!(*it1)->hasFactory())
    144148                continue;
    145149
    146             Identifiable* temp = it1->second->fabricate(&temporaryContext);
    147 
    148             for (std::map<std::string, Identifier*>::const_iterator it2 = this->identifierByTypeidName_.begin(); it2 != this->identifierByTypeidName_.end(); ++it2)
     150            Identifiable* temp = (*it1)->fabricate(&temporaryContext);
     151
     152            for (std::set<Identifier*>::const_iterator it2 = this->identifiers_.begin(); it2 != this->identifiers_.end(); ++it2)
    149153            {
    150                 bool isA_AccordingToRtti = it2->second->canDynamicCastObjectToIdentifierClass(temp);
    151                 bool isA_AccordingToClassHierarchy = temp->isA(it2->second);
     154                bool isA_AccordingToRtti = (*it2)->canDynamicCastObjectToIdentifierClass(temp);
     155                bool isA_AccordingToClassHierarchy = temp->isA((*it2));
    152156
    153157                if (isA_AccordingToRtti != isA_AccordingToClassHierarchy)
    154158                {
    155                     orxout(internal_error) << "Class hierarchy does not match RTTI: Class hierarchy claims that " << it1->second->getName() <<
    156                         (isA_AccordingToClassHierarchy ? " is a " : " is not a ") << it2->second->getName() << " but RTTI says the opposite." << endl;
     159                    orxout(internal_error) << "Class hierarchy does not match RTTI: Class hierarchy claims that " << (*it1)->getName() <<
     160                        (isA_AccordingToClassHierarchy ? " is a " : " is not a ") << (*it2)->getName() << " but RTTI says the opposite." << endl;
    157161                }
    158162            }
     
    172176    void IdentifierManager::destroyAllIdentifiers()
    173177    {
    174         for (std::map<std::string, Identifier*>::iterator it = this->identifierByTypeidName_.begin(); it != this->identifierByTypeidName_.end(); ++it)
    175             delete (it->second);
    176 
    177         this->identifierByTypeidName_.clear();
     178        for (std::set<Identifier*>::iterator it = this->identifiers_.begin(); it != this->identifiers_.end(); ++it)
     179            delete (*it);
     180
     181        this->identifiers_.clear();
    178182        this->identifierByString_.clear();
    179183        this->identifierByLowercaseString_.clear();
     
    250254        @return The Identifier
    251255    */
    252     Identifier* IdentifierManager::getIdentifierByTypeidName(const std::string& typeidName)
    253     {
    254         std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeidName_.find(typeidName);
    255         if (it != this->identifierByTypeidName_.end())
    256             return it->second;
    257         else
    258             return 0;
     256    Identifier* IdentifierManager::getIdentifierByTypeInfo(const std::type_info& typeInfo)
     257    {
     258        // TODO: use std::type_index and a map to find identifiers by type_info (only with c++11)
     259        for (std::set<Identifier*>::iterator it = this->identifiers_.begin(); it != this->identifiers_.end(); ++it)
     260            if ((*it)->getTypeInfo() == typeInfo)
     261                return (*it);
     262        return 0;
    259263    }
    260264
  • code/branches/core7/src/libraries/core/class/IdentifierManager.h

    r10395 r10399  
    3838
    3939#include <map>
     40#include <set>
    4041#include <list>
    4142#include <string>
     
    7475            Identifier* getIdentifierByLowercaseString(const std::string& name);
    7576            Identifier* getIdentifierByID(uint32_t id);
    76             Identifier* getIdentifierByTypeidName(const std::string& typeidName);
     77            Identifier* getIdentifierByTypeInfo(const std::type_info& typeInfo);
    7778
    7879            void clearNetworkIDs();
     
    100101                { hierarchyCreatingCounter_s--; }
    101102
    102             std::map<std::string, Identifier*> identifierByTypeidName_;      //!< Map with the names as received by typeid(). This is only used internally.
    103 
     103            std::set<Identifier*> identifiers_;                              //!< All identifiers. This is only used internally.
    104104            std::map<std::string, Identifier*> identifierByString_;          //!< Map that stores all Identifiers with their names.
    105105            std::map<std::string, Identifier*> identifierByLowercaseString_; //!< Map that stores all Identifiers with their names in lowercase.
Note: See TracChangeset for help on using the changeset viewer.