[Orxonox-commit 6339] r10996 - in code/branches/cpp11_v2/src: libraries/network/synchronisable orxonox/graphics

landauf at orxonox.net landauf at orxonox.net
Wed Dec 30 11:59:18 CET 2015


Author: landauf
Date: 2015-12-30 11:59:18 +0100 (Wed, 30 Dec 2015)
New Revision: 10996

Modified:
   code/branches/cpp11_v2/src/libraries/network/synchronisable/Synchronisable.h
   code/branches/cpp11_v2/src/orxonox/graphics/Light.cc
   code/branches/cpp11_v2/src/orxonox/graphics/Light.h
Log:
using a strongly typed enum class for Light-Type.
added support for enum classes in Synchronisable. they are cast to the underlying type.

Modified: code/branches/cpp11_v2/src/libraries/network/synchronisable/Synchronisable.h
===================================================================
--- code/branches/cpp11_v2/src/libraries/network/synchronisable/Synchronisable.h	2015-12-30 10:41:01 UTC (rev 10995)
+++ code/branches/cpp11_v2/src/libraries/network/synchronisable/Synchronisable.h	2015-12-30 10:59:18 UTC (rev 10996)
@@ -37,6 +37,7 @@
 #include <map>
 #include <queue>
 #include <set>
+#include <type_traits>
 
 #include "util/mbool.h"
 #include "util/Output.h"
@@ -202,22 +203,35 @@
     static std::queue<uint32_t> deletedObjects_;
   };
 
