Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 365


Ignore:
Timestamp:
Dec 1, 2007, 4:24:56 AM (16 years ago)
Author:
landauf
Message:

added comments

Location:
code/branches/objecthierarchy/src/orxonox/core
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • code/branches/objecthierarchy/src/orxonox/core/ClassFactory.h

    r258 r365  
     1/*!
     2    @file ClassFactory.h
     3    @brief Definition of the ClassFactory class
     4
     5    The ClassFactory is able to create new objects of a specific class.
     6*/
     7
    18#ifndef _ClassFactory_H__
    29#define _ClassFactory_H__
     
    916    // ###      ClassFactory       ###
    1017    // ###############################
     18    //! The ClassFactory is able to create new objects of a specific class.
    1119    template <class T>
    1220    class ClassFactory : public BaseFactory
     
    1725
    1826        private:
    19             ClassFactory() {}
    20             ClassFactory(const ClassFactory& factory) {}
    21             ~ClassFactory() {}
     27            ClassFactory() {}                               // Don't create
     28            ClassFactory(const ClassFactory& factory) {}    // Don't copy
     29            ~ClassFactory() {}                              // Don't delete
    2230
    2331            static T* createNewObject();
    2432    };
    2533
     34    /**
     35        @brief Adds the ClassFactory to the Identifier of the same type and creates a new object to retrieve the parents.
     36        @return True, because the compiler only allows assignments before main()
     37    */
    2638    template <class T>
    2739    bool ClassFactory<T>::create()
    2840    {
     41        // Add the ClassFactory to the Classidentifier of type T
    2942        ClassIdentifier<T>::getIdentifier()->addFactory(new ClassFactory<T>);
    3043
     44        // To create the new branch of the class-hierarchy, we create a new object and delete it afterwards.
    3145        ClassIdentifier<T>::getIdentifier()->startCreatingHierarchy();
    3246#if HIERARCHY_VERBOSE
     
    4054    }
    4155
     56    /**
     57        @brief Creates and returns a new object of class T.
     58        @return The new object
     59    */
    4260    template <class T>
    4361    BaseObject* ClassFactory<T>::fabricate()
     
    4664    }
    4765
     66    /**
     67        @brief Creates and returns a new object of class T; this is a wrapper for the new operator.
     68        @return The new object
     69    */
    4870    template <class T>
    4971    T* ClassFactory<T>::createNewObject()
  • code/branches/objecthierarchy/src/orxonox/core/Factory.cc

    r362 r365  
     1/*!
     2    @file Factory.cc
     3    @brief Implementation of the Factory class.
     4*/
     5
    16#include "Factory.h"
    27#include "Identifier.h"
     
    49namespace orxonox
    510{
    6     Factory* Factory::pointer_s = NULL;
     11    Factory* Factory::pointer_s = NULL; // Set the static member variable pointer_s to zero
    712
     13    /**
     14        @returns the Identifier with a given name.
     15        @param name The name of the wanted Identifier
     16    */
    817    Identifier* Factory::getIdentifier(const std::string& name)
    918    {
     
    1423    }
    1524
     25    /**
     26        @returns the Identifier with a given networkID.
     27        @param id The networkID of the wanted Identifier
     28    */
    1629    Identifier* Factory::getIdentifier(const unsigned int id)
    1730    {
     
    2235    }
    2336
     37    /**
     38        @brief Adds a new Identifier to both maps.
     39        @param name The name of the identifier
     40        @param identifier The identifier to add
     41    */
    2442    void Factory::add(const std::string& name, Identifier* identifier)
    2543    {
     
    3149    }
    3250
     51    /**
     52        @brief Removes the entry with the old networkID and adds a new one.
     53        @param identifier The identifier to change
     54        @param oldID The old networkID
     55        @param newID The new networkID
     56    */
    3357    void Factory::changeNetworkID(Identifier* identifier, const unsigned int oldID, const unsigned int newID)
    3458    {
  • code/branches/objecthierarchy/src/orxonox/core/Factory.h

    r362 r365  
     1/*!
     2    @file Factory.h
     3    @brief Definition of the Factory and the BaseFactory class.
     4
     5    The Factory is a singleton, containing two maps to map either the name or the networkID
     6    of a class with the corresponding Identifier.
     7
     8    Usage:
     9    ID(classname) or ID(networkID) returns the corresponding Identifier.
     10
     11
     12    BaseObject is the parent of ClassFactory which is defined in ClassFactory.h.
     13    It can't be defined in ClassFactory.h, because of circular dependencies.
     14*/
     15
    116#ifndef _Factory_H__
    217#define _Factory_H__
     
    722namespace orxonox
    823{
    9     class BaseObject;
    10     class Identifier;
     24    class BaseObject; // Forward declaration
     25    class Identifier; // Forward declaration
    1126
    1227    // ###############################
    1328    // ###         Factory         ###
    1429    // ###############################
     30    //! The Factory is used to map name or networkID of a class with its Identifier.
    1531    class Factory
    1632    {
     
    2238
    2339        private:
    24             Factory() {}
    25             Factory(const Factory& factory) {}
    26             ~Factory() {}
     40            Factory() {}                        // don't create
     41            Factory(const Factory& factory) {}  // don't copy
     42            ~Factory() {}                       // don't delete
    2743
    28             static Factory* pointer_s;
    29             std::map<std::string, Identifier*> identifierStringMap_;
    30             std::map<unsigned int, Identifier*> identifierNetworkIDMap_;
     44            static Factory* pointer_s;                                          //!< The pointer to the singleton
     45            std::map<std::string, Identifier*> identifierStringMap_;            //!< The map mapping string with Identifier
     46            std::map<unsigned int, Identifier*> identifierNetworkIDMap_;        //!< The map mapping networkID with Identifier
    3147    };
    3248
     
    3450    // ###       BaseFactory       ###
    3551    // ###############################
     52    //! Base-class of ClassFactory. Has to be defined separate because of circular dependencies.
    3653    class BaseFactory
    3754    {
  • code/branches/objecthierarchy/src/orxonox/core/Identifier.cc

    r362 r365  
     1/*!
     2    @file Identifier.cc
     3    @brief Implementation of the Identifier class.
     4*/
     5
    16#include "Identifier.h"
    27
     
    611    // ###       Identifier        ###
    712    // ###############################
    8     int Identifier::hierarchyCreatingCounter_s = 0;
    9     unsigned int Identifier::classIDcounter_s = 0;
     13    int Identifier::hierarchyCreatingCounter_s = 0; // Set the static member variable hierarchyCreatingCounter_s to zero
     14    unsigned int Identifier::classIDcounter_s = 0; // Set the static member variable classIDcounter_s to zero
    1015
     16    /**
     17        @brief Constructor: No factory, no object created, new ObjectList and a unique networkID.
     18    */
    1119    Identifier::Identifier()
    1220    {
     
    1826    }
    1927
     28    /**
     29        @brief Destructor: Deletes the name and the IdentifierList containing the children.
     30    */
    2031    Identifier::~Identifier()
    2132    {
    2233        delete &this->name_;
    23 
    2434        delete this->children_;
    2535    }
    2636
     37    /**
     38        @brief Initializes the Identifier with an IdentifierList containing all parents of the class the Identifier belongs to.
     39        @param parents The IdentifierList containing all parents
     40    */
    2741    void Identifier::initialize(const IdentifierList* parents)
    2842    {
     
    3852            {
    3953                this->parents_.add(temp1->identifier_);
    40                 temp1->identifier_->getChildren().add(this);
     54                temp1->identifier_->getChildren().add(this); // We're a child of our parents
    4155
    4256                temp1 = temp1->next_;
     
    4559    }
    4660
     61    /**
     62        @brief Creates an object of the type the Identifier belongs to.
     63        @return The new object
     64    */
    4765    BaseObject* Identifier::fabricate()
    4866    {
    4967        if (this->factory_)
    5068        {
    51             return this->factory_->fabricate();
     69            return this->factory_->fabricate(); // We have to return a BaseObject, because we don't know the exact type.
    5270        }
    5371        else
    5472        {
     73            // Abstract classes don't have a factory and therefore can't create new objects
    5574            std::cout << "Error: Cannot create an object of type '" << this->name_ << "'. Class is abstract.\n";
    5675            std::cout << "Aborting...";
     
    5978    }
    6079
     80    /**
     81        @brief Sets the networkID to a new value and changes the entry in the Factory.
     82        @param id The new networkID
     83    */
    6184    void Identifier::setNetworkID(unsigned int id)
    6285    {
     
    6588    }
    6689
     90    /**
     91        @returns true, if the Identifier is at least of the given type.
     92        @param identifier The identifier to compare with
     93    */
    6794    bool Identifier::isA(const Identifier* identifier) const
    6895    {
     
    7097    }
    7198
     99    /**
     100        @returns true, if the Identifier is exactly of the given type.
     101        @param identifier The identifier to compare with
     102    */
    72103    bool Identifier::isDirectlyA(const Identifier* identifier) const
    73104    {
     
    75106    }
    76107
     108    /**
     109        @returns true, if the assigned identifier is a child of the given identifier.
     110        @param identifier The identifier to compare with
     111    */
    77112    bool Identifier::isChildOf(const Identifier* identifier) const
    78113    {
     
    80115    }
    81116
     117    /**
     118        @returns true, if the assigned identifier is a parent of the given identifier.
     119        @param identifier The identifier to compare with
     120    */
    82121    bool Identifier::isParentOf(const Identifier* identifier) const
    83122    {
  • code/branches/objecthierarchy/src/orxonox/core/Identifier.h

    r362 r365  
     1/*!
     2    @file Identifier.h
     3    @brief Definition of the Identifier, ClassIdentifier and SubclassIdentifier classes.
     4
     5    The Identifier contains all needed informations about the class it belongs to:
     6     - the name
     7     - a list with all objects
     8     - parents and childs
     9     - the factory, if available
     10     - the networkID that can be synchronised with the server
     11
     12    Every object has a pointer to the Identifier of its class. This allows the use isA(...),
     13    isDirectlyA(...), isChildOf(...) and isParentOf(...).
     14
     15    To create the class-hierarchy, the Identifier has some intern functions and variables.
     16
     17    Every Identifier is in fact a ClassIdentifier, but they are derived from Identifier.
     18
     19    SubclassIdentifier is a separated class, acting like an Identifier, but has a given class.
     20    You can only assign Identifiers of the given class or a derivative to a SubclassIdentifier.
     21*/
     22
    123#ifndef _Identifier_H__
    224#define _Identifier_H__
     
    1335namespace orxonox
    1436{
    15     class BaseObject;
     37    class BaseObject; // Forward declaration
    1638
    1739    // ###############################
    1840    // ###       Identifier        ###
    1941    // ###############################
     42    //! The Identifier is used to identify the class of an object and to store informations about the class.
     43    /**
     44        The Identifier contains all needed informations about the class it belongs to:
     45         - the name
     46         - a list with all objects
     47         - parents and childs
     48         - the factory, if available
     49         - the networkID that can be synchronised with the server
     50
     51        Every object has a pointer to the Identifier of its class. This allows the use isA(...),
     52        isDirectlyA(...), isChildOf(...) and isParentOf(...).
     53
     54        You can't directly create an Identifier, it's just the base-class for ClassIdentifier.
     55    */
    2056    class Identifier
    2157    {
    2258        template <class T>
    23         friend class ClassIdentifier;
     59        friend class ClassIdentifier; // Forward declaration
    2460
    2561        template <class T>
    26         friend class SubclassIdentifier;
     62        friend class SubclassIdentifier; // Forward declaration
    2763
    2864        template <class T>
    29         friend class ClassFactory;
     65        friend class ClassFactory; // Forward declaration
    3066
    3167        public:
     68            /** @brief Sets the Factory. @param facotry The factory to assign */
    3269            inline void addFactory(BaseFactory* factory) { this->factory_ = factory; }
     70
    3371            BaseObject* fabricate();
    3472
     
    3876            bool isParentOf(const Identifier* identifier) const;
    3977
     78            /** @returns the name of the class the Identifier belongs to. */
    4079            inline const std::string& getName() const { return this->name_; }
     80
     81            /** @returns the parents of the class the Identifier belongs to. */
    4182            inline const IdentifierList& getParents() const { return this->parents_; }
     83
     84            /** @returns the children of the class the Identifier belongs to. */
    4285            inline IdentifierList& getChildren() const { return *this->children_; }
    4386
     87            /** @returns true, if a branch of the class-hierarchy is getting created, causing all new objects to store their parents. */
    4488            inline static bool isCreatingHierarchy() { return (hierarchyCreatingCounter_s > 0); }
    4589
     90            /** @returns the NetworkID to identify a class through the network. */
    4691            inline const unsigned int getNetworkID() const { return this->classID_; }
     92
    4793            void setNetworkID(unsigned int id);
    4894
    4995        private:
    5096            Identifier();
    51             Identifier(const Identifier& identifier) {}
     97            Identifier(const Identifier& identifier) {} // don't copy
    5298            virtual ~Identifier();
    5399            void initialize(const IdentifierList* parents);
    54100
     101            /**
     102                @brief Increases the hierarchyCreatingCounter_s variable, causing all new objects to store their parents.
     103            */
    55104            inline static void startCreatingHierarchy()
    56105            {
     
    61110            }
    62111
     112            /**
     113                @brief Decreases the hierarchyCreatingCounter_s variable, causing the objects to stop storing their parents.
     114            */
    63115            inline static void stopCreatingHierarchy()
    64116            {
     
    69121            }
    70122
    71             IdentifierList parents_;
    72             IdentifierList* children_;
    73 
    74             std::string name_;
    75 
    76             BaseFactory* factory_;
    77             bool bCreatedOneObject_;
    78             static int hierarchyCreatingCounter_s;
    79             static unsigned int classIDcounter_s;
    80             unsigned int classID_;
     123            IdentifierList parents_;                    //!< The Parents of the class the Identifier belongs to
     124            IdentifierList* children_;                  //!< The Children of the class the Identifier belongs to
     125
     126            std::string name_;                          //!< The name of the class the Identifier belongs to
     127
     128            BaseFactory* factory_;                      //!< The Factory, able to create new objects of the given class
     129            bool bCreatedOneObject_;                    //!< True if at least one object of the given type was created (used to determine the need of storing the parents)
     130            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)
     131            static unsigned int classIDcounter_s;       //!< The number of unique Identifiers
     132            unsigned int classID_;                      //!< The networkID to identify a class through the network
    81133    };
    82134
     
    85137    // ###     ClassIdentifier     ###
    86138    // ###############################
     139    //! The ClassIdentifier is derived from Identifier and holds all class-specific functions and variables the Identifier cannot have.
     140    /**
     141        ClassIdentifier is a Singleton, which means that only one object of a given type T exists.
     142        This makes it possible to store informations about a class, sharing them with all
     143        objects of that class without defining static variables in every class.
     144    */
    87145    template <class T>
    88146    class ClassIdentifier : public Identifier
     
    95153        private:
    96154            ClassIdentifier();
    97             ClassIdentifier(const ClassIdentifier<T>& identifier) {}
     155            ClassIdentifier(const ClassIdentifier<T>& identifier) {} // don't copy
    98156            ~ClassIdentifier();
    99157
    100             static ClassIdentifier<T>* pointer_s;
    101             ObjectList<T>* objects_;
     158            static ClassIdentifier<T>* pointer_s;       //!< A pointer to the singleton-object
     159            ObjectList<T>* objects_;                    //!< The ObjectList, containing all objects of type T
    102160    };
    103161
    104162    template <class T>
    105     ClassIdentifier<T>* ClassIdentifier<T>::pointer_s = NULL;
    106 
     163    ClassIdentifier<T>* ClassIdentifier<T>::pointer_s = NULL; // Set the static member variable pointer_s to zero
     164
     165    /**
     166        @brief Constructor: Create the ObjectList.
     167    */
    107168    template <class T>
    108169    ClassIdentifier<T>::ClassIdentifier()
     
    111172    }
    112173
     174    /**
     175        @brief Destructor: Delete the ObjectList, set the singleton-pointer to zero.
     176    */
    113177    template <class T>
    114178    ClassIdentifier<T>::~ClassIdentifier()
     
    118182    }
    119183
     184    /**
     185        @brief Registers a class, which means that the name and the parents get stored.
     186        @param parents An IdentifierList, containing the Identifiers of all parents of the class
     187        @param name A string, containing exactly the name of the class
     188        @param bRootClass True if the class is either an Interface or BaseObject itself
     189        @return The ClassIdentifier itself
     190    */
    120191    template <class T>
    121192    ClassIdentifier<T>* ClassIdentifier<T>::registerClass(const IdentifierList* parents, const std::string& name, bool bRootClass)
     
    124195        std::cout << "*** Register Class in " << name << "-Singleton.\n";
    125196#endif
     197
     198        // It's a singleton, so maybe we have to create it first
    126199        if (!pointer_s)
    127200        {
     
    132205        }
    133206
     207        // Check if at least one object of the given type was created
    134208        if (!pointer_s->bCreatedOneObject_)
    135209        {
     210            // If no: We have to store the informations and initialize the Identifier
     211
    136212#if HIERARCHY_VERBOSE
    137213            std::cout << "*** Register Class in " << name << "-Singleton -> Initialize Singleton.\n";
    138214#endif
    139215            pointer_s->name_ = name;
    140             Factory::add(name, pointer_s);
     216            Factory::add(name, pointer_s); // Add the Identifier to the Factory
    141217
    142218            if (bRootClass)
    143                 pointer_s->initialize(NULL);
     219                pointer_s->initialize(NULL); // If a class is derived from two interfaces, the second interface might think it's derived from the first because of the order of constructor-calls. Thats why we set parents to zero in that case.
    144220            else
    145221                pointer_s->initialize(parents);
     
    149225    }
    150226
     227    /**
     228        @returns the Identifier itself
     229    */
    151230    template <class T>
    152231    ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier()
     
    163242    }
    164243
     244    /**
     245        @brief Adds an object of the given type to the ObjectList.
     246        @param object The object to add
     247    */
    165248    template <class T>
    166249    void ClassIdentifier<T>::addObject(T* object)
     
    169252        std::cout << "*** Added object to " << ClassIdentifier<T>::getIdentifier()->getName() << "-list.\n";
    170253#endif
    171         object->getMetaList()->add(ClassIdentifier<T>::getIdentifier()->objects_, ClassIdentifier<T>::getIdentifier()->objects_->add(object));
     254        object->getMetaList().add(ClassIdentifier<T>::getIdentifier()->objects_, ClassIdentifier<T>::getIdentifier()->objects_->add(object));
    172255    }
    173256
     
    176259    // ###   SubclassIdentifier    ###
    177260    // ###############################
    178     template <class B>
     261    //! The SubclassIdentifier acts almost like an Identifier, but has some prerequisites.
     262    /**
     263        You can only assign Identifiers that belong to a class of at least B (or derived) to a SubclassIdentifier<T>.
     264        If you assign something else, the program aborts.
     265        Because we know the minimal type, a dynamic_cast is done, which makes it easier to create a new object.
     266    */
     267    template <class T>
    179268    class SubclassIdentifier
    180269    {
    181270        public:
    182             SubclassIdentifier();
    183 
    184             SubclassIdentifier<B>& operator=(Identifier* identifier)
    185             {
    186                 if (!identifier->isA(ClassIdentifier<B>::getIdentifier()))
     271            /**
     272                @brief Constructor: Automaticaly assigns the Identifier of the given class.
     273            */
     274            SubclassIdentifier()
     275            {
     276                this->identifier_ = ClassIdentifier<T>::getIdentifier();
     277            }
     278
     279            /**
     280                @brief Overloading of the = operator: assigns the identifier and checks its type.
     281                @param identifier The Identifier to assign
     282                @return The SubclassIdentifier itself
     283            */
     284            SubclassIdentifier<T>& operator=(Identifier* identifier)
     285            {
     286                if (!identifier->isA(ClassIdentifier<T>::getIdentifier()))
    187287                {
    188                     std::cout << "Error: Class " << identifier->getName() << " is not a " << ClassIdentifier<B>::getIdentifier()->getName() << "!\n";
    189                     std::cout << "Error: SubclassIdentifier<" << ClassIdentifier<B>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden.\n";
     288                    std::cout << "Error: Class " << identifier->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << "!\n";
     289                    std::cout << "Error: SubclassIdentifier<" << ClassIdentifier<T>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden.\n";
    190290                    std::cout << "Aborting...\n";
    191291                    abort();
     
    195295            }
    196296
     297            /**
     298                @brief Overloading of the * operator: returns the assigned identifier.
     299                @return The assigned identifier
     300            */
    197301            Identifier* operator*()
    198302            {
     
    200304            }
    201305
     306            /**
     307                @brief Overloading of the -> operator: returns the assigned identifier.
     308                @return The assigned identifier
     309            */
    202310            Identifier* operator->() const
    203311            {
     
    205313            }
    206314
    207             B* fabricate()
     315            /**
     316                @brief Creates a new object of the type of the assigned identifier and dynamic_casts it to the minimal type given by the SubclassIdentifier.
     317                @return The new object
     318            */
     319            T* fabricate()
    208320            {
    209321                BaseObject* newObject = this->identifier_->fabricate();
     322
     323                // Check if the creation worked
    210324                if (newObject)
    211325                {
    212                     return dynamic_cast<B*>(newObject);
     326                    // Do a dynamic_cast, because an object of type T is much better than of type BaseObject
     327                    return dynamic_cast<T*>(newObject);
    213328                }
    214329                else
    215330                {
     331                    // Something went terribly wrong
    216332                    if (this->identifier_)
    217333                    {
    218                         std::cout << "Error: Class " << this->identifier_->getName() << " is not a " << ClassIdentifier<B>::getIdentifier()->getName() << "!\n";
     334                        std::cout << "Error: Class " << this->identifier_->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << "!\n";
    219335                        std::cout << "Error: Couldn't fabricate a new Object.\n";
    220336                        std::cout << "Aborting...\n";
     
    230346            }
    231347
     348            /** @returns the assigned identifier. */
    232349            inline const Identifier* getIdentifier() const
    233350                { return this->identifier_; }
     351
     352            /** @returns true, if the assigned identifier is at least of the given type. @param identifier The identifier to compare with */
    234353            inline bool isA(const Identifier* identifier) const
    235354                { return this->identifier_->isA(identifier); }
     355
     356            /** @returns true, if the assigned identifier is exactly of the given type. @param identifier The identifier to compare with */
    236357            inline bool isDirectlyA(const Identifier* identifier) const
    237358                { return this->identifier_->isDirectlyA(identifier); }
     359
     360            /** @returns true, if the assigned identifier is a child of the given identifier. @param identifier The identifier to compare with */
    238361            inline bool isChildOf(const Identifier* identifier) const
    239362                { return this->identifier_->isChildOf(identifier); }
     363
     364            /** @returns true, if the assigned identifier is a parent of the given identifier. @param identifier The identifier to compare with */
    240365            inline bool isParentOf(const Identifier* identifier) const
    241366                { return this->identifier_->isParentOf(identifier); }
    242367
    243368        private:
    244             Identifier* identifier_;
     369            Identifier* identifier_;        //!< The assigned identifier
    245370    };
    246 
    247     template <class B>
    248     SubclassIdentifier<B>::SubclassIdentifier()
    249     {
    250         this->identifier_ = ClassIdentifier<B>::getIdentifier();
    251     }
    252371}
    253372
  • code/branches/objecthierarchy/src/orxonox/core/IdentifierIncludes.h

    r362 r365  
     1/**
     2    @file IDentifierIncludes.h
     3    @brief Definition of macros for the class-hierarchy and the factory.
     4
     5    Every class needs the RegisterObject(class) macro in its constructor. If the class is an interface
     6    or the BaseObject itself, it needs the macro RegisterRootObject(class) instead.
     7
     8    To allow the object being created through the factory, use the CreateFactory(class) macro outside
     9    the of the class implementation, so it gets executed before main().
     10*/
     11
     12// All needed header-files
    113#include "Identifier.h"
    214#include "Factory.h"
     
    618
    719
     20// Intern macro, containing the common parts of RegisterObject and RegisterRootObject
    821#define InternRegisterObject(ClassName, bRootClass) \
    922    this->setIdentifier(ClassIdentifier<ClassName>::registerClass(this->getParents(), #ClassName, bRootClass)); \
     
    1225    ClassIdentifier<ClassName>::addObject(this)
    1326
     27// Intern macro, containing the specific part of RegisterRootObject
    1428#define InternRegisterRootObject(ClassName) \
    1529    if (Identifier::isCreatingHierarchy() && !this->getParents()) \
     
    1731    InternRegisterObject(ClassName, true)
    1832
     33// RegisterObject - with and without debug output
    1934#if HIERARCHY_VERBOSE
    2035#define RegisterObject(ClassName) \
     
    2641#endif
    2742
     43// RegisterRootObject - with and without debug output
    2844#if HIERARCHY_VERBOSE
    2945#define RegisterRootObject(ClassName) \
     
    3551#endif
    3652
     53// Class(ClassName) returns the Identifier of the given class
    3754#define Class(ClassName) \
    3855    ClassIdentifier<ClassName>::getIdentifier()
    3956
     57// Creates the entry in the Factory
    4058#define CreateFactory(ClassName) \
    4159    bool bCreated##ClassName##Factory = ClassFactory<ClassName>::create()
    4260
    43 #define ID(NameOrID) \
    44     Factory::getIdentifier(NameOrID)
     61// ID(StringOrInt) returns the Identifier with either a given name or a given NetworkID through the factory
     62#define ID(StringOrInt) \
     63    Factory::getIdentifier(StringOrInt)
  • code/branches/objecthierarchy/src/orxonox/core/IdentifierList.cc

    r258 r365  
     1/*!
     2    @file IdentifierList.cc
     3    @brief Implementation of the IdentifierList class.
     4*/
     5
    16#include "IdentifierList.h"
    27#include "Identifier.h"
     
    712    // ###     IdentifierList      ###
    813    // ###############################
     14    /**
     15        @brief Constructor: Sets first_ to zero.
     16    */
    917    IdentifierList::IdentifierList()
    1018    {
     
    1220    }
    1321
     22    /**
     23        @brief Destructor: Deletes all elements in the list, but NOT THE IDENTIFIERS.
     24    */
    1425    IdentifierList::~IdentifierList()
    1526    {
     
    2334    }
    2435
     36    /**
     37        @brief Adds an Identifier to the list.
     38        @param identifier The Identifier to add
     39    */
    2540    void IdentifierList::add(const Identifier* identifier)
    2641    {
     
    3045    }
    3146
     47    /**
     48        @brief Removes an Identifier from the list.
     49        @param identifier The Identifier to remove
     50    */
    3251    void IdentifierList::remove(const Identifier* identifier)
    3352    {
     
    3554            return;
    3655
     56        // Check if we have to delete the first element
    3757        if (this->first_->identifier_ == identifier)
    3858        {
     
    4464        }
    4565
     66        // Iterate through the list
    4667        IdentifierListElement* temp = this->first_;
    4768        while (temp->next_)
     
    6081    }
    6182
     83    /**
     84        @brief Checks if a given Identifier is in the list and returns true if yes.
     85        @param identifier The Identifier to check
     86        @return True if the Identifier is in the list
     87    */
    6288    bool IdentifierList::isInList(const Identifier* identifier) const
    6389    {
     
    74100    }
    75101
     102    /**
     103        @returns a string, containing the names of all Identifiers in the list.
     104    */
    76105    std::string IdentifierList::toString() const
    77106    {
     
    94123    // ###  IdentifierListElement  ###
    95124    // ###############################
     125    /**
     126        @brief Constructor: Creates the list-element with a given identifier.
     127        @param identifier The Identifier to store
     128    */
    96129    IdentifierListElement::IdentifierListElement(const Identifier* identifier)
    97130    {
     
    99132        this->next_ = 0;
    100133    }
    101 
    102     IdentifierListElement::~IdentifierListElement()
    103     {
    104     }
    105134}
  • code/branches/objecthierarchy/src/orxonox/core/IdentifierList.h

    r258 r365  
    11#ifndef _IdentifierList_H__
     2/*!
     3    @file IdentifierList.h
     4    @brief Definition of the IdentifierList class
     5
     6    The IdentifierList is a single-linked list, containing Identifiers.
     7    The IdentifierList is used to store parents and childs of each Identifier.
     8*/
     9
    210#define _IdentifierList_H__
    311
     
    614namespace orxonox
    715{
    8     class Identifier;
     16    class Identifier; // Forward declaration
    917
     18    //! The list-element of the IdentifierList
    1019    class IdentifierListElement
    1120    {
    1221        public:
    1322            IdentifierListElement(const Identifier* identifier);
    14             ~IdentifierListElement();
    1523
    16             const Identifier* identifier_;
    17             IdentifierListElement* next_;
     24            const Identifier* identifier_;      //!< The identifier
     25            IdentifierListElement* next_;       //!< The next element in the list
    1826    };
    1927
     28    //! The IdentifierList contains Identifiers
    2029    class IdentifierList
    2130    {
     
    2837            std::string toString() const;
    2938
    30             IdentifierListElement* first_;
     39            IdentifierListElement* first_;      //!< The first element in the list
    3140    };
    3241}
  • code/branches/objecthierarchy/src/orxonox/core/Iterator.h

    r258 r365  
     1/*!
     2    @file Iterator.h
     3    @brief Definition of the Iterator class.
     4
     5    The Iterator of a given class allows to iterate through an ObjectList, containing all objects of that type.
     6    This is the only way to access the objects in an ObjectList.
     7
     8    Usage:
     9    for (Iterator<class> it = ObjectList<class>::start(); it != 0; ++it)
     10    {
     11        it->someFunction(...);
     12        class* myObject = *it;
     13    }
     14
     15    Warning: Don't delete an object directly through the iterator.
     16*/
     17
    118#ifndef _Iterator_H__
    219#define _Iterator_H__
     
    421namespace orxonox
    522{
     23    //! The iterator allows to iterate through an ObjectList of a given class.
    624    template <class T>
    725    class Iterator
    826    {
    927        public:
     28            /**
     29                @brief Constructor: Sets the element whereon the iterator points to zero.
     30            */
    1031            Iterator()
    1132            {
     
    1334            }
    1435
     36            /**
     37                @brief Constructor: Sets the element whereon the iterator points to a given element.
     38                @param element The element to start with
     39            */
    1540            Iterator(ObjectListElement<T>* element)
    1641            {
     
    1843            }
    1944
     45            /**
     46                @brief Overloading of the ++it operator: Iterator iterates to the next object in the list.
     47                @return The Iterator itself
     48            */
    2049            Iterator<T> operator++()
    2150            {
     
    2453            }
    2554
     55            /**
     56                @brief Overloading of the --it operator: Iterator iterates to the previous object in the list.
     57                @return The Iterator itself
     58            */
    2659            Iterator<T> operator--()
    2760            {
     
    3063            }
    3164
     65            /**
     66                @brief Overloading of the *it operator: returns the pointer to the object.
     67                @return The object the Iterator points at
     68            */
    3269            T* operator*()
    3370            {
     
    3572            }
    3673
     74            /**
     75                @brief Overloading of the it-> operator: returns the pointer to the object.
     76                @return The object the Iterator points at
     77            */
    3778            T* operator->() const
    3879            {
     
    4182            }
    4283
     84            /**
     85                @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
     86                @return True if the iterator points to an existing object.
     87            */
    4388            operator bool()
    4489            {
     
    4691            }
    4792
     93            /**
     94                @brief Overloading of the (it != int) operator: Used for (it != 0) instead of typecast-operator to bool.
     95                @param compare The integer (must be zero, everything else makes no sense).
     96                @return True if the iterator points to an existing object.
     97            */
    4898            bool operator!=(int compare)
    4999            {
     100                // Comparing with something except zero makes no sense
    50101                if (compare != 0)
    51102                    std::cout << "Warning: Comparing the " << ClassIdentifier<T>::getIdentifier()->getName() << "-List-Iterator with " << compare << " has no effect. Only comparison with 0 works.\n";
     
    55106
    56107        private:
    57             ObjectListElement<T>* element_;
     108            ObjectListElement<T>* element_;     //!< The element the Iterator points to
    58109    };
    59110}
  • code/branches/objecthierarchy/src/orxonox/core/MetaObjectList.cc

    r258 r365  
     1/*!
     2    @file MetaObjectList.cc
     3    @brief Implementation of the MetaObjectList class.
     4*/
     5
    16#include "MetaObjectList.h"
    27
    38namespace orxonox
    49{
     10    /**
     11        @brief Constructor: Sets first_ to zero.
     12    */
    513    MetaObjectList::MetaObjectList()
    614    {
     
    816    }
    917
     18    /**
     19        @brief Destructor: Removes all elements from the list, causing them to remove the stored ObjectListElement from the ObjectList.
     20    */
    1021    MetaObjectList::~MetaObjectList()
    1122    {
  • code/branches/objecthierarchy/src/orxonox/core/MetaObjectList.h

    r258 r365  
     1/*!
     2    @file MetaObjectList.h
     3    @brief Definition of the MetaObjectList class.
     4
     5    The MetaObjectList is a single-linked list, containing all list-elements and their
     6    lists wherein the object that owns the MetaObjectList is registered.
     7    This allows much faster deleting of objects.
     8*/
     9
    110#ifndef _MetaObjectList_H__
    211#define _MetaObjectList_H__
     
    716namespace orxonox
    817{
     18    //! Base-class of MetaObjectListElement, because those is a template
    919    class BaseMetaObjectListElement
    1020    {
    1121        public:
     22            /** @brief Defaultdestructor */
    1223            virtual ~BaseMetaObjectListElement() {};
    1324
    14             BaseMetaObjectListElement* next_;
     25            BaseMetaObjectListElement* next_;       //!< The next Element in the list
    1526    };
    1627
     
    1829    // ###  MetaObjectListElement  ###
    1930    // ###############################
     31    //! The list-element of the MetaObjectList
    2032    template <class T>
    2133    class MetaObjectListElement : public BaseMetaObjectListElement
     
    2537            ~MetaObjectListElement();
    2638
    27             ObjectListElement<T>* element_;
    28             ObjectList<T>* list_;
     39            ObjectListElement<T>* element_;         //!< The list element, containing the object
     40            ObjectList<T>* list_;                   //!< The list, containing the element
    2941    };
    3042
     43    /**
     44        @brief Constructor: Creates the list-element with given list and element.
     45    */
    3146    template <class T>
    3247    MetaObjectListElement<T>::MetaObjectListElement(ObjectList<T>* list, ObjectListElement<T>* element)
     
    3752    }
    3853
     54    /**
     55        @brief Destructor: Removes the ObjectListElement from the ObjectList by linking next_ and prev_ of the ObjectListElement.
     56    */
    3957    template <class T>
    4058    MetaObjectListElement<T>::~MetaObjectListElement()
     
    4361            this->element_->next_->prev_ = this->element_->prev_;
    4462        else
    45             this->list_->last_ = this->element_->prev_;
     63            this->list_->last_ = this->element_->prev_; // If there is no next_, we deleted the last object and have to update the last_ pointer of the list
    4664
    4765        if (this->element_->prev_)
    4866            this->element_->prev_->next_ = this->element_->next_;
    4967        else
    50             this->list_->first_ = this->element_->next_;
     68            this->list_->first_ = this->element_->next_; // If there is no prev_, we deleted the first object and have to update the first_ pointer of the list
    5169
    5270
     
    6179    // ###       ObjectList        ###
    6280    // ###############################
     81    //!  The MetaObjectList contains ObjectListElements and their ObjectLists.
     82    /**
     83        The MetaObjectList is a single-linked list, containing all list-elements and their
     84        lists wherein the object that owns the MetaObjectList is registered.
     85        This allows much faster deleting of objects.
     86    */
    6387    class MetaObjectList
    6488    {
     
    6993            void add(ObjectList<T>* list, ObjectListElement<T>* element);
    7094
    71             BaseMetaObjectListElement* first_;
     95            BaseMetaObjectListElement* first_;      //!< The first element in the list
    7296    };
    7397
     98    /**
     99        @brief Adds an ObjectList and an element of that list to the MetaObjectList.
     100        @param list The ObjectList wherein the element is
     101        @param element The element wherein the object is
     102    */
    74103    template <class T>
    75104    void MetaObjectList::add(ObjectList<T>* list, ObjectListElement<T>* element)
  • code/branches/objecthierarchy/src/orxonox/core/ObjectList.h

    r258 r365  
     1/*!
     2    @file ObjectList.h
     3    @brief Definition of the ObjectList class.
     4
     5    The ObjectList is a double-linked list, used by Identifiers to store all objects of a specific class in it.
     6    Created objects are added through the RegisterObject-macro in its constructor.
     7    Use Iterator<class> to iterate through all objects of the class.
     8*/
     9
    110#ifndef _ObjectList_H__
    211#define _ObjectList_H__
     
    413namespace orxonox
    514{
    6     class OrxonoxClass;
     15    class OrxonoxClass; // Forward declaration
    716
    817    // ###############################
    918    // ###    ObjectListElement    ###
    1019    // ###############################
     20    //! The list-element of the ObjectList
    1121    template <class T>
    1222    class ObjectListElement
     
    1424        public:
    1525            ObjectListElement(T* object);
    16             ~ObjectListElement();
    17 
    18             T* object_;
    19             ObjectListElement* next_;
    20             ObjectListElement* prev_;
     26
     27            T* object_;                     //!< The object
     28            ObjectListElement* next_;       //!< The next element in the list
     29            ObjectListElement* prev_;       //!< The previous element in the list
    2130    };
    2231
     32    /**
     33        @brief Constructor: Creates the list-element with an object.
     34        @param Object The object to store
     35    */
    2336    template <class T>
    2437    ObjectListElement<T>::ObjectListElement(T* object)
     
    2942    }
    3043
    31     template <class T>
    32     ObjectListElement<T>::~ObjectListElement()
    33     {
    34     }
    35 
    3644
    3745    // ###############################
     
    3947    // ###############################
    4048    template <class T>
    41     class Iterator;
    42 
     49    class Iterator; // Forward declaration
     50
     51    //! The ObjectList contains all objects of a specific class.
     52    /**
     53        The ObjectList is used by Identifiers to store all objects of a specific class in it.
     54        Use Iterator<class> to iterate through all objects in the list.
     55    */
    4356    template <class T>
    4457    class ObjectList
     
    4861            ~ObjectList();
    4962            ObjectListElement<T>* add(T* object);
    50             void remove(OrxonoxClass* object, bool bIterateForwards = true);
    51 
     63//            void remove(OrxonoxClass* object, bool bIterateForwards = true);
     64
     65            /** @returns the first element in the list */
    5266            inline static Iterator<T> start()
    5367                { return Iterator<T>(pointer_s->first_); }
     68
     69            /** @returns the last element in the list */
    5470            inline static Iterator<T> end()
    5571                { return Iterator<T>(pointer_s->last_); }
    5672
    57             ObjectListElement<T>* first_;
    58             ObjectListElement<T>* last_;
     73            ObjectListElement<T>* first_;       //!< The first element in the list
     74            ObjectListElement<T>* last_;        //!< The last element in the list
    5975
    6076        private:
    61             static ObjectList<T>* pointer_s;
     77            static ObjectList<T>* pointer_s;    //!< A static pointer to the last created list (different for all T)
    6278    };
    6379
    6480    template <class T>
    65     ObjectList<T>* ObjectList<T>::pointer_s = 0;
    66 
     81    ObjectList<T>* ObjectList<T>::pointer_s = 0; // Set the static member variable pointer_s to zero
     82
     83    /**
     84        @brief Constructor: Sets first_ and last_ to zero and the static member variable pointer_s to _this_
     85    */
    6786    template <class T>
    6887    ObjectList<T>::ObjectList()
     
    7190        this->last_ = 0;
    7291
     92        // ObjectLists are only created by Identifiers and therefore only one ObjectList of each T will exist.
     93        // Thats why pointer_s is in fact a pointer to the only ObjectList for a type, which makes it almost a singleton.
    7394        this->pointer_s = this;
    7495    }
    7596
     97    /**
     98        @brief Destructor: Deletes all list-elements, but NOT THE OBJECTS.
     99    */
    76100    template <class T>
    77101    ObjectList<T>::~ObjectList()
     
    86110    }
    87111
     112    /**
     113        @brief Adds a new object to the end of the list.
     114        @param object The object to add
     115        @return The pointer to the new ObjectListElement, needed by the MetaObjectList of the added object
     116    */
    88117    template <class T>
    89118    ObjectListElement<T>* ObjectList<T>::add(T* object)
     
    91120        if (!this->last_)
    92121        {
     122            // If the list is empty
    93123            this->last_ = new ObjectListElement<T>(object);
    94             this->first_ = this->last_;
     124            this->first_ = this->last_; // There's only one object in the list now
    95125        }
    96126        else
    97127        {
     128            // If the list isn't empty
    98129            ObjectListElement<T>* temp = this->last_;
    99130            this->last_ = new ObjectListElement<T>(object);
     
    105136    }
    106137
     138
     139//    /**
     140//        @brief Removes an object from the list.
     141//        @param object The object to remove
     142//        @param bIterateForwards If true: Start searching the object at the beginning of the list
     143//    */
     144    /*
    107145    template <class T>
    108146    void ObjectList<T>::remove(OrxonoxClass* object, bool bIterateForwards)
     
    111149            return;
    112150
     151        // If there's only one object in the list, we have to set first_ and last_ to zero
    113152        if (this->first_ == this->last_)
    114153        {
     
    123162        }
    124163
     164        // Now we are sure we have more than one element in the list
    125165        if (bIterateForwards)
    126166        {
     167            // Start at the beginning of the list
     168
     169            // Check if it's the first object
    127170            if (this->first_->object_ == object)
    128171            {
     
    135178            }
    136179
     180            // Iterate through the whole list
    137181            ObjectListElement<T>* temp = this->first_;
    138182            while (temp->next_)
     
    146190                        temp2->prev_ = temp;
    147191                    else
    148                         this->last_ = temp;
     192                        this->last_ = temp; // If there is no next_, we deleted the last element and have to update the last_ pointer.
    149193
    150194                    return;
     
    156200        else
    157201        {
     202            // Start at the end of the list
     203
     204            // Check if it's the last object
    158205            if (this->last_->object_ == object)
    159206            {
     
    166213            }
    167214
     215            // Iterate through the whole list
    168216            ObjectListElement<T>* temp = this->last_;
    169217            while (temp->prev_)
     
    177225                        temp2->next_ = temp;
    178226                    else
    179                         this->first_ = temp;
     227                        this->first_ = temp; // If there is no prev_, we deleted the first element and have to update the first_ pointer.
    180228
    181229                    return;
     
    186234        }
    187235    }
     236    */
    188237}
    189238
  • code/branches/objecthierarchy/src/orxonox/core/OrxonoxClass.cc

    r258 r365  
     1/*!
     2    @file OrxonoxClass.cc
     3    @brief Implementation of the OrxonoxClass Class.
     4*/
     5
    16#include "OrxonoxClass.h"
    27
    38namespace orxonox
    49{
     10    /** @brief Constructor: Sets identifier_ and parents_ to zero. */
    511    OrxonoxClass::OrxonoxClass()
    612    {
    713        this->identifier_ = 0;
    814        this->parents_ = 0;
    9 
    10         this->metaList_ = new MetaObjectList;
    1115    }
    1216
     17    /** @brief Destructor: Deletes, if existing, the list of the parents. */
    1318    OrxonoxClass::~OrxonoxClass()
    1419    {
     20        // parents_ exists only if isCreatingHierarchy() of the associated Identifier returned true while creating the class
    1521        if (this->parents_)
    1622            delete this->parents_;
    17 
    18         delete this->metaList_;
    1923    }
    2024}
  • code/branches/objecthierarchy/src/orxonox/core/OrxonoxClass.h

    r258 r365  
     1/*!
     2    @file OrxonoxClass.h
     3    @brief Definition of the OrxonoxClass Class.
     4
     5    All objects and interfaces of the game-logic are derived from OrxonoxClass.
     6    It stores the Identifier and the MetaObjectList and has all needed functions to create the class-hierarchy.
     7*/
     8
    19#ifndef _OrxonoxClass_H__
    210#define _OrxonoxClass_H__
     
    917namespace orxonox
    1018{
     19    //! The class all objects and interfaces of the game-logic are derived from.
     20    /**
     21        BaseObject and Interaces are derived with 'virtual public OrxonoxClass' from OrxonoxClass.
     22        OrxonoxClass is needed to create the class-hierarchy at startup and to store the Identifier and the MetaObjectList.
     23    */
    1124    class OrxonoxClass
    1225    {
     
    1427            OrxonoxClass();
    1528            virtual ~OrxonoxClass();
     29
     30            /** @returns the Identifier of the object */
    1631            inline Identifier* getIdentifier() const { return this->identifier_; }
     32
     33            /** @brief Sets the Identifier of the object. Used by the RegisterObject-macro. */
    1734            inline void setIdentifier(Identifier* identifier) { this->identifier_ = identifier; }
     35
     36            /** @returns the list of all parents of the object */
    1837            inline IdentifierList* getParents() const { return this->parents_; }
     38
     39            /** @brief Sets the Parents of the object. Used by the RegisterObject-macro. */
    1940            inline void setParents(IdentifierList* parents) { this->parents_ = parents; }
    20             inline MetaObjectList* getMetaList() { return this->metaList_; }
     41
     42            /** @returns the MetaObjectList of the object, containing a link to all ObjectLists and ObjectListElements the object is registered in. */
     43            inline MetaObjectList& getMetaList() { return this->metaList_; }
    2144
    2245        private:
    23             Identifier* identifier_;
    24             IdentifierList* parents_;
    25             MetaObjectList* metaList_;
     46            Identifier* identifier_;        //!< The Identifier of the object
     47            IdentifierList* parents_;       //!< List of all parents of the object
     48            MetaObjectList metaList_;       //!< MetaObjectList, containing all ObjectLists and ObjectListElements the object is registered in
    2649    };
    2750}
Note: See TracChangeset for help on using the changeset viewer.