diff options
author | Stephen Kelly <steveire@gmail.com> | 2015-02-10 20:50:16 (GMT) |
---|---|---|
committer | Stephen Kelly <steveire@gmail.com> | 2015-02-10 21:14:18 (GMT) |
commit | ac26d4b343aece40b6b9a931ab619fc88d4b9492 (patch) | |
tree | 7c13c270325312345d84cbd1a147893c8a88a628 /Source/cmAlgorithms.h | |
parent | cbffbf7437ea2d571e26bedee032c23be9347d94 (diff) | |
download | CMake-ac26d4b343aece40b6b9a931ab619fc88d4b9492.zip CMake-ac26d4b343aece40b6b9a931ab619fc88d4b9492.tar.gz CMake-ac26d4b343aece40b6b9a931ab619fc88d4b9492.tar.bz2 |
Split cmAlgorithms into a separate header file.
Diffstat (limited to 'Source/cmAlgorithms.h')
-rw-r--r-- | Source/cmAlgorithms.h | 151 |
1 files changed, 151 insertions, 0 deletions
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 <steveire@gmail.com> + + 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<typename T, size_t N> +const T* cmArrayBegin(const T (&a)[N]) { return a; } +template<typename T, size_t N> +const T* cmArrayEnd(const T (&a)[N]) { return a + N; } +template<typename T, size_t N> +size_t cmArraySize(const T (&)[N]) { return N; } + +template<typename T, size_t N> +bool cmHasLiteralPrefix(T str1, const char (&str2)[N]) +{ + return cmHasLiteralPrefixImpl(str1, str2, N - 1); +} + +template<typename T, size_t N> +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<typename T> +struct cmIsPair +{ + enum { value = false }; +}; + +template<typename K, typename V> +struct cmIsPair<std::pair<K, V> > +{ + enum { value = true }; +}; + +template<typename Container, + bool valueTypeIsPair = cmIsPair<typename Container::value_type>::value> +struct DefaultDeleter +{ + void operator()(typename Container::value_type value) { + delete value; + } +}; + +template<typename Container> +struct DefaultDeleter<Container, /* valueTypeIsPair = */ true> +{ + void operator()(typename Container::value_type value) { + delete value.second; + } +}; + +} + +template<typename Container> +void cmDeleteAll(Container const& c) +{ + std::for_each(c.begin(), c.end(), + ContainerAlgorithms::DefaultDeleter<Container>()); +} + +template<typename Range> +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<ValueType>(os, delimiter)); + + os << *last; + + return os.str(); +} + +template<typename Range> +std::string cmJoin(Range const& r, std::string delimiter) +{ + return cmJoin(r, delimiter.c_str()); +}; + +#endif |