helpers: cleanup properly if action throws an exception

master
Andrzej Rybczak 13 years ago
parent 0731c13026
commit e5e6de8d31
  1. 30
      src/helpers.h

@ -118,17 +118,27 @@ template <typename T, typename F>
void withUnfilteredMenu(NC::Menu<T> &m, F action) void withUnfilteredMenu(NC::Menu<T> &m, F action)
{ {
bool is_filtered = m.isFiltered(); bool is_filtered = m.isFiltered();
m.showAll(); auto cleanup = [&]() {
action();
if (is_filtered) if (is_filtered)
m.showFiltered(); m.showFiltered();
};
m.showAll();
try
{
action();
}
catch (...)
{
cleanup();
throw;
}
cleanup();
} }
template <typename T, typename F> template <typename T, typename F>
void withUnfilteredMenuReapplyFilter(NC::Menu<T> &m, F action) void withUnfilteredMenuReapplyFilter(NC::Menu<T> &m, F action)
{ {
m.showAll(); auto cleanup = [&]() {
action();
if (m.getFilter()) if (m.getFilter())
{ {
m.applyCurrentFilter(m.begin(), m.end()); m.applyCurrentFilter(m.begin(), m.end());
@ -138,6 +148,18 @@ void withUnfilteredMenuReapplyFilter(NC::Menu<T> &m, F action)
m.clearFilterResults(); m.clearFilterResults();
} }
} }
};
m.showAll();
try
{
action();
}
catch (...)
{
cleanup();
throw;
}
cleanup();
} }
template <typename F> template <typename F>

Loading…
Cancel
Save