1 //===--- fallible_iterator.h - Wrapper for fallible iterators ---*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_ADT_FALLIBLE_ITERATOR_H
10 #define LLVM_ADT_FALLIBLE_ITERATOR_H
12 #include "llvm/ADT/PointerIntPair.h"
13 #include "llvm/ADT/iterator_range.h"
14 #include "llvm/Support/Error.h"
16 #include <type_traits>
20 /// A wrapper class for fallible iterators.
22 /// The fallible_iterator template wraps an underlying iterator-like class
23 /// whose increment and decrement operations are replaced with fallible versions
31 /// It produces an interface that is (mostly) compatible with a traditional
32 /// c++ iterator, including ++ and -- operators that do not fail.
34 /// Instances of the wrapper are constructed with an instance of the
35 /// underlying iterator and (for non-end iterators) a reference to an Error
36 /// instance. If the underlying increment/decrement operations fail, the Error
37 /// is returned via this reference, and the resulting iterator value set to an
38 /// end-of-range sentinel value. This enables the following loop idiom:
41 /// class Archive { // E.g. Potentially malformed on-disk archive
43 /// fallible_iterator<ArchiveChildItr> children_begin(Error &Err);
44 /// fallible_iterator<ArchiveChildItr> children_end();
45 /// iterator_range<fallible_iterator<ArchiveChildItr>>
46 /// children(Error &Err) {
47 /// return make_range(children_begin(Err), children_end());
51 /// void walk(Archive &A) {
52 /// Error Err = Error::success();
53 /// for (auto &C : A.children(Err)) {
54 /// // Loop body only entered when increment succeeds.
62 /// The wrapper marks the referenced Error as unchecked after each increment
63 /// and/or decrement operation, and clears the unchecked flag when a non-end
64 /// value is compared against end (since, by the increment invariant, not being
65 /// an end value proves that there was no error, and is equivalent to checking
66 /// that the Error is success). This allows early exits from the loop body
67 /// without requiring redundant error checks.
68 template <typename Underlying
> class fallible_iterator
{
71 using enable_if_struct_deref_supported
= std::enable_if
<
72 !std::is_void
<decltype(std::declval
<T
>().operator->())>::value
,
73 decltype(std::declval
<T
>().operator->())>;
76 /// Construct a fallible iterator that *cannot* be used as an end-of-range
79 /// A value created by this method can be dereferenced, incremented,
80 /// decremented and compared, providing the underlying type supports it.
82 /// The error that is passed in will be initially marked as checked, so if the
83 /// iterator is not used at all the Error need not be checked.
84 static fallible_iterator
itr(Underlying I
, Error
&Err
) {
86 return fallible_iterator(std::move(I
), &Err
);
89 /// Construct a fallible iteratro that can be used as an end-of-range value.
91 /// A value created by this method can be dereferenced (if the underlying
92 /// value points at a valid value) and compared, but not incremented or
94 static fallible_iterator
end(Underlying I
) {
95 return fallible_iterator(std::move(I
), nullptr);
98 /// Forward dereference to the underlying iterator.
99 auto operator*() -> decltype(*std::declval
<Underlying
>()) { return *I
; }
101 /// Forward const dereference to the underlying iterator.
102 auto operator*() const -> decltype(*std::declval
<const Underlying
>()) {
106 /// Forward structure dereference to the underlying iterator (if the
107 /// underlying iterator supports it).
108 template <typename T
= Underlying
>
109 typename enable_if_struct_deref_supported
<T
>::type
operator->() {
110 return I
.operator->();
113 /// Forward const structure dereference to the underlying iterator (if the
114 /// underlying iterator supports it).
115 template <typename T
= Underlying
>
116 typename enable_if_struct_deref_supported
<const T
>::type
operator->() const {
117 return I
.operator->();
120 /// Increment the fallible iterator.
122 /// If the underlying 'inc' operation fails, this will set the Error value
123 /// and update this iterator value to point to end-of-range.
125 /// The Error value is marked as needing checking, regardless of whether the
126 /// 'inc' operation succeeds or fails.
127 fallible_iterator
&operator++() {
128 assert(getErrPtr() && "Cannot increment end iterator");
129 if (auto Err
= I
.inc())
130 handleError(std::move(Err
));
136 /// Decrement the fallible iterator.
138 /// If the underlying 'dec' operation fails, this will set the Error value
139 /// and update this iterator value to point to end-of-range.
141 /// The Error value is marked as needing checking, regardless of whether the
142 /// 'dec' operation succeeds or fails.
143 fallible_iterator
&operator--() {
144 assert(getErrPtr() && "Cannot decrement end iterator");
145 if (auto Err
= I
.dec())
146 handleError(std::move(Err
));
152 /// Compare fallible iterators for equality.
154 /// Returns true if both LHS and RHS are end-of-range values, or if both are
155 /// non-end-of-range values whose underlying iterator values compare equal.
157 /// If this is a comparison between an end-of-range iterator and a
158 /// non-end-of-range iterator, then the Error (referenced by the
159 /// non-end-of-range value) is marked as checked: Since all
160 /// increment/decrement operations result in an end-of-range value, comparing
161 /// false against end-of-range is equivalent to checking that the Error value
162 /// is success. This flag management enables early returns from loop bodies
163 /// without redundant Error checks.
164 friend bool operator==(const fallible_iterator
&LHS
,
165 const fallible_iterator
&RHS
) {
166 // If both iterators are in the end state they compare
167 // equal, regardless of whether either is valid.
168 if (LHS
.isEnd() && RHS
.isEnd())
171 assert(LHS
.isValid() && RHS
.isValid() &&
172 "Invalid iterators can only be compared against end");
174 bool Equal
= LHS
.I
== RHS
.I
;
176 // If the iterators differ and this is a comparison against end then mark
177 // the Error as checked.
180 (void)!!*RHS
.getErrPtr();
182 (void)!!*LHS
.getErrPtr();
188 /// Compare fallible iterators for inequality.
190 /// See notes for operator==.
191 friend bool operator!=(const fallible_iterator
&LHS
,
192 const fallible_iterator
&RHS
) {
193 return !(LHS
== RHS
);
197 fallible_iterator(Underlying I
, Error
*Err
)
198 : I(std::move(I
)), ErrState(Err
, false) {}
200 Error
*getErrPtr() const { return ErrState
.getPointer(); }
202 bool isEnd() const { return getErrPtr() == nullptr; }
204 bool isValid() const { return !ErrState
.getInt(); }
206 void handleError(Error Err
) {
207 *getErrPtr() = std::move(Err
);
208 ErrState
.setPointer(nullptr);
209 ErrState
.setInt(true);
212 void resetCheckedFlag() {
213 *getErrPtr() = Error::success();
217 mutable PointerIntPair
<Error
*, 1> ErrState
;
220 /// Convenience wrapper to make a fallible_iterator value from an instance
221 /// of an underlying iterator and an Error reference.
222 template <typename Underlying
>
223 fallible_iterator
<Underlying
> make_fallible_itr(Underlying I
, Error
&Err
) {
224 return fallible_iterator
<Underlying
>::itr(std::move(I
), Err
);
227 /// Convenience wrapper to make a fallible_iterator end value from an instance
228 /// of an underlying iterator.
229 template <typename Underlying
>
230 fallible_iterator
<Underlying
> make_fallible_end(Underlying E
) {
231 return fallible_iterator
<Underlying
>::end(std::move(E
));
234 template <typename Underlying
>
235 iterator_range
<fallible_iterator
<Underlying
>>
236 make_fallible_range(Underlying I
, Underlying E
, Error
&Err
) {
237 return make_range(make_fallible_itr(std::move(I
), Err
),
238 make_fallible_end(std::move(E
)));
241 } // end namespace llvm
243 #endif // LLVM_ADT_FALLIBLE_ITERATOR_H