Home | History | Annotate | Line # | Download | only in gdbsupport
      1      1.1  christos /* An iterator wrapper that yields pointers instead of references.
      2  1.1.1.2  christos    Copyright (C) 2021-2024 Free Software Foundation, Inc.
      3      1.1  christos 
      4      1.1  christos    This file is part of GDB.
      5      1.1  christos 
      6      1.1  christos    This program is free software; you can redistribute it and/or modify
      7      1.1  christos    it under the terms of the GNU General Public License as published by
      8      1.1  christos    the Free Software Foundation; either version 3 of the License, or
      9      1.1  christos    (at your option) any later version.
     10      1.1  christos 
     11      1.1  christos    This program is distributed in the hope that it will be useful,
     12      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14      1.1  christos    GNU General Public License for more details.
     15      1.1  christos 
     16      1.1  christos    You should have received a copy of the GNU General Public License
     17      1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     18      1.1  christos 
     19      1.1  christos #ifndef GDBSUPPORT_REFERENCE_TO_POINTER_ITERATOR_H
     20      1.1  christos #define GDBSUPPORT_REFERENCE_TO_POINTER_ITERATOR_H
     21      1.1  christos 
     22      1.1  christos /* Wrap an iterator that yields references to objects so that it yields
     23      1.1  christos    pointers to objects instead.
     24      1.1  christos 
     25      1.1  christos    This is useful for example to bridge the gap between iterators on intrusive
     26      1.1  christos    lists, which yield references, and the rest of GDB, which for legacy reasons
     27      1.1  christos    expects to iterate on pointers.  */
     28      1.1  christos 
     29      1.1  christos template <typename IteratorType>
     30      1.1  christos struct reference_to_pointer_iterator
     31      1.1  christos {
     32      1.1  christos   using self_type = reference_to_pointer_iterator;
     33      1.1  christos   using value_type = typename IteratorType::value_type *;
     34      1.1  christos   using reference = typename IteratorType::value_type *&;
     35      1.1  christos   using pointer = typename IteratorType::value_type **;
     36      1.1  christos   using iterator_category = typename IteratorType::iterator_category;
     37      1.1  christos   using difference_type = typename IteratorType::difference_type;
     38      1.1  christos 
     39  1.1.1.2  christos   /* Construct a reference_to_pointer_iterator, passing args to the underlying
     40      1.1  christos      iterator.  */
     41      1.1  christos   template <typename... Args>
     42      1.1  christos   reference_to_pointer_iterator (Args &&...args)
     43      1.1  christos     : m_it (std::forward<Args> (args)...)
     44      1.1  christos   {}
     45      1.1  christos 
     46      1.1  christos   /* Create a past-the-end iterator.
     47      1.1  christos 
     48      1.1  christos      Assumes that default-constructing an underlying iterator creates a
     49      1.1  christos      past-the-end iterator.  */
     50      1.1  christos   reference_to_pointer_iterator ()
     51      1.1  christos   {}
     52      1.1  christos 
     53      1.1  christos   /* Need these as the variadic constructor would be a better match
     54      1.1  christos      otherwise.  */
     55      1.1  christos   reference_to_pointer_iterator (reference_to_pointer_iterator &) = default;
     56      1.1  christos   reference_to_pointer_iterator (const reference_to_pointer_iterator &) = default;
     57      1.1  christos   reference_to_pointer_iterator (reference_to_pointer_iterator &&) = default;
     58      1.1  christos 
     59      1.1  christos   reference_to_pointer_iterator &operator= (const reference_to_pointer_iterator &) = default;
     60      1.1  christos   reference_to_pointer_iterator &operator= (reference_to_pointer_iterator &&) = default;
     61      1.1  christos 
     62      1.1  christos   value_type operator* () const
     63      1.1  christos   { return &*m_it; }
     64      1.1  christos 
     65      1.1  christos   self_type &operator++ ()
     66      1.1  christos   {
     67      1.1  christos     ++m_it;
     68      1.1  christos     return *this;
     69      1.1  christos   }
     70      1.1  christos 
     71  1.1.1.2  christos   self_type &operator++ (int)
     72  1.1.1.2  christos   {
     73  1.1.1.2  christos     m_it++;
     74  1.1.1.2  christos     return *this;
     75  1.1.1.2  christos   }
     76  1.1.1.2  christos 
     77  1.1.1.2  christos   self_type &operator-- ()
     78  1.1.1.2  christos   {
     79  1.1.1.2  christos     --m_it;
     80  1.1.1.2  christos     return *this;
     81  1.1.1.2  christos   }
     82  1.1.1.2  christos 
     83  1.1.1.2  christos   self_type &operator-- (int)
     84  1.1.1.2  christos   {
     85  1.1.1.2  christos     m_it--;
     86  1.1.1.2  christos     return *this;
     87  1.1.1.2  christos   }
     88  1.1.1.2  christos 
     89      1.1  christos   bool operator== (const self_type &other) const
     90      1.1  christos   { return m_it == other.m_it; }
     91      1.1  christos 
     92      1.1  christos   bool operator!= (const self_type &other) const
     93      1.1  christos   { return m_it != other.m_it; }
     94      1.1  christos 
     95      1.1  christos private:
     96      1.1  christos   /* The underlying iterator.  */
     97      1.1  christos   IteratorType m_it;
     98      1.1  christos };
     99      1.1  christos 
    100      1.1  christos #endif /* GDBSUPPORT_REFERENCE_TO_POINTER_ITERATOR_H */
    101