Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • 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
Note: See TracChangeset for help on using the changeset viewer.