Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10996


Ignore:
Timestamp:
Dec 30, 2015, 11:59:18 AM (9 years ago)
Author:
landauf
Message:

using a strongly typed enum class for Light-Type.
added support for enum classes in Synchronisable. they are cast to the underlying type.

Location:
code/branches/cpp11_v2/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/cpp11_v2/src/libraries/network/synchronisable/Synchronisable.h

    r10769 r10996  
    3838#include <queue>
    3939#include <set>
     40#include <type_traits>
    4041
    4142#include "util/mbool.h"
     
    203204  };
    204205
    205   template <class T> void Synchronisable::registerVariable(T& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
    206   {
     206  namespace detail
     207  {
     208    template <class T, bool = std::is_enum<T>::value>
     209    struct RealType;
     210    template <class T>
     211    struct RealType<T, true> { typedef typename std::underlying_type<T>::type type; };
     212    template <class T>
     213    struct RealType<T, false> { typedef T type; };
     214  }
     215
     216  template <class T>
     217  void Synchronisable::registerVariable(T& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
     218  {
     219    typedef typename detail::RealType<T>::type RealType;
    207220    if (bidirectional)
    208221    {
    209       syncList_.push_back(new SynchronisableVariableBidirectional<T>(variable, mode, cb));
     222      syncList_.push_back(new SynchronisableVariableBidirectional<RealType>(reinterpret_cast<RealType&>(variable), mode, cb));
    210223      this->dataSize_ += syncList_.back()->getSize(state_);
    211224    }
    212225    else
    213226    {
    214       syncList_.push_back(new SynchronisableVariable<T>(variable, mode, cb));
     227      syncList_.push_back(new SynchronisableVariable<RealType>(reinterpret_cast<RealType&>(variable), mode, cb));
    215228      if ( this->state_ == mode )
    216229        this->dataSize_ += syncList_.back()->getSize(state_);
     
    218231  }
    219232 
    220   template <class T> void Synchronisable::unregisterVariable(T& variable)
     233  template <class T>
     234  void Synchronisable::unregisterVariable(T& variable)
    221235  {
    222236    std::vector<SynchronisableVariableBase*>::iterator it = syncList_.begin();
     
    238252  }
    239253
    240   template <class T> void Synchronisable::registerVariable( std::set<T>& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
    241   {
     254  template <class T>
     255  void Synchronisable::registerVariable( std::set<T>& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
     256  {
     257    typedef typename detail::RealType<T>::type RealType;
    242258    SynchronisableVariableBase* sv;
    243259    if (bidirectional)
    244       sv = new SynchronisableVariableBidirectional<std::set<T>>(variable, mode, cb);
     260      sv = new SynchronisableVariableBidirectional<std::set<RealType>>(reinterpret_cast<std::set<RealType>&>(variable), mode, cb);
    245261    else
    246       sv = new SynchronisableVariable<std::set<T>>(variable, mode, cb);
     262      sv = new SynchronisableVariable<std::set<RealType>>(reinterpret_cast<std::set<RealType>&>(variable), mode, cb);
    247263    syncList_.push_back(sv);
    248264    stringList_.push_back(sv);
  • code/branches/cpp11_v2/src/orxonox/graphics/Light.cc

    r10774 r10996  
    4444
    4545    // Be sure we don't do bad conversions
    46     static_assert((int)Ogre::Light::LT_POINT       == (int)Light::Point,       "check enum");
    47     static_assert((int)Ogre::Light::LT_DIRECTIONAL == (int)Light::Directional, "check enum");
    48     static_assert((int)Ogre::Light::LT_SPOTLIGHT   == (int)Light::Spotlight,   "check enum");
     46    static_assert((int)Ogre::Light::LT_POINT       == (int)Light::Type::Point,       "check enum");
     47    static_assert((int)Ogre::Light::LT_DIRECTIONAL == (int)Light::Type::Directional, "check enum");
     48    static_assert((int)Ogre::Light::LT_SPOTLIGHT   == (int)Light::Type::Spotlight,   "check enum");
    4949
    5050    Light::Light(Context* context) : StaticEntity(context)
     
    5555        this->diffuse_ = ColourValue::White;
    5656        this->specular_ = ColourValue::White;
    57         this->type_ = Light::Point;
     57        this->type_ = Type::Point;
    5858        this->attenuation_ = Vector4(100000, 1, 0, 0);
    5959        this->spotlightRange_ = Vector3(40.0f, 30.0f, 1.0f);
     
    105105    void Light::registerVariables()
    106106    {
    107         registerVariable((int&)this->type_,     VariableDirection::ToClient, new NetworkCallback<Light>(this, &Light::updateType));
     107        registerVariable(this->type_,           VariableDirection::ToClient, new NetworkCallback<Light>(this, &Light::updateType));
    108108        registerVariable(this->diffuse_,        VariableDirection::ToClient, new NetworkCallback<Light>(this, &Light::updateDiffuseColour));
    109109        registerVariable(this->specular_,       VariableDirection::ToClient, new NetworkCallback<Light>(this, &Light::updateSpecularColour));
     
    126126    void Light::updateAttenuation()
    127127    {
    128         if (this->light_ && this->type_ != Light::Directional)
     128        if (this->light_ && this->type_ != Type::Directional)
    129129            this->light_->setAttenuation(this->attenuation_.x, this->attenuation_.y, this->attenuation_.z, this->attenuation_.w);
    130130    }
     
    132132    void Light::updateSpotlightRange()
    133133    {
    134         if (this->light_ && this->type_ == Light::Spotlight)
     134        if (this->light_ && this->type_ == Type::Spotlight)
    135135            this->light_->setSpotlightRange(Degree(this->spotlightRange_.x), Degree(this->spotlightRange_.y), this->spotlightRange_.z);
    136136    }
     
    139139    {
    140140        if (type == "point")
    141             this->setType(Light::Point);
     141            this->setType(Type::Point);
    142142        else if (type == "directional")
    143             this->setType(Light::Directional);
     143            this->setType(Type::Directional);
    144144        else if (type == "spotlight")
    145             this->setType(Light::Spotlight);
     145            this->setType(Type::Spotlight);
    146146        else
    147             this->setType(Light::Point);
     147            this->setType(Type::Point);
    148148    }
    149149
     
    152152        switch (this->type_)
    153153        {
    154             case Light::Directional:
     154            case Type::Directional:
    155155                return "directional";
    156             case Light::Spotlight:
     156            case Type::Spotlight:
    157157                return "spotlight";
    158             case Light::Point:
     158            case Type::Point:
    159159            default:
    160160                return "point";
     
    168168            this->light_->setType(static_cast<Ogre::Light::LightTypes>(this->type_));
    169169
    170             if (this->type_ != Light::Directional)
     170            if (this->type_ != Type::Directional)
    171171                this->updateAttenuation();
    172             if (this->type_ == Light::Spotlight)
     172            if (this->type_ == Type::Spotlight)
    173173                this->updateSpotlightRange();
    174174        }
  • code/branches/cpp11_v2/src/orxonox/graphics/Light.h

    r10817 r10996  
    4242    {
    4343        public:
    44             enum LightTypes // Copy from the Ogre enum
     44            enum class Type // Copy from the Ogre enum
    4545            {
    4646                /// Point light sources give off light equally in all directions, so require only position not direction
     
    6363                { return this->light_; }
    6464
    65             inline void setType(Light::LightTypes type)
     65            inline void setType(Light::Type type)
    6666                { this->type_ = type; this->updateType(); }
    67             inline Light::LightTypes getType() const
     67            inline Light::Type getType() const
    6868                { return this->type_; }
    6969
     
    144144
    145145            Ogre::Light* light_;
    146             LightTypes type_;
     146            Light::Type type_;
    147147            ColourValue diffuse_;
    148148            ColourValue specular_;
Note: See TracChangeset for help on using the changeset viewer.