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:
parent
e4fd66b192
commit
abb4a6781f
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue