array-view.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* Copyright (C) 2017-2022 Free Software Foundation, Inc.
  2. This file is part of GDB.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef COMMON_ARRAY_VIEW_H
  14. #define COMMON_ARRAY_VIEW_H
  15. #include "traits.h"
  16. #include <algorithm>
  17. #include <type_traits>
  18. /* An array_view is an abstraction that provides a non-owning view
  19. over a sequence of contiguous objects.
  20. A way to put it is that array_view is to std::vector (and
  21. std::array and built-in arrays with rank==1) like std::string_view
  22. is to std::string.
  23. The main intent of array_view is to use it as function input
  24. parameter type, making it possible to pass in any sequence of
  25. contiguous objects, irrespective of whether the objects live on the
  26. stack or heap and what actual container owns them. Implicit
  27. construction from the element type is supported too, making it easy
  28. to call functions that expect an array of elements when you only
  29. have one element (usually on the stack). For example:
  30. struct A { .... };
  31. void function (gdb::array_view<A> as);
  32. std::vector<A> std_vec = ...;
  33. std::array<A, N> std_array = ...;
  34. A array[] = {...};
  35. A elem;
  36. function (std_vec);
  37. function (std_array);
  38. function (array);
  39. function (elem);
  40. Views can be either mutable or const. A const view is simply
  41. created by specifying a const T as array_view template parameter,
  42. in which case operator[] of non-const array_view objects ends up
  43. returning const references. Making the array_view itself const is
  44. analogous to making a pointer itself be const. I.e., disables
  45. re-seating the view/pointer.
  46. Since array_view objects are small (pointer plus size), and
  47. designed to be trivially copyable, they should generally be passed
  48. around by value.
  49. You can find unit tests covering the whole API in
  50. unittests/array-view-selftests.c. */
  51. namespace gdb {
  52. template <typename T>
  53. class array_view
  54. {
  55. /* True iff decayed T is the same as decayed U. E.g., we want to
  56. say that 'T&' is the same as 'const T'. */
  57. template <typename U>
  58. using IsDecayedT = typename std::is_same<typename std::decay<T>::type,
  59. typename std::decay<U>::type>;
  60. /* True iff decayed T is the same as decayed U, and 'U *' is
  61. implicitly convertible to 'T *'. This is a requirement for
  62. several methods. */
  63. template <typename U>
  64. using DecayedConvertible = gdb::And<IsDecayedT<U>,
  65. std::is_convertible<U *, T *>>;
  66. public:
  67. using value_type = T;
  68. using reference = T &;
  69. using const_reference = const T &;
  70. using size_type = size_t;
  71. /* Default construction creates an empty view. */
  72. constexpr array_view () noexcept
  73. : m_array (nullptr), m_size (0)
  74. {}
  75. /* Create an array view over a single object of the type of an
  76. array_view element. The created view as size==1. This is
  77. templated on U to allow constructing a array_view<const T> over a
  78. (non-const) T. The "convertible" requirement makes sure that you
  79. can't create an array_view<T> over a const T. */
  80. template<typename U,
  81. typename = Requires<DecayedConvertible<U>>>
  82. constexpr array_view (U &elem) noexcept
  83. : m_array (&elem), m_size (1)
  84. {}
  85. /* Same as above, for rvalue references. */
  86. template<typename U,
  87. typename = Requires<DecayedConvertible<U>>>
  88. constexpr array_view (U &&elem) noexcept
  89. : m_array (&elem), m_size (1)
  90. {}
  91. /* Create an array view from a pointer to an array and an element
  92. count. */
  93. template<typename U,
  94. typename = Requires<DecayedConvertible<U>>>
  95. constexpr array_view (U *array, size_t size) noexcept
  96. : m_array (array), m_size (size)
  97. {}
  98. /* Create an array view from a range. This is templated on both U
  99. an V to allow passing in a mix of 'const T *' and 'T *'. */
  100. template<typename U, typename V,
  101. typename = Requires<DecayedConvertible<U>>,
  102. typename = Requires<DecayedConvertible<V>>>
  103. constexpr array_view (U *begin, V *end) noexcept
  104. : m_array (begin), m_size (end - begin)
  105. {}
  106. /* Create an array view from an array. */
  107. template<typename U, size_t Size,
  108. typename = Requires<DecayedConvertible<U>>>
  109. constexpr array_view (U (&array)[Size]) noexcept
  110. : m_array (array), m_size (Size)
  111. {}
  112. /* Create an array view from a contiguous container. E.g.,
  113. std::vector and std::array. */
  114. template<typename Container,
  115. typename = Requires<gdb::Not<IsDecayedT<Container>>>,
  116. typename
  117. = Requires<DecayedConvertible
  118. <typename std::remove_pointer
  119. <decltype (std::declval<Container> ().data ())
  120. >::type>>,
  121. typename
  122. = Requires<std::is_convertible
  123. <decltype (std::declval<Container> ().size ()),
  124. size_type>>>
  125. constexpr array_view (Container &&c) noexcept
  126. : m_array (c.data ()), m_size (c.size ())
  127. {}
  128. /* Observer methods. Some of these can't be constexpr until we
  129. require C++14. */
  130. /*constexpr14*/ T *data () noexcept { return m_array; }
  131. constexpr const T *data () const noexcept { return m_array; }
  132. /*constexpr14*/ T *begin () noexcept { return m_array; }
  133. constexpr const T *begin () const noexcept { return m_array; }
  134. /*constexpr14*/ T *end () noexcept { return m_array + m_size; }
  135. constexpr const T *end () const noexcept { return m_array + m_size; }
  136. /*constexpr14*/ reference operator[] (size_t index) noexcept
  137. {
  138. #if defined(_GLIBCXX_DEBUG)
  139. gdb_assert (index < m_size);
  140. #endif
  141. return m_array[index];
  142. }
  143. constexpr const_reference operator[] (size_t index) const noexcept
  144. {
  145. #if defined(_GLIBCXX_DEBUG) && __cplusplus >= 201402L
  146. gdb_assert (index < m_size);
  147. #endif
  148. return m_array[index];
  149. }
  150. constexpr size_type size () const noexcept { return m_size; }
  151. constexpr bool empty () const noexcept { return m_size == 0; }
  152. /* Slice an array view. */
  153. /* Return a new array view over SIZE elements starting at START. */
  154. constexpr array_view<T> slice (size_type start, size_type size) const noexcept
  155. {
  156. #if defined(_GLIBCXX_DEBUG) && __cplusplus >= 201402L
  157. gdb_assert (start + size <= m_size);
  158. #endif
  159. return {m_array + start, size};
  160. }
  161. /* Return a new array view over all the elements after START,
  162. inclusive. */
  163. constexpr array_view<T> slice (size_type start) const noexcept
  164. {
  165. #if defined(_GLIBCXX_DEBUG) && __cplusplus >= 201402L
  166. gdb_assert (start <= m_size);
  167. #endif
  168. return {m_array + start, size () - start};
  169. }
  170. private:
  171. T *m_array;
  172. size_type m_size;
  173. };
  174. /* Copy the contents referenced by the array view SRC to the array view DEST.
  175. The two array views must have the same length. */
  176. template <typename U, typename T>
  177. void copy (gdb::array_view<U> src, gdb::array_view<T> dest)
  178. {
  179. gdb_assert (dest.size () == src.size ());
  180. if (dest.data () < src.data ())
  181. std::copy (src.begin (), src.end (), dest.begin ());
  182. else if (dest.data () > src.data ())
  183. std::copy_backward (src.begin (), src.end (), dest.end ());
  184. }
  185. /* Compare LHS and RHS for (deep) equality. That is, whether LHS and
  186. RHS have the same sizes, and whether each pair of elements of LHS
  187. and RHS at the same position compares equal. */
  188. template <typename T>
  189. bool
  190. operator== (const gdb::array_view<T> &lhs, const gdb::array_view<T> &rhs)
  191. {
  192. if (lhs.size () != rhs.size ())
  193. return false;
  194. for (size_t i = 0; i < lhs.size (); i++)
  195. if (!(lhs[i] == rhs[i]))
  196. return false;
  197. return true;
  198. }
  199. /* Compare two array_views for inequality. */
  200. template <typename T>
  201. bool
  202. operator!= (const gdb::array_view<T> &lhs, const gdb::array_view<T> &rhs)
  203. {
  204. return !(lhs == rhs);
  205. }
  206. /* Create an array view from a pointer to an array and an element
  207. count.
  208. This is useful as alternative to constructing an array_view using
  209. brace initialization when the size variable you have handy is of
  210. signed type, since otherwise without an explicit cast the code
  211. would be ill-formed.
  212. For example, with:
  213. extern void foo (int, int, gdb::array_view<value *>);
  214. value *args[2];
  215. int nargs;
  216. foo (1, 2, {values, nargs});
  217. You'd get:
  218. source.c:10: error: narrowing conversion of ‘nargs’ from ‘int’ to
  219. ‘size_t {aka long unsigned int}’ inside { } [-Werror=narrowing]
  220. You could fix it by writing the somewhat distracting explicit cast:
  221. foo (1, 2, {values, (size_t) nargs});
  222. Or by instantiating an array_view explicitly:
  223. foo (1, 2, gdb::array_view<value *>(values, nargs));
  224. Or, better, using make_array_view, which has the advantage of
  225. inferring the arrav_view element's type:
  226. foo (1, 2, gdb::make_array_view (values, nargs));
  227. */
  228. template<typename U>
  229. constexpr inline array_view<U>
  230. make_array_view (U *array, size_t size) noexcept
  231. {
  232. return {array, size};
  233. }
  234. } /* namespace gdb */
  235. #endif