LLDB  mainline
Iterable.h
Go to the documentation of this file.
1 //===-- Iterable.h ----------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_UTILITY_ITERABLE_H
10 #define LLDB_UTILITY_ITERABLE_H
11 
12 #include <utility>
13 
14 
15 namespace lldb_private {
16 
17 template <typename I, typename E> E map_adapter(I &iter) {
18  return iter->second;
19 }
20 
21 template <typename I, typename E> E vector_adapter(I &iter) { return *iter; }
22 
23 template <typename I, typename E> E list_adapter(I &iter) { return *iter; }
24 
25 template <typename C, typename E, E (*A)(typename C::const_iterator &)>
27 public:
28  typedef typename C::const_iterator BackingIterator;
29 
30  // Wrapping constructor
32  : m_iter(backing_iterator) {}
33 
34  // Default-constructible
36 
37  // Copy-constructible
39 
40  // Copy-assignable
42  m_iter = rhs.m_iter;
43  return *this;
44  }
45 
46  // Destructible
47  ~AdaptedConstIterator() = default;
48 
49  // Comparable
50  bool operator==(const AdaptedConstIterator &rhs) {
51  return m_iter == rhs.m_iter;
52  }
53 
54  bool operator!=(const AdaptedConstIterator &rhs) {
55  return m_iter != rhs.m_iter;
56  }
57 
58  // Rvalue dereferenceable
59  E operator*() { return (*A)(m_iter); }
60 
61  E operator->() { return (*A)(m_iter); }
62 
63  // Offset dereferenceable
64  E operator[](typename BackingIterator::difference_type offset) {
65  return AdaptedConstIterator(m_iter + offset);
66  }
67 
68  // Incrementable
70  m_iter++;
71  return *this;
72  }
73 
74  // Decrementable
76  m_iter--;
77  return *this;
78  }
79 
80  // Compound assignment
82  operator+=(typename BackingIterator::difference_type offset) {
83  m_iter += offset;
84  return *this;
85  }
86 
88  operator-=(typename BackingIterator::difference_type offset) {
89  m_iter -= offset;
90  return *this;
91  }
92 
93  // Arithmetic
95  operator+(typename BackingIterator::difference_type offset) {
96  return AdaptedConstIterator(m_iter + offset);
97  }
98 
100  operator-(typename BackingIterator::difference_type offset) {
101  return AdaptedConstIterator(m_iter - offset);
102  }
103 
104  // Comparable
105  bool operator<(AdaptedConstIterator &rhs) { return m_iter < rhs.m_iter; }
106 
107  bool operator<=(AdaptedConstIterator &rhs) { return m_iter <= rhs.m_iter; }
108 
109  bool operator>(AdaptedConstIterator &rhs) { return m_iter > rhs.m_iter; }
110 
111  bool operator>=(AdaptedConstIterator &rhs) { return m_iter >= rhs.m_iter; }
112 
113  template <typename C1, typename E1, E1 (*A1)(typename C1::const_iterator &)>
115  operator+(typename C1::const_iterator::difference_type,
117 
118  template <typename C1, typename E1, E1 (*A1)(typename C1::const_iterator &)>
119  friend typename C1::const_iterator::difference_type
122 
123  template <typename C1, typename E1, E1 (*A1)(typename C1::const_iterator &)>
126 
127 private:
129 };
130 
131 template <typename C, typename E, E (*A)(typename C::const_iterator &)>
134  offset,
136  return rhs.operator+(offset);
137 }
138 
139 template <typename C, typename E, E (*A)(typename C::const_iterator &)>
140 typename AdaptedConstIterator<C, E, A>::BackingIterator::difference_type
143  return (lhs.m_iter - rhs.m_iter);
144 }
145 
146 template <typename C, typename E, E (*A)(typename C::const_iterator &)>
149  std::swap(lhs.m_iter, rhs.m_iter);
150 }
151 
152 template <typename C, typename E, E (*A)(typename C::const_iterator &)>
154 private:
155  const C &m_container;
156 
157 public:
158  AdaptedIterable(const C &container) : m_container(container) {}
159 
162  }
163 
166  }
167 };
168 
169 template <typename C, typename E, E (*A)(typename C::const_iterator &),
170  typename MutexType>
172 public:
173  LockingAdaptedIterable(const C &container, MutexType &mutex)
174  : AdaptedIterable<C, E, A>(container), m_mutex(&mutex) {
175  m_mutex->lock();
176  }
177 
179  : AdaptedIterable<C, E, A>(rhs), m_mutex(rhs.m_mutex) {
180  rhs.m_mutex = nullptr;
181  }
182 
184  if (m_mutex)
185  m_mutex->unlock();
186  }
187 
188 private:
189  MutexType *m_mutex = nullptr;
190 
192  LockingAdaptedIterable &operator=(const LockingAdaptedIterable &) = delete;
193 };
194 
195 } // namespace lldb_private
196 
197 #endif // LLDB_UTILITY_ITERABLE_H
lldb_private::AdaptedIterable::m_container
const C & m_container
Definition: Iterable.h:155
lldb_private::LockingAdaptedIterable::LockingAdaptedIterable
LockingAdaptedIterable(const C &container, MutexType &mutex)
Definition: Iterable.h:173
lldb_private::AdaptedConstIterator::~AdaptedConstIterator
~AdaptedConstIterator()=default
lldb_private::AdaptedConstIterator::swap
friend void swap(AdaptedConstIterator< C1, E1, A1 > &, AdaptedConstIterator< C1, E1, A1 > &)
lldb_private::LockingAdaptedIterable::~LockingAdaptedIterable
~LockingAdaptedIterable()
Definition: Iterable.h:183
lldb_private::AdaptedConstIterator
Definition: Iterable.h:26
lldb_private::swap
void swap(AdaptedConstIterator< C, E, A > &lhs, AdaptedConstIterator< C, E, A > &rhs)
Definition: Iterable.h:147
lldb_private::vector_adapter
E vector_adapter(I &iter)
Definition: Iterable.h:21
lldb_private::AdaptedConstIterator::operator==
bool operator==(const AdaptedConstIterator &rhs)
Definition: Iterable.h:50
lldb_private::AdaptedIterable
Definition: Iterable.h:153
lldb_private::AdaptedConstIterator::operator-=
AdaptedConstIterator & operator-=(typename BackingIterator::difference_type offset)
Definition: Iterable.h:88
lldb_private::AdaptedConstIterator::operator<
bool operator<(AdaptedConstIterator &rhs)
Definition: Iterable.h:105
lldb_private::list_adapter
E list_adapter(I &iter)
Definition: Iterable.h:23
lldb_private::LockingAdaptedIterable
Definition: Iterable.h:171
lldb_private::AdaptedConstIterator::BackingIterator
C::const_iterator BackingIterator
Definition: Iterable.h:28
lldb_private::AdaptedConstIterator::operator++
AdaptedConstIterator & operator++()
Definition: Iterable.h:69
lldb_private::AdaptedConstIterator::operator*
E operator*()
Definition: Iterable.h:59
lldb_private::AdaptedConstIterator::operator+
AdaptedConstIterator operator+(typename BackingIterator::difference_type offset)
Definition: Iterable.h:95
lldb_private::AdaptedConstIterator::operator-
AdaptedConstIterator operator-(typename BackingIterator::difference_type offset)
Definition: Iterable.h:100
lldb_private::AdaptedConstIterator::operator->
E operator->()
Definition: Iterable.h:61
lldb_private::AdaptedConstIterator::operator+=
AdaptedConstIterator & operator+=(typename BackingIterator::difference_type offset)
Definition: Iterable.h:82
lldb_private::operator-
AdaptedConstIterator< C, E, A >::BackingIterator::difference_type operator-(AdaptedConstIterator< C, E, A > &lhs, AdaptedConstIterator< C, E, A > &rhs)
Definition: Iterable.h:141
lldb_private::AdaptedIterable::AdaptedIterable
AdaptedIterable(const C &container)
Definition: Iterable.h:158
lldb_private::AdaptedIterable::begin
AdaptedConstIterator< C, E, A > begin()
Definition: Iterable.h:160
lldb_private::AdaptedConstIterator::operator=
AdaptedConstIterator & operator=(const AdaptedConstIterator &rhs)
Definition: Iterable.h:41
lldb_private::AdaptedConstIterator::operator!=
bool operator!=(const AdaptedConstIterator &rhs)
Definition: Iterable.h:54
lldb_private::operator+
AdaptedConstIterator< C, E, A > operator+(typename AdaptedConstIterator< C, E, A >::BackingIterator::difference_type offset, AdaptedConstIterator< C, E, A > &rhs)
Definition: Iterable.h:132
lldb_private::AdaptedIterable::end
AdaptedConstIterator< C, E, A > end()
Definition: Iterable.h:164
lldb_private::AdaptedConstIterator::operator>=
bool operator>=(AdaptedConstIterator &rhs)
Definition: Iterable.h:111
lldb_private::AdaptedConstIterator::operator>
bool operator>(AdaptedConstIterator &rhs)
Definition: Iterable.h:109
lldb_private::AdaptedConstIterator::m_iter
BackingIterator m_iter
Definition: Iterable.h:128
lldb_private::AdaptedConstIterator::operator--
AdaptedConstIterator & operator--()
Definition: Iterable.h:75
lldb_private::AdaptedConstIterator::AdaptedConstIterator
AdaptedConstIterator(const AdaptedConstIterator &rhs)
Definition: Iterable.h:38
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::LockingAdaptedIterable::LockingAdaptedIterable
LockingAdaptedIterable(LockingAdaptedIterable &&rhs)
Definition: Iterable.h:178
lldb_private::AdaptedConstIterator::AdaptedConstIterator
AdaptedConstIterator(BackingIterator backing_iterator)
Definition: Iterable.h:31
lldb_private::AdaptedConstIterator::operator[]
E operator[](typename BackingIterator::difference_type offset)
Definition: Iterable.h:64
lldb_private::AdaptedConstIterator::AdaptedConstIterator
AdaptedConstIterator()
Definition: Iterable.h:35
lldb_private::AdaptedConstIterator::operator<=
bool operator<=(AdaptedConstIterator &rhs)
Definition: Iterable.h:107
lldb_private::map_adapter
E map_adapter(I &iter)
Definition: Iterable.h:17