Make range delete scope guard more generic.

This commit is contained in:
vng 2012-09-07 19:13:12 +03:00 committed by Alex Zolotarev
parent e874d2adbf
commit 44c86c6e57
2 changed files with 27 additions and 11 deletions

View file

@ -111,17 +111,33 @@ struct DeleteFunctor
}
};
template <class TContainer> class DeleteRangeGuard
namespace impl
{
TContainer & m_cont;
public:
DeleteRangeGuard(TContainer & cont) : m_cont(cont) {}
~DeleteRangeGuard()
template <class TContainer, class TDeletor> class DeleteRangeFunctor
{
for_each(m_cont.begin(), m_cont.end(), DeleteFunctor());
m_cont.clear();
}
};
TContainer & m_cont;
TDeletor m_deletor;
public:
DeleteRangeFunctor(TContainer & cont, TDeletor const & deletor)
: m_cont(cont), m_deletor(deletor)
{
}
void operator() ()
{
for_each(m_cont.begin(), m_cont.end(), m_deletor);
m_cont.clear();
}
};
}
template <class TContainer, class TDeletor>
impl::DeleteRangeFunctor<TContainer, TDeletor>
GetRangeDeletor(TContainer & cont, TDeletor const & deletor)
{
return impl::DeleteRangeFunctor<TContainer, TDeletor>(cont, deletor);
}
struct NoopFunctor
{

View file

@ -5,6 +5,7 @@
#include "../base/string_utils.hpp"
#include "../base/stl_add.hpp"
#include "../base/scope_guard.hpp"
//#include "../base/logging.hpp"
#include "../std/algorithm.hpp"
@ -142,8 +143,7 @@ void PrefixMatchInTrie(TrieIterator const & trieRoot,
}
// 'f' can throw an exception. So be prepared to delete unprocessed elements.
DeleteRangeGuard<QueueT> doDelete(trieQueue);
UNUSED_VALUE(doDelete);
MY_SCOPE_GUARD(doDelete, GetRangeDeletor(trieQueue, DeleteFunctor()));
while (!trieQueue.empty())
{