gdb_vecs.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Some commonly-used VEC types.
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef COMMON_GDB_VECS_H
  15. #define COMMON_GDB_VECS_H
  16. /* Split STR, a list of DELIMITER-separated fields, into a char pointer vector.
  17. You may modify the returned strings. */
  18. extern std::vector<gdb::unique_xmalloc_ptr<char>>
  19. delim_string_to_char_ptr_vec (const char *str, char delimiter);
  20. /* Like dirnames_to_char_ptr_vec, but append the directories to *VECP. */
  21. extern void dirnames_to_char_ptr_vec_append
  22. (std::vector<gdb::unique_xmalloc_ptr<char>> *vecp, const char *dirnames);
  23. /* Split DIRNAMES by DIRNAME_SEPARATOR delimiter and return a list of all the
  24. elements in their original order. For empty string ("") DIRNAMES return
  25. list of one empty string ("") element.
  26. You may modify the returned strings. */
  27. extern std::vector<gdb::unique_xmalloc_ptr<char>>
  28. dirnames_to_char_ptr_vec (const char *dirnames);
  29. /* Remove the element pointed by iterator IT from VEC, not preserving the order
  30. of the remaining elements. Return the removed element. */
  31. template <typename T>
  32. T
  33. unordered_remove (std::vector<T> &vec, typename std::vector<T>::iterator it)
  34. {
  35. gdb_assert (it >= vec.begin () && it < vec.end ());
  36. T removed = std::move (*it);
  37. if (it != vec.end () - 1)
  38. *it = std::move (vec.back ());
  39. vec.pop_back ();
  40. return removed;
  41. }
  42. /* Remove the element at position IX from VEC, not preserving the order of the
  43. remaining elements. Return the removed element. */
  44. template <typename T>
  45. T
  46. unordered_remove (std::vector<T> &vec, typename std::vector<T>::size_type ix)
  47. {
  48. gdb_assert (ix < vec.size ());
  49. return unordered_remove (vec, vec.begin () + ix);
  50. }
  51. /* Remove the element at position IX from VEC, preserving the order the
  52. remaining elements. Return the removed element. */
  53. template <typename T>
  54. T
  55. ordered_remove (std::vector<T> &vec, typename std::vector<T>::size_type ix)
  56. {
  57. gdb_assert (ix < vec.size ());
  58. T removed = std::move (vec[ix]);
  59. vec.erase (vec.begin () + ix);
  60. return removed;
  61. }
  62. #endif /* COMMON_GDB_VECS_H */