[Orxonox-commit 811] r3332 - in trunk/src: core network/synchronisable util

rgrieder at orxonox.net rgrieder at orxonox.net
Tue Jul 21 12:54:15 CEST 2009


Author: rgrieder
Date: 2009-07-21 12:54:15 +0200 (Tue, 21 Jul 2009)
New Revision: 3332

Added:
   trunk/src/util/TypeTraits.h
Modified:
   trunk/src/core/Identifier.h
   trunk/src/network/synchronisable/SynchronisableVariable.h
   trunk/src/util/MultiType.h
   trunk/src/util/TemplateUtils.h
Log:
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 ;)

Modified: trunk/src/core/Identifier.h
===================================================================
--- trunk/src/core/Identifier.h	2009-07-21 10:08:59 UTC (rev 3331)
+++ trunk/src/core/Identifier.h	2009-07-21 10:54:15 UTC (rev 3332)
@@ -61,6 +61,7 @@
 #include <typeinfo>
 
 #include "util/Debug.h"
+#include "util/TypeTraits.h"
 #include "MetaObjectList.h"
 #include "ObjectList.h"
 #include "ObjectListBase.h"
@@ -482,32 +483,6 @@
     // ###############################
     // ###      orxonox_cast       ###
     // ###############################
-    //! Helper struct to have orxonox_cast<T*> instead of orxonox_cast<T>
-    template <class T, class U>
-    struct OrxonoxCaster
-    {
-        static T* cast(U* source)
-        {
-            // If you see this function in a compiler error description, it means
-            // you were misusing orxonox_cast. You must always cast to a pointer type!
-            *****T();
-        }
-    };
-
-    //! Helper struct to have orxonox_cast<T*> instead of orxonox_cast<T*>
-    template <class T, class U>
-    struct OrxonoxCaster<T*, U>
-    {
-        FORCEINLINE static T* cast(U* source)
-        {
-#ifdef ORXONOX_COMPILER_MSVC
-            return source->template getDerivedPointer<T>(ClassIdentifier<T>::getIdentifier()->getClassID());
-#else
-            return dynamic_cast<T*>(source);
-#endif
-        }
-    };
-
     /**
     @brief
         Casts on object of type OrxonoxClass to any derived type that is
@@ -522,7 +497,12 @@
     template <class T, class U>
     FORCEINLINE T orxonox_cast(U* source)
     {
-        return OrxonoxCaster<T, U>::cast(source);
+#ifdef ORXONOX_COMPILER_MSVC
+        typedef Loki::TypeTraits<T>::PointeeType ClassType;
+        return source->template getDerivedPointer<ClassType>(ClassIdentifier<ClassType>::getIdentifier()->getClassID());
+#else
+        return dynamic_cast<T>(source);
+#endif
     }
 
 

Modified: trunk/src/network/synchronisable/SynchronisableVariable.h
===================================================================
--- trunk/src/network/synchronisable/SynchronisableVariable.h	2009-07-21 10:08:59 UTC (rev 3331)
+++ trunk/src/network/synchronisable/SynchronisableVariable.h	2009-07-21 10:54:15 UTC (rev 3332)
@@ -35,7 +35,7 @@
 #include <cassert>
 #include <cstring>
 #include "util/Serialise.h"
-#include "util/TemplateUtils.h"
+#include "util/TypeTraits.h"
 #include "core/GameMode.h"
 #include "network/synchronisable/NetworkCallbackManager.h"
 
@@ -78,7 +78,7 @@
       virtual inline uint32_t getData(uint8_t*& mem, uint8_t mode);
       virtual inline void putData(uint8_t*& mem, uint8_t mode, bool forceCallback = false);
       virtual inline uint32_t getSize(uint8_t mode);
-      virtual inline void* getReference(){ return static_cast<void*>(const_cast<typename TypeStripper<T>::RawType*>(&this->variable_)); }
+      virtual inline void* getReference(){ return static_cast<void*>(const_cast<typename Loki::TypeTraits<T>::UnqualifiedType*>(&this->variable_)); }
     protected:
       
       T& variable_;
@@ -181,7 +181,7 @@
         if( this->varBuffer_ != this->variable_ )
         {
           this->varReference_++;
-          memcpy(static_cast<void*>(const_cast<typename TypeStripper<T>::RawType*>(&this->varBuffer_)), &this->variable_, sizeof(this->variable_));
+          memcpy(static_cast<void*>(const_cast<typename Loki::TypeTraits<T>::UnqualifiedType*>(&this->varBuffer_)), &this->variable_, sizeof(this->variable_));
         }
       }
   // write the reference number to the stream
@@ -214,7 +214,7 @@
           else
           {
             mem += sizeof(varReference_);
-            memcpy(static_cast<void*>(const_cast<typename TypeStripper<T>::RawType*>(&this->varBuffer_)), &this->variable_, sizeof(T));
+            memcpy(static_cast<void*>(const_cast<typename Loki::TypeTraits<T>::UnqualifiedType*>(&this->varBuffer_)), &this->variable_, sizeof(T));
             if ( this->callback_ != 0 )
               callback = true;
           }

Modified: trunk/src/util/MultiType.h
===================================================================
--- trunk/src/util/MultiType.h	2009-07-21 10:08:59 UTC (rev 3331)
+++ trunk/src/util/MultiType.h	2009-07-21 10:54:15 UTC (rev 3332)
@@ -76,7 +76,7 @@
 #include <OgreQuaternion.h>
 #include <OgreColourValue.h>
 
-#include "TemplateUtils.h"
+#include "TypeTraits.h"
 
 namespace orxonox
 {
@@ -302,7 +302,13 @@
             inline bool                                   setValue(const orxonox::Degree& value);
             inline bool                                   setValue(const char* value);
             /** @brief Assigns a pointer. */
-            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))); } }
+            template <typename V> inline bool setValue(V* value)
+            {
+                if (this->value_)
+                    return this->value_->setValue(static_cast<void*>(const_cast<typename Loki::TypeTraits<V>::UnqualifiedType*>(value)));
+                else
+                    return this->assignValue     (static_cast<void*>(const_cast<typename Loki::TypeTraits<V>::UnqualifiedType*>(value)));
+            }
             /** @brief Assigns the value of the other MultiType and converts it to the current type. */
             bool                                          setValue(const MultiType& other) { if (this->value_) { return this->value_->assimilate(other); } else { if (other.value_) { this->value_ = other.value_->clone(); } return true; } }
             /** @brief Changes the type to T and assigns the new value (which might be of another type than T - it gets converted). */
@@ -321,9 +327,9 @@
             /** @brief Current content gets overridden with default zero value */
             inline void                       resetValue()                    { if (this->value_) this->value_->reset(); }
 
-            template <typename T> inline void setType()                       { this->assignValue(typename TypeStripper<T>::RawType()); } /** @brief Resets the value and changes the internal type to T. */
-            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. */
-            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. */
+            template <typename T> inline void setType()                       { this->assignValue(typename Loki::TypeTraits<T>::UnqualifiedReferredType()); } /** @brief Resets the value and changes the internal type to T. */
+            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. */
+            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. */
 
             /** @brief Returns the current type. */
             inline MT_Type::Value             getType()                   const { return (this->value_) ? this->value_->type_ : MT_Type::Null; }

