helpers: cleanup properly if action throws an exception

This commit is contained in:
Andrzej Rybczak
2013-05-17 13:44:02 +02:00
parent 0731c13026
commit e5e6de8d31

View File

@@ -118,26 +118,48 @@ 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();
auto cleanup = [&]() {
if (is_filtered)
m.showFiltered();
};
m.showAll(); m.showAll();
action(); try
if (is_filtered) {
m.showFiltered(); 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());
if (m.empty())
{ {
m.clearFilter(); m.applyCurrentFilter(m.begin(), m.end());
m.clearFilterResults(); if (m.empty())
{
m.clearFilter();
m.clearFilterResults();
}
} }
};
m.showAll();
try
{
action();
} }
catch (...)
{
cleanup();
throw;
}
cleanup();
} }
template <typename F> template <typename F>