1 //===-- Iterable.h ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Iterable_h_
11 #define liblldb_Iterable_h_
13 #include "lldb/Host/Mutex.h"
15 namespace lldb_private
18 template <typename I, typename E> E map_adapter(I &iter)
23 template <typename I, typename E> E vector_adapter(I &iter)
28 template <typename I, typename E> E list_adapter(I &iter)
33 template <typename C, typename E, E (*A)(typename C::const_iterator &)> class AdaptedConstIterator
36 typedef typename C::const_iterator BackingIterator;
38 BackingIterator m_iter;
40 // Wrapping constructor
41 AdaptedConstIterator (BackingIterator backing_iterator) :
42 m_iter(backing_iterator)
46 // Default-constructible
47 AdaptedConstIterator () :
53 AdaptedConstIterator (const AdaptedConstIterator &rhs) :
59 AdaptedConstIterator &operator= (const AdaptedConstIterator &rhs)
66 ~AdaptedConstIterator () { }
69 bool operator== (const AdaptedConstIterator &rhs)
71 return m_iter == rhs.m_iter;
74 bool operator!= (const AdaptedConstIterator &rhs)
76 return m_iter != rhs.m_iter;
79 // Rvalue dereferenceable
90 // Offset dereferenceable
91 E operator[] (typename BackingIterator::difference_type offset)
93 return AdaptedConstIterator(m_iter + offset);
97 AdaptedConstIterator &operator++ ()
104 AdaptedConstIterator &operator-- ()
110 // Compound assignment
111 AdaptedConstIterator &operator+= (typename BackingIterator::difference_type offset)
117 AdaptedConstIterator &operator-= (typename BackingIterator::difference_type offset)
124 AdaptedConstIterator operator+ (typename BackingIterator::difference_type offset)
126 return AdaptedConstIterator(m_iter + offset);
129 AdaptedConstIterator operator- (typename BackingIterator::difference_type offset)
131 return AdaptedConstIterator(m_iter - offset);
135 bool operator< (AdaptedConstIterator &rhs)
137 return m_iter < rhs.m_iter;
140 bool operator<= (AdaptedConstIterator &rhs)
142 return m_iter <= rhs.m_iter;
145 bool operator> (AdaptedConstIterator &rhs)
147 return m_iter > rhs.m_iter;
150 bool operator>= (AdaptedConstIterator &rhs)
152 return m_iter >= rhs.m_iter;
155 template <typename C1, typename E1, E1 (*A1)(typename C1::const_iterator &)>
156 friend AdaptedConstIterator<C1, E1, A1> operator+(typename C1::const_iterator::difference_type, AdaptedConstIterator<C1, E1, A1> &);
158 template <typename C1, typename E1, E1 (*A1)(typename C1::const_iterator &)>
159 friend typename C1::const_iterator::difference_type operator-(AdaptedConstIterator<C1, E1, A1> &, AdaptedConstIterator<C1, E1, A1> &);
161 template <typename C1, typename E1, E1 (*A1)(typename C1::const_iterator &)>
162 friend void swap(AdaptedConstIterator<C1, E1, A1> &, AdaptedConstIterator<C1, E1, A1> &);
165 template <typename C, typename E, E (*A)(typename C::const_iterator &)>
166 AdaptedConstIterator<C, E, A> operator+ (typename AdaptedConstIterator<C, E, A>::BackingIterator::difference_type offset, AdaptedConstIterator<C, E, A> &rhs)
168 return rhs.operator+(offset);
171 template <typename C, typename E, E (*A)(typename C::const_iterator &)>
172 typename AdaptedConstIterator<C, E, A>::BackingIterator::difference_type operator- (AdaptedConstIterator<C, E, A> &lhs, AdaptedConstIterator<C, E, A> &rhs)
174 return(lhs.m_iter - rhs.m_iter);
177 template <typename C, typename E, E (*A)(typename C::const_iterator &)>
178 void swap (AdaptedConstIterator<C, E, A> &lhs, AdaptedConstIterator<C, E, A> &rhs)
180 std::swap(lhs.m_iter, rhs.m_iter);
183 template <typename C, typename E, E (*A)(typename C::const_iterator &)> class AdaptedIterable
186 const C &m_container;
188 AdaptedIterable (const C &container) :
189 m_container(container)
193 AdaptedConstIterator<C, E, A> begin ()
195 return AdaptedConstIterator<C, E, A>(m_container.begin());
198 AdaptedConstIterator<C, E, A> end ()
200 return AdaptedConstIterator<C, E, A>(m_container.end());
204 template <typename C, typename E, E (*A)(typename C::const_iterator &)> class LockingAdaptedIterable : public AdaptedIterable<C, E, A>
207 Mutex *m_mutex = nullptr;
209 LockingAdaptedIterable (C &container, Mutex &mutex) :
210 AdaptedIterable<C,E,A>(container),
216 LockingAdaptedIterable (LockingAdaptedIterable &&rhs) :
217 AdaptedIterable<C,E,A>(rhs),
223 ~LockingAdaptedIterable ()
230 DISALLOW_COPY_AND_ASSIGN(LockingAdaptedIterable);