Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 25, 2015, 10:49:34 PM (10 years ago)
Author:
landauf
Message:

create and initialize Identifiers explicitly via registerClass(). registerClass() must be called before using an Identifier of this type.
(some tests will currently fail)

Location:
code/branches/core7/src/libraries/core
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core7/src/libraries/core/CoreIncludes.h

    r10381 r10395  
    134134*/
    135135#define RegisterObject(ClassName) \
    136     if (ClassIdentifier<ClassName>::getIdentifier(#ClassName)->initializeObject(this)) \
     136    if (ClassIdentifier<ClassName>::getIdentifier()->initializeObject(this)) \
    137137        return; \
    138138    else \
     
    168168    {
    169169        orxout(verbose, context::misc::factory) << "Create entry for " << name << " in Factory." << endl;
    170         Identifier* identifier = ClassIdentifier<T>::getIdentifier(name);
    171         identifier->setFactory(factory);
    172         identifier->setLoadable(bLoadable);
     170        Identifier* identifier = new ClassIdentifier<T>(name, factory, bLoadable);
     171        IdentifierManager::getInstance().addIdentifier(identifier);
    173172        return identifier;
    174173    }
  • code/branches/core7/src/libraries/core/class/Identifier.cc

    r10381 r10395  
    5050        @brief Constructor: No factory, no object created, new ObjectList and a unique networkID.
    5151    */
    52     Identifier::Identifier()
     52    Identifier::Identifier(const std::string& name, Factory* factory, bool bLoadable)
    5353        : classID_(IdentifierManager::getInstance().getUniqueClassId())
    5454    {
    55         this->factory_ = 0;
     55        this->name_ = name;
     56        this->factory_ = factory;
     57        this->bLoadable_ = bLoadable;
    5658        this->bInitialized_ = false;
    57         this->bLoadable_ = false;
    5859        this->bIsVirtualBase_ = false;
    5960
     
    7879        for (std::map<std::string, XMLPortObjectContainer*>::iterator it = this->xmlportObjectContainers_.begin(); it != this->xmlportObjectContainers_.end(); ++it)
    7980            delete (it->second);
    80     }
    81 
    82     /**
    83         @brief Sets the name of the class.
    84     */
    85     void Identifier::setName(const std::string& name)
    86     {
    87         if (name != this->name_)
    88         {
    89             this->name_ = name;
    90             IdentifierManager::getInstance().addIdentifierToLookupMaps(this);
    91         }
    92     }
    93 
    94     void Identifier::setFactory(Factory* factory)
    95     {
    96         if (this->factory_)
    97             delete this->factory_;
    98 
    99         this->factory_ = factory;
    10081    }
    10182
     
    127108    {
    128109        this->networkID_ = id;
    129         IdentifierManager::getInstance().addIdentifierToLookupMaps(this);
     110        IdentifierManager::getInstance().addIdentifier(this);
    130111    }
    131112
  • code/branches/core7/src/libraries/core/class/Identifier.h

    r10381 r10395  
    8282
    8383#include "util/Output.h"
     84#include "util/OrxAssert.h"
    8485#include "core/object/ObjectList.h"
    8586#include "core/object/Listable.h"
     
    109110    {
    110111        public:
    111             Identifier();
     112            Identifier(const std::string& name, Factory* factory, bool bLoadable);
    112113            Identifier(const Identifier& identifier); // don't copy
    113114            virtual ~Identifier();
     
    115116            /// Returns the name of the class the Identifier belongs to.
    116117            inline const std::string& getName() const { return this->name_; }
    117             void setName(const std::string& name);
    118118
    119119            /// Returns the name of the class as it is returned by typeid(T).name()
     
    127127            ORX_FORCEINLINE unsigned int getClassID() const { return this->classID_; }
    128128
    129             /// Sets the Factory.
    130             void setFactory(Factory* factory);
    131129            /// Returns true if the Identifier has a Factory.
    132130            inline bool hasFactory() const { return (this->factory_ != 0); }
     
    136134            /// Returns true if the class can be loaded through XML.
    137135            inline bool isLoadable() const { return this->bLoadable_; }
    138             /// Set the class to be loadable through XML or not.
    139             inline void setLoadable(bool bLoadable) { this->bLoadable_ = bLoadable; }
    140136
    141137            /// Returns true if child classes should inherit virtually from this class.
     
    264260
    265261        public:
    266             static ClassIdentifier<T>* getIdentifier();
    267             static ClassIdentifier<T>* getIdentifier(const std::string& name);
    268 
    269             bool initializeObject(T* object);
    270 
    271             void setConfigValues(T* object, Configurable*) const;
    272             void setConfigValues(T* object, Identifiable*) const;
    273 
    274             void addObjectToList(T* object, Listable*);
    275             void addObjectToList(T* object, Identifiable*);
    276 
    277             virtual void updateConfigValues(bool updateChildren = true) const;
    278 
    279             virtual const std::string& getTypeidName()
    280                 { return this->typeidName_; }
    281 
    282             virtual bool canDynamicCastObjectToIdentifierClass(Identifiable* object) const
    283                 { return dynamic_cast<T*>(object) != 0; }
    284 
    285         private:
    286             static void initializeIdentifier();
    287 
    288             ClassIdentifier(const ClassIdentifier<T>& identifier) {}    // don't copy
    289             ClassIdentifier()
     262            ClassIdentifier(const std::string& name, Factory* factory, bool bLoadable) : Identifier(name, factory, bLoadable)
    290263            {
     264                OrxVerify(ClassIdentifier<T>::classIdentifier_s == NULL, "Assertion failed in ClassIdentifier of type " << typeid(T).name());
     265                ClassIdentifier<T>::classIdentifier_s = this;
     266
    291267                this->typeidName_ = typeid(T).name();
    292268                SuperFunctionInitialization<0, T>::initialize(this);
     
    297273            }
    298274
     275            bool initializeObject(T* object);
     276
     277            void setConfigValues(T* object, Configurable*) const;
     278            void setConfigValues(T* object, Identifiable*) const;
     279
     280            void addObjectToList(T* object, Listable*);
     281            void addObjectToList(T* object, Identifiable*);
     282
     283            virtual void updateConfigValues(bool updateChildren = true) const;
     284
     285            virtual const std::string& getTypeidName()
     286                { return this->typeidName_; }
     287
     288            virtual bool canDynamicCastObjectToIdentifierClass(Identifiable* object) const
     289                { return dynamic_cast<T*>(object) != 0; }
     290
     291            static ClassIdentifier<T>* getIdentifier();
     292
     293        private:
     294            ClassIdentifier(const ClassIdentifier<T>& identifier) {}    // don't copy
     295
    299296            void updateConfigValues(bool updateChildren, Listable*) const;
    300297            void updateConfigValues(bool updateChildren, Identifiable*) const;
     
    312309    */
    313310    template <class T>
    314     inline ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier()
    315     {
    316         // check if the Identifier already exists
    317         if (!ClassIdentifier<T>::classIdentifier_s)
    318             ClassIdentifier<T>::initializeIdentifier();
    319 
     311    /*static*/ inline ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier()
     312    {
     313        if (ClassIdentifier<T>::classIdentifier_s == NULL)
     314            ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*) IdentifierManager::getInstance().getIdentifierByTypeidName(typeid(T).name());
     315
     316        OrxVerify(ClassIdentifier<T>::classIdentifier_s != NULL, "Assertion failed in ClassIdentifier of type " << typeid(T).name());
    320317        return ClassIdentifier<T>::classIdentifier_s;
    321     }
    322 
    323     /**
    324         @brief Does the same as getIdentifier() but sets the name if this wasn't done yet.
    325         @param name The name of this Identifier
    326         @return The Identifier
    327     */
    328     template <class T>
    329     inline ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier(const std::string& name)
    330     {
    331         ClassIdentifier<T>* identifier = ClassIdentifier<T>::getIdentifier();
    332         identifier->setName(name);
    333         return identifier;
    334     }
    335 
    336     /**
    337         @brief Assigns the static field for the identifier singleton.
    338     */
    339     template <class T>
    340     /*static */ void ClassIdentifier<T>::initializeIdentifier()
    341     {
    342         // create a new identifier anyway. Will be deleted if not used.
    343         ClassIdentifier<T>* proposal = new ClassIdentifier<T>();
    344 
    345         // Get the entry from the map
    346         ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*)IdentifierManager::getInstance().getGloballyUniqueIdentifier(proposal);
    347 
    348         if (ClassIdentifier<T>::classIdentifier_s == proposal)
    349             orxout(verbose, context::identifier) << "Requested Identifier for " << proposal->getTypeidName() << " was not yet existing and got created." << endl;
    350         else
    351         {
    352             orxout(verbose, context::identifier) << "Requested Identifier for " << proposal->getTypeidName() << " was already existing and got assigned." << endl;
    353             delete proposal; // delete proposal (it is not used anymore)
    354         }
    355318    }
    356319
  • code/branches/core7/src/libraries/core/class/IdentifierManager.cc

    r10372 r10395  
    5858
    5959    /**
    60         @brief Returns an identifier by name and adds it if not available
    61         @param proposal A pointer to a newly created identifier for the case of non existence in the map
    62         @return The identifier (unique instance)
    63     */
    64     Identifier* IdentifierManager::getGloballyUniqueIdentifier(Identifier* proposal)
    65     {
    66         const std::string& typeidName = proposal->getTypeidName();
    67         std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeidName_.find(typeidName);
    68 
    69         if (it != this->identifierByTypeidName_.end())
    70         {
    71             // There is already an entry: return it
    72             return it->second;
    73         }
    74         else
    75         {
    76             // There is no entry: put the proposal into the map and return it
    77             this->identifierByTypeidName_[typeidName] = proposal;
    78             return proposal;
    79         }
    80     }
    81 
    82     /**
    8360     * Registers the identifier in all maps of the IdentifierManager.
    8461     */
    85     void IdentifierManager::addIdentifierToLookupMaps(Identifier* identifier)
    86     {
    87         const std::string& typeidName = identifier->getTypeidName();
    88         if (this->identifierByTypeidName_.find(typeidName) != this->identifierByTypeidName_.end())
    89         {
    90             this->identifierByString_[identifier->getName()] = identifier;
    91             this->identifierByLowercaseString_[getLowercase(identifier->getName())] = identifier;
    92             this->identifierByNetworkId_[identifier->getNetworkID()] = identifier;
    93         }
    94         else
    95             orxout(internal_warning) << "Trying to add an identifier to lookup maps which is not known to IdentifierManager" << endl;
     62    void IdentifierManager::addIdentifier(Identifier* identifier)
     63    {
     64        orxout(verbose, context::identifier) << "Adding identifier for " << identifier->getName() << " / " << identifier->getTypeidName() << endl;
     65
     66        this->identifierByTypeidName_[identifier->getTypeidName()] = identifier;
     67        this->identifierByString_[identifier->getName()] = identifier;
     68        this->identifierByLowercaseString_[getLowercase(identifier->getName())] = identifier;
     69        this->identifierByNetworkId_[identifier->getNetworkID()] = identifier;
    9670    }
    9771
     
    272246
    273247    /**
     248        @brief Returns the Identifier with a given typeid-name.
     249        @param name The typeid-name of the wanted Identifier
     250        @return The Identifier
     251    */
     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;
     259    }
     260
     261    /**
    274262        @brief Cleans the NetworkID map (needed on clients for correct initialization)
    275263    */
  • code/branches/core7/src/libraries/core/class/IdentifierManager.h

    r10372 r10395  
    4848            static IdentifierManager& getInstance();
    4949
    50             Identifier* getGloballyUniqueIdentifier(Identifier* proposal);
    51             void addIdentifierToLookupMaps(Identifier* identifier);
     50            void addIdentifier(Identifier* identifier);
    5251
    5352            unsigned int getUniqueClassId()
     
    7574            Identifier* getIdentifierByLowercaseString(const std::string& name);
    7675            Identifier* getIdentifierByID(uint32_t id);
     76            Identifier* getIdentifierByTypeidName(const std::string& typeidName);
    7777
    7878            void clearNetworkIDs();
Note: See TracChangeset for help on using the changeset viewer.