Modified: trunk/src/util/TemplateUtils.h
===================================================================
--- trunk/src/util/TemplateUtils.h	2009-07-21 10:08:59 UTC (rev 3331)
+++ trunk/src/util/TemplateUtils.h	2009-07-21 10:54:15 UTC (rev 3332)
@@ -38,20 +38,6 @@
 
 namespace orxonox
 {
-    /**
-    @brief
-        Use TypeStripper to get rid of the const and the reference of type T
-    @note
-        Main use of this is when trying to instantiate type T as T().
-    */
-    template <class T> struct TypeStripper
-        { typedef T RawType; };
-    template <class T> struct TypeStripper<const T>
-        { typedef T RawType; };
-    template <class T> struct TypeStripper<const T&>
-        { typedef T RawType; };
-
-
     ///////////////////////////////////////////////////
     // Static detection of implicit type conversions //
     ///////////////////////////////////////////////////

Added: trunk/src/util/TypeTraits.h
===================================================================
--- trunk/src/util/TypeTraits.h	                        (rev 0)
+++ trunk/src/util/TypeTraits.h	2009-07-21 10:54:15 UTC (rev 3332)
@@ -0,0 +1,285 @@
+////////////////////////////////////////////////////////////////////////////////
+// The Loki Library
+// Copyright (c) 2001 by Andrei Alexandrescu
+// This code accompanies the book:
+// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design 
+//     Patterns Applied". Copyright (c) 2001. Addison-Wesley.
+// Permission to use, copy, modify, distribute and sell this software for any 
+//     purpose is hereby granted without fee, provided that the above copyright 
+//     notice appear in all copies and that both that copyright notice and this 
+//     permission notice appear in supporting documentation.
+// The author or Addison-Wesley Longman make no representations about the 
+//     suitability of this software for any purpose. It is provided "as is" 
+//     without express or implied warranty.
+//
+// Changes by Orxonox (Reto)
+//     Removed all stdInt, etc. type traits and function pointer traits
+//     and added UnqualifiedReferredType.
+////////////////////////////////////////////////////////////////////////////////
+#ifndef LOKI_TYPETRAITS_INC_
+#define LOKI_TYPETRAITS_INC_
+
+// $Id: TypeTraits.h 835 2007-08-02 19:39:02Z syntheticpp $
+
+
+#if (defined _MSC_VER) && (_MSC_VER < 1400)
+#include <string>
+#endif
+
+
+#ifdef _MSC_VER
+#pragma warning( push ) 
+#pragma warning( disable : 4180 ) //qualifier applied to function type has no meaning; ignored
+#endif
+
+namespace Loki
+{
+////////////////////////////////////////////////////////////////////////////////
+// class NullType
+// Used as a placeholder for "no type here"
+// Useful as an end marker in typelists 
+////////////////////////////////////////////////////////////////////////////////
+
+    class NullType {};
+
+////////////////////////////////////////////////////////////////////////////////
+// class template IsCustomUnsignedInt
+// Offers a means to integrate nonstandard built-in unsigned integral types
+// (such as unsigned __int64 or unsigned long long int) with the TypeTraits 
+//     class template defined below.
+// Invocation: IsCustomUnsignedInt<T> where T is any type
+// Defines 'value', an enum that is 1 iff T is a custom built-in unsigned
+//     integral type
+// Specialize this class template for nonstandard unsigned integral types
+//     and define value = 1 in those specializations
+////////////////////////////////////////////////////////////////////////////////
+
+    template <typename T>
+    struct IsCustomUnsignedInt
+    {
+        enum { value = 0 };
+    };        
+
+////////////////////////////////////////////////////////////////////////////////
+// class template IsCustomSignedInt
+// Offers a means to integrate nonstandard built-in unsigned integral types
+// (such as unsigned __int64 or unsigned long long int) with the TypeTraits 
+//     class template defined below.
+// Invocation: IsCustomSignedInt<T> where T is any type
+// Defines 'value', an enum that is 1 iff T is a custom built-in signed
+//     integral type
+// Specialize this class template for nonstandard unsigned integral types
+//     and define value = 1 in those specializations
+////////////////////////////////////////////////////////////////////////////////
+
+    template <typename T>
+    struct IsCustomSignedInt
+    {
+        enum { value = 0 };
+    };        
+
+////////////////////////////////////////////////////////////////////////////////
+// class template IsCustomFloat
+// Offers a means to integrate nonstandard floating point types with the
+//     TypeTraits class template defined below.
+// Invocation: IsCustomFloat<T> where T is any type
+// Defines 'value', an enum that is 1 iff T is a custom built-in
+//     floating point type
+// Specialize this class template for nonstandard unsigned integral types
+//     and define value = 1 in those specializations
+////////////////////////////////////////////////////////////////////////////////
+
+    template <typename T>
+    struct IsCustomFloat
+    {
+        enum { value = 0 };
+    };        
+
+////////////////////////////////////////////////////////////////////////////////
+// Helper types for class template TypeTraits defined below
+////////////////////////////////////////////////////////////////////////////////
+
+    namespace Private
+    {
+        template <typename U> struct AddPointer
+        {
+            typedef U* Result;
+        };
+
+        template <typename U> struct AddPointer<U&>
+        {
+            typedef U* Result;
+        };
+
+        template <class U> struct AddReference
+        {
+            typedef U & Result;
+        };
+
+        template <class U> struct AddReference<U &>
+        {
+            typedef U & Result;
+        };
+
+        template <> struct AddReference<void>
+        {
+            typedef NullType Result;
+        };
+
+        template <class U> struct AddParameterType
+        {
+            typedef const U & Result;
+        };
+
+        template <class U> struct AddParameterType<U &>
+        {
+            typedef U & Result;
+        };
+
+        template <> struct AddParameterType<void>
+        {
+            typedef NullType Result;
+        };
+    }// namespace Private
+        
+////////////////////////////////////////////////////////////////////////////////
+// class template TypeTraits
+//
+// Figures out at compile time various properties of any given type
+// Invocations (T is a type, TypeTraits<T>::Property):
+//
+// - isPointer       : returns true if T is a pointer type
+// - PointeeType     : returns the type to which T points if T is a pointer 
+//                     type, NullType otherwise
+// - isReference     : returns true if T is a reference type
+// - ReferredType    : returns the type to which T refers if T is a reference 
+//                     type, NullType otherwise
+// - ParameterType   : returns the optimal type to be used as a parameter for 
+//                     functions that take Ts
+// - isConst         : returns true if T is a const-qualified type
+// - NonConstType    : Type with removed 'const' qualifier from T, if any
+// - isVolatile      : returns true if T is a volatile-qualified type
+// - NonVolatileType : Type with removed 'volatile' qualifier from T, if any
+// - UnqualifiedType : Type with removed 'const' and 'volatile' qualifiers from 
+//                     T, if any
+// - ParameterType   : returns the optimal type to be used as a parameter 
+//                       for functions that take 'const T's
+//
+////////////////////////////////////////////////////////////////////////////////
+
+    template <typename T>
+    class TypeTraits
+    {
+    private:
+    
+        template <class U> struct ReferenceTraits
+        {
+            enum { result = false };
+            typedef U ReferredType;
+        };
+        
+        template <class U> struct ReferenceTraits<U&>
+        {
+            enum { result = true };
+            typedef U ReferredType;
+        };
+               
+        template <class U> struct PointerTraits
+        {
+            enum { result = false };
+            typedef NullType PointeeType;
+        };
+        
+        template <class U> struct PointerTraits<U*>
+        {
+            enum { result = true };
+            typedef U PointeeType;
+        };
+        
+        template <class U> struct PointerTraits<U*&>
+        {
+            enum { result = true };
+            typedef U PointeeType;
+        };
+          
+        template <class U> struct PToMTraits
+        {
+            enum { result = false };
+        };
+        
+        template <class U, class V> struct PToMTraits<U V::*>
+        {
+            enum { result = true };
+        };
+        
+        template <class U, class V> struct PToMTraits<U V::*&>
+        {
+            enum { result = true };
+        };
+        
+        template <class U> struct UnConst
+        {
+            typedef U Result;
+            enum { isConst = 0 };
+        };
+        
+        template <class U> struct UnConst<const U>
+        {
+            typedef U Result;
+            enum { isConst = 1 };
+        };
+
+        template <class U> struct UnConst<const U&>
+        {
+            typedef U& Result;
+            enum { isConst = 1 };
+        };
+  
+        template <class U> struct UnVolatile
+        {
+            typedef U Result;
+            enum { isVolatile = 0 };
+        };
+       
+        template <class U> struct UnVolatile<volatile U>
+        {
+            typedef U Result;
+            enum { isVolatile = 1 };
+        };
+
+        template <class U> struct UnVolatile<volatile U&>
+        {
+            typedef U& Result;
+            enum { isVolatile = 1 };
+        };
+        
+    public:
+        typedef typename UnConst<T>::Result 
+            NonConstType;
+        typedef typename UnVolatile<T>::Result 
+            NonVolatileType;
+        typedef typename UnVolatile<typename UnConst<T>::Result>::Result 
+            UnqualifiedType;
+        typedef typename PointerTraits<UnqualifiedType>::PointeeType 
+            PointeeType;
+        typedef typename ReferenceTraits<T>::ReferredType 
+            ReferredType;
+        typedef typename ReferenceTraits<typename UnVolatile<typename UnConst<T>::Result>::Result>::ReferredType
+            UnqualifiedReferredType;
+
+        enum { isConst          = UnConst<T>::isConst };
+        enum { isVolatile       = UnVolatile<T>::isVolatile };
+        enum { isReference      = ReferenceTraits<UnqualifiedType>::result };
+        enum { isPointer        = PointerTraits<
+                                        typename ReferenceTraits<UnqualifiedType>::ReferredType >::result};
+                
+    };
+}
+
+#ifdef _MSC_VER
+#pragma warning( pop )
+#endif // _MSC_VER
+
+
+#endif // end file guardian
+


Property changes on: trunk/src/util/TypeTraits.h
___________________________________________________________________
Added: svn:eol-style
   + native




More information about the Orxonox-commit mailing list