buildargv.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* RAII wrapper for buildargv
  2. Copyright (C) 2021 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef GDBSUPPORT_BUILDARGV_H
  15. #define GDBSUPPORT_BUILDARGV_H
  16. #include "libiberty.h"
  17. /* A wrapper for an array of char* that was allocated in the way that
  18. 'buildargv' does, and should be freed with 'freeargv'. */
  19. class gdb_argv
  20. {
  21. public:
  22. /* A constructor that initializes to NULL. */
  23. gdb_argv ()
  24. : m_argv (NULL)
  25. {
  26. }
  27. /* A constructor that calls buildargv on STR. STR may be NULL, in
  28. which case this object is initialized with a NULL array. */
  29. explicit gdb_argv (const char *str)
  30. : m_argv (NULL)
  31. {
  32. reset (str);
  33. }
  34. /* A constructor that takes ownership of an existing array. */
  35. explicit gdb_argv (char **array)
  36. : m_argv (array)
  37. {
  38. }
  39. gdb_argv (const gdb_argv &) = delete;
  40. gdb_argv &operator= (const gdb_argv &) = delete;
  41. gdb_argv &operator= (gdb_argv &&other)
  42. {
  43. freeargv (m_argv);
  44. m_argv = other.m_argv;
  45. other.m_argv = nullptr;
  46. return *this;
  47. }
  48. gdb_argv (gdb_argv &&other)
  49. {
  50. m_argv = other.m_argv;
  51. other.m_argv = nullptr;
  52. }
  53. ~gdb_argv ()
  54. {
  55. freeargv (m_argv);
  56. }
  57. /* Call buildargv on STR, storing the result in this object. Any
  58. previous state is freed. STR may be NULL, in which case this
  59. object is reset with a NULL array. If buildargv fails due to
  60. out-of-memory, call malloc_failure. Therefore, the value is
  61. guaranteed to be non-NULL, unless the parameter itself is
  62. NULL. */
  63. void reset (const char *str)
  64. {
  65. char **argv = buildargv (str);
  66. freeargv (m_argv);
  67. m_argv = argv;
  68. }
  69. /* Return the underlying array. */
  70. char **get ()
  71. {
  72. return m_argv;
  73. }
  74. const char * const * get () const
  75. {
  76. return m_argv;
  77. }
  78. /* Return the underlying array, transferring ownership to the
  79. caller. */
  80. ATTRIBUTE_UNUSED_RESULT char **release ()
  81. {
  82. char **result = m_argv;
  83. m_argv = NULL;
  84. return result;
  85. }
  86. /* Return the number of items in the array. */
  87. int count () const
  88. {
  89. return countargv (m_argv);
  90. }
  91. /* Index into the array. */
  92. char *operator[] (int arg)
  93. {
  94. gdb_assert (m_argv != NULL);
  95. return m_argv[arg];
  96. }
  97. /* Return the arguments array as an array view. */
  98. gdb::array_view<char *> as_array_view ()
  99. {
  100. return gdb::array_view<char *> (this->get (), this->count ());
  101. }
  102. gdb::array_view<const char * const> as_array_view () const
  103. {
  104. return gdb::array_view<const char * const> (this->get (), this->count ());
  105. }
  106. /* Append arguments to this array. */
  107. void append (gdb_argv &&other)
  108. {
  109. int size = count ();
  110. int argc = other.count ();
  111. m_argv = XRESIZEVEC (char *, m_argv, (size + argc + 1));
  112. for (int argi = 0; argi < argc; argi++)
  113. {
  114. /* Transfer ownership of the string. */
  115. m_argv[size++] = other.m_argv[argi];
  116. /* Ensure that destruction of OTHER works correctly. */
  117. other.m_argv[argi] = nullptr;
  118. }
  119. m_argv[size] = nullptr;
  120. }
  121. /* Append arguments to this array. */
  122. void append (const gdb_argv &other)
  123. {
  124. int size = count ();
  125. int argc = other.count ();
  126. m_argv = XRESIZEVEC (char *, m_argv, (size + argc + 1));
  127. for (int argi = 0; argi < argc; argi++)
  128. m_argv[size++] = xstrdup (other.m_argv[argi]);
  129. m_argv[size] = nullptr;
  130. }
  131. /* The iterator type. */
  132. typedef char **iterator;
  133. /* Return an iterator pointing to the start of the array. */
  134. iterator begin ()
  135. {
  136. return m_argv;
  137. }
  138. /* Return an iterator pointing to the end of the array. */
  139. iterator end ()
  140. {
  141. return m_argv + count ();
  142. }
  143. bool operator!= (std::nullptr_t)
  144. {
  145. return m_argv != NULL;
  146. }
  147. bool operator== (std::nullptr_t)
  148. {
  149. return m_argv == NULL;
  150. }
  151. private:
  152. /* The wrapped array. */
  153. char **m_argv;
  154. };
  155. #endif /* GDBSUPPORT_BUILDARGV_H */