Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3332


Ignore:
Timestamp:
Jul 21, 2009, 12:54:15 PM (15 years ago)
Author:
rgrieder
Message:

Added TypeTraits.h from the Loki library (stripped it a bit though). This should make things a bit less error prone since Andrei Alexandrescu really is the master of templates ;)

Location:
code/trunk/src
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/core/Identifier.h

    r3325 r3332  
    6262
    6363#include "util/Debug.h"
     64#include "util/TypeTraits.h"
    6465#include "MetaObjectList.h"
    6566#include "ObjectList.h"
     
    483484    // ###      orxonox_cast       ###
    484485    // ###############################
    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 
    511486    /**
    512487    @brief
     
    523498    FORCEINLINE T orxonox_cast(U* source)
    524499    {
    525         return OrxonoxCaster<T, U>::cast(source);
     500#ifdef ORXONOX_COMPILER_MSVC
     501        typedef Loki::TypeTraits<T>::PointeeType ClassType;
     502        return source->template getDerivedPointer<ClassType>(ClassIdentifier<ClassType>::getIdentifier()->getClassID());
     503#else
     504        return dynamic_cast<T>(source);
     505#endif
    526506    }
    527507
  • code/trunk/src/network/synchronisable/SynchronisableVariable.h

    r3304 r3332  
    3636#include <cstring>
    3737#include "util/Serialise.h"
    38 #include "util/TemplateUtils.h"
     38#include "util/TypeTraits.h"
    3939#include "core/GameMode.h"
    4040#include "network/synchronisable/NetworkCallbackManager.h"
     
    7979      virtual inline void putData(uint8_t*& mem, uint8_t mode, bool forceCallback = false);
    8080      virtual inline uint32_t getSize(uint8_t mode);
    81       virtual inline void* getReference(){ return static_cast<void*>(const_cast<typename TypeStripper<T>::RawType*>(&this->variable_)); }
     81      virtual inline void* getReference(){ return static_cast<void*>(const_cast<typename Loki::TypeTraits<T>::UnqualifiedType*>(&this->variable_)); }
    8282    protected:
    8383     
     
    182182        {
    183183          this->varReference_++;
    184           memcpy(static_cast<void*>(const_cast<typename TypeStripper<T>::RawType*>(&this->varBuffer_)), &this->variable_, sizeof(this->variable_));
     184          memcpy(static_cast<void*>(const_cast<typename Loki::TypeTraits<T>::UnqualifiedType*>(&this->varBuffer_)), &this->variable_, sizeof(this->variable_));
    185185        }
    186186      }
     
    215215          {
    216216            mem += sizeof(varReference_);
    217             memcpy(static_cast<void*>(const_cast<typename TypeStripper<T>::RawType*>(&this->varBuffer_)), &this->variable_, sizeof(T));
     217            memcpy(static_cast<void*>(const_cast<typename Loki::TypeTraits<T>::UnqualifiedType*>(&this->varBuffer_)), &this->variable_, sizeof(T));
    218218            if ( this->callback_ != 0 )
    219219              callback = true;
  • code/trunk/src/util/MultiType.h

    r3301 r3332  
    7777#include <OgreColourValue.h>
    7878
    79 #include "TemplateUtils.h"
     79#include "TypeTraits.h"
    8080
    8181namespace orxonox
     
    303303            inline bool                                   setValue(const char* value);
    304304            /** @brief Assigns a pointer. */
    305             template <typename V> inline bool             setValue(V* value)               { if (this->value_) { return this->value_->setValue(static_cast<void*>(const_cast<typename TypeStripper<V>::RawType*>(value))); } else { return this->assignValue(static_cast<void*>(const_cast<typename TypeStripper<V>::RawType*>(value))); } }
     305            template <typename V> inline bool setValue(V* value)
     306            {
     307                if (this->value_)
     308                    return this->value_->setValue(static_cast<void*>(const_cast<typename Loki::TypeTraits<V>::UnqualifiedType*>(value)));
     309                else
     310                    return this->assignValue     (static_cast<void*>(const_cast<typename Loki::TypeTraits<V>::UnqualifiedType*>(value)));
     311            }
    306312            /** @brief Assigns the value of the other MultiType and converts it to the current type. */
    307313            bool                                          setValue(const MultiType& other) { if (this->value_) { return this->value_->assimilate(other); } else { if (other.value_) { this->value_ = other.value_->clone(); } return true; } }
     
    322328            inline void                       resetValue()                    { if (this->value_) this->value_->reset(); }
    323329
    324             template <typename T> inline void setType()                       { this->assignValue(typename TypeStripper<T>::RawType()); } /** @brief Resets the value and changes the internal type to T. */
    325             inline void                       setType(const MultiType& other) { this->setType(other.getType());                         } /** @brief Resets the value and changes the internal type to the type of the other MultiType. */
    326             inline void                       setType(MT_Type::Value type)    { this->reset(); this->convert(type); this->resetValue(); } /** @brief Resets the value and changes the internal type to the given type. */
     330            template <typename T> inline void setType()                       { this->assignValue(typename Loki::TypeTraits<T>::UnqualifiedReferredType()); } /** @brief Resets the value and changes the internal type to T. */
     331            inline void                       setType(const MultiType& other) { this->setType(other.getType());                                             } /** @brief Resets the value and changes the internal type to the type of the other MultiType. */
     332            inline void                       setType(MT_Type::Value type)    { this->reset(); this->convert(type); this->resetValue();                     } /** @brief Resets the value and changes the internal type to the given type. */
    327333
    328334            /** @brief Returns the current type. */
  • code/trunk/src/util/TemplateUtils.h

    r3233 r3332  
    3939namespace orxonox
    4040{
    41     /**
    42     @brief
    43         Use TypeStripper to get rid of the const and the reference of type T
    44     @note
    45         Main use of this is when trying to instantiate type T as T().
    46     */
    47     template <class T> struct TypeStripper
    48         { typedef T RawType; };
    49     template <class T> struct TypeStripper<const T>
    50         { typedef T RawType; };
    51     template <class T> struct TypeStripper<const T&>
    52         { typedef T RawType; };
    53 
    54 
    5541    ///////////////////////////////////////////////////
    5642    // Static detection of implicit type conversions //
Note: See TracChangeset for help on using the changeset viewer.