StringList.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright (C) 2009-2022, 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. ANKI_CHECK_FORMAT(2, 3)
  43. void pushBackSprintf(Allocator alloc, const Char* fmt, ...);
  44. /// Push at the beginning of the list a formated string.
  45. ANKI_CHECK_FORMAT(2, 3)
  46. void pushFrontSprintf(Allocator alloc, const Char* fmt, ...);
  47. /// Push back plain CString.
  48. void pushBack(Allocator alloc, CString cstr)
  49. {
  50. String str;
  51. str.create(alloc, cstr);
  52. Base::emplaceBack(alloc);
  53. Base::getBack() = std::move(str);
  54. }
  55. /// Push front plain CString
  56. void pushFront(Allocator alloc, CString cstr)
  57. {
  58. String str;
  59. str.create(alloc, cstr);
  60. Base::emplaceFront(alloc);
  61. Base::getFront() = std::move(str);
  62. }
  63. /// Split a string using a separator (@a separator) and return these strings in a string list.
  64. void splitString(Allocator alloc, const CString& s, const Char separator, Bool keepEmpty = false);
  65. };
  66. /// String list with automatic destruction.
  67. class StringListAuto : public StringList
  68. {
  69. public:
  70. using Base = StringList;
  71. using Allocator = typename Base::Allocator;
  72. /// Create using an allocator.
  73. StringListAuto(Allocator alloc)
  74. : Base()
  75. , m_alloc(alloc)
  76. {
  77. }
  78. /// Move.
  79. StringListAuto(StringListAuto&& b)
  80. : Base()
  81. {
  82. move(b);
  83. }
  84. ~StringListAuto()
  85. {
  86. Base::destroy(m_alloc);
  87. }
  88. /// Move.
  89. StringListAuto& operator=(StringListAuto&& b)
  90. {
  91. move(b);
  92. return *this;
  93. }
  94. /// Destroy.
  95. void destroy()
  96. {
  97. Base::destroy(m_alloc);
  98. }
  99. /// Push at the end of the list a formated string
  100. ANKI_CHECK_FORMAT(1, 2)
  101. void pushBackSprintf(const Char* fmt, ...);
  102. /// Push at the beginning of the list a formated string
  103. ANKI_CHECK_FORMAT(1, 2)
  104. void pushFrontSprintf(const Char* fmt, ...);
  105. /// Push back plain CString.
  106. void pushBack(CString cstr)
  107. {
  108. Base::pushBack(m_alloc, cstr);
  109. }
  110. /// Push front plain CString.
  111. void pushFront(CString cstr)
  112. {
  113. Base::pushFront(m_alloc, cstr);
  114. }
  115. /// Pop front element.
  116. void popFront()
  117. {
  118. getFront().destroy(m_alloc);
  119. Base::popFront(m_alloc);
  120. }
  121. /// Split a string using a separator (@a separator) and return these strings in a string list.
  122. void splitString(const CString& s, const Base::Char separator, Bool keepEmpty = false)
  123. {
  124. Base::splitString(m_alloc, s, separator, keepEmpty);
  125. }
  126. private:
  127. Allocator m_alloc;
  128. void move(StringListAuto& b)
  129. {
  130. Base::operator=(std::move(b));
  131. m_alloc = std::move(b.m_alloc);
  132. }
  133. };
  134. /// @}
  135. } // end namespace anki