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 <cassert>
13#include <mutex>
14#include <utility>
15
16#include <llvm/ADT/iterator.h>
17
18namespace lldb_private {
19
20template <typename WrappedIteratorT,
21 typename T = typename std::iterator_traits<
22 WrappedIteratorT>::value_type::second_type>
24 : llvm::iterator_adaptor_base<
25 ValueMapIterator<WrappedIteratorT, T>, WrappedIteratorT,
26 typename std::iterator_traits<WrappedIteratorT>::iterator_category,
27 T> {
28 ValueMapIterator() = default;
29 explicit ValueMapIterator(WrappedIteratorT u)
30 : ValueMapIterator::iterator_adaptor_base(std::move(u)) {}
31
32 const T &operator*() { return (*this->I).second; }
33 const T &operator*() const { return (*this->I).second; }
34};
35
36template <typename MutexType, typename C,
37 typename IteratorT = typename C::const_iterator>
38class LockingAdaptedIterable : private std::unique_lock<MutexType>,
39 public llvm::iterator_range<IteratorT> {
40public:
41 // The lock is the first base so that it is held before iterator_range reads
42 // the container's begin and end.
43 LockingAdaptedIterable(const C &container, MutexType &mutex)
44 : std::unique_lock<MutexType>(mutex),
45 llvm::iterator_range<IteratorT>(container) {}
46
47 /// Takes ownership of an already-held lock.
48 LockingAdaptedIterable(const C &container, std::unique_lock<MutexType> &&lock)
49 : std::unique_lock<MutexType>(std::move(lock)),
50 llvm::iterator_range<IteratorT>(container) {
51 assert(this->owns_lock() &&
52 "LockingAdaptedIterable requires an owning lock");
53 }
54
56
57private:
60};
61
62} // namespace lldb_private
63
64#endif // LLDB_UTILITY_ITERABLE_H
LockingAdaptedIterable(LockingAdaptedIterable &&rhs)=default
LockingAdaptedIterable & operator=(const LockingAdaptedIterable &)=delete
LockingAdaptedIterable(const C &container, std::unique_lock< MutexType > &&lock)
Takes ownership of an already-held lock.
Definition Iterable.h:48
LockingAdaptedIterable(const C &container, MutexType &mutex)
Definition Iterable.h:43
LockingAdaptedIterable(const LockingAdaptedIterable &)=delete
A class that represents a running process on the host machine.
ValueMapIterator(WrappedIteratorT u)
Definition Iterable.h:29
const T & operator*() const
Definition Iterable.h:33