xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/iterator-range.h (revision 5ba1f45f2a09259cc846f20c7c5501604d633c90)
14b169a6bSchristos /* A range adapter that wraps begin / end iterators.
2*5ba1f45fSchristos    Copyright (C) 2021-2024 Free Software Foundation, Inc.
34b169a6bSchristos 
44b169a6bSchristos    This file is part of GDB.
54b169a6bSchristos 
64b169a6bSchristos    This program is free software; you can redistribute it and/or modify
74b169a6bSchristos    it under the terms of the GNU General Public License as published by
84b169a6bSchristos    the Free Software Foundation; either version 3 of the License, or
94b169a6bSchristos    (at your option) any later version.
104b169a6bSchristos 
114b169a6bSchristos    This program is distributed in the hope that it will be useful,
124b169a6bSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
134b169a6bSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
144b169a6bSchristos    GNU General Public License for more details.
154b169a6bSchristos 
164b169a6bSchristos    You should have received a copy of the GNU General Public License
174b169a6bSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
184b169a6bSchristos 
194b169a6bSchristos #ifndef GDBSUPPORT_ITERATOR_RANGE_H
204b169a6bSchristos #define GDBSUPPORT_ITERATOR_RANGE_H
214b169a6bSchristos 
224b169a6bSchristos /* A wrapper that allows using ranged for-loops on a range described by two
234b169a6bSchristos    iterators.  */
244b169a6bSchristos 
254b169a6bSchristos template <typename IteratorType>
264b169a6bSchristos struct iterator_range
274b169a6bSchristos {
284b169a6bSchristos   using iterator = IteratorType;
294b169a6bSchristos 
304b169a6bSchristos   /* Create an iterator_range using BEGIN as the begin iterator.
314b169a6bSchristos 
324b169a6bSchristos      Assume that the end iterator can be default-constructed.  */
334b169a6bSchristos   template <typename... Args>
344b169a6bSchristos   iterator_range (Args &&...args)
354b169a6bSchristos     : m_begin (std::forward<Args> (args)...)
364b169a6bSchristos   {}
374b169a6bSchristos 
384b169a6bSchristos   /* Create an iterator range using explicit BEGIN and END iterators.  */
394b169a6bSchristos   template <typename... Args>
404b169a6bSchristos   iterator_range (IteratorType begin, IteratorType end)
414b169a6bSchristos     : m_begin (std::move (begin)), m_end (std::move (end))
424b169a6bSchristos   {}
434b169a6bSchristos 
444b169a6bSchristos   /* Need these as the variadic constructor would be a better match
454b169a6bSchristos      otherwise.  */
464b169a6bSchristos   iterator_range (iterator_range &) = default;
474b169a6bSchristos   iterator_range (const iterator_range &) = default;
484b169a6bSchristos   iterator_range (iterator_range &&) = default;
494b169a6bSchristos 
504b169a6bSchristos   IteratorType begin () const
514b169a6bSchristos   { return m_begin; }
524b169a6bSchristos 
534b169a6bSchristos   IteratorType end () const
544b169a6bSchristos   { return m_end; }
554b169a6bSchristos 
564b169a6bSchristos   /* The number of items in this iterator_range.  */
574b169a6bSchristos   std::size_t size () const
584b169a6bSchristos   { return std::distance (m_begin, m_end); }
594b169a6bSchristos 
604b169a6bSchristos private:
614b169a6bSchristos   IteratorType m_begin, m_end;
624b169a6bSchristos };
634b169a6bSchristos 
644b169a6bSchristos #endif /* GDBSUPPORT_ITERATOR_RANGE_H */
65