StringList.h 4.0 KB

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