Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3325 for code/trunk/src/core


Ignore:
Timestamp:
Jul 19, 2009, 3:48:00 PM (15 years ago)
Author:
rgrieder
Message:

Merged orxonox_cast related revisions from core4 back to trunk.

Location:
code/trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/core/CoreIncludes.h

    r3196 r3325  
    5656*/
    5757#define InternRegisterObject(ClassName, bRootClass) \
    58     this->setIdentifier(orxonox::ClassIdentifier<ClassName>::getIdentifier(#ClassName)); \
    59     if (orxonox::Identifier::isCreatingHierarchy()) \
    60     { \
    61         if (this->getParents()) \
    62         { \
    63             orxonox::ClassIdentifier<ClassName>::getIdentifier(#ClassName)->initializeClassHierarchy(this->getParents(), bRootClass); \
    64             this->getParents()->insert(this->getParents()->end(), this->getIdentifier()); \
    65         } \
    66         this->setConfigValues(); \
     58    if (ClassIdentifier<ClassName>::getIdentifier(#ClassName)->initialiseObject(this, #ClassName, bRootClass)) \
    6759        return; \
    68     } \
    69     orxonox::ClassIdentifier<ClassName>::getIdentifier()->addObject(this)
    70 
    71 /**
    72     @brief Intern macro, containing the specific part of RegisterRootObject.
    73     @param ClassName The name of the class
    74 */
    75 #define InternRegisterRootObject(ClassName) \
    76     if (orxonox::Identifier::isCreatingHierarchy() && !this->getParents()) \
    77         this->createParents(); \
    78     InternRegisterObject(ClassName, true)
     60    else \
     61        ((void)0)
    7962
    8063/**
     
    8366*/
    8467#define RegisterObject(ClassName) \
    85     COUT(5) << "*** Register Object: " << #ClassName << std::endl; \
    8668    InternRegisterObject(ClassName, false)
    8769
     
    9173*/
    9274#define RegisterRootObject(ClassName) \
    93     COUT(5) << "*** Register Root-Object: " << #ClassName << std::endl; \
    94     InternRegisterRootObject(ClassName)
     75    InternRegisterObject(ClassName, true)
    9576
    9677/**
     
    11596    orxonox::ClassIdentifier<ClassName>::getIdentifier()
    11697
    117 /**
    118     @brief Returns the Identifier with a given name through the factory.
    119     @param String The name of the class
    120 */
    121 #define ClassByString(String) \
    122     orxonox::Factory::getIdentifier(String)
    12398
    124 /**
    125     @brief Returns the Identifier with a given network ID through the factory.
    126     @param networkID The network ID of the class
    127 */
    128 #define ClassByID(networkID) \
    129     orxonox::Factory::getIdentifier(networkID)
     99namespace orxonox
     100{
     101    /**
     102        @brief Returns the Identifier with a given name through the factory.
     103        @param String The name of the class
     104    */
     105    inline Identifier* ClassByString(const std::string& name)
     106    {
     107        return Factory::getIdentifier(name);
     108    }
     109
     110    /**
     111        @brief Returns the Identifier with a given network ID through the factory.
     112        @param networkID The network ID of the class
     113    */
     114    inline Identifier* ClassByID(uint32_t id)
     115    {
     116        return Factory::getIdentifier(id);
     117    }
     118}
    130119
    131120#endif /* _CoreIncludes_H__ */
  • code/trunk/src/core/EventIncludes.h

    r3196 r3325  
    5454        this->addEventContainer(eventname, containername); \
    5555    } \
    56     event.castedOriginator_ = dynamic_cast<subclassname*>(event.originator_); \
     56    event.castedOriginator_ = orxonox::orxonox_cast<subclassname*>(event.originator_); \
    5757    containername->process(this, event)
    5858
     
    6666        this->addEventContainer(eventname, containername); \
    6767    } \
    68     event.castedOriginator_ = dynamic_cast<subclassname*>(event.originator_); \
     68    event.castedOriginator_ = orxonox::orxonox_cast<subclassname*>(event.originator_); \
    6969    containername->process(this, event)
    7070
  • code/trunk/src/core/Identifier.cc

    r3280 r3325  
    4747    // ###       Identifier        ###
    4848    // ###############################
    49     int Identifier::hierarchyCreatingCounter_s = 0; // Set the static member variable hierarchyCreatingCounter_s to zero (this static member variable is ok: it's used in main(), not before)
     49    int Identifier::hierarchyCreatingCounter_s = 0;
     50    unsigned int Identifier::classIDCounter_s = 0;
    5051
    5152    /**
     
    5354    */
    5455    Identifier::Identifier()
     56        : classID_(classIDCounter_s++)
    5557    {
    5658        this->objects_ = new ObjectListBase(this);
     
    6769        this->directChildren_ = new std::set<const Identifier*>();
    6870
    69         // Use a static variable because the classID gets created before main() and that's why we should avoid static member variables
    70         static unsigned int classIDcounter_s = 0;
    71         this->classID_ = classIDcounter_s++;
     71        // Default network ID is the class ID
     72        this->networkID_ = this->classID_;
    7273    }
    7374
     
    244245    void Identifier::setNetworkID(uint32_t id)
    245246    {
    246         Factory::changeNetworkID(this, this->classID_, id);
    247         this->classID_ = id;
     247        Factory::changeNetworkID(this, this->networkID_, id);
     248        this->networkID_ = id;
    248249    }
    249250
  • code/trunk/src/core/Identifier.h

    r3196 r3325  
    224224
    225225            /** @brief Returns the network ID to identify a class through the network. @return the network ID */
    226             inline const uint32_t getNetworkID() const { return this->classID_; }
     226            inline const uint32_t getNetworkID() const { return this->networkID_; }
    227227
    228228            /** @brief Sets the network ID to a new value. @param id The new value */
    229229            void setNetworkID(uint32_t id);
     230
     231            /** @brief Returns the unique ID of the class */
     232            FORCEINLINE unsigned int getClassID() const { return this->classID_; }
    230233
    231234            void addConfigValueContainer(const std::string& varname, ConfigValueContainer* container);
     
    305308            BaseFactory* factory_;                                         //!< The Factory, able to create new objects of the given class (if available)
    306309            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)
    307             uint32_t classID_;                                             //!< The network ID to identify a class through the network
     310            uint32_t networkID_;                                           //!< The network ID to identify a class through the network
     311            const unsigned int classID_;                                   //!< Uniquely identifies a class (might not be the same as the networkID_)
     312            static unsigned int classIDCounter_s;                          //!< Static counter for the unique classIDs
    308313
    309314            bool bHasConfigValues_;                                        //!< True if this class has at least one assigned config value
     
    344349            static ClassIdentifier<T> *getIdentifier();
    345350            static ClassIdentifier<T> *getIdentifier(const std::string& name);
    346             void addObject(T* object);
     351
     352            bool initialiseObject(T* object, const std::string& className, bool bRootClass);
    347353
    348354            void updateConfigValues(bool updateChildren = true) const;
     
    423429    */
    424430    template <class T>
    425     inline void ClassIdentifier<T>::addObject(T* object)
    426     {
    427         COUT(5) << "*** ClassIdentifier: Added object to " << this->getName() << "-list." << std::endl;
    428         object->getMetaList().add(this->objects_, this->objects_->add(new ObjectListElement<T>(object)));
     431    bool ClassIdentifier<T>::initialiseObject(T* object, const std::string& className, bool bRootClass)
     432    {
     433        if (bRootClass)
     434            COUT(5) << "*** Register Root-Object: " << className << std::endl;
     435        else
     436            COUT(5) << "*** Register Object: " << className << std::endl;
     437
     438        object->identifier_ = this;
     439        if (Identifier::isCreatingHierarchy())
     440        {
     441            if (bRootClass && !object->parents_)
     442                object->parents_ = new std::set<const Identifier*>();
     443
     444            if (object->parents_)
     445            {
     446                this->initializeClassHierarchy(object->parents_, bRootClass);
     447                object->parents_->insert(object->parents_->end(), this);
     448            }
     449
     450            object->setConfigValues();
     451            return true;
     452        }
     453        else
     454        {
     455            COUT(5) << "*** ClassIdentifier: Added object to " << this->getName() << "-list." << std::endl;
     456            object->metaList_->add(this->objects_, this->objects_->add(new ObjectListElement<T>(object)));
     457
     458            // Add pointer of type T to the map in the OrxonoxClass instance that enables "dynamic_casts"
     459            object->objectPointers_.push_back(std::make_pair(this->getClassID(), reinterpret_cast<void*>(object)));
     460            return false;
     461        }
    429462    }
    430463
     
    444477            for (std::set<const Identifier*>::const_iterator it = this->getChildrenBegin(); it != this->getChildrenEnd(); ++it)
    445478                (*it)->updateConfigValues(false);
     479    }
     480
     481
     482    // ###############################
     483    // ###      orxonox_cast       ###
     484    // ###############################
     485    //! Helper struct to have orxonox_cast<T*> instead of orxonox_cast<T>
     486    template <class T, class U>
     487    struct OrxonoxCaster
     488    {
     489        static T* cast(U* source)
     490        {
     491            // If you see this function in a compiler error description, it means
     492            // you were misusing orxonox_cast. You must always cast to a pointer type!
     493            *****T();
     494        }
     495    };
     496
     497    //! Helper struct to have orxonox_cast<T*> instead of orxonox_cast<T*>
     498    template <class T, class U>
     499    struct OrxonoxCaster<T*, U>
     500    {
     501        FORCEINLINE static T* cast(U* source)
     502        {
     503#ifdef ORXONOX_COMPILER_MSVC
     504            return source->template getDerivedPointer<T>(ClassIdentifier<T>::getIdentifier()->getClassID());
     505#else
     506            return dynamic_cast<T*>(source);
     507#endif
     508        }
     509    };
     510
     511    /**
     512    @brief
     513        Casts on object of type OrxonoxClass to any derived type that is
     514        registered in the class hierarchy.
     515    @return
     516        Returns NULL if the cast is not possible
     517    @note
     518        In case of NULL return (and using MSVC), a dynamic_cast might still be possible if
     519        a class forgot to register its objects.
     520        Also note that the function is implemented differently for GCC/MSVC.
     521    */
     522    template <class T, class U>
     523    FORCEINLINE T orxonox_cast(U* source)
     524    {
     525        return OrxonoxCaster<T, U>::cast(source);
    446526    }
    447527
     
    539619                if (newObject)
    540620                {
    541                     return dynamic_cast<T*>(newObject);
     621                    return orxonox_cast<T*>(newObject);
    542622                }
    543623                else
  • code/trunk/src/core/Iterator.h

    r3196 r3325  
    240240            {
    241241                if (this->element_)
    242                     return dynamic_cast<T*>(this->element_->objectBase_);
     242                    return orxonox_cast<T*>(this->element_->objectBase_);
    243243                else
    244244                    return 0;
     
    252252            {
    253253                if (this->element_)
    254                     return dynamic_cast<T*>(this->element_->objectBase_);
     254                    return orxonox_cast<T*>(this->element_->objectBase_);
    255255                else
    256256                    return 0;
  • code/trunk/src/core/OrxonoxClass.h

    r3280 r3325  
    5050    class _CoreExport OrxonoxClass
    5151    {
     52        template <class T>
     53        friend class ClassIdentifier;
     54
    5255        public:
    5356            OrxonoxClass();
     
    5962            /** @brief Returns the Identifier of the object. @return The Identifier */
    6063            inline Identifier* getIdentifier() const { return this->identifier_; }
    61 
    62             /** @brief Sets the Identifier of the object. Used by the RegisterObject-macro. */
    63             inline void setIdentifier(Identifier* identifier) { this->identifier_ = identifier; }
    64 
    65             /** @brief Returns the list of all parents of the object. @return The list */
    66             inline std::set<const Identifier*>* getParents() const { return this->parents_; }
    67 
    68             /** @brief Creates the parents-list. */
    69             inline void createParents() { this->parents_ = new std::set<const Identifier*>(); }
    70 
    71             /** @brief Returns the MetaObjectList of the object, containing a link to all ObjectLists and ObjectListElements the object is registered in. @return The list */
    72             inline MetaObjectList& getMetaList() { return (*this->metaList_); }
    73 
    7464
    7565            bool isA(const Identifier* identifier);
     
    10191            bool isDirectParentOf(const OrxonoxClass* object);
    10292
     93            /**
     94            @brief
     95                Returns a valid pointer of any derived type that is
     96                registered in the class hierarchy.
     97            @return
     98                Returns NULL if the no pointer was found.
     99            */
     100            template <class T>
     101            FORCEINLINE T* getDerivedPointer(unsigned int classID) const
     102            {
     103                for (int i = this->objectPointers_.size() - 1; i >= 0; --i)
     104                {
     105                    if (this->objectPointers_[i].first == classID)
     106                        return reinterpret_cast<T*>(this->objectPointers_[i].second);
     107                }
     108                return NULL;
     109            }
     110
    103111        private:
    104112            Identifier* identifier_;                   //!< The Identifier of the object
    105113            std::set<const Identifier*>* parents_;     //!< List of all parents of the object
    106114            MetaObjectList* metaList_;                 //!< MetaObjectList, containing all ObjectLists and ObjectListElements the object is registered in
     115            //! 'Fast map' that holds this-pointers of all derived types
     116            std::vector<std::pair<unsigned int, void*> > objectPointers_;
    107117    };
    108118}
  • code/trunk/src/core/Super.h

    r3301 r3325  
    206206
    207207    // SUPER-macro: Calls Parent::functionname() where Parent is the direct parent of classname
    208     #define SUPER(classname, functionname, ...) \
    209         SUPER_##functionname(classname, functionname, __VA_ARGS__)
     208    #ifdef ORXONOX_COMPILER_MSVC
     209        #define SUPER(classname, functionname, ...) \
     210            __super::functionname(__VA_ARGS__)
     211    #else
     212        #define SUPER(classname, functionname, ...) \
     213            SUPER_##functionname(classname, functionname, __VA_ARGS__)
     214    #endif
    210215
    211216    // helper macro: for functions without arguments
  • code/trunk/src/core/XMLPort.h

    r3301 r3325  
    565565                                                    newObject->setLoaderIndentation(object->getLoaderIndentation() + "  ");
    566566
    567                                                     O* castedObject = dynamic_cast<O*>(newObject);
     567                                                    O* castedObject = orxonox_cast<O*>(newObject);
    568568                                                    assert(castedObject);
    569569
Note: See TracChangeset for help on using the changeset viewer.