-  template <class T> void Synchronisable::registerVariable(T& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
+  namespace detail
   {
+    template <class T, bool = std::is_enum<T>::value>
+    struct RealType;
+    template <class T>
+    struct RealType<T, true> { typedef typename std::underlying_type<T>::type type; };
+    template <class T>
+    struct RealType<T, false> { typedef T type; };
+  }
+
+  template <class T>
+  void Synchronisable::registerVariable(T& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
+  {
+    typedef typename detail::RealType<T>::type RealType;
     if (bidirectional)
     {
-      syncList_.push_back(new SynchronisableVariableBidirectional<T>(variable, mode, cb));
+      syncList_.push_back(new SynchronisableVariableBidirectional<RealType>(reinterpret_cast<RealType&>(variable), mode, cb));
       this->dataSize_ += syncList_.back()->getSize(state_);
     }
     else
     {
-      syncList_.push_back(new SynchronisableVariable<T>(variable, mode, cb));
+      syncList_.push_back(new SynchronisableVariable<RealType>(reinterpret_cast<RealType&>(variable), mode, cb));
       if ( this->state_ == mode )
         this->dataSize_ += syncList_.back()->getSize(state_);
     }
   }
   
-  template <class T> void Synchronisable::unregisterVariable(T& variable)
+  template <class T>
+  void Synchronisable::unregisterVariable(T& variable)
   {
     std::vector<SynchronisableVariableBase*>::iterator it = syncList_.begin();
     while(it!=syncList_.end())
@@ -237,13 +251,15 @@
     // the variable has not been registered before
   }
 
-  template <class T> void Synchronisable::registerVariable( std::set<T>& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
+  template <class T>
+  void Synchronisable::registerVariable( std::set<T>& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
   {
+    typedef typename detail::RealType<T>::type RealType;
     SynchronisableVariableBase* sv;
     if (bidirectional)
-      sv = new SynchronisableVariableBidirectional<std::set<T>>(variable, mode, cb);
+      sv = new SynchronisableVariableBidirectional<std::set<RealType>>(reinterpret_cast<std::set<RealType>&>(variable), mode, cb);
     else
-      sv = new SynchronisableVariable<std::set<T>>(variable, mode, cb);
+      sv = new SynchronisableVariable<std::set<RealType>>(reinterpret_cast<std::set<RealType>&>(variable), mode, cb);
     syncList_.push_back(sv);
     stringList_.push_back(sv);
   }

Modified: code/branches/cpp11_v2/src/orxonox/graphics/Light.cc
===================================================================
--- code/branches/cpp11_v2/src/orxonox/graphics/Light.cc	2015-12-30 10:41:01 UTC (rev 10995)
+++ code/branches/cpp11_v2/src/orxonox/graphics/Light.cc	2015-12-30 10:59:18 UTC (rev 10996)
@@ -43,9 +43,9 @@
     RegisterClass(Light);
 
     // Be sure we don't do bad conversions
-    static_assert((int)Ogre::Light::LT_POINT       == (int)Light::Point,       "check enum");
-    static_assert((int)Ogre::Light::LT_DIRECTIONAL == (int)Light::Directional, "check enum");
-    static_assert((int)Ogre::Light::LT_SPOTLIGHT   == (int)Light::Spotlight,   "check enum");
+    static_assert((int)Ogre::Light::LT_POINT       == (int)Light::Type::Point,       "check enum");
+    static_assert((int)Ogre::Light::LT_DIRECTIONAL == (int)Light::Type::Directional, "check enum");
+    static_assert((int)Ogre::Light::LT_SPOTLIGHT   == (int)Light::Type::Spotlight,   "check enum");
 
     Light::Light(Context* context) : StaticEntity(context)
     {
@@ -54,7 +54,7 @@
         this->light_ = nullptr;
         this->diffuse_ = ColourValue::White;
         this->specular_ = ColourValue::White;
-        this->type_ = Light::Point;
+        this->type_ = Type::Point;
         this->attenuation_ = Vector4(100000, 1, 0, 0);
         this->spotlightRange_ = Vector3(40.0f, 30.0f, 1.0f);
 
@@ -104,7 +104,7 @@
 
     void Light::registerVariables()
     {
-        registerVariable((int&)this->type_,     VariableDirection::ToClient, new NetworkCallback<Light>(this, &Light::updateType));
+        registerVariable(this->type_,           VariableDirection::ToClient, new NetworkCallback<Light>(this, &Light::updateType));
         registerVariable(this->diffuse_,        VariableDirection::ToClient, new NetworkCallback<Light>(this, &Light::updateDiffuseColour));
         registerVariable(this->specular_,       VariableDirection::ToClient, new NetworkCallback<Light>(this, &Light::updateSpecularColour));
         registerVariable(this->attenuation_,    VariableDirection::ToClient, new NetworkCallback<Light>(this, &Light::updateAttenuation));
@@ -125,37 +125,37 @@
 
     void Light::updateAttenuation()
     {
-        if (this->light_ && this->type_ != Light::Directional)
+        if (this->light_ && this->type_ != Type::Directional)
             this->light_->setAttenuation(this->attenuation_.x, this->attenuation_.y, this->attenuation_.z, this->attenuation_.w);
     }
 
     void Light::updateSpotlightRange()
     {
-        if (this->light_ && this->type_ == Light::Spotlight)
+        if (this->light_ && this->type_ == Type::Spotlight)
             this->light_->setSpotlightRange(Degree(this->spotlightRange_.x), Degree(this->spotlightRange_.y), this->spotlightRange_.z);
     }
 
     void Light::setTypeString(const std::string& type)
     {
         if (type == "point")
-            this->setType(Light::Point);
+            this->setType(Type::Point);
         else if (type == "directional")
-            this->setType(Light::Directional);
+            this->setType(Type::Directional);
         else if (type == "spotlight")
-            this->setType(Light::Spotlight);
+            this->setType(Type::Spotlight);
         else
-            this->setType(Light::Point);
+            this->setType(Type::Point);
     }
 
     std::string Light::getTypeString() const
     {
         switch (this->type_)
         {
-            case Light::Directional:
+            case Type::Directional:
                 return "directional";
-            case Light::Spotlight:
+            case Type::Spotlight:
                 return "spotlight";
-            case Light::Point:
+            case Type::Point:
             default:
                 return "point";
         }
@@ -167,9 +167,9 @@
         {
             this->light_->setType(static_cast<Ogre::Light::LightTypes>(this->type_));
 
-            if (this->type_ != Light::Directional)
+            if (this->type_ != Type::Directional)
                 this->updateAttenuation();
-            if (this->type_ == Light::Spotlight)
+            if (this->type_ == Type::Spotlight)
                 this->updateSpotlightRange();
         }
     }

Modified: code/branches/cpp11_v2/src/orxonox/graphics/Light.h
===================================================================
--- code/branches/cpp11_v2/src/orxonox/graphics/Light.h	2015-12-30 10:41:01 UTC (rev 10995)
+++ code/branches/cpp11_v2/src/orxonox/graphics/Light.h	2015-12-30 10:59:18 UTC (rev 10996)
@@ -41,7 +41,7 @@
     class _OrxonoxExport Light : public StaticEntity, public TeamColourable
     {
         public:
-            enum LightTypes // Copy from the Ogre enum
+            enum class Type // Copy from the Ogre enum
             {
                 /// Point light sources give off light equally in all directions, so require only position not direction
                 Point,
@@ -62,9 +62,9 @@
             inline Ogre::Light* getLight()
                 { return this->light_; }
 
-            inline void setType(Light::LightTypes type)
+            inline void setType(Light::Type type)
                 { this->type_ = type; this->updateType(); }
-            inline Light::LightTypes getType() const
+            inline Light::Type getType() const
                 { return this->type_; }
 
             inline void setDiffuseColour(const ColourValue& colour)
@@ -143,7 +143,7 @@
             void updateSpotlightRange();
 
             Ogre::Light* light_;
-            LightTypes type_;
+            Light::Type type_;
             ColourValue diffuse_;
             ColourValue specular_;
             Vector4 attenuation_;




More information about the Orxonox-commit mailing list