From 969d9bd0709787f57fcca5232964ef6d9265c066 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Mon, 14 Dec 2015 11:11:49 -0800 Subject: [PATCH] Use std::unique_ptr inside NavigationView. --- firmware/application/ui_navigation.cpp | 10 ++++++---- firmware/application/ui_navigation.hpp | 8 +++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/firmware/application/ui_navigation.cpp b/firmware/application/ui_navigation.cpp index 3f6a0a380..d988792aa 100644 --- a/firmware/application/ui_navigation.cpp +++ b/firmware/application/ui_navigation.cpp @@ -48,12 +48,15 @@ NavigationView::NavigationView() { } -void NavigationView::push_view(View* new_view) { +View* NavigationView::push_view(std::unique_ptr new_view) { free_view(); - view_stack.push_back(new_view); + const auto p = new_view.get(); + view_stack.emplace_back(std::move(new_view)); update_view(); + + return p; } void NavigationView::pop() { @@ -61,7 +64,6 @@ void NavigationView::pop() { if( view_stack.size() > 1 ) { free_view(); - delete view_stack.back(); view_stack.pop_back(); update_view(); @@ -73,7 +75,7 @@ void NavigationView::free_view() { } void NavigationView::update_view() { - const auto new_view = view_stack.back(); + const auto new_view = view_stack.back().get(); add_child(new_view); new_view->set_parent_rect({ {0, 0}, size() }); focus(); diff --git a/firmware/application/ui_navigation.hpp b/firmware/application/ui_navigation.hpp index 4d4cd2adf..a43a83446 100644 --- a/firmware/application/ui_navigation.hpp +++ b/firmware/application/ui_navigation.hpp @@ -59,9 +59,7 @@ public: template T* push(Args&&... args) { - const auto new_view = new T(std::forward(args)...); - push_view(new_view); - return reinterpret_cast(new_view); + return reinterpret_cast(push_view(std::unique_ptr(new T(std::forward(args)...)))); } void pop(); @@ -69,13 +67,13 @@ public: void focus() override; private: - std::vector view_stack; + std::vector> view_stack; Widget* view() const; void free_view(); void update_view(); - void push_view(View* new_view); + View* push_view(std::unique_ptr new_view); }; class SystemMenuView : public MenuView {