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