From e5e6de8d3154cedcf008285e3169d0480ef73d03 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Fri, 17 May 2013 13:44:02 +0200 Subject: [PATCH] helpers: cleanup properly if action throws an exception --- src/helpers.h | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/src/helpers.h b/src/helpers.h index e2dcc4e3..1a851c85 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -118,26 +118,48 @@ template void withUnfilteredMenu(NC::Menu &m, F action) { bool is_filtered = m.isFiltered(); + auto cleanup = [&]() { + if (is_filtered) + m.showFiltered(); + }; m.showAll(); - action(); - if (is_filtered) - m.showFiltered(); + try + { + action(); + } + catch (...) + { + cleanup(); + throw; + } + cleanup(); } template void withUnfilteredMenuReapplyFilter(NC::Menu &m, F action) { - m.showAll(); - action(); - if (m.getFilter()) - { - m.applyCurrentFilter(m.begin(), m.end()); - if (m.empty()) + auto cleanup = [&]() { + if (m.getFilter()) { - m.clearFilter(); - m.clearFilterResults(); + m.applyCurrentFilter(m.begin(), m.end()); + if (m.empty()) + { + m.clearFilter(); + m.clearFilterResults(); + } } + }; + m.showAll(); + try + { + action(); } + catch (...) + { + cleanup(); + throw; + } + cleanup(); } template