1 /* Wrap std::vector<char *> that owns the 'char *' strings. 2 3 Copyright (C) 2025 Free Software Foundation, Inc. 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 20 #ifndef GDBSUPPORT_GDB_ARGV_VEC_H 21 #define GDBSUPPORT_GDB_ARGV_VEC_H 22 23 #include "gdbsupport/common-utils.h" 24 #include <vector> 25 26 namespace gdb 27 { 28 29 /* A class that wraps around a vector of 'char *' strings. The class owns 30 the strings within the vector and will release them by calling xfree() 31 on each string when this classes destructor is called. */ 32 class argv_vec final 33 { 34 /* Vector of strings. This class owns the strings within the vector. 35 This is at the start of the class so we can use decltype(). */ 36 std::vector<char *> m_args; 37 38 public: 39 40 using value_type = decltype (m_args)::value_type; 41 using iterator = decltype (m_args)::iterator; 42 using const_iterator = decltype (m_args)::const_iterator; 43 using reference = decltype (m_args)::reference; 44 using const_reference = decltype (m_args)::const_reference; 45 46 argv_vec () 47 { 48 /* Nothing. */ 49 } 50 51 /* Move the owned strings from OTHER. */ 52 argv_vec (argv_vec &&other) 53 { 54 this->m_args = std::move (other.m_args); 55 } 56 57 /* Don't currently support copying the strings from OTHER. */ 58 argv_vec (const argv_vec &other) = delete; 59 60 /* Move elements from OTHER. Free currently owned strings. */ 61 argv_vec &operator= (argv_vec &&other) 62 { 63 free_vector_argv (m_args); 64 this->m_args = std::move (other.m_args); 65 return *this; 66 } 67 68 /* Don't currently support copying the strings from OTHER. */ 69 argv_vec &operator= (const argv_vec &other) = delete; 70 71 /* Release the owned strings. */ 72 ~argv_vec () 73 { 74 free_vector_argv (m_args); 75 } 76 77 /* Append VALUE to the end of m_args. This class takes ownership of 78 VALUE, and will release VALUE by calling xfree() on it when this 79 object is destroyed. */ 80 void push_back (const value_type &value) 81 { 82 m_args.push_back (value); 83 } 84 85 /* Append VALUE to the end of m_args. This class takes ownership of 86 VALUE, and will release VALUE by calling xfree() on it when this 87 object is destroyed. */ 88 void push_back (value_type &&value) 89 { 90 m_args.push_back (value); 91 } 92 93 /* Non constant iterator to start of m_args. */ 94 iterator begin () 95 { 96 return m_args.begin (); 97 } 98 99 /* Non constant iterator to end of m_args. */ 100 iterator end () 101 { 102 return m_args.end (); 103 } 104 105 /* Constant iterator to start of m_args. */ 106 const_iterator begin () const 107 { 108 return m_args.begin (); 109 } 110 111 /* Constant iterator to end of m_args. */ 112 const_iterator end () const 113 { 114 return m_args.end (); 115 } 116 117 /* Return contiguous block of 'char *' pointers. Ideally this would be 118 const, but the execve interface to which this data is passed doesn't 119 accept 'const char **'. */ 120 char **argv () 121 { 122 return m_args.data (); 123 } 124 125 /* Return a constant reference to the underlying vector. */ 126 const decltype (m_args) &get () const 127 { 128 return m_args; 129 } 130 131 /* Return true when m_args is empty. */ 132 bool empty () const 133 { 134 return m_args.empty (); 135 } 136 }; 137 } /* namespac gdb */ 138 139 #endif /* GDBSUPPORT_GDB_ARGV_VEC_H */ 140