Add a generic algorithm for deleting items in a container.

Specialize for std::map types to delete the second element from
the iterator.  This is not quite general enough that it can
be used everywhere, because CMake inherits from std::map and
creates typedefs with custom comparison functors etc, which
can not use this algorithm.
This commit is contained in:
Stephen Kelly 2015-01-04 14:53:24 +01:00
parent e4fd66b192
commit abb4a6781f
1 changed files with 27 additions and 0 deletions

View File

@ -237,4 +237,31 @@ private:
const std::string m_test;
};
namespace ContainerAlgorithms {
template<typename Container>
struct DefaultDeleter
{
void operator()(typename Container::value_type value) {
delete value;
}
};
template<typename K, typename V>
struct DefaultDeleter<std::map<K, V> >
{
void operator()(typename std::map<K, V>::value_type value) {
delete value.second;
}
};
}
template<typename Container>
void cmDeleteAll(Container const& c)
{
std::for_each(c.begin(), c.end(),
ContainerAlgorithms::DefaultDeleter<Container>());
}
#endif