StringList.h 2.5 KB

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