StringList.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (C) 2009-present, 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. #include <cstdarg>
  10. namespace anki {
  11. /// @addtogroup util_containers
  12. /// @{
  13. /// A simple convenience class for string lists
  14. template<typename TMemoryPool>
  15. class BaseStringList : public List<BaseString<TMemoryPool>, TMemoryPool>
  16. {
  17. public:
  18. using Char = char; ///< Char type
  19. using StringType = BaseString<TMemoryPool>;
  20. using Base = List<StringType, TMemoryPool>; ///< Base
  21. using MemoryPool = TMemoryPool;
  22. /// Sort method for sortAll().
  23. enum class Sort
  24. {
  25. kAscending,
  26. kDescending
  27. };
  28. BaseStringList(const TMemoryPool& pool = TMemoryPool())
  29. : Base(pool)
  30. {
  31. }
  32. BaseStringList(BaseStringList&& b)
  33. : Base(std::move(b))
  34. {
  35. }
  36. BaseStringList(const BaseStringList& b)
  37. : Base(b)
  38. {
  39. }
  40. BaseStringList& operator=(const BaseStringList& b)
  41. {
  42. static_cast<Base&>(*this) = static_cast<const Base&>(b);
  43. return *this;
  44. }
  45. BaseStringList& operator=(BaseStringList&& b)
  46. {
  47. static_cast<Base&>(*this) = std::move(static_cast<Base&>(b));
  48. return *this;
  49. }
  50. explicit operator Bool() const
  51. {
  52. return !Base::isEmpty();
  53. }
  54. /// Join all the elements into a single big string using a the seperator @a separator
  55. template<typename TStringMemoryPool>
  56. void join(const CString& separator, BaseString<TStringMemoryPool>& out) const;
  57. /// Returns the index position of the last occurrence of @a value in the list.
  58. /// @return -1 of not found
  59. I getIndexOf(const CString& value) const;
  60. /// Sort the string list
  61. void sortAll(const Sort method = Sort::kAscending);
  62. /// Push at the end of the list a formated string.
  63. ANKI_CHECK_FORMAT(1, 2)
  64. void pushBackSprintf(const Char* fmt, ...);
  65. /// Push at the beginning of the list a formated string.
  66. ANKI_CHECK_FORMAT(1, 2)
  67. void pushFrontSprintf(const Char* fmt, ...);
  68. /// Push back plain CString.
  69. typename Base::Iterator pushBack(CString cstr)
  70. {
  71. return Base::emplaceBack(cstr, Base::getMemoryPool());
  72. }
  73. /// Push front plain CString
  74. typename Base::Iterator pushFront(CString cstr)
  75. {
  76. return Base::emplaceFront(cstr, Base::getMemoryPool());
  77. }
  78. /// Split a string using a separator (@a separator) and return these strings in a string list.
  79. void splitString(const CString& s, const Char separator, Bool keepEmpty = false);
  80. };
  81. using StringList = BaseStringList<SingletonMemoryPoolWrapper<DefaultMemoryPool>>;
  82. /// @}
  83. } // end namespace anki
  84. #include <AnKi/Util/StringList.inl.h>