From 1662b06f3698fcd9fb69814c82b217e5ae806664 Mon Sep 17 00:00:00 2001 From: Ruben Dashyan Date: Tue, 12 Feb 2019 16:18:10 +0100 Subject: [PATCH] [escher/scroll_view] Put the decorators inside a union --- escher/include/escher/scroll_view.h | 24 ++++++++++++--- escher/src/scroll_view.cpp | 48 +++++++++++++++++++---------- 2 files changed, 51 insertions(+), 21 deletions(-) diff --git a/escher/include/escher/scroll_view.h b/escher/include/escher/scroll_view.h index fde6512ce..ea8490838 100644 --- a/escher/include/escher/scroll_view.h +++ b/escher/include/escher/scroll_view.h @@ -8,6 +8,7 @@ class ScrollView : public View { public: ScrollView(View * contentView, ScrollViewDataSource * dataSource); + ScrollView(ScrollView&& other); KDSize minimalSizeForOptimalDisplay() const override; void setTopMargin(KDCoordinate m) { m_topMargin = m; } @@ -73,8 +74,11 @@ public: ScrollViewArrow m_leftArrow; }; - Decorator * decorator(); - void setDecoratorType(Decorator::Type t) { m_decoratorType = t; } + Decorator * decorator() { return m_decorators.activeDecorator(); } + void setDecoratorType(Decorator::Type t) { + m_decoratorType = t; + m_decorators.setActiveDecorator(t); + } virtual void setBackgroundColor(KDColor c) { m_backgroundColor = c; decorator()->setBackgroundColor(m_backgroundColor); @@ -126,9 +130,19 @@ private: InnerView m_innerView; Decorator::Type m_decoratorType; - Decorator m_decorator; - BarDecorator m_barDecorator; - ArrowDecorator m_arrowDecorator; + union Decorators { + public: + /* Enforce trivial constructor and destructor that just leaves the memory unmodified. */ + Decorators() {} + ~Decorators() {} + Decorator * activeDecorator() { return &m_none; } + void setActiveDecorator(Decorator::Type t); + private: + Decorator m_none; + BarDecorator m_bars; + ArrowDecorator m_arrows; + }; + Decorators m_decorators; KDColor m_backgroundColor; }; diff --git a/escher/src/scroll_view.cpp b/escher/src/scroll_view.cpp index 4209ce91e..d60756393 100644 --- a/escher/src/scroll_view.cpp +++ b/escher/src/scroll_view.cpp @@ -2,6 +2,8 @@ #include #include +#include + extern "C" { #include } @@ -15,13 +17,24 @@ ScrollView::ScrollView(View * contentView, ScrollViewDataSource * dataSource) : m_bottomMargin(0), m_leftMargin(0), m_innerView(this), - m_decoratorType(Decorator::Type::Bars), - m_decorator(), - m_barDecorator(), - m_arrowDecorator(), + m_decorators(), m_backgroundColor(Palette::WallScreen) { assert(m_dataSource != nullptr); + setDecoratorType(Decorator::Type::Bars); +} + +ScrollView::ScrollView(ScrollView&& other) : + m_contentView(other.m_contentView), + m_dataSource(other.m_dataSource), + m_topMargin(other.m_topMargin), + m_rightMargin(other.m_rightMargin), + m_bottomMargin(other.m_bottomMargin), + m_leftMargin(other.m_leftMargin), + m_innerView(this), + m_backgroundColor(other.m_backgroundColor) +{ + setDecoratorType(other.m_decoratorType); } KDSize ScrollView::minimalSizeForOptimalDisplay() const { @@ -39,18 +52,6 @@ void ScrollView::setCommonMargins() { setLeftMargin(Metric::CommonLeftMargin); } -ScrollView::Decorator * ScrollView::decorator() { - switch (m_decoratorType) { - case Decorator::Type::Bars: - return &m_barDecorator; - case Decorator::Type::Arrows: - return &m_arrowDecorator; - default: - assert(m_decoratorType == Decorator::Type::None); - return &m_decorator; - } -} - void ScrollView::scrollToContentPoint(KDPoint p, bool allowOverscroll) { if (!allowOverscroll && !m_contentView->bounds().contains(p)) { return; @@ -197,6 +198,21 @@ void ScrollView::ArrowDecorator::setBackgroundColor(KDColor c) { } } +void ScrollView::Decorators::setActiveDecorator(Decorator::Type t) { + /* We do NOT need to destroy the previous decorator because they don't have a destructor */ + switch (t) { + case Decorator::Type::Bars: + new (&m_bars) BarDecorator(); + break; + case Decorator::Type::Arrows: + new (&m_arrows) ArrowDecorator(); + break; + default: + assert(t == Decorator::Type::None); + new (&m_none) Decorator(); + } +} + #if ESCHER_VIEW_LOGGING const char * ScrollView::className() const { return "ScrollView";