Updated French translations for GOLD and LD
[binutils-gdb.git] / gdbsupport / buildargv.h
blob8fdd085b3867a967c76d20f9ba32adfbe7a35fea
1 /* RAII wrapper for buildargv
3 Copyright (C) 2021-2024 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'. */
28 class gdb_argv
30 public:
32 /* A constructor that initializes to NULL. */
34 gdb_argv ()
35 : m_argv (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)
43 : m_argv (NULL)
45 reset (str);
48 /* A constructor that takes ownership of an existing array. */
50 explicit gdb_argv (char **array)
51 : m_argv (array)
55 gdb_argv (const gdb_argv &) = delete;
56 gdb_argv &operator= (const gdb_argv &) = delete;
58 gdb_argv &operator= (gdb_argv &&other)
60 freeargv (m_argv);
61 m_argv = other.m_argv;
62 other.m_argv = nullptr;
63 return *this;
66 gdb_argv (gdb_argv &&other)
68 m_argv = other.m_argv;
69 other.m_argv = nullptr;
72 ~gdb_argv ()
74 freeargv (m_argv);
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
82 NULL. */
84 void reset (const char *str)
86 char **argv = buildargv (str);
87 freeargv (m_argv);
88 m_argv = argv;
91 /* Return the underlying array. */
93 char **get ()
95 return m_argv;
98 const char * const * get () const
100 return m_argv;
103 /* Return the underlying array, transferring ownership to the
104 caller. */
106 ATTRIBUTE_UNUSED_RESULT char **release ()
108 char **result = m_argv;
109 m_argv = NULL;
110 return result;
113 /* Return the number of items in the array. */
115 int count () const
117 return countargv (m_argv);
120 /* Index into the array. */
122 char *operator[] (int arg)
124 gdb_assert (m_argv != NULL);
125 return m_argv[arg];
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)
143 int size = count ();
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)
160 int size = count ();
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. */
175 iterator begin ()
177 return m_argv;
180 /* Return an iterator pointing to the end of the array. */
182 iterator end ()
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;
197 private:
199 /* The wrapped array. */
201 char **m_argv;
204 #endif /* GDBSUPPORT_BUILDARGV_H */