From ac26d4b343aece40b6b9a931ab619fc88d4b9492 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Tue, 10 Feb 2015 21:50:16 +0100 Subject: Split cmAlgorithms into a separate header file. --- Source/CPack/cmCPackGeneratorFactory.cxx | 1 + Source/CTest/cmCTestGIT.cxx | 1 + Source/cmAlgorithms.h | 151 +++++++++++++++++++++++++++++++ Source/cmExportSet.cxx | 1 + Source/cmExportSetMap.cxx | 1 + Source/cmFileLockPool.cxx | 1 + Source/cmInstalledFile.h | 1 + Source/cmRST.cxx | 1 + Source/cmStandardIncludes.h | 134 --------------------------- Source/cmSystemTools.cxx | 1 + Source/cmVariableWatch.cxx | 2 + 11 files changed, 161 insertions(+), 134 deletions(-) create mode 100644 Source/cmAlgorithms.h diff --git a/Source/CPack/cmCPackGeneratorFactory.cxx b/Source/CPack/cmCPackGeneratorFactory.cxx index a07c29a..4626142 100644 --- a/Source/CPack/cmCPackGeneratorFactory.cxx +++ b/Source/CPack/cmCPackGeneratorFactory.cxx @@ -46,6 +46,7 @@ #endif #include "cmCPackLog.h" +#include "cmAlgorithms.h" //---------------------------------------------------------------------- diff --git a/Source/CTest/cmCTestGIT.cxx b/Source/CTest/cmCTestGIT.cxx index 98bc9d7..6b84bab 100644 --- a/Source/CTest/cmCTestGIT.cxx +++ b/Source/CTest/cmCTestGIT.cxx @@ -13,6 +13,7 @@ #include "cmCTest.h" #include "cmSystemTools.h" +#include "cmAlgorithms.h" #include "cmXMLSafe.h" #include diff --git a/Source/cmAlgorithms.h b/Source/cmAlgorithms.h new file mode 100644 index 0000000..4938140 --- /dev/null +++ b/Source/cmAlgorithms.h @@ -0,0 +1,151 @@ +/*============================================================================ + CMake - Cross Platform Makefile Generator + Copyright 2015 Stephen Kelly + + Distributed under the OSI-approved BSD License (the "License"); + see accompanying file Copyright.txt for details. + + This software is distributed WITHOUT ANY WARRANTY; without even the + implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the License for more information. +============================================================================*/ +#ifndef cmAlgorithms_h +#define cmAlgorithms_h + +#include "cmStandardIncludes.h" + +inline bool cmHasLiteralPrefixImpl(const std::string &str1, + const char *str2, + size_t N) +{ + return strncmp(str1.c_str(), str2, N) == 0; +} + +inline bool cmHasLiteralPrefixImpl(const char* str1, + const char *str2, + size_t N) +{ + return strncmp(str1, str2, N) == 0; +} + +inline bool cmHasLiteralSuffixImpl(const std::string &str1, + const char *str2, + size_t N) +{ + size_t len = str1.size(); + return len >= N && strcmp(str1.c_str() + len - N, str2) == 0; +} + +inline bool cmHasLiteralSuffixImpl(const char* str1, + const char* str2, + size_t N) +{ + size_t len = strlen(str1); + return len >= N && strcmp(str1 + len - N, str2) == 0; +} + +template +const T* cmArrayBegin(const T (&a)[N]) { return a; } +template +const T* cmArrayEnd(const T (&a)[N]) { return a + N; } +template +size_t cmArraySize(const T (&)[N]) { return N; } + +template +bool cmHasLiteralPrefix(T str1, const char (&str2)[N]) +{ + return cmHasLiteralPrefixImpl(str1, str2, N - 1); +} + +template +bool cmHasLiteralSuffix(T str1, const char (&str2)[N]) +{ + return cmHasLiteralSuffixImpl(str1, str2, N - 1); +} + +struct cmStrCmp { + cmStrCmp(const char *test) : m_test(test) {} + cmStrCmp(const std::string &test) : m_test(test) {} + + bool operator()(const std::string& input) const + { + return m_test == input; + } + + bool operator()(const char * input) const + { + return strcmp(input, m_test.c_str()) == 0; + } + +private: + const std::string m_test; +}; + +namespace ContainerAlgorithms { + +template +struct cmIsPair +{ + enum { value = false }; +}; + +template +struct cmIsPair > +{ + enum { value = true }; +}; + +template::value> +struct DefaultDeleter +{ + void operator()(typename Container::value_type value) { + delete value; + } +}; + +template +struct DefaultDeleter +{ + void operator()(typename Container::value_type value) { + delete value.second; + } +}; + +} + +template +void cmDeleteAll(Container const& c) +{ + std::for_each(c.begin(), c.end(), + ContainerAlgorithms::DefaultDeleter()); +} + +template +std::string cmJoin(Range const& r, const char* delimiter) +{ + if (r.empty()) + { + return std::string(); + } + std::ostringstream os; + typedef typename Range::value_type ValueType; + typedef typename Range::const_iterator InputIt; + InputIt first = r.begin(); + InputIt last = r.end(); + --last; + std::copy(first, last, + std::ostream_iterator(os, delimiter)); + + os << *last; + + return os.str(); +} + +template +std::string cmJoin(Range const& r, std::string delimiter) +{ + return cmJoin(r, delimiter.c_str()); +}; + +#endif diff --git a/Source/cmExportSet.cxx b/Source/cmExportSet.cxx index 14812e4..4148fb5 100644 --- a/Source/cmExportSet.cxx +++ b/Source/cmExportSet.cxx @@ -12,6 +12,7 @@ #include "cmExportSet.h" #include "cmTargetExport.h" +#include "cmAlgorithms.h" cmExportSet::~cmExportSet() { diff --git a/Source/cmExportSetMap.cxx b/Source/cmExportSetMap.cxx index 14c4458..cf431c6 100644 --- a/Source/cmExportSetMap.cxx +++ b/Source/cmExportSetMap.cxx @@ -12,6 +12,7 @@ #include "cmExportSetMap.h" #include "cmExportSet.h" +#include "cmAlgorithms.h" cmExportSet* cmExportSetMap::operator[](const std::string &name) { diff --git a/Source/cmFileLockPool.cxx b/Source/cmFileLockPool.cxx index cf8e9a9..3710eb0 100644 --- a/Source/cmFileLockPool.cxx +++ b/Source/cmFileLockPool.cxx @@ -16,6 +16,7 @@ #include "cmFileLock.h" #include "cmFileLockResult.h" +#include "cmAlgorithms.h" cmFileLockPool::cmFileLockPool() { diff --git a/Source/cmInstalledFile.h b/Source/cmInstalledFile.h index 503f92c..cdb0866 100644 --- a/Source/cmInstalledFile.h +++ b/Source/cmInstalledFile.h @@ -13,6 +13,7 @@ #define cmInstalledFile_h #include "cmGeneratorExpression.h" +#include "cmAlgorithms.h" /** \class cmInstalledFile * \brief Represents a file intended for installation. diff --git a/Source/cmRST.cxx b/Source/cmRST.cxx index f4607c6..d20d999 100644 --- a/Source/cmRST.cxx +++ b/Source/cmRST.cxx @@ -12,6 +12,7 @@ #include "cmRST.h" #include "cmSystemTools.h" +#include "cmAlgorithms.h" #include "cmVersion.h" #include #include diff --git a/Source/cmStandardIncludes.h b/Source/cmStandardIncludes.h index 77b4f62..a9796b9 100644 --- a/Source/cmStandardIncludes.h +++ b/Source/cmStandardIncludes.h @@ -131,138 +131,4 @@ static thisClass* SafeDownCast(cmObject *c) \ } \ class cmTypeMacro_UseTrailingSemicolon -template -std::string cmJoin(Range const& r, const char* delimiter) -{ - if (r.empty()) - { - return std::string(); - } - std::ostringstream os; - typedef typename Range::value_type ValueType; - typedef typename Range::const_iterator InputIt; - InputIt first = r.begin(); - InputIt last = r.end(); - --last; - std::copy(first, last, - std::ostream_iterator(os, delimiter)); - - os << *last; - - return os.str(); -} - -template -std::string cmJoin(Range const& r, std::string delimiter) -{ - return cmJoin(r, delimiter.c_str()); -}; - -inline bool cmHasLiteralPrefixImpl(const std::string &str1, - const char *str2, - size_t N) -{ - return strncmp(str1.c_str(), str2, N) == 0; -} - -inline bool cmHasLiteralPrefixImpl(const char* str1, - const char *str2, - size_t N) -{ - return strncmp(str1, str2, N) == 0; -} - -inline bool cmHasLiteralSuffixImpl(const std::string &str1, - const char *str2, - size_t N) -{ - size_t len = str1.size(); - return len >= N && strcmp(str1.c_str() + len - N, str2) == 0; -} - -inline bool cmHasLiteralSuffixImpl(const char* str1, - const char* str2, - size_t N) -{ - size_t len = strlen(str1); - return len >= N && strcmp(str1 + len - N, str2) == 0; -} - -template -const T* cmArrayBegin(const T (&a)[N]) { return a; } -template -const T* cmArrayEnd(const T (&a)[N]) { return a + N; } -template -size_t cmArraySize(const T (&)[N]) { return N; } - -template -bool cmHasLiteralPrefix(T str1, const char (&str2)[N]) -{ - return cmHasLiteralPrefixImpl(str1, str2, N - 1); -} - -template -bool cmHasLiteralSuffix(T str1, const char (&str2)[N]) -{ - return cmHasLiteralSuffixImpl(str1, str2, N - 1); -} - -struct cmStrCmp { - cmStrCmp(const char *test) : m_test(test) {} - cmStrCmp(const std::string &test) : m_test(test) {} - - bool operator()(const std::string& input) const - { - return m_test == input; - } - - bool operator()(const char * input) const - { - return strcmp(input, m_test.c_str()) == 0; - } - -private: - const std::string m_test; -}; - -namespace ContainerAlgorithms { - -template -struct cmIsPair -{ - enum { value = false }; -}; - -template -struct cmIsPair > -{ - enum { value = true }; -}; - -template::value> -struct DefaultDeleter -{ - void operator()(typename Container::value_type value) { - delete value; - } -}; - -template -struct DefaultDeleter -{ - void operator()(typename Container::value_type value) { - delete value.second; - } -}; - -} - -template -void cmDeleteAll(Container const& c) -{ - std::for_each(c.begin(), c.end(), - ContainerAlgorithms::DefaultDeleter()); -} - #endif diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 7dd6121..bf496e9 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -11,6 +11,7 @@ ============================================================================*/ #include "cmSystemTools.h" +#include "cmAlgorithms.h" #include #include #include diff --git a/Source/cmVariableWatch.cxx b/Source/cmVariableWatch.cxx index b8a6df2..57dde31 100644 --- a/Source/cmVariableWatch.cxx +++ b/Source/cmVariableWatch.cxx @@ -11,6 +11,8 @@ ============================================================================*/ #include "cmVariableWatch.h" +#include "cmAlgorithms.h" + static const char* const cmVariableWatchAccessStrings[] = { "READ_ACCESS", -- cgit v0.12