From abb4a6781f96b28e4c30014915f566dee9130db7 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Sun, 4 Jan 2015 14:53:24 +0100 Subject: [PATCH] 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. --- Source/cmStandardIncludes.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Source/cmStandardIncludes.h b/Source/cmStandardIncludes.h index 2d988c9ef..251a04397 100644 --- a/Source/cmStandardIncludes.h +++ b/Source/cmStandardIncludes.h @@ -237,4 +237,31 @@ private: const std::string m_test; }; +namespace ContainerAlgorithms { + +template +struct DefaultDeleter +{ + void operator()(typename Container::value_type value) { + delete value; + } +}; + +template +struct DefaultDeleter > +{ + void operator()(typename std::map::value_type value) { + delete value.second; + } +}; + +} + +template +void cmDeleteAll(Container const& c) +{ + std::for_each(c.begin(), c.end(), + ContainerAlgorithms::DefaultDeleter()); +} + #endif