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