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