1 /* RAII wrapper for buildargv
3 Copyright (C) 2021-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #ifndef GDBSUPPORT_BUILDARGV_H
21 #define GDBSUPPORT_BUILDARGV_H
23 #include "libiberty.h"
25 /* A wrapper for an array of char* that was allocated in the way that
26 'buildargv' does, and should be freed with 'freeargv'. */
32 /* A constructor that initializes to NULL. */
39 /* A constructor that calls buildargv on STR. STR may be NULL, in
40 which case this object is initialized with a NULL array. */
42 explicit gdb_argv (const char *str
)
48 /* A constructor that takes ownership of an existing array. */
50 explicit gdb_argv (char **array
)
55 gdb_argv (const gdb_argv
&) = delete;
56 gdb_argv
&operator= (const gdb_argv
&) = delete;
58 gdb_argv
&operator= (gdb_argv
&&other
)
61 m_argv
= other
.m_argv
;
62 other
.m_argv
= nullptr;
66 gdb_argv (gdb_argv
&&other
)
68 m_argv
= other
.m_argv
;
69 other
.m_argv
= nullptr;
77 /* Call buildargv on STR, storing the result in this object. Any
78 previous state is freed. STR may be NULL, in which case this
79 object is reset with a NULL array. If buildargv fails due to
80 out-of-memory, call malloc_failure. Therefore, the value is
81 guaranteed to be non-NULL, unless the parameter itself is
84 void reset (const char *str
)
86 char **argv
= buildargv (str
);
91 /* Return the underlying array. */
98 const char * const * get () const
103 /* Return the underlying array, transferring ownership to the
106 ATTRIBUTE_UNUSED_RESULT
char **release ()
108 char **result
= m_argv
;
113 /* Return the number of items in the array. */
117 return countargv (m_argv
);
120 /* Index into the array. */
122 char *operator[] (int arg
)
124 gdb_assert (m_argv
!= NULL
);
128 /* Return the arguments array as an array view. */
130 gdb::array_view
<char *> as_array_view ()
132 return gdb::array_view
<char *> (this->get (), this->count ());
135 gdb::array_view
<const char * const> as_array_view () const
137 return gdb::array_view
<const char * const> (this->get (), this->count ());
140 /* Append arguments to this array. */
141 void append (gdb_argv
&&other
)
144 int argc
= other
.count ();
145 m_argv
= XRESIZEVEC (char *, m_argv
, (size
+ argc
+ 1));
147 for (int argi
= 0; argi
< argc
; argi
++)
149 /* Transfer ownership of the string. */
150 m_argv
[size
++] = other
.m_argv
[argi
];
151 /* Ensure that destruction of OTHER works correctly. */
152 other
.m_argv
[argi
] = nullptr;
154 m_argv
[size
] = nullptr;
157 /* Append arguments to this array. */
158 void append (const gdb_argv
&other
)
161 int argc
= other
.count ();
162 m_argv
= XRESIZEVEC (char *, m_argv
, (size
+ argc
+ 1));
164 for (int argi
= 0; argi
< argc
; argi
++)
165 m_argv
[size
++] = xstrdup (other
.m_argv
[argi
]);
166 m_argv
[size
] = nullptr;
169 /* The iterator type. */
171 typedef char **iterator
;
173 /* Return an iterator pointing to the start of the array. */
180 /* Return an iterator pointing to the end of the array. */
184 return m_argv
+ count ();
187 bool operator!= (std::nullptr_t
)
189 return m_argv
!= NULL
;
192 bool operator== (std::nullptr_t
)
194 return m_argv
== NULL
;
199 /* The wrapped array. */
204 #endif /* GDBSUPPORT_BUILDARGV_H */