17#ifndef LIEF_ITERATORS_H
18#define LIEF_ITERATORS_H
31using decay_t =
typename std::decay<T>::type;
42template<class T, typename U = typename decay_t<T>::value_type,
51 using pointer =
typename std::remove_pointer<U>::type*;
52 using reference =
typename std::remove_pointer<U>::type&;
61 container_{std::forward<T>(container)}
63 it_ = std::begin(container_);
67 container_{copy.container_},
68 it_{std::
begin(container_)},
69 distance_{copy.distance_}
71 std::advance(it_, distance_);
83 std::swap(it_, other.it_);
84 std::swap(distance_, other.distance_);
101 if (it_ != std::begin(container_)) {
102 it_ = std::prev(it_);
116 std::advance(it_, movement);
117 distance_ += movement;
123 return (*
this) += -movement;
134 assert(n <
size() &&
"integrity error: out of bound");
139 no_const_this->it_ = std::begin(no_const_this->container_);
140 std::advance(no_const_this->it_, n);
144 no_const_this->it_ = std::begin(no_const_this->container_);
145 std::advance(no_const_this->it_, saved_dist);
163 return distance_ - rhs.distance_;
167 return (rhs - *
this) > 0;
177 return !(*
this < rhs);
182 return !(*
this > rhs);
195 it.it_ = std::end(it.container_);
196 it.distance_ = it.
size();
205 return (
size() == other.
size() && distance_ == other.distance_);
209 return !(*
this == other);
213 return container_.size();
217 return container_.empty();
225 template<
typename V = DT_VAL>
228 assert(*it_ &&
"integrity error: nullptr");
232 template<
typename V = DT_VAL>
239 typename std::enable_if<!std::is_const<pointer_t>::value,
pointer_t>::type
254template<class T, typename U = typename decay_t<T>::value_type,
class CT =
typename std::add_const<T>::type>
259template<class T, typename U = typename decay_t<T>::value_type,
269 using pointer =
typename std::remove_pointer<U>::type*;
270 using reference =
typename std::remove_pointer<U>::type&;
277 using filter_t = std::function<bool (
const typename DT::value_type&)>;
280 container_{std::forward<T>(container)},
284 it_ = std::begin(container_);
286 filters_.push_back(filter),
287 it_ = std::begin(container_);
289 if (it_ != std::end(container_)) {
290 if (!std::all_of(std::begin(filters_), std::end(filters_), [
this] (
const filter_t& f) {
return f(*it_);})) {
297 container_{std::forward<T>(container)},
301 it_ = std::begin(container_);
303 if (it_ != std::end(container_)) {
304 if (!std::all_of(std::begin(filters_), std::end(filters_), [
this] (
const filter_t& f) {
return f(*it_);})) {
311 container_{std::forward<T>(container)},
314 it_ = std::begin(container_);
318 container_{copy.container_},
319 it_{std::
begin(container_)},
320 filters_{copy.filters_},
321 distance_{copy.distance_}
323 std::advance(it_, distance_);
333 std::swap(it_, other.it_);
334 std::swap(filters_, other.filters_);
335 std::swap(size_c_, other.size_c_);
336 std::swap(distance_, other.distance_);
341 filters_.push_back(func);
358 return {container_, filters_};
369 it_end.it_ = it_end.container_.end();
370 it_end.distance_ = it_end.container_.size();
384 template<
typename V = DT_VAL>
387 assert(*it_ &&
"integrity error: nullptr");
391 template<
typename V = DT_VAL>
404 assert(n <
size() &&
"integrity error: out of bound");
412 typename std::enable_if<!std::is_const<pointer_t>::value,
pointer_t>::type
422 if (filters_.empty()) {
423 return container_.size();
432 auto end_iter = std::end(it);
433 for (; it != end_iter; ++it) ++
size;
445 return (container_.size() == other.container_.size() && distance_ == other.distance_);
449 return !(*
this == other);
454 if (it_ == std::end(container_)) {
455 distance_ = container_.size();
460 it_ = std::next(it_);
462 }
while(it_ != std::end(container_) &&
463 !std::all_of(std::begin(filters_), std::end(filters_),
464 [
this] (
const filter_t& f) {
return f(*it_); }));
469 mutable size_t size_c_ = 0;
472 std::vector<filter_t> filters_;
475template<class T, typename U = typename decay_t<T>::value_type,
478 class CT =
typename std::add_const<T>::type>
485template <
typename IteratorT>
490 : begin_(std::forward<IteratorT>(it_begin)),
491 end_(std::forward<IteratorT>(it_end)) {}
493 IteratorT
begin()
const {
return begin_; }
494 IteratorT
end()
const {
return end_; }
495 bool empty()
const {
return begin_ == end_; }
497 typename IteratorT::value_type
at(
typename IteratorT::difference_type pos)
const {
498 static_assert(IsRandomAccess,
"at() needs random access iterator");
500 std::advance(it, pos);
504 typename IteratorT::value_type
operator[](
typename IteratorT::difference_type pos)
const {
509 static_assert(IsRandomAccess,
"size() needs random access iterator");
510 return std::distance(begin_, end_);
515 IsRandomAccess = std::is_base_of<std::random_access_iterator_tag,
516 typename IteratorT::iterator_category>::value,
517 IsBidirectional = std::is_base_of<std::bidirectional_iterator_tag,
518 typename IteratorT::iterator_category>::value,
529template <
typename DerivedT,
typename IteratorCategoryT,
typename T,
588 typename DifferenceTypeT = std::ptrdiff_t,
typename PointerT = T *,
589 typename ReferenceT = T &>
600 IsRandomAccess = std::is_base_of<std::random_access_iterator_tag,
601 IteratorCategoryT>::value,
602 IsBidirectional = std::is_base_of<std::bidirectional_iterator_tag,
603 IteratorCategoryT>::value,
605 class ReferenceProxy {
612 friend iterator_facade_base;
616 ReferenceProxy(DerivedT I) : I(std::move(I)) {}
619 operator ReferenceT()
const {
return *I; }
627 friend iterator_facade_base;
631 template <
typename RefT>
632 PointerProxy(RefT &&R) : R(std::forward<RefT>(R)) {}
635 PointerT operator->()
const {
return &R; }
640 static_assert(std::is_base_of<iterator_facade_base, DerivedT>::value,
641 "Must pass the derived type to this template!");
644 "The '+' operator is only defined for random access iterators.");
645 DerivedT tmp = *
static_cast<const DerivedT *
>(
this);
649 friend DerivedT
operator+(DifferenceTypeT n,
const DerivedT &i) {
652 "The '+' operator is only defined for random access iterators.");
658 "The '-' operator is only defined for random access iterators.");
659 DerivedT tmp = *
static_cast<const DerivedT *
>(
this);
665 static_assert(std::is_base_of<iterator_facade_base, DerivedT>::value,
666 "Must pass the derived type to this template!");
667 return static_cast<DerivedT *
>(
this)->
operator+=(1);
670 DerivedT tmp = *
static_cast<DerivedT *
>(
this);
671 ++*
static_cast<DerivedT *
>(
this);
677 "The decrement operator is only defined for bidirectional iterators.");
678 return static_cast<DerivedT *
>(
this)->
operator-=(1);
683 "The decrement operator is only defined for bidirectional iterators.");
684 DerivedT tmp = *
static_cast<DerivedT *
>(
this);
685 --*
static_cast<DerivedT *
>(
this);
689#ifndef __cpp_impl_three_way_comparison
691 return !(
static_cast<const DerivedT &
>(*this) == RHS);
698 "Relational operators are only defined for random access iterators.");
699 return !(
static_cast<const DerivedT &
>(*this) < RHS) &&
700 !(
static_cast<const DerivedT &
>(*
this) == RHS);
705 "Relational operators are only defined for random access iterators.");
706 return !(
static_cast<const DerivedT &
>(*this) > RHS);
711 "Relational operators are only defined for random access iterators.");
712 return !(
static_cast<const DerivedT &
>(*this) < RHS);
716 return static_cast<const DerivedT *
>(
this)->
operator*();
719 static_assert(IsRandomAccess,
720 "Subscripting is only defined for random access iterators.");
721 return static_cast<const DerivedT *
>(
this)->
operator+(n);
731 typename DerivedT,
typename WrappedIteratorT,
732 typename IteratorCategoryT =
733 typename std::iterator_traits<WrappedIteratorT>::iterator_category,
734 typename T =
typename std::iterator_traits<WrappedIteratorT>::value_type,
735 typename DifferenceTypeT =
736 typename std::iterator_traits<WrappedIteratorT>::difference_type,
737 typename PointerT =
typename std::conditional<
738 std::is_same<T,
typename std::iterator_traits<
739 WrappedIteratorT>::value_type>::value,
740 typename std::iterator_traits<WrappedIteratorT>::pointer, T *>,
741 typename ReferenceT =
typename std::conditional<
742 std::is_same<T,
typename std::iterator_traits<
743 WrappedIteratorT>::value_type>::value,
744 typename std::iterator_traits<WrappedIteratorT>::reference, T &>::type>
747 DifferenceTypeT, PointerT, ReferenceT> {
748 using BaseT =
typename iterator_adaptor_base::iterator_facade_base;
753 iterator_adaptor_base() =
default;
755 explicit iterator_adaptor_base(WrappedIteratorT u) : I(std::move(u)) {
756 static_assert(std::is_base_of<iterator_adaptor_base, DerivedT>::value,
757 "Must pass the derived type to this template!");
760 const WrappedIteratorT &wrapped()
const {
return I; }
767 BaseT::IsRandomAccess,
768 "The '+=' operator is only defined for random access iterators.");
770 return *
static_cast<DerivedT *
>(
this);
774 BaseT::IsRandomAccess,
775 "The '-=' operator is only defined for random access iterators.");
777 return *
static_cast<DerivedT *
>(
this);
779 using BaseT::operator-;
782 BaseT::IsRandomAccess,
783 "The '-' operator is only defined for random access iterators.");
789 using BaseT::operator++;
792 return *
static_cast<DerivedT *
>(
this);
794 using BaseT::operator--;
797 BaseT::IsBidirectional,
798 "The decrement operator is only defined for bidirectional iterators.");
800 return *
static_cast<DerivedT *
>(
this);
804 const iterator_adaptor_base &RHS) {
805 return LHS.I == RHS.I;
808 const iterator_adaptor_base &RHS) {
810 BaseT::IsRandomAccess,
811 "Relational operators are only defined for random access iterators.");
812 return LHS.I < RHS.I;
817template <
typename WrappedIteratorT,
828 typename T =
typename std::remove_reference<
decltype(
829 **std::declval<WrappedIteratorT>())>::type>
833 typename std::iterator_traits<WrappedIteratorT>::iterator_category,
836 template <
typename U>
843template <
typename RangeT,
typename WrappedIteratorT =
844 decltype(std::begin(std::declval<RangeT>()))>
848 return make_range(PointeeIteratorT(std::begin(std::forward<RangeT>(Range))),
849 PointeeIteratorT(std::end(std::forward<RangeT>(Range))));
852template <
typename WrappedIteratorT,
853 typename T =
decltype(&*std::declval<WrappedIteratorT>())>
857 typename std::iterator_traits<WrappedIteratorT>::iterator_category,
870template <
typename RangeT,
typename WrappedIteratorT =
871 decltype(std::begin(std::declval<RangeT>()))>
875 return make_range(PointerIteratorT(std::begin(std::forward<RangeT>(Range))),
876 PointerIteratorT(std::end(std::forward<RangeT>(Range))));
879template <
typename WrappedIteratorT,
880 typename T1 =
typename std::remove_reference<
decltype(
881 **std::declval<WrappedIteratorT>())>::type,
882 typename T2 =
typename std::add_pointer<T1>::type>
Iterator which return a ref on container's values given predicates.
Definition iterators.hpp:263
std::enable_if<!std::is_pointer< V >::value, add_const_t< ref_t > >::type operator*() const
Definition iterators.hpp:393
bool operator==(const filter_iterator &other) const
Definition iterators.hpp:444
filter_iterator(const filter_iterator ©)
Definition iterators.hpp:317
add_const_t< ref_t > operator[](size_t n) const
Definition iterators.hpp:403
filter_iterator cend() const
Definition iterators.hpp:375
typename std::remove_pointer< U >::type * pointer
Definition iterators.hpp:269
filter_iterator(T container, filter_t filter)
Definition iterators.hpp:279
std::forward_iterator_tag iterator_category
Definition iterators.hpp:266
filter_iterator(T container)
Definition iterators.hpp:310
void swap(filter_iterator &other) noexcept
Definition iterators.hpp:331
filter_iterator & operator++()
Definition iterators.hpp:346
std::enable_if<!std::is_const< pointer_t >::value, pointer_t >::type operator->()
Definition iterators.hpp:413
filter_iterator(T container, const std::vector< filter_t > &filters)
Definition iterators.hpp:296
filter_iterator end() const
Definition iterators.hpp:365
typename filter_iterator::pointer pointer_t
Definition iterators.hpp:276
filter_iterator & operator=(filter_iterator other)
Definition iterators.hpp:326
decay_t< U > value_type
Definition iterators.hpp:267
typename filter_iterator::reference ref_t
Definition iterators.hpp:275
std::enable_if<!std::is_const< ref_t >::value, remove_const_t< ref_t > >::type operator*()
Definition iterators.hpp:380
filter_iterator begin() const
Definition iterators.hpp:357
bool empty() const
Definition iterators.hpp:439
U DT_VAL
Definition iterators.hpp:273
ptrdiff_t difference_type
Definition iterators.hpp:268
add_const_t< pointer_t > operator->() const
Definition iterators.hpp:417
std::function< bool(const typename DT::value_type &)> filter_t
Definition iterators.hpp:277
size_t size() const
Definition iterators.hpp:421
std::enable_if< std::is_pointer< V >::value, add_const_t< ref_t > >::type operator*() const
Definition iterators.hpp:386
bool operator!=(const filter_iterator &other) const
Definition iterators.hpp:448
T container_type
Definition iterators.hpp:272
filter_iterator operator++(int)
Definition iterators.hpp:351
decay_t< T > DT
Definition iterators.hpp:274
filter_iterator cbegin() const
Definition iterators.hpp:361
filter_iterator & def(filter_t func)
Definition iterators.hpp:340
typename std::remove_pointer< U >::type & reference
Definition iterators.hpp:270
std::enable_if<!std::is_const< ref_t >::value, remove_const_t< ref_t > >::type operator[](size_t n)
Definition iterators.hpp:399
CRTP base class for adapting an iterator to a different type.
Definition iterators.hpp:747
DerivedT & operator++()
Definition iterators.hpp:790
DerivedT & operator--()
Definition iterators.hpp:795
DerivedT & operator-=(difference_type n)
Definition iterators.hpp:772
ReferenceT operator*() const
Definition iterators.hpp:815
friend bool operator<(const iterator_adaptor_base &LHS, const iterator_adaptor_base &RHS)
Definition iterators.hpp:807
friend bool operator==(const iterator_adaptor_base &LHS, const iterator_adaptor_base &RHS)
Definition iterators.hpp:803
DifferenceTypeT difference_type
Definition iterators.hpp:763
DerivedT & operator+=(difference_type n)
Definition iterators.hpp:765
difference_type operator-(const DerivedT &RHS) const
Definition iterators.hpp:780
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition iterators.hpp:590
DifferenceTypeT difference_type
Definition iterators.hpp:594
PointerProxy operator->() const
Definition iterators.hpp:715
DerivedT & operator--()
Definition iterators.hpp:674
DerivedT operator++(int)
Definition iterators.hpp:669
T value_type
Definition iterators.hpp:593
friend DerivedT operator+(DifferenceTypeT n, const DerivedT &i)
Definition iterators.hpp:649
bool operator>(const DerivedT &RHS) const
Definition iterators.hpp:695
ReferenceProxy operator[](DifferenceTypeT n) const
Definition iterators.hpp:718
ReferenceT reference
Definition iterators.hpp:596
bool operator>=(const DerivedT &RHS) const
Definition iterators.hpp:708
DerivedT operator-(DifferenceTypeT n) const
Definition iterators.hpp:655
DerivedT & operator++()
Definition iterators.hpp:664
PointerT pointer
Definition iterators.hpp:595
DerivedT operator--(int)
Definition iterators.hpp:680
IteratorCategoryT iterator_category
Definition iterators.hpp:592
DerivedT operator+(DifferenceTypeT n) const
Definition iterators.hpp:639
bool operator!=(const DerivedT &RHS) const
Definition iterators.hpp:690
bool operator<=(const DerivedT &RHS) const
Definition iterators.hpp:702
Definition iterators.hpp:486
IteratorT begin() const
Definition iterators.hpp:493
IteratorT end() const
Definition iterators.hpp:494
IteratorT::value_type operator[](typename IteratorT::difference_type pos) const
Definition iterators.hpp:504
bool empty() const
Definition iterators.hpp:495
IteratorT::value_type at(typename IteratorT::difference_type pos) const
Definition iterators.hpp:497
iterator_range(IteratorT &&it_begin, IteratorT &&it_end)
Definition iterators.hpp:489
IteratorT IteratorTy
Definition iterators.hpp:488
std::ptrdiff_t size() const
Definition iterators.hpp:508
Definition iterators.hpp:858
pointer_iterator()=default
T & operator*() const
Definition iterators.hpp:867
pointer_iterator(WrappedIteratorT u)
Definition iterators.hpp:864
Iterator which returns reference on container's values.
Definition iterators.hpp:46
bool operator<=(const ref_iterator &rhs) const
Definition iterators.hpp:181
ref_iterator & operator--()
Definition iterators.hpp:100
size_t size() const
Definition iterators.hpp:212
ref_iterator & operator++()
Definition iterators.hpp:88
void swap(ref_iterator &other) noexcept
Definition iterators.hpp:80
bool operator<(const ref_iterator &rhs) const
Definition iterators.hpp:166
ref_iterator operator-(typename ref_iterator::difference_type n) const
Definition iterators.hpp:156
ref_iterator operator++(int)
Definition iterators.hpp:94
ref_iterator begin() const
Definition iterators.hpp:185
T container_type
Definition iterators.hpp:54
typename ref_iterator::reference ref_t
Definition iterators.hpp:57
ref_iterator operator--(int)
Definition iterators.hpp:108
ref_iterator cend() const
Definition iterators.hpp:200
ref_iterator & operator+=(const typename ref_iterator::difference_type &movement)
Definition iterators.hpp:115
std::bidirectional_iterator_tag iterator_category
Definition iterators.hpp:48
ptrdiff_t difference_type
Definition iterators.hpp:50
ref_iterator operator+(typename ref_iterator::difference_type n) const
Definition iterators.hpp:150
typename std::remove_pointer< U >::type & reference
Definition iterators.hpp:52
ref_iterator cbegin() const
Definition iterators.hpp:189
bool empty() const
Definition iterators.hpp:216
ref_iterator & operator=(ref_iterator other)
Definition iterators.hpp:75
typename std::remove_pointer< U >::type * pointer
Definition iterators.hpp:51
bool operator!=(const ref_iterator &other) const
Definition iterators.hpp:208
std::enable_if<!std::is_const< ref_t >::value, remove_const_t< ref_t > >::type operator[](size_t n)
Definition iterators.hpp:128
bool operator>(const ref_iterator &rhs) const
Definition iterators.hpp:171
bool operator>=(const ref_iterator &rhs) const
Definition iterators.hpp:176
ref_iterator(const ref_iterator ©)
Definition iterators.hpp:66
ref_iterator end() const
Definition iterators.hpp:193
std::enable_if< std::is_pointer< V >::value, add_const_t< ref_t > >::type operator*() const
Definition iterators.hpp:227
std::enable_if<!std::is_pointer< V >::value, add_const_t< ref_t > >::type operator*() const
Definition iterators.hpp:234
ref_iterator & operator-=(const typename ref_iterator::difference_type &movement)
Definition iterators.hpp:122
add_const_t< ref_t > operator[](size_t n) const
Definition iterators.hpp:133
decay_t< U > value_type
Definition iterators.hpp:49
std::enable_if<!std::is_const< ref_t >::value, remove_const_t< ref_t > >::type operator*()
Definition iterators.hpp:221
ref_iterator::difference_type operator-(const ref_iterator &rhs) const
Definition iterators.hpp:162
U DT_VAL
Definition iterators.hpp:55
typename ref_iterator::pointer pointer_t
Definition iterators.hpp:58
ref_iterator(T container)
Definition iterators.hpp:60
std::enable_if<!std::is_const< pointer_t >::value, pointer_t >::type operator->()
Definition iterators.hpp:240
add_const_t< pointer_t > operator->() const
Definition iterators.hpp:244
bool operator==(const ref_iterator &other) const
Definition iterators.hpp:204
decay_t< T > DT
Definition iterators.hpp:56
LIEF namespace.
Definition Abstract/Binary.hpp:36
filter_iterator< CT, U, typename decay_t< CT >::const_iterator > const_filter_iterator
Iterator which return a const ref on container's values given predicates.
Definition iterators.hpp:479
typename std::add_lvalue_reference< T >::type add_lvalue_reference_t
Definition iterators.hpp:40
typename std::remove_const< T >::type remove_const_t
Definition iterators.hpp:37
pointer_iterator< pointee_iterator< WrappedIteratorT, T1 >, T2 > raw_pointer_iterator
Definition iterators.hpp:883
typename std::decay< T >::type decay_t
Definition iterators.hpp:31
iterator_range< pointee_iterator< WrappedIteratorT > > make_pointee_range(RangeT &&Range)
Definition iterators.hpp:846
iterator_range< T > make_range(T &&x, T &&y)
Definition iterators.hpp:525
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
Definition iterators.hpp:873
ref_iterator< CT, U, typename decay_t< CT >::const_iterator > const_ref_iterator
Iterator which return const ref on container's values.
Definition iterators.hpp:257
typename std::add_const< T >::type add_const_t
Definition iterators.hpp:34
An iterator type that allows iterating over the pointees via some other iterator.
Definition iterators.hpp:834
pointee_iterator()=default
T & operator*() const
Definition iterators.hpp:840
pointee_iterator(U &&u)
Definition iterators.hpp:837