Browse Source

Add EnumAttributeAccessorImpl class, remove all Variant::Get<> functions for enum type.

aster2013 11 years ago
parent
commit
9e5172a959

+ 0 - 5
Source/Engine/Core/Spline.cpp

@@ -33,11 +33,6 @@ const char* interpolationModeNames[] =
     0
 };
 
-template<> InterpolationMode Variant::Get<InterpolationMode>() const
-{
-    return (InterpolationMode) GetInt();
-}
-
 Spline::Spline() :
     interpolationMode_(BEZIER_CURVE)
 {

+ 0 - 5
Source/Engine/Graphics/Light.cpp

@@ -81,11 +81,6 @@ void FocusParameters::Validate()
     minView_ = Max(minView_, SHADOW_MIN_VIEW);
 }
 
-template<> LightType Variant::Get<LightType>() const
-{
-    return (LightType)GetInt();
-}
-
 Light::Light(Context* context) :
     Drawable(context, DRAWABLE_LIGHT),
     lightType_(DEFAULT_LIGHTTYPE),

+ 39 - 1
Source/Engine/Scene/Serializable.h

@@ -203,6 +203,44 @@ public:
     SetFunctionPtr setFunction_;
 };
 
+/// Template implementation of the attribute accessor invoke helper class.
+template <class T, class U> class EnumAttributeAccessorImpl : public AttributeAccessor
+{
+public:
+    typedef U (T::*GetFunctionPtr)() const;
+    typedef void (T::*SetFunctionPtr)(U);
+
+    /// Construct with function pointers.
+    EnumAttributeAccessorImpl(GetFunctionPtr getFunction, SetFunctionPtr setFunction) :
+        getFunction_(getFunction),
+        setFunction_(setFunction)
+    {
+        assert(getFunction_);
+        assert(setFunction_);
+    }
+
+    /// Invoke getter function.
+    virtual void Get(const Serializable* ptr, Variant& dest) const
+    {
+        assert(ptr);
+        const T* classPtr = static_cast<const T*>(ptr);
+        dest = (classPtr->*getFunction_)();
+    }
+
+    /// Invoke setter function.
+    virtual void Set(Serializable* ptr, const Variant& value)
+    {
+        assert(ptr);
+        T* classPtr = static_cast<T*>(ptr);
+        (classPtr->*setFunction_)((U)value.GetInt());
+    }
+
+    /// Class-specific pointer to getter function.
+    GetFunctionPtr getFunction_;
+    /// Class-specific pointer to setter function.
+    SetFunctionPtr setFunction_;
+};
+
 /// Template implementation of the attribute accessor invoke helper class using const references for setter only.
 template <class T, class U> class MixedAttributeAccessorImpl : public AttributeAccessor
 {
@@ -246,7 +284,7 @@ public:
 #define ATTRIBUTE(type, name, variable, defaultValue, mode) context->RegisterAttribute<ClassName>(Urho3D::AttributeInfo(type, name, offsetof(ClassName, variable), defaultValue, mode))
 #define ENUM_ATTRIBUTE(name, variable, enumNames, defaultValue, mode) context->RegisterAttribute<ClassName>(Urho3D::AttributeInfo(name, offsetof(ClassName, variable), enumNames, defaultValue, mode))
 #define ACCESSOR_ATTRIBUTE(name, getFunction, setFunction, typeName, defaultValue, mode) context->RegisterAttribute<ClassName>(Urho3D::AttributeInfo(GetVariantType<typeName>(), name, new Urho3D::AttributeAccessorImpl<ClassName, typeName>(&ClassName::getFunction, &ClassName::setFunction), defaultValue, mode))
-#define ENUM_ACCESSOR_ATTRIBUTE(name, getFunction, setFunction, typeName, enumNames, defaultValue, mode) context->RegisterAttribute<ClassName>(Urho3D::AttributeInfo(name, new Urho3D::AttributeAccessorImpl<ClassName, typeName>(&ClassName::getFunction, &ClassName::setFunction), enumNames, defaultValue, mode))
+#define ENUM_ACCESSOR_ATTRIBUTE(name, getFunction, setFunction, typeName, enumNames, defaultValue, mode) context->RegisterAttribute<ClassName>(Urho3D::AttributeInfo(name, new Urho3D::EnumAttributeAccessorImpl<ClassName, typeName>(&ClassName::getFunction, &ClassName::setFunction), enumNames, defaultValue, mode))
 #define REF_ACCESSOR_ATTRIBUTE(name, getFunction, setFunction, typeName, defaultValue, mode) context->RegisterAttribute<ClassName>(Urho3D::AttributeInfo(GetVariantType<typeName>(), name, new Urho3D::RefAttributeAccessorImpl<ClassName, typeName>(&ClassName::getFunction, &ClassName::setFunction), defaultValue, mode))
 #define MIXED_ACCESSOR_ATTRIBUTE(name, getFunction, setFunction, typeName, defaultValue, mode) context->RegisterAttribute<ClassName>(Urho3D::AttributeInfo(GetVariantType<typeName>(), name, new Urho3D::MixedAttributeAccessorImpl<ClassName, typeName>(&ClassName::getFunction, &ClassName::setFunction), defaultValue, mode))
 #define UPDATE_ATTRIBUTE_DEFAULT_VALUE(name, defaultValue) context->UpdateAttributeDefaultValue<ClassName>(name, defaultValue)

+ 0 - 5
Source/Engine/UI/BorderImage.cpp

@@ -34,11 +34,6 @@ namespace Urho3D
 extern const char* blendModeNames[];
 extern const char* UI_CATEGORY;
 
-template<> BlendMode Variant::Get<BlendMode>() const
-{
-    return (BlendMode)GetInt();
-}
-
 BorderImage::BorderImage(Context* context) :
     UIElement(context),
     imageRect_(IntRect::ZERO),

+ 0 - 5
Source/Engine/UI/ListView.cpp

@@ -44,11 +44,6 @@ static const char* highlightModes[] =
     0
 };
 
-template<> HighlightMode Variant::Get<HighlightMode>() const
-{
-    return (HighlightMode)GetInt();
-}
-
 static const StringHash expandedHash("Expanded");
 
 extern const char* UI_CATEGORY;

+ 0 - 5
Source/Engine/UI/Slider.cpp

@@ -39,11 +39,6 @@ const char* orientations[] =
     0
 };
 
-template<> Orientation Variant::Get<Orientation>() const
-{
-    return (Orientation)GetInt();
-}
-
 extern const char* UI_CATEGORY;
 
 Slider::Slider(Context* context) :

+ 0 - 20
Source/Engine/UI/UIElement.cpp

@@ -87,26 +87,6 @@ static bool CompareUIElements(const UIElement* lhs, const UIElement* rhs)
     return lhs->GetPriority() < rhs->GetPriority();
 }
 
-template<> HorizontalAlignment Variant::Get<HorizontalAlignment>() const
-{
-    return (HorizontalAlignment)GetInt();
-}
-
-template<> VerticalAlignment Variant::Get<VerticalAlignment>() const
-{
-    return (VerticalAlignment)GetInt();
-}
-
-template<> FocusMode Variant::Get<FocusMode>() const
-{
-    return (FocusMode)GetInt();
-}
-
-template<> LayoutMode Variant::Get<LayoutMode>() const
-{
-    return (LayoutMode)GetInt();
-}
-
 XPathQuery UIElement::styleXPathQuery_("/elements/element[@type=$typeName]", "typeName:String");
 
 UIElement::UIElement(Context* context) :

+ 0 - 5
Source/Engine/Urho2D/AnimatedSprite2D.cpp

@@ -48,11 +48,6 @@ const char* loopModeNames[] =
     0
 };
 
-template<> LoopMode2D Variant::Get<LoopMode2D>() const
-{
-    return (LoopMode2D)GetInt();
-}
-
 AnimatedSprite2D::AnimatedSprite2D(Context* context) :
     Drawable(context, DRAWABLE_GEOMETRY),
     layer_(0),

+ 0 - 5
Source/Engine/Urho2D/RigidBody2D.cpp

@@ -46,11 +46,6 @@ static const char* bodyTypeNames[] =
     0
 };
 
-template<> BodyType2D Variant::Get<BodyType2D>() const
-{
-    return (BodyType2D)GetInt();
-}
-
 RigidBody2D::RigidBody2D(Context* context) :
     Component(context),
     massData_(),    // b2MassData structure does not have a constructor so need to zero-initialize all its members