StringList.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  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. /// Sort method for sortAll().
  20. enum class Sort
  21. {
  22. ASCENDING,
  23. DESCENDING
  24. };
  25. // Use the base constructors
  26. using Base::Base;
  27. template<typename TAllocator>
  28. void destroy(TAllocator alloc);
  29. /// Join all the elements into a single big string using a the
  30. /// seperator @a separator
  31. template<typename TAllocator>
  32. void join(TAllocator alloc, const CString& separator, String& out) const;
  33. /// Returns the index position of the last occurrence of @a value in
  34. /// the list
  35. /// @return -1 of not found
  36. I getIndexOf(const CString& value) const;
  37. /// Sort the string list
  38. void sortAll(const Sort method = Sort::ASCENDING);
  39. /// Push at the end of the list a formated string
  40. template<typename TAllocator, typename... TArgs>
  41. void pushBackSprintf(TAllocator alloc, const TArgs&... args);
  42. /// Split a string using a separator (@a separator) and return these
  43. /// strings in a string list
  44. template<typename TAllocator>
  45. void splitString(TAllocator alloc, const CString& s, const Char separator);
  46. };
  47. /// String list with automatic destruction.
  48. class StringListAuto : public StringList
  49. {
  50. public:
  51. using Base = StringList;
  52. /// Create using an allocator.
  53. template<typename TAllocator>
  54. StringListAuto(TAllocator alloc)
  55. : Base()
  56. , m_alloc(alloc)
  57. {
  58. }
  59. /// Move.
  60. StringListAuto(StringListAuto&& b)
  61. : Base()
  62. {
  63. move(b);
  64. }
  65. ~StringListAuto()
  66. {
  67. Base::destroy(m_alloc);
  68. }
  69. /// Move.
  70. StringListAuto& operator=(StringListAuto&& b)
  71. {
  72. move(b);
  73. return *this;
  74. }
  75. /// Push at the end of the list a formated string
  76. template<typename... TArgs>
  77. void pushBackSprintf(const TArgs&... args)
  78. {
  79. Base::pushBackSprintf(m_alloc, args...);
  80. }
  81. /// Split a string using a separator (@a separator) and return these
  82. /// strings in a string list
  83. void splitString(const CString& s, const Base::Char separator)
  84. {
  85. Base::splitString(m_alloc, s, separator);
  86. }
  87. private:
  88. GenericMemoryPoolAllocator<Char> m_alloc;
  89. void move(StringListAuto& b)
  90. {
  91. Base::operator=(std::move(b));
  92. m_alloc = std::move(b.m_alloc);
  93. }
  94. };
  95. /// @}
  96. } // end namespace anki
  97. #include <anki/util/StringList.inl.h>