Change Visitor Pattern codes to for-loop codes for Perforcemance improvement
authorJaewon Cho <jaewon7.cho@samsung.com>
Tue, 27 Aug 2013 07:57:34 +0000 (16:57 +0900)
committerJaewon Cho <jaewon7.cho@samsung.com>
Tue, 27 Aug 2013 08:46:58 +0000 (17:46 +0900)
Change-Id: I8c4b3ac567f5c3b198611d6c15c53f1e1b166b70
Signed-off-by: Jaewon Cho <jaewon7.cho@samsung.com>
src/ui/FUi_Control.cpp
src/ui/inc/FUi_Control.h

index 0e28d75..5bc9a8d 100644 (file)
@@ -1074,36 +1074,29 @@ _Control::Show(void)
 }
 
 void
-_Control::ChangeLayout(_ControlOrientation orientation, bool callRotation)
+_Control::ChangeLayout(_Control& control, _ControlOrientation orientation)
 {
-       ClearLastResult();
-
-       struct _Visitor
-               : public Visitor
+       if (control.__orientation != orientation)
        {
-               _Visitor(_ControlOrientation orientation)
-                       : __orientation(orientation){}
+               control.__orientation = orientation;
+               control.GetControlDelegate().OnChangeLayout(orientation);
+       }
 
-               virtual VisitType Visit(_Control& control)
+       for (int i = 0; i < control.GetChildCount(); ++i)
+       {
+               _Control* pChild = control.GetChild(i);
+               if (pChild)
                {
-                       if (control.__orientation != __orientation)
-                       {
-                               control.__orientation = __orientation;
-                               control.GetControlDelegate().OnChangeLayout(__orientation);
-                               ClearLastResult();
-                       }
-
-                       return VISIT_DOWNWARD;
+                       ChangeLayout(*pChild, orientation);
                }
+       }
+}
 
-private:
-               _ControlOrientation __orientation;
-       };
-
-       _Visitor visitor(orientation);
-       Accept(visitor);
-
-       SysAssert(GetLastResult() == E_SUCCESS);
+void
+_Control::ChangeLayout(_ControlOrientation orientation, bool callRotation)
+{
+       ClearLastResult();
+       ChangeLayout(*this, orientation);
 
        if (callRotation == true)
        {
@@ -1117,36 +1110,30 @@ private:
 }
 
 void
-_Control::ChangeLayout(_ControlRotation rotation)
+_Control::ChangeLayout(_Control& control, _ControlRotation rotation)
 {
-       ClearLastResult();
-
-       struct _Visitor
-               : public Visitor
+       if (control.__rotation != rotation)
        {
-               _Visitor(_ControlRotation rotation)
-                       : __rotation(rotation){}
+               control.__rotation = rotation;
+               control.GetControlDelegate().OnChangeLayout(rotation);
+               ClearLastResult();
+       }
 
-               virtual VisitType Visit(_Control& control)
+       for (int i = 0; i < control.GetChildCount(); ++i)
+       {
+               _Control* pChild = control.GetChild(i);
+               if (pChild)
                {
-                       if (control.__rotation != __rotation)
-                       {
-                               control.__rotation = __rotation;
-                               control.GetControlDelegate().OnChangeLayout(__rotation);
-                               ClearLastResult();
-                       }
-
-                       return VISIT_DOWNWARD;
+                       ChangeLayout(*pChild, rotation);
                }
+       }
+}
 
-private:
-               _ControlRotation __rotation;
-       };
-
-       _Visitor visitor(rotation);
-       Accept(visitor);
-
-       SysAssert(GetLastResult() == E_SUCCESS);
+void
+_Control::ChangeLayout(_ControlRotation rotation)
+{
+       ClearLastResult();
+       ChangeLayout(*this, rotation);
 }
 
 bool
@@ -1650,69 +1637,48 @@ _Control::CallOnDetachingFromMainTree(_Control& control)
 }
 
 void
-_Control::CallOnAncestorVisibleStateChanged(void)
+_Control::CallOnAncestorVisibleStateChanged(_Control& control)
 {
-       struct _Visitor
-               : public Visitor
-       {
-               _Visitor(_Control& parent)
-                       : __parent(parent){}
+       control.GetControlDelegate().OnAncestorVisibleStateChanged(*this);
 
-               virtual VisitType Visit(_Control& control)
+       for (int i = 0; i < control.GetChildCount(); ++i)
+       {
+               _Control* pChild = control.GetChild(i);
+               if (pChild)
                {
-                       control.GetControlDelegate().OnAncestorVisibleStateChanged(__parent);
-                       return VISIT_DOWNWARD;
+                       CallOnAncestorVisibleStateChanged(*pChild);
                }
-
-               _Control& __parent;
-       };
-
-       _Visitor visitor(*this);
-       Accept(visitor);
+       }
 }
 
 void
