cmAlgorithms.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2015 Stephen Kelly <[email protected]>
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef cmAlgorithms_h
  11. #define cmAlgorithms_h
  12. #include "cmStandardIncludes.h"
  13. inline bool cmHasLiteralPrefixImpl(const std::string &str1,
  14. const char *str2,
  15. size_t N)
  16. {
  17. return strncmp(str1.c_str(), str2, N) == 0;
  18. }
  19. inline bool cmHasLiteralPrefixImpl(const char* str1,
  20. const char *str2,
  21. size_t N)
  22. {
  23. return strncmp(str1, str2, N) == 0;
  24. }
  25. inline bool cmHasLiteralSuffixImpl(const std::string &str1,
  26. const char *str2,
  27. size_t N)
  28. {
  29. size_t len = str1.size();
  30. return len >= N && strcmp(str1.c_str() + len - N, str2) == 0;
  31. }
  32. inline bool cmHasLiteralSuffixImpl(const char* str1,
  33. const char* str2,
  34. size_t N)
  35. {
  36. size_t len = strlen(str1);
  37. return len >= N && strcmp(str1 + len - N, str2) == 0;
  38. }
  39. template<typename T, size_t N>
  40. const T* cmArrayBegin(const T (&a)[N]) { return a; }
  41. template<typename T, size_t N>
  42. const T* cmArrayEnd(const T (&a)[N]) { return a + N; }
  43. template<typename T, size_t N>
  44. size_t cmArraySize(const T (&)[N]) { return N; }
  45. template<typename T, size_t N>
  46. bool cmHasLiteralPrefix(T str1, const char (&str2)[N])
  47. {
  48. return cmHasLiteralPrefixImpl(str1, str2, N - 1);
  49. }
  50. template<typename T, size_t N>
  51. bool cmHasLiteralSuffix(T str1, const char (&str2)[N])
  52. {
  53. return cmHasLiteralSuffixImpl(str1, str2, N - 1);
  54. }
  55. struct cmStrCmp {
  56. cmStrCmp(const char *test) : m_test(test) {}
  57. cmStrCmp(const std::string &test) : m_test(test) {}
  58. bool operator()(const std::string& input) const
  59. {
  60. return m_test == input;
  61. }
  62. bool operator()(const char * input) const
  63. {
  64. return strcmp(input, m_test.c_str()) == 0;
  65. }
  66. private:
  67. const std::string m_test;
  68. };
  69. namespace ContainerAlgorithms {
  70. template<typename T>
  71. struct cmIsPair
  72. {
  73. enum { value = false };
  74. };
  75. template<typename K, typename V>
  76. struct cmIsPair<std::pair<K, V> >
  77. {
  78. enum { value = true };
  79. };
  80. template<typename Container,
  81. bool valueTypeIsPair = cmIsPair<typename Container::value_type>::value>
  82. struct DefaultDeleter
  83. {
  84. void operator()(typename Container::value_type value) {
  85. delete value;
  86. }
  87. };
  88. template<typename Container>
  89. struct DefaultDeleter<Container, /* valueTypeIsPair = */ true>
  90. {
  91. void operator()(typename Container::value_type value) {
  92. delete value.second;
  93. }
  94. };
  95. template<typename const_iterator_>
  96. struct Range
  97. {
  98. typedef const_iterator_ const_iterator;
  99. typedef typename std::iterator_traits<const_iterator>::value_type value_type;
  100. Range(const_iterator begin_, const_iterator end_)
  101. : Begin(begin_), End(end_) {}
  102. const_iterator begin() const { return Begin; }
  103. const_iterator end() const { return End; }
  104. bool empty() const { return std::distance(Begin, End) == 0; }
  105. Range& advance(cmIML_INT_intptr_t amount)
  106. {
  107. std::advance(Begin, amount);
  108. return *this;
  109. }
  110. Range& retreat(cmIML_INT_intptr_t amount)
  111. {
  112. std::advance(End, -amount);
  113. return *this;
  114. }
  115. private:
  116. const_iterator Begin;
  117. const_iterator End;
  118. };
  119. }
  120. template<typename Iter1, typename Iter2>
  121. ContainerAlgorithms::Range<Iter1> cmRange(Iter1 begin, Iter2 end)
  122. {
  123. return ContainerAlgorithms::Range<Iter1>(begin, end);
  124. }
  125. template<typename Range>
  126. ContainerAlgorithms::Range<typename Range::const_iterator>
  127. cmRange(Range const& range)
  128. {
  129. return ContainerAlgorithms::Range<typename Range::const_iterator>(
  130. range.begin(), range.end());
  131. }
  132. template<typename Container>
  133. void cmDeleteAll(Container const& c)
  134. {
  135. std::for_each(c.begin(), c.end(),
  136. ContainerAlgorithms::DefaultDeleter<Container>());
  137. }
  138. template<typename Range>
  139. std::string cmJoin(Range const& r, const char* delimiter)
  140. {
  141. if (r.empty())
  142. {
  143. return std::string();
  144. }
  145. std::ostringstream os;
  146. typedef typename Range::value_type ValueType;
  147. typedef typename Range::const_iterator InputIt;
  148. InputIt first = r.begin();
  149. InputIt last = r.end();
  150. --last;
  151. std::copy(first, last,
  152. std::ostream_iterator<ValueType>(os, delimiter));
  153. os << *last;
  154. return os.str();
  155. }
  156. template<typename Range>
  157. std::string cmJoin(Range const& r, std::string delimiter)
  158. {
  159. return cmJoin(r, delimiter.c_str());
  160. };
  161. #endif