Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 25, 2013, 9:08:42 PM (11 years ago)
Author:
landauf
Message:

merged core6 back to trunk

Location:
code/trunk
Files:
12 edited
3 copied

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/libraries/core/class/CMakeLists.txt

    r9577 r9667  
    33  Identifier.cc
    44  IdentifierManager.cc
     5  OrxonoxClass.cc
     6  OrxonoxInterface.cc
    57)
  • code/trunk/src/libraries/core/class/Identifiable.cc

    r9574 r9667  
    3535
    3636#include <cassert>
     37#include "core/CoreIncludes.h"
    3738#include "core/object/Context.h"
    3839#include "Identifier.h"
     
    4041namespace orxonox
    4142{
     43    RegisterClassNoArgs(Identifiable);
     44
    4245    /**
    4346        @brief Constructor: Sets the default values.
     
    4649    {
    4750        this->identifier_ = 0;
    48         this->parents_ = 0;
    49         // Optimisation
    50         this->objectPointers_.reserve(6);
    51     }
     51        this->objectPointers_.reserve(6); // Optimisation
    5252
    53     /**
    54         @brief Destructor: Removes the object from the object-lists
    55     */
    56     Identifiable::~Identifiable()
    57     {
    58         // parents_ exists only if isCreatingHierarchy() of the associated Identifier returned true while creating the class
    59         if (this->parents_)
    60             delete this->parents_;
     53        RegisterObject(Identifiable);
    6154    }
    6255
  • code/trunk/src/libraries/core/class/Identifiable.h

    r9574 r9667  
    5555        public:
    5656            Identifiable();
    57             virtual ~Identifiable();
     57            virtual ~Identifiable() {}
    5858
    5959            /// Returns the Identifier of the object.
     
    119119        private:
    120120            Identifier* identifier_;               //!< The Identifier of the object
    121             std::set<const Identifier*>* parents_; //!< List of all parents of the object
    122121
    123122            /// 'Fast map' that holds this-pointers of all derived types
  • code/trunk/src/libraries/core/class/Identifier.cc

    r9593 r9667  
    3737
    3838#include "util/StringUtils.h"
     39#include "core/CoreIncludes.h"
    3940#include "core/config/ConfigValueContainer.h"
    4041#include "core/XMLPort.h"
     
    5051    */
    5152    Identifier::Identifier()
    52         : classID_(IdentifierManager::classIDCounter_s++)
    53     {
    54         this->objects_ = new ObjectListBase();
    55 
    56         this->bCreatedOneObject_ = false;
    57         this->bSetName_ = false;
     53        : classID_(IdentifierManager::getInstance().getUniqueClassId())
     54    {
    5855        this->factory_ = 0;
     56        this->bInitialized_ = false;
    5957        this->bLoadable_ = false;
    6058
     
    7068    Identifier::~Identifier()
    7169    {
    72         delete this->objects_;
    73 
    7470        if (this->factory_)
    7571            delete this->factory_;
     
    8480
    8581    /**
    86         @brief Registers a class, which means that the name and the parents get stored.
    87         @param parents A list, containing the Identifiers of all parents of the class
    88         @param bRootClass True if the class is either an Interface or the BaseObject itself
    89     */
    90     void Identifier::initializeClassHierarchy(std::set<const Identifier*>* parents, bool bRootClass)
    91     {
    92         // Check if at least one object of the given type was created
    93         if (!this->bCreatedOneObject_ && IdentifierManager::isCreatingHierarchy())
    94         {
    95             // If no: We have to store the information and initialize the Identifier
    96             orxout(verbose, context::identifier) << "Register Class in ClassIdentifier<" << this->getName() << ">-Singleton -> Initialize Singleton." << endl;
    97             if (bRootClass)
    98                 this->initialize(0); // 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.
    99             else
    100                 this->initialize(parents);
    101         }
    102     }
    103 
    104     /**
    105         @brief Initializes the Identifier with a list containing all parents of the class the Identifier belongs to.
    106         @param parents A list containing all parents
    107     */
    108     void Identifier::initialize(std::set<const Identifier*>* parents)
    109     {
    110         orxout(verbose, context::identifier) << "Initialize ClassIdentifier<" << this->name_ << ">-Singleton." << endl;
    111         this->bCreatedOneObject_ = true;
    112 
    113         if (parents)
    114         {
    115             this->parents_ = (*parents);
    116             this->directParents_ = (*parents);
    117 
    118             // Iterate through all parents
    119             for (std::set<const Identifier*>::iterator it = parents->begin(); it != parents->end(); ++it)
    120             {
    121                 // Tell the parent we're one of it's children
    122                 (*it)->children_.insert((*it)->children_.end(), this);
    123 
    124                 // Erase all parents of our parent from our direct-parent-list
    125                 for (std::set<const Identifier*>::const_iterator it1 = (*it)->getParents().begin(); it1 != (*it)->getParents().end(); ++it1)
    126                 {
    127                     // Search for the parent's parent in our direct-parent-list
    128                     for (std::set<const Identifier*>::iterator it2 = this->directParents_.begin(); it2 != this->directParents_.end(); ++it2)
    129                     {
    130                         if ((*it1) == (*it2))
    131                         {
    132                             // We've found a non-direct parent in our list: Erase it
    133                             this->directParents_.erase(it2);
    134                             break;
    135                         }
    136                     }
    137                 }
    138             }
    139 
    140             // Now iterate through all direct parents
    141             for (std::set<const Identifier*>::iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it)
    142             {
    143                 // Tell the parent we're one of it's direct children
    144                 (*it)->directChildren_.insert((*it)->directChildren_.end(), this);
    145 
    146                 // Create the super-function dependencies
    147                 (*it)->createSuperFunctionCaller();
    148             }
    149         }
    150     }
    151 
    152     /**
    15382        @brief Sets the name of the class.
    15483    */
    15584    void Identifier::setName(const std::string& name)
    15685    {
    157         if (!this->bSetName_)
     86        if (name != this->name_)
    15887        {
    15988            this->name_ = name;
    160             this->bSetName_ = true;
    161             IdentifierManager::getStringIdentifierMapIntern()[name] = this;
    162             IdentifierManager::getLowercaseStringIdentifierMapIntern()[getLowercase(name)] = this;
    163             IdentifierManager::getIDIdentifierMapIntern()[this->networkID_] = this;
    164         }
    165     }
     89            IdentifierManager::getInstance().addIdentifierToLookupMaps(this);
     90        }
     91    }
     92
     93    void Identifier::setFactory(Factory* factory)
     94    {
     95        if (this->factory_)
     96            delete this->factory_;
     97
     98        this->factory_ = factory;
     99    }
     100
    166101
    167102    /**
     
    169104        @return The new object
    170105    */
    171     OrxonoxClass* Identifier::fabricate(BaseObject* creator)
     106    Identifiable* Identifier::fabricate(Context* context)
    172107    {
    173108        if (this->factory_)
    174109        {
    175             return this->factory_->fabricate(creator);
     110            return this->factory_->fabricate(context);
    176111        }
    177112        else
     
    190125    void Identifier::setNetworkID(uint32_t id)
    191126    {
    192 //        Identifier::getIDIdentifierMapIntern().erase(this->networkID_);
    193         IdentifierManager::getIDIdentifierMapIntern()[id] = this;
    194127        this->networkID_ = id;
     128        IdentifierManager::getInstance().addIdentifierToLookupMaps(this);
     129    }
     130
     131    /**
     132     * @brief Used to define the direct parents of an Identifier of an abstract class.
     133     */
     134    Identifier& Identifier::inheritsFrom(Identifier* directParent)
     135    {
     136        if (this->parents_.empty())
     137            this->directParents_.insert(directParent);
     138        else
     139            orxout(internal_error) << "Trying to add " << directParent->getName() << " as a direct parent of " << this->getName() << " after the latter was already initialized" << endl;
     140
     141        return *this;
     142    }
     143
     144    /**
     145     * @brief Initializes the parents of this Identifier while creating the class hierarchy.
     146     * @param identifiers All identifiers that were used to create an instance of this class (including this identifier itself)
     147     */
     148    void Identifier::initializeParents(const std::set<const Identifier*>& identifiers)
     149    {
     150        if (!IdentifierManager::getInstance().isCreatingHierarchy())
     151        {
     152            orxout(internal_warning) << "Identifier::initializeParents() created outside of class hierarchy creation" << endl;
     153            return;
     154        }
     155
     156        for (std::set<const Identifier*>::const_iterator it = identifiers.begin(); it != identifiers.end(); ++it)
     157            if (*it != this)
     158                this->parents_.insert(*it);
     159    }
     160
     161    /**
     162     * @brief Initializes the direct parents of this Identifier while creating the class hierarchy. This is only intended for abstract classes.
     163     */
     164    void Identifier::initializeDirectParentsOfAbstractClass()
     165    {
     166        if (!IdentifierManager::getInstance().isCreatingHierarchy())
     167        {
     168            orxout(internal_warning) << "Identifier::initializeDirectParentsOfAbstractClass() created outside of class hierarchy creation" << endl;
     169            return;
     170        }
     171
     172        // only Identifiable is allowed to have no parents (even tough it's currently not abstract)
     173        if (this->directParents_.empty() && !this->isExactlyA(Class(Identifiable)))
     174        {
     175            orxout(internal_error) << "Identifier " << this->getName() << " / " << this->getTypeidName() << " is marked as abstract but has no direct parents defined" << endl;
     176            orxout(internal_error) << "  If this class is not abstract, use RegisterClass(ThisClass);" << endl;
     177            orxout(internal_error) << "  If this class is abstract, use RegisterAbstractClass(ThisClass).inheritsFrom(Class(BaseClass));" << endl;
     178        }
     179    }
     180
     181    /**
     182     * @brief Finishes the initialization of this Identifier after creating the class hierarchy by wiring the (direct) parent/child references correctly.
     183     */
     184    void Identifier::finishInitialization()
     185    {
     186        if (!IdentifierManager::getInstance().isCreatingHierarchy())
     187        {
     188            orxout(internal_warning) << "Identifier::finishInitialization() created outside of class hierarchy creation" << endl;
     189            return;
     190        }
     191
     192        if (this->isInitialized())
     193            return;
     194
     195        // if no direct parents were defined, initialize them with the set of all parents
     196        if (this->directParents_.empty())
     197            this->directParents_ = this->parents_;
     198
     199        // initialize all parents before continuing to initialize this identifier
     200        for (std::set<const Identifier*>::const_iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it)
     201        {
     202            Identifier* directParent = const_cast<Identifier*>(*it);
     203            directParent->finishInitialization(); // initialize parent
     204            this->parents_.insert(directParent);  // direct parent is also a parent
     205            this->parents_.insert(directParent->parents_.begin(), directParent->parents_.end()); // parents of direct parent are also parents
     206        }
     207
     208        // parents of parents are no direct parents of this identifier
     209        for (std::set<const Identifier*>::const_iterator it_parent = this->parents_.begin(); it_parent != this->parents_.end(); ++it_parent)
     210            for (std::set<const Identifier*>::const_iterator it_parent_parent = const_cast<Identifier*>(*it_parent)->parents_.begin(); it_parent_parent != const_cast<Identifier*>(*it_parent)->parents_.end(); ++it_parent_parent)
     211                this->directParents_.erase(*it_parent_parent);
     212
     213        // tell all parents that this identifier is a child
     214        for (std::set<const Identifier*>::const_iterator it = this->parents_.begin(); it != this->parents_.end(); ++it)
     215            const_cast<Identifier*>(*it)->children_.insert(this);
     216
     217        // tell all direct parents that this identifier is a direct child
     218        for (std::set<const Identifier*>::const_iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it)
     219        {
     220            const_cast<Identifier*>(*it)->directChildren_.insert(this);
     221
     222            // Create the super-function dependencies
     223            (*it)->createSuperFunctionCaller();
     224        }
     225
     226        this->bInitialized_ = true;
    195227    }
    196228
  • code/trunk/src/libraries/core/class/Identifier.h

    r9593 r9667  
    5656    object->getIdentifier()->getName();                                         // returns "MyClass"
    5757
    58     OrxonoxClass* other = object->getIdentifier()->fabricate(0);                // fabricates a new instance of MyClass
    59 
    60 
    61     // iterate through all objects of type MyClass:
    62     ObjectListBase* objects = object->getIdentifier()->getObjects();            // get a pointer to the object-list
    63     int count;
    64     for (Iterator<MyClass> it = objects.begin(); it != objects.end(); ++it)     // iterate through the objects
    65         ++count;
    66     orxout() << count << endl;                                                  // prints "2" because we created 2 instances of MyClass so far
     58    Identifiable* other = object->getIdentifier()->fabricate(0);                // fabricates a new instance of MyClass
    6759
    6860
     
    9082
    9183#include "util/Output.h"
    92 #include "core/object/MetaObjectList.h"
    9384#include "core/object/ObjectList.h"
    94 #include "core/object/ObjectListBase.h"
     85#include "core/object/Listable.h"
     86#include "core/object/Context.h"
     87#include "core/object/Destroyable.h"
     88#include "core/object/WeakPtr.h"
    9589#include "IdentifierManager.h"
    9690#include "Super.h"
     
    112106        @note You can't directly create an Identifier, it's just the base-class of ClassIdentifier<T>.
    113107    */
    114     class _CoreExport Identifier
    115     {
    116         friend class IdentifierManager;
    117 
     108    class _CoreExport Identifier : public Destroyable
     109    {
    118110        public:
     111            Identifier();
     112            Identifier(const Identifier& identifier); // don't copy
     113            virtual ~Identifier();
     114
    119115            /// Returns the name of the class the Identifier belongs to.
    120116            inline const std::string& getName() const { return this->name_; }
    121117            void setName(const std::string& name);
    122118
     119            /// Returns the name of the class as it is returned by typeid(T).name()
     120            virtual const std::string& getTypeidName() = 0;
     121
    123122            /// Returns the network ID to identify a class through the network.
    124123            inline uint32_t getNetworkID() const { return this->networkID_; }
     
    128127            ORX_FORCEINLINE unsigned int getClassID() const { return this->classID_; }
    129128
    130             /// Returns the list of all existing objects of this class.
    131             inline ObjectListBase* getObjects() const { return this->objects_; }
    132 
    133129            /// Sets the Factory.
    134             inline void addFactory(Factory* factory) { this->factory_ = factory; }
     130            void setFactory(Factory* factory);
    135131            /// Returns true if the Identifier has a Factory.
    136132            inline bool hasFactory() const { return (this->factory_ != 0); }
    137133
    138             OrxonoxClass* fabricate(BaseObject* creator);
     134            Identifiable* fabricate(Context* context);
    139135
    140136            /// Returns true if the class can be loaded through XML.
     
    142138            /// Set the class to be loadable through XML or not.
    143139            inline void setLoadable(bool bLoadable) { this->bLoadable_ = bLoadable; }
     140
     141            /// Returns true if the Identifier was completely initialized.
     142            inline bool isInitialized() const { return this->bInitialized_; }
     143
     144
     145            /////////////////////////////
     146            ////// Class Hierarchy //////
     147            /////////////////////////////
     148            Identifier& inheritsFrom(Identifier* directParent);
     149
     150            void initializeParents(const std::set<const Identifier*>& identifiers);
     151            void initializeDirectParentsOfAbstractClass();
     152            void finishInitialization();
    144153
    145154            bool isA(const Identifier* identifier) const;
     
    150159            bool isDirectParentOf(const Identifier* identifier) const;
    151160
    152 
    153             /////////////////////////////
    154             ////// Class Hierarchy //////
    155             /////////////////////////////
    156161            /// Returns the parents of the class the Identifier belongs to.
    157162            inline const std::set<const Identifier*>& getParents() const { return this->parents_; }
     
    220225
    221226        protected:
    222             Identifier();
    223             Identifier(const Identifier& identifier); // don't copy
    224             virtual ~Identifier();
    225 
    226227            virtual void createSuperFunctionCaller() const = 0;
    227228
    228             void initializeClassHierarchy(std::set<const Identifier*>* parents, bool bRootClass);
    229 
    230             /// Returns the children of the class the Identifier belongs to.
    231             inline std::set<const Identifier*>& getChildrenIntern() const { return this->children_; }
    232             /// Returns the direct children of the class the Identifier belongs to.
    233             inline std::set<const Identifier*>& getDirectChildrenIntern() const { return this->directChildren_; }
    234 
    235             ObjectListBase* objects_;                                      //!< The list of all objects of this class
    236 
    237229        private:
    238             void initialize(std::set<const Identifier*>* parents);
    239 
    240230            std::set<const Identifier*> parents_;                          //!< The parents of the class the Identifier belongs to
    241             mutable std::set<const Identifier*> children_;                 //!< The children of the class the Identifier belongs to
     231            std::set<const Identifier*> children_;                         //!< The children of the class the Identifier belongs to
    242232
    243233            std::set<const Identifier*> directParents_;                    //!< The direct parents of the class the Identifier belongs to
    244             mutable std::set<const Identifier*> directChildren_;           //!< The direct children of the class the Identifier belongs to
    245 
    246             bool bCreatedOneObject_;                                       //!< True if at least one object of the given type was created (used to determine the need of storing the parents)
    247             bool bSetName_;                                                //!< True if the name is set
     234            std::set<const Identifier*> directChildren_;                   //!< The direct children of the class the Identifier belongs to
     235
     236            bool bInitialized_;                                            //!< Is true if the Identifier was completely initialized
    248237            bool bLoadable_;                                               //!< False = it's not permitted to load the object through XML
    249238            std::string name_;                                             //!< The name of the class the Identifier belongs to
     
    287276            static ClassIdentifier<T>* getIdentifier(const std::string& name);
    288277
    289             bool initialiseObject(T* object, const std::string& className, bool bRootClass);
     278            bool initializeObject(T* object);
    290279
    291280            void setConfigValues(T* object, Configurable*) const;
     
    295284            void addObjectToList(T* object, Identifiable*);
    296285
    297             void updateConfigValues(bool updateChildren = true) const;
     286            virtual void updateConfigValues(bool updateChildren = true) const;
     287
     288            virtual const std::string& getTypeidName()
     289                { return this->typeidName_; }
    298290
    299291        private:
    300             static void initialiseIdentifier();
     292            static void initializeIdentifier();
     293
    301294            ClassIdentifier(const ClassIdentifier<T>& identifier) {}    // don't copy
    302295            ClassIdentifier()
    303296            {
     297                this->typeidName_ = typeid(T).name();
    304298                SuperFunctionInitialization<0, T>::initialize(this);
    305299            }
     
    309303            }
    310304
    311             static ClassIdentifier<T>* classIdentifier_s;
     305            void updateConfigValues(bool updateChildren, Listable*) const;
     306            void updateConfigValues(bool updateChildren, Identifiable*) const;
     307
     308            std::string typeidName_;
     309            static WeakPtr<ClassIdentifier<T> > classIdentifier_s;
    312310    };
    313311
    314312    template <class T>
    315     ClassIdentifier<T>* ClassIdentifier<T>::classIdentifier_s = 0;
     313    WeakPtr<ClassIdentifier<T> > ClassIdentifier<T>::classIdentifier_s;
    316314
    317315    /**
     
    324322        // check if the Identifier already exists
    325323        if (!ClassIdentifier<T>::classIdentifier_s)
    326             ClassIdentifier<T>::initialiseIdentifier();
     324            ClassIdentifier<T>::initializeIdentifier();
    327325
    328326        return ClassIdentifier<T>::classIdentifier_s;
     
    346344    */
    347345    template <class T>
    348     void ClassIdentifier<T>::initialiseIdentifier()
    349     {
    350         // Get the name of the class
    351         std::string name = typeid(T).name();
    352 
    353         // create a new identifier anyway. Will be deleted in Identifier::getIdentifier if not used.
     346    /*static */ void ClassIdentifier<T>::initializeIdentifier()
     347    {
     348        // create a new identifier anyway. Will be deleted if not used.
    354349        ClassIdentifier<T>* proposal = new ClassIdentifier<T>();
    355350
    356351        // Get the entry from the map
    357         ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*)IdentifierManager::getIdentifierSingleton(name, proposal);
     352        ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*)IdentifierManager::getInstance().getGloballyUniqueIdentifier(proposal);
    358353
    359354        if (ClassIdentifier<T>::classIdentifier_s == proposal)
    360         {
    361             orxout(verbose, context::identifier) << "Requested Identifier for " << name << " was not yet existing and got created." << endl;
    362         }
     355            orxout(verbose, context::identifier) << "Requested Identifier for " << proposal->getTypeidName() << " was not yet existing and got created." << endl;
    363356        else
    364357        {
    365             orxout(verbose, context::identifier) << "Requested Identifier for " << name << " was already existing and got assigned." << endl;
     358            orxout(verbose, context::identifier) << "Requested Identifier for " << proposal->getTypeidName() << " was already existing and got assigned." << endl;
     359            delete proposal; // delete proposal (it is not used anymore)
    366360        }
    367361    }
     
    370364        @brief Adds an object of the given type to the ObjectList.
    371365        @param object The object to add
    372         @param className The name of the class T
    373         @param bRootClass True if this is a root class (i.e. it inherits directly from OrxonoxClass)
    374     */
    375     template <class T>
    376     bool ClassIdentifier<T>::initialiseObject(T* object, const std::string& className, bool bRootClass)
    377     {
    378         if (bRootClass)
    379             orxout(verbose, context::object_list) << "Register Root-Object: " << className << endl;
    380         else
    381             orxout(verbose, context::object_list) << "Register Object: " << className << endl;
     366    */
     367    template <class T>
     368    bool ClassIdentifier<T>::initializeObject(T* object)
     369    {
     370        orxout(verbose, context::object_list) << "Register Object: " << this->getName() << endl;
    382371
    383372        object->identifier_ = this;
    384         if (IdentifierManager::isCreatingHierarchy())
     373        if (IdentifierManager::getInstance().isCreatingHierarchy())
    385374        {
    386             if (bRootClass && !object->parents_)
    387                 object->parents_ = new std::set<const Identifier*>();
    388 
    389             if (object->parents_)
    390             {
    391                 this->initializeClassHierarchy(object->parents_, bRootClass);
    392                 object->parents_->insert(object->parents_->end(), this);
    393             }
     375            IdentifierManager::getInstance().createdObject(object);
    394376
    395377            this->setConfigValues(object, object);
     
    425407     */
    426408    template <class T>
    427     void ClassIdentifier<T>::addObjectToList(T* object, Listable*)
    428     {
    429         orxout(verbose, context::object_list) << "Added object to " << this->getName() << "-list." << endl;
    430         object->metaList_->add(this->objects_, this->objects_->add(object));
     409    void ClassIdentifier<T>::addObjectToList(T* object, Listable* listable)
     410    {
     411        if (listable->getContext())
     412            listable->getContext()->addObject(object);
    431413    }
    432414
     
    442424    template <class T>
    443425    void ClassIdentifier<T>::updateConfigValues(bool updateChildren) const
     426    {
     427        this->updateConfigValues(updateChildren, static_cast<T*>(NULL));
     428    }
     429
     430    template <class T>
     431    void ClassIdentifier<T>::updateConfigValues(bool updateChildren, Listable*) const
    444432    {
    445433        if (!this->hasConfigValues())
     
    452440            for (std::set<const Identifier*>::const_iterator it = this->getChildrenBegin(); it != this->getChildrenEnd(); ++it)
    453441                (*it)->updateConfigValues(false);
     442    }
     443
     444    template <class T>
     445    void ClassIdentifier<T>::updateConfigValues(bool updateChildren, Identifiable*) const
     446    {
     447        // no action
    454448    }
    455449
  • code/trunk/src/libraries/core/class/IdentifierManager.cc

    r9564 r9667  
    3737
    3838#include "util/StringUtils.h"
     39#include "core/CoreIncludes.h"
    3940#include "core/config/ConfigValueContainer.h"
    4041#include "core/XMLPort.h"
     
    4344namespace orxonox
    4445{
    45     int IdentifierManager::hierarchyCreatingCounter_s = 0;
    46     unsigned int IdentifierManager::classIDCounter_s = 0;
    47 
    48     /**
    49         @brief Returns the identifier map with the names as received by typeid(). This is only used internally.
    50     */
    51     std::map<std::string, Identifier*>& IdentifierManager::getTypeIDIdentifierMap()
    52     {
    53         static std::map<std::string, Identifier*> identifiers;    //!< The map to store all Identifiers.
    54         return identifiers;
     46    /* static */ IdentifierManager& IdentifierManager::getInstance()
     47    {
     48        static IdentifierManager instance;
     49        return instance;
     50    }
     51
     52    IdentifierManager::IdentifierManager()
     53    {
     54        this->hierarchyCreatingCounter_s = 0;
     55        this->classIDCounter_s = 0;
    5556    }
    5657
    5758    /**
    5859        @brief Returns an identifier by name and adds it if not available
    59         @param name The name of the identifier as typeid().name() suggests
    6060        @param proposal A pointer to a newly created identifier for the case of non existence in the map
    6161        @return The identifier (unique instance)
    6262    */
    63     Identifier* IdentifierManager::getIdentifierSingleton(const std::string& name, Identifier* proposal)
    64     {
    65         std::map<std::string, Identifier*>::const_iterator it = getTypeIDIdentifierMap().find(name);
    66 
    67         if (it != getTypeIDIdentifierMap().end())
    68         {
    69             // There is already an entry: return it and delete the proposal
    70             delete proposal;
     63    Identifier* IdentifierManager::getGloballyUniqueIdentifier(Identifier* proposal)
     64    {
     65        const std::string& typeidName = proposal->getTypeidName();
     66        std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeidName_.find(typeidName);
     67
     68        if (it != this->identifierByTypeidName_.end())
     69        {
     70            // There is already an entry: return it
    7171            return it->second;
    7272        }
     
    7474        {
    7575            // There is no entry: put the proposal into the map and return it
    76             getTypeIDIdentifierMap()[name] = proposal;
     76            this->identifierByTypeidName_[typeidName] = proposal;
    7777            return proposal;
    7878        }
     
    8080
    8181    /**
     82     * Registers the identifier in all maps of the IdentifierManager.
     83     */
     84    void IdentifierManager::addIdentifierToLookupMaps(Identifier* identifier)
     85    {
     86        const std::string& typeidName = identifier->getTypeidName();
     87        if (this->identifierByTypeidName_.find(typeidName) != this->identifierByTypeidName_.end())
     88        {
     89            this->identifierByString_[identifier->getName()] = identifier;
     90            this->identifierByLowercaseString_[getLowercase(identifier->getName())] = identifier;
     91            this->identifierByNetworkId_[identifier->getNetworkID()] = identifier;
     92        }
     93        else
     94            orxout(internal_warning) << "Trying to add an identifier to lookup maps which is not known to IdentifierManager" << endl;
     95    }
     96
     97    /**
    8298        @brief Creates the class-hierarchy by creating and destroying one object of each type.
    8399    */
     
    85101    {
    86102        orxout(internal_status) << "Create class-hierarchy" << endl;
    87         IdentifierManager::startCreatingHierarchy();
    88         for (std::map<std::string, Identifier*>::const_iterator it = IdentifierManager::getStringIdentifierMap().begin(); it != IdentifierManager::getStringIdentifierMap().end(); ++it)
    89         {
    90             // To create the new branch of the class-hierarchy, we create a new object and delete it afterwards.
    91             if (it->second->hasFactory())
     103        this->startCreatingHierarchy();
     104
     105        std::set<Identifier*> initializedIdentifiers;
     106
     107        // ensure root context exists before starting to create objects. if the root context is dynamically created while creating the class hierarchy, we
     108        // would mistakenly assume the class of the currently created object inherits from Context
     109        Context::getRootContext();
     110
     111        // iterate over all identifiers, create one instance of each class and initialize the identifiers
     112        {
     113            Context temporaryContext(NULL);
     114            for (std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeidName_.begin(); it != this->identifierByTypeidName_.end(); ++it)
    92115            {
    93                 OrxonoxClass* temp = it->second->fabricate(0);
    94                 temp->destroy();
     116                orxout(verbose, context::identifier) << "Initialize ClassIdentifier<" << it->second->getName() << ">-Singleton." << endl;
     117                // To initialize the identifier, we create a new object and delete it afterwards.
     118                if (it->second->hasFactory())
     119                {
     120                    this->identifiersOfNewObject_.clear();
     121                    Identifiable* temp = it->second->fabricate(&temporaryContext);
     122                    if (temp->getIdentifier() != it->second)
     123                        orxout(internal_error) << "Newly created object of type " << it->second->getName() << " has unexpected identifier. Did you forget to use RegisterObject(classname)?" << endl;
     124                    delete temp;
     125
     126                    it->second->initializeParents(this->identifiersOfNewObject_);
     127                }
     128                else
     129                    it->second->initializeDirectParentsOfAbstractClass();
     130
     131                initializedIdentifiers.insert(it->second);
    95132            }
    96         }
    97         IdentifierManager::stopCreatingHierarchy();
     133
     134            size_t numberOfObjects = temporaryContext.getObjectList<Listable>()->size();
     135            if (numberOfObjects > 0)
     136                orxout(internal_warning) << "There are still " << numberOfObjects << " listables left after creating the class hierarchy" << endl;
     137        }
     138
     139        // finish the initialization of all identifiers
     140        for (std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeidName_.begin(); it != this->identifierByTypeidName_.end(); ++it)
     141        {
     142            if (initializedIdentifiers.find(it->second) != initializedIdentifiers.end())
     143                it->second->finishInitialization();
     144            else
     145                orxout(internal_error) << "Identifier was registered late and is not initialized: " << it->second->getName() << " / " << it->second->getTypeidName() << endl;
     146        }
     147
     148        this->stopCreatingHierarchy();
    98149        orxout(internal_status) << "Finished class-hierarchy creation" << endl;
    99150    }
     
    104155    void IdentifierManager::destroyAllIdentifiers()
    105156    {
    106         for (std::map<std::string, Identifier*>::iterator it = IdentifierManager::getTypeIDIdentifierMap().begin(); it != IdentifierManager::getTypeIDIdentifierMap().end(); ++it)
     157        for (std::map<std::string, Identifier*>::iterator it = this->identifierByTypeidName_.begin(); it != this->identifierByTypeidName_.end(); ++it)
    107158            delete (it->second);
    108     }
    109 
    110     /**
    111         @brief Returns the map that stores all Identifiers with their names.
    112         @return The map
    113     */
    114     std::map<std::string, Identifier*>& IdentifierManager::getStringIdentifierMapIntern()
    115     {
    116         static std::map<std::string, Identifier*> identifierMap;
    117         return identifierMap;
    118     }
    119 
    120     /**
    121         @brief Returns the map that stores all Identifiers with their names in lowercase.
    122         @return The map
    123     */
    124     std::map<std::string, Identifier*>& IdentifierManager::getLowercaseStringIdentifierMapIntern()
    125     {
    126         static std::map<std::string, Identifier*> lowercaseIdentifierMap;
    127         return lowercaseIdentifierMap;
    128     }
    129 
    130     /**
    131         @brief Returns the map that stores all Identifiers with their network IDs.
    132         @return The map
    133     */
    134     std::map<uint32_t, Identifier*>& IdentifierManager::getIDIdentifierMapIntern()
    135     {
    136         static std::map<uint32_t, Identifier*> identifierMap;
    137         return identifierMap;
     159
     160        this->identifierByTypeidName_.clear();
     161        this->identifierByString_.clear();
     162        this->identifierByLowercaseString_.clear();
     163        this->identifierByNetworkId_.clear();
     164    }
     165
     166    /**
     167     * @brief Notifies the IdentifierManager about a newly created object while creating the class hierarchy.
     168     */
     169    void IdentifierManager::createdObject(Identifiable* identifiable)
     170    {
     171        if (this->isCreatingHierarchy())
     172            this->identifiersOfNewObject_.insert(identifiable->getIdentifier());
     173        else
     174            orxout(internal_warning) << "createdObject() called outside of class hierarchy creation" << endl;
    138175    }
    139176
     
    145182    Identifier* IdentifierManager::getIdentifierByString(const std::string& name)
    146183    {
    147         std::map<std::string, Identifier*>::const_iterator it = IdentifierManager::getStringIdentifierMapIntern().find(name);
    148         if (it != IdentifierManager::getStringIdentifierMapIntern().end())
     184        std::map<std::string, Identifier*>::const_iterator it = this->identifierByString_.find(name);
     185        if (it != this->identifierByString_.end())
    149186            return it->second;
    150187        else
     
    159196    Identifier* IdentifierManager::getIdentifierByLowercaseString(const std::string& name)
    160197    {
    161         std::map<std::string, Identifier*>::const_iterator it = IdentifierManager::getLowercaseStringIdentifierMapIntern().find(name);
    162         if (it != IdentifierManager::getLowercaseStringIdentifierMapIntern().end())
     198        std::map<std::string, Identifier*>::const_iterator it = this->identifierByLowercaseString_.find(name);
     199        if (it != this->identifierByLowercaseString_.end())
    163200            return it->second;
    164201        else
     
    173210    Identifier* IdentifierManager::getIdentifierByID(const uint32_t id)
    174211    {
    175         std::map<uint32_t, Identifier*>::const_iterator it = IdentifierManager::getIDIdentifierMapIntern().find(id);
    176         if (it != IdentifierManager::getIDIdentifierMapIntern().end())
     212        std::map<uint32_t, Identifier*>::const_iterator it = this->identifierByNetworkId_.find(id);
     213        if (it != this->identifierByNetworkId_.end())
    177214            return it->second;
    178215        else
     
    185222    void IdentifierManager::clearNetworkIDs()
    186223    {
    187         IdentifierManager::getIDIdentifierMapIntern().clear();
     224        this->identifierByNetworkId_.clear();
    188225    }
    189226}
  • code/trunk/src/libraries/core/class/IdentifierManager.h

    r9564 r9667  
    3838
    3939#include <map>
     40#include <set>
    4041#include <string>
    4142
     
    4445    class _CoreExport IdentifierManager
    4546    {
    46         friend class Identifier;
    47         template <class T> friend class ClassIdentifier;
     47        public:
     48            static IdentifierManager& getInstance();
    4849
    49         public:
     50            Identifier* getGloballyUniqueIdentifier(Identifier* proposal);
     51            void addIdentifierToLookupMaps(Identifier* identifier);
     52
     53            unsigned int getUniqueClassId()
     54                { return this->classIDCounter_s++; }
     55
     56
    5057            /////////////////////////////
    5158            ////// Class Hierarchy //////
    5259            /////////////////////////////
    53             static void createClassHierarchy();
     60            void createClassHierarchy();
     61            void destroyAllIdentifiers();
     62
     63            void createdObject(Identifiable* identifiable);
    5464
    5565            /// Returns true, if a branch of the class-hierarchy is being created, causing all new objects to store their parents.
    56             inline static bool isCreatingHierarchy()
     66            inline bool isCreatingHierarchy()
    5767                { return (hierarchyCreatingCounter_s > 0); }
    5868
     
    6171            ///// Identifier Map /////
    6272            //////////////////////////
    63             static void destroyAllIdentifiers();
     73            Identifier* getIdentifierByString(const std::string& name);
     74            Identifier* getIdentifierByLowercaseString(const std::string& name);
     75            Identifier* getIdentifierByID(uint32_t id);
    6476
    65             static Identifier* getIdentifierByString(const std::string& name);
    66             static Identifier* getIdentifierByLowercaseString(const std::string& name);
    67             static Identifier* getIdentifierByID(uint32_t id);
    68 
    69             static void clearNetworkIDs();
     77            void clearNetworkIDs();
    7078
    7179            /// Returns the map that stores all Identifiers with their names.
    72             static inline const std::map<std::string, Identifier*>& getStringIdentifierMap()
    73                 { return IdentifierManager::getStringIdentifierMapIntern(); }
    74             /// Returns a const_iterator to the beginning of the map that stores all Identifiers with their names.
    75             static inline std::map<std::string, Identifier*>::const_iterator getStringIdentifierMapBegin()
    76                 { return IdentifierManager::getStringIdentifierMap().begin(); }
    77             /// Returns a const_iterator to the end of the map that stores all Identifiers with their names.
    78             static inline std::map<std::string, Identifier*>::const_iterator getStringIdentifierMapEnd()
    79                 { return IdentifierManager::getStringIdentifierMap().end(); }
    80 
     80            inline const std::map<std::string, Identifier*>& getIdentifierByStringMap()
     81                { return this->identifierByString_; }
    8182            /// Returns the map that stores all Identifiers with their names in lowercase.
    82             static inline const std::map<std::string, Identifier*>& getLowercaseStringIdentifierMap()
    83                 { return IdentifierManager::getLowercaseStringIdentifierMapIntern(); }
    84             /// Returns a const_iterator to the beginning of the map that stores all Identifiers with their names in lowercase.
    85             static inline std::map<std::string, Identifier*>::const_iterator getLowercaseStringIdentifierMapBegin()
    86                 { return IdentifierManager::getLowercaseStringIdentifierMap().begin(); }
    87             /// Returns a const_iterator to the end of the map that stores all Identifiers with their names in lowercase.
    88             static inline std::map<std::string, Identifier*>::const_iterator getLowercaseStringIdentifierMapEnd()
    89                 { return IdentifierManager::getLowercaseStringIdentifierMap().end(); }
    90 
     83            inline const std::map<std::string, Identifier*>& getIdentifierByLowercaseStringMap()
     84                { return this->identifierByLowercaseString_; }
    9185            /// Returns the map that stores all Identifiers with their IDs.
    92             static inline const std::map<uint32_t, Identifier*>& getIDIdentifierMap()
    93                 { return IdentifierManager::getIDIdentifierMapIntern(); }
    94             /// Returns a const_iterator to the beginning of the map that stores all Identifiers with their IDs.
    95             static inline std::map<uint32_t, Identifier*>::const_iterator getIDIdentifierMapBegin()
    96                 { return IdentifierManager::getIDIdentifierMap().begin(); }
    97             /// Returns a const_iterator to the end of the map that stores all Identifiers with their IDs.
    98             static inline std::map<uint32_t, Identifier*>::const_iterator getIDIdentifierMapEnd()
    99                 { return IdentifierManager::getIDIdentifierMap().end(); }
    100 
    101         protected:
    102             static Identifier* getIdentifierSingleton(const std::string& name, Identifier* proposal);
    103 
    104             /// Returns the map that stores all Identifiers with their names.
    105             static std::map<std::string, Identifier*>& getStringIdentifierMapIntern();
    106             /// Returns the map that stores all Identifiers with their names in lowercase.
    107             static std::map<std::string, Identifier*>& getLowercaseStringIdentifierMapIntern();
    108             /// Returns the map that stores all Identifiers with their network IDs.
    109             static std::map<uint32_t, Identifier*>& getIDIdentifierMapIntern();
     86            inline const std::map<uint32_t, Identifier*>& getIdentifierByNetworkIdMap()
     87                { return this->identifierByNetworkId_; }
    11088
    11189        private:
     90            IdentifierManager();
     91            IdentifierManager(const IdentifierManager&);
     92            ~IdentifierManager() {}
     93
    11294            /// Increases the hierarchyCreatingCounter_s variable, causing all new objects to store their parents.
    113             inline static void startCreatingHierarchy()
     95            inline void startCreatingHierarchy()
    11496                { hierarchyCreatingCounter_s++; }
    11597            /// Decreases the hierarchyCreatingCounter_s variable, causing the objects to stop storing their parents.
    116             inline static void stopCreatingHierarchy()
     98            inline void stopCreatingHierarchy()
    11799                { hierarchyCreatingCounter_s--; }
    118100
    119             static std::map<std::string, Identifier*>& getTypeIDIdentifierMap();
     101            std::map<std::string, Identifier*> identifierByTypeidName_;      //!< Map with the names as received by typeid(). This is only used internally.
    120102
    121             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)
    122             static unsigned int classIDCounter_s;                          //!< Static counter for the unique classIDs
     103            std::map<std::string, Identifier*> identifierByString_;          //!< Map that stores all Identifiers with their names.
     104            std::map<std::string, Identifier*> identifierByLowercaseString_; //!< Map that stores all Identifiers with their names in lowercase.
     105            std::map<uint32_t, Identifier*> identifierByNetworkId_;          //!< Returns the map that stores all Identifiers with their network IDs.
     106
     107            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)
     108            std::set<const Identifier*> identifiersOfNewObject_;    //!< Used while creating the object hierarchy to keep track of the identifiers of a newly created object
     109            unsigned int classIDCounter_s;                          //!< counter for the unique classIDs
    123110    };
    124111}
  • code/trunk/src/libraries/core/class/OrxonoxClass.h

    r9585 r9667  
    5050    /**
    5151        @brief This is the class from which all objects of the game-logic (not the engine) are derived from.
    52 
    53         The BaseObject and other classes are derived with @c virtual @c public @c OrxonoxClass from OrxonoxClass.
    5452    */
    5553    class _CoreExport OrxonoxClass : virtual public Configurable, virtual public Destroyable
    5654    {
     55        public:
     56            OrxonoxClass();
    5757    };
    5858}
  • code/trunk/src/libraries/core/class/OrxonoxInterface.h

    r9585 r9667  
    5050    class _CoreExport OrxonoxInterface : virtual public Configurable, virtual public Destroyable
    5151    {
     52        public:
     53            OrxonoxInterface();
    5254    };
    5355}
  • code/trunk/src/libraries/core/class/SubclassIdentifier.h

    r9563 r9667  
    163163
    164164            /// Creates a new object of the type of the assigned Identifier and dynamic_casts it to the minimal type given by T.
    165             T* fabricate(BaseObject* creator) const
    166             {
    167                 OrxonoxClass* newObject = this->identifier_->fabricate(creator);
     165            T* fabricate(Context* context) const
     166            {
     167                Identifiable* newObject = this->identifier_->fabricate(context);
    168168
    169169                // Check if the creation was successful
  • code/trunk/src/libraries/core/class/Super.h

    r9568 r9667  
    103103            { \
    104104                ClassIdentifier<T>* identifier = ClassIdentifier<T>::getIdentifier(); \
    105                 for (std::set<const Identifier*>::iterator it = identifier->getDirectChildrenIntern().begin(); it != identifier->getDirectChildrenIntern().end(); ++it) \
     105                for (std::set<const Identifier*>::iterator it = identifier->getDirectChildren().begin(); it != identifier->getDirectChildren().end(); ++it) \
    106106                { \
    107107                    if (((ClassIdentifier<T>*)(*it))->bSuperFunctionCaller_##functionname##_isFallback_ && ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_) \
     
    171171
    172172                // Iterate through all children
    173                 for (std::set<const Identifier*>::iterator it = identifier->getDirectChildrenIntern().begin(); it != identifier->getDirectChildrenIntern().end(); ++it)
     173                for (std::set<const Identifier*>::iterator it = identifier->getDirectChildren().begin(); it != identifier->getDirectChildren().end(); ++it)
    174174                {
    175175                    // Check if the caller is a fallback-caller
Note: See TracChangeset for help on using the changeset viewer.