summaryrefslogtreecommitdiff
path: root/src/helpers.h
diff options
context:
space:
mode:
authorAndrzej Rybczak <electricityispower@gmail.com>2013-05-17 13:44:02 +0200
committerAndrzej Rybczak <electricityispower@gmail.com>2013-05-17 13:44:25 +0200
commite5e6de8d3154cedcf008285e3169d0480ef73d03 (patch)
tree8574d0ca94a5775d8c29dc7b842ef0bfa768185b /src/helpers.h
parent0731c130263de18af0f07619f0128f3b1466a5f3 (diff)
helpers: cleanup properly if action throws an exception
Diffstat (limited to 'src/helpers.h')
-rw-r--r--src/helpers.h44
1 files 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 <typename T, typename F>
void withUnfilteredMenu(NC::Menu<T> &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 <typename T, typename F>
void withUnfilteredMenuReapplyFilter(NC::Menu<T> &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 <typename F>