StringList.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Util/String.h>
  7. #include <AnKi/Util/List.h>
  8. #include <algorithm>
  9. namespace anki
  10. {
  11. /// @addtogroup util_containers
  12. /// @{
  13. /// A simple convenience class for string lists
  14. class StringList : public List<String>
  15. {
  16. public:
  17. using Char = char; ///< Char type
  18. using Base = List<String>; ///< Base
  19. using Allocator = GenericMemoryPoolAllocator<Char>;
  20. /// Sort method for sortAll().
  21. enum class Sort
  22. {
  23. ASCENDING,
  24. DESCENDING
  25. };
  26. // Use the base constructors
  27. using Base::Base;
  28. explicit operator Bool() const
  29. {
  30. return !Base::isEmpty();
  31. }
  32. void destroy(Allocator alloc);
  33. /// Join all the elements into a single big string using a the seperator @a separator
  34. void join(Allocator alloc, const CString& separator, String& out) const;
  35. /// Join all the elements into a single big string using a the seperator @a separator
  36. void join(const CString& separator, StringAuto& out) const;
  37. /// Returns the index position of the last occurrence of @a value in the list.
  38. /// @return -1 of not found
  39. I getIndexOf(const CString& value) const;
  40. /// Sort the string list
  41. void sortAll(const Sort method = Sort::ASCENDING);
  42. /// Push at the end of the list a formated string.
  43. template<typename... TArgs>
  44. void pushBackSprintf(Allocator alloc, CString fmt, TArgs... args)
  45. {
  46. String str;
  47. str.sprintf(alloc, fmt, args...);
  48. Base::emplaceBack(alloc);
  49. Base::getBack() = std::move(str);
  50. }
  51. /// Push at the beginning of the list a formated string.
  52. template<typename... TArgs>
  53. void pushFrontSprintf(Allocator alloc, CString fmt, TArgs... args)
  54. {
  55. String str;
  56. str.sprintf(alloc, fmt, args...);
  57. Base::emplaceFront(alloc);
  58. Base::getFront() = std::move(str);
  59. }
  60. /// Push back plain CString.
  61. void pushBack(Allocator alloc, CString cstr)
  62. {
  63. String str;
  64. str.create(alloc, cstr);
  65. Base::emplaceBack(alloc);
  66. Base::getBack() = std::move(str);
  67. }
  68. /// Push front plain CString
  69. void pushFront(Allocator alloc, CString cstr)
  70. {
  71. String str;
  72. str.create(alloc, cstr);
  73. Base::emplaceFront(alloc);
  74. Base::getFront() = std::move(str);
  75. }
  76. /// Split a string using a separator (@a separator) and return these strings in a string list.
  77. void splitString(Allocator alloc, const CString& s, const Char separator, Bool keepEmpty = false);
  78. };
  79. /// String list with automatic destruction.
  80. class StringListAuto : public StringList
  81. {
  82. public:
  83. using Base = StringList;
  84. using Allocator = typename Base::Allocator;
  85. /// Create using an allocator.
  86. StringListAuto(Allocator alloc)
  87. : Base()
  88. , m_alloc(alloc)
  89. {
  90. }
  91. /// Move.
  92. StringListAuto(StringListAuto&& b)
  93. : Base()
  94. {
  95. move(b);
  96. }
  97. ~StringListAuto()
  98. {
  99. Base::destroy(m_alloc);
  100. }
  101. /// Move.
  102. StringListAuto& operator=(StringListAuto&& b)
  103. {
  104. move(b);
  105. return *this;
  106. }
  107. /// Destroy.
  108. void destroy()
  109. {
  110. Base::destroy(m_alloc);
  111. }
  112. /// Push at the end of the list a formated string
  113. template<typename... TArgs>
  114. void pushBackSprintf(CString fmt, TArgs... args)
  115. {
  116. Base::pushBackSprintf(m_alloc, fmt, args...);
  117. }
  118. /// Push at the beginning of the list a formated string
  119. template<typename... TArgs>
  120. void pushFrontSprintf(CString fmt, TArgs... args)
  121. {
  122. Base::pushFrontSprintf(m_alloc, fmt, args...);
  123. }
  124. /// Push back plain CString.
  125. void pushBack(CString cstr)
  126. {
  127. Base::pushBack(m_alloc, cstr);
  128. }
  129. /// Push front plain CString.
  130. void pushFront(CString cstr)
  131. {
  132. Base::pushFront(m_alloc, cstr);
  133. }
  134. /// Pop front element.
  135. void popFront()
  136. {
  137. getFront().destroy(m_alloc);
  138. Base::popFront(m_alloc);
  139. }
  140. /// Split a string using a separator (@a separator) and return these strings in a string list.
  141. void splitString(const CString& s, const Base::Char separator, Bool keepEmpty = false)
  142. {
  143. Base::splitString(m_alloc, s, separator, keepEmpty);
  144. }
  145. private:
  146. Allocator m_alloc;
  147. void move(StringListAuto& b)
  148. {
  149. Base::operator=(std::move(b));
  150. m_alloc = std::move(b.m_alloc);
  151. }
  152. };
  153. /// @}
  154. } // end namespace anki