-_Control::CallOnAncestorEnableStateChanged(void)
+_Control::CallOnAncestorEnableStateChanged(_Control& control)
 {
-       struct _Visitor
-               : public Visitor
-       {
-               _Visitor(_Control& parent)
-                       : __parent(parent){}
+       control.GetControlDelegate().OnAncestorEnableStateChanged(*this);
 
-               virtual VisitType Visit(_Control& control)
+       for (int i = 0; i < control.GetChildCount(); ++i)
+       {
+               _Control* pChild = control.GetChild(i);
+               if (pChild)
                {
-                       control.GetControlDelegate().OnAncestorEnableStateChanged(__parent);
-                       return VISIT_DOWNWARD;
+                       CallOnAncestorEnableStateChanged(*pChild);
                }
-
-               _Control& __parent;
-       };
-
-       _Visitor visitor(*this);
-       Accept(visitor);
+       }
 }
 
 void
-_Control::CallOnAncestorInputEnableStateChanged(void)
+_Control::CallOnAncestorInputEnableStateChanged(_Control& control)
 {
-       struct _Visitor
-               : public Visitor
-       {
-               _Visitor(_Control& parent)
-                       : __parent(parent){}
+       control.GetControlDelegate().OnAncestorInputEnableStateChanged(*this);
 
-               virtual VisitType Visit(_Control& control)
+       for (int i = 0; i < control.GetChildCount(); ++i)
+       {
+               _Control* pChild = control.GetChild(i);
+               if (pChild)
                {
-                       control.GetControlDelegate().OnAncestorInputEnableStateChanged(__parent);
-                       return VISIT_DOWNWARD;
+                       CallOnAncestorInputEnableStateChanged(*pChild);
                }
-
-               _Control& __parent;
-       };
-
-       _Visitor visitor(*this);
-       Accept(visitor);
+       }
 }
 
 // E_INVALID_ARG
@@ -2517,45 +2483,23 @@ bool
 _Control::IsAncestorOf(const _Control& control) const
 {
        ClearLastResult();
+       return IsAncestorOf(control, *this);
+}
 
+bool
+_Control::IsAncestorOf(const _Control& control, const _Control& ancestor) const
+{
        const _Control* pParent = control.GetParent();
-       if (!pParent)
+       if (pParent)
        {
-               return false;
-       }
-
-       struct _Visitor
-               : public Visitor
-       {
-private:
-               const _Control& __ancestor;
-
-public:
-               bool yes;
-
-               _Visitor(const _Control& ancestor)
-                       : __ancestor(ancestor)
-                       , yes(false){}
-
-               virtual VisitType Visit(_Control& control)
+               if (pParent == &ancestor)
                {
-                       if (&__ancestor == &control)
-                       {
-                               yes = true;
-                               return VISIT_STOP;
-                       }
-                       else
-                       {
-                               return VISIT_UPWARD;
-                       }
+                       return true;
                }
-       };
-
-       _Visitor visitor(*this);
-       pParent->Accept(visitor);
+               return IsAncestorOf(*pParent, ancestor);
+       }
 
-       SysAssert(GetLastResult() == E_SUCCESS);
-       return visitor.yes;
+       return false;
 }
 
 _Window*
@@ -2568,35 +2512,23 @@ _Control::GetRootWindow(void) const
                return __pRootWindow;
        }
 
-       struct _Visitor
-               : public Visitor
-       {
-               _Window* pRoot;
+       _Window* pRoot = null;
+       _Control* pControl = const_cast<_Control*>(this);
 
-               _Visitor(void)
-                       : pRoot(null){}
-
-               virtual VisitType Visit(_Control& control)
+       while (pControl)
+       {
+               pRoot = dynamic_cast <_Window*>(pControl);
+               if (pRoot)
                {
-                       pRoot = dynamic_cast <_Window*>(&control);
-                       if (pRoot != null)
-                       {
-                               return VISIT_STOP;
-                       }
-
-                       return VISIT_UPWARD;
+                       break;
                }
-       };
-
-       _Visitor visitor;
-       Accept(visitor);
-
-       SysAssert(GetLastResult() == E_SUCCESS);
+               pControl = pControl->GetParent();
+       }
 
-       const_cast<_Control*>(this)->__pRootWindow = visitor.pRoot;
+       const_cast<_Control*>(this)->__pRootWindow = pRoot;
        const_cast<_Control*>(this)->__needRecalcRootWindow = false;
 
-       return visitor.pRoot;
+       return pRoot;
 }
 
 bool
@@ -2859,29 +2791,20 @@ _Control::IsEnabled(void) const
 {
        ClearLastResult();
 
-       struct _Visitor
-               : public Visitor
-       {
-               bool enabled;
-
-               _Visitor(void)
-                       : enabled(true){}
+       bool enabled = true;
+       const _Control* pControl = this;
 
-               virtual VisitType Visit(_Control& control)
+       while (pControl)
+       {
+               if (!pControl->GetEnableState())
                {
-                       if (!control.GetEnableState())
-                       {
-                               enabled = false;
-                               return VISIT_STOP;
-                       }
-
-                       return VISIT_UPWARD;
+                       enabled = false;
+                       break;
                }
-       };
+               pControl = pControl->GetParent();
+       }
 
-       _Visitor visitor;
-       Accept(visitor);
-       return visitor.enabled;
+       return enabled;
 }
 
 bool
@@ -2899,7 +2822,7 @@ _Control::SetEnableState(bool enabledState)
        if (changed)
        {
                __enabledState = enabledState;
-               CallOnAncestorEnableStateChanged();
+               CallOnAncestorEnableStateChanged(*this);
        }
        __pAccessibilityContainer->SetEnableState(enabledState);
 }
@@ -2909,29 +2832,20 @@ _Control::IsInputEventEnabled(void) const
 {
        ClearLastResult();
 
-       struct _Visitor
-               : public Visitor
-       {
-               bool inputEnabled;
-
-               _Visitor(void)
-                       : inputEnabled(true){}
+       bool inputEnabled = true;
+       const _Control* pControl = this;
 
-               virtual VisitType Visit(_Control& control)
+       while (pControl)
+       {
+               if (!pControl->GetInputEnableState())
                {
-                       if (!control.GetInputEnableState())
-                       {
-                               inputEnabled = false;
-                               return VISIT_STOP;
-                       }
-
-                       return VISIT_UPWARD;
+                       inputEnabled = false;
+                       break;
                }
-       };
+               pControl = pControl->GetParent();
+       }
 
-       _Visitor visitor;
-       Accept(visitor);
-       return visitor.inputEnabled;
+       return inputEnabled;
 }
 
 bool
@@ -2953,7 +2867,7 @@ void
 _Control::LockInputEvent(void)
 {
        __inputLockRefCount++;
-       CallOnAncestorInputEnableStateChanged();
+       CallOnAncestorInputEnableStateChanged(*this);
 }
 
 void
@@ -2977,31 +2891,20 @@ _Control::IsVisible(void) const
                return false;
        }
 
-       struct _Visitor
-               : public Visitor
-       {
-               bool visible;
-
-               _Visitor(void)
-                       : visible(true){}
+       bool visible = true;
+       const _Control* pControl = this;
 
-               virtual VisitType Visit(_Control& control)
+       while (pControl)
+       {
+               if (!pControl->GetVisibleState())
                {
-                       if (!control.GetVisibleState())
-                       {
-                               visible = false;
-                               return VISIT_STOP;
-                       }
-
-                       return VISIT_UPWARD;
+                       visible = false;
+                       break;
                }
-       };
-
-       _Visitor visitor;
-       Accept(visitor);
+               pControl = pControl->GetParent();
+       }
 
-       SysAssert(GetLastResult() == E_SUCCESS);
-       return visitor.visible;
+       return visible;
 }
 
 bool
@@ -3042,7 +2945,7 @@ _Control::SetVisibleState(bool visibleState)
        if (changed)
        {
                GetControlDelegate().OnVisibleStateChanged();
-               CallOnAncestorVisibleStateChanged();
+               CallOnAncestorVisibleStateChanged(*this);
                _Control* pParent = GetParent();
                if (pParent)
                {
@@ -3157,52 +3060,39 @@ _Control::UpdateBoundsOfVisualElement(const FloatRectangle& controlBounds)
 }
 
 result
-_Control::AdjustAbsoluteBounds(void)
+_Control::AdjustAbsoluteBounds(_Control& control)
 {
-       struct _Visitor
-               : public Visitor
+       if (&control == this)
        {
-               _Visitor(_Control* pControl)
-                       : __pControl(pControl){}
-
-               virtual VisitType Visit(_Control& control)
-               {
-                       result r = E_SUCCESS;
+               return E_SUCCESS;
+       }
 
-                       if (__pControl == &control)
-                       {
-                               return VISIT_DOWNWARD;
-                       }
-                       FloatRectangle fbounds = control.GetBoundsF();
-                       if (control.IsLayoutChangable() == false)
-                       {
-                               r = control.UpdateBoundsOfVisualElement(FloatRectangle(0.0f, 0.0f, fbounds.width, fbounds.height));
-                       }
-                       else
-                       {
-                               r = control.UpdateBoundsOfVisualElement(fbounds);
-                       }
+       result r = E_SUCCESS;
+       FloatRectangle fbounds = control.GetBoundsF();
 
-                       ControlList& children = control.GetChildList();
+       if (control.IsLayoutChangable() == false)
+       {
+               r = control.UpdateBoundsOfVisualElement(FloatRectangle(0.0f, 0.0f, fbounds.width, fbounds.height));
+       }
+       else
+       {
+               r = control.UpdateBoundsOfVisualElement(fbounds);
+       }
 
-                       int childrenCount = children.GetCount();
-                       if (childrenCount <= 0)
-                       {
-                               return VISIT_STOP;
-                       }
-                       else
+       for (int i = 0; i < control.GetChildCount(); ++i)
+       {
+               _Control* pChild = control.GetChild(i);
+               if (pChild)
+               {
+                       r = AdjustAbsoluteBounds(*pChild);
+                       if (IsFailed(r))
                        {
-                               return VISIT_DOWNWARD;
+                               SysLog(NID_UI_CTRL, "[%s] Propagated", GetErrorMessage(GetLastResult()));
                        }
                }
-       private :
-               _Control* __pControl;
-       };
-
-       _Visitor visitor(this);
-       Accept(visitor);
+       }
 
-       return E_SUCCESS;
+       return r;
 }
 
 bool
@@ -3815,7 +3705,7 @@ _Control::SetClientBounds(const Rectangle& bounds)
 
        if (moved || resized)
        {
-               result r = AdjustAbsoluteBounds();
+               result r = AdjustAbsoluteBounds(*this);
                SysTryReturn(NID_UI, r == E_SUCCESS, r,         r, "[%s] Propagating.", GetErrorMessage(r));
 
                SetUpdateLayoutState(true);
@@ -3839,7 +3729,7 @@ _Control::SetClientBounds(const FloatRectangle& bounds)
 
        if (moved || resized)
        {
-               result r = AdjustAbsoluteBounds();
+               result r = AdjustAbsoluteBounds(*this);
                SysTryReturn(NID_UI, r == E_SUCCESS, r,         r, "[%s] Propagating.", GetErrorMessage(r));
 
                SetUpdateLayoutState(true);
@@ -5649,7 +5539,7 @@ _Control::MakeFocusList(const _Control* pControl, IListT<_Control*>*  pFocusCont
        for(int i = 0; i < childCount; i++)
        {
                _Control* pChildControl = pControl->GetChild(i);
-               Rectangle rect = pChildControl->GetAbsoluteBounds(true);
+               Rectangle rect = pChildControl->GetAbsoluteBounds();
                unique_ptr<IEnumeratorT<_Control*> > pEnum (pFocusControlList->GetEnumeratorN());
                int index = 0;
                while (pEnum->MoveNext() == E_SUCCESS)
@@ -5739,4 +5629,3 @@ _Control::IsChildAttachable(_Control& child) const
 }
 
 }} // Tizen::Ui
-
index 5107efc..3dea1b9 100644 (file)
@@ -605,6 +605,9 @@ private:
        void Invalidate(_Control& control);
        void InvalidateHierarchyRootWindow(_Control& control);
        void InvalidateHierarchyAbsoluteBounds(_Control& control);
+       bool IsAncestorOf(const _Control& control, const _Control& ancestor) const;
+       void ChangeLayout(_Control& control, _ControlOrientation orientation);
+       void ChangeLayout(_Control& control, _ControlRotation rotation);
 
        void ReleaseHandle(void);
 
@@ -615,7 +618,7 @@ private:
        result SetBoundsInternal(const Tizen::Graphics::FloatRectangle& bounds, bool callBoundsChangeCallbacks);
        result SetBoundsFinal(const Tizen::Graphics::FloatRectangle& newBounds, bool changeLayoutBaseRect, bool callBoundsChangeCallbacks);
        result UpdateBoundsOfVisualElement(const Tizen::Graphics::FloatRectangle& controlBounds);
-       result AdjustAbsoluteBounds(void);
+       result AdjustAbsoluteBounds(_Control& control);
        result StartAttaching(_Control& child, _ControlArea area);
        result EndAttaching(_Control& child);
        void SetParent(_Control* pParent);
@@ -631,9 +634,9 @@ private:
        result CallOnPreAttachedToMainTree(_Control& control);
        result CallOnAttachedToMainTree(_Control& control);
        result CallOnDetachingFromMainTree(_Control& control);
-       void CallOnAncestorVisibleStateChanged(void);
-       void CallOnAncestorEnableStateChanged(void);
-       void CallOnAncestorInputEnableStateChanged(void);
+       void CallOnAncestorVisibleStateChanged(_Control& control);
+       void CallOnAncestorEnableStateChanged(_Control& control);
+       void CallOnAncestorInputEnableStateChanged(_Control& control);
        void SetLayer(_ControlLayer layer);
 
        int PrintDescription(bool printChildren, int depth, int level);