X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FADT%2Fiterator.h;h=e2c9e5ea6bdae1b736018425449271c4c3b8b2ee;hb=1e2c16606dc69989c6358918d57099320d9acb51;hp=b1d29a886230acde894f1da4da24e035a872a3ae;hpb=3c1f8e0054d0f3b0614565388b0f7e2fd65ea60f;p=oota-llvm.git diff --git a/include/llvm/ADT/iterator.h b/include/llvm/ADT/iterator.h index b1d29a88623..e2c9e5ea6bd 100644 --- a/include/llvm/ADT/iterator.h +++ b/include/llvm/ADT/iterator.h @@ -10,6 +10,7 @@ #ifndef LLVM_ADT_ITERATOR_H #define LLVM_ADT_ITERATOR_H +#include #include namespace llvm { @@ -32,20 +33,38 @@ namespace llvm { /// terms of addition of one. These aren't equivalent for all iterator /// categories, and respecting that adds a lot of complexity for little gain. template -struct iterator_facade_base - : std::iterator { +class iterator_facade_base + : public std::iterator { +protected: + enum { + IsRandomAccess = + std::is_base_of::value, + IsBidirectional = + std::is_base_of::value, + }; + +public: DerivedT operator+(DifferenceTypeT n) const { + static_assert( + IsRandomAccess, + "The '+' operator is only defined for random access iterators."); DerivedT tmp = *static_cast(this); tmp += n; return tmp; } friend DerivedT operator+(DifferenceTypeT n, const DerivedT &i) { + static_assert( + IsRandomAccess, + "The '+' operator is only defined for random access iterators."); return i + n; } DerivedT operator-(DifferenceTypeT n) const { + static_assert( + IsRandomAccess, + "The '-' operator is only defined for random access iterators."); DerivedT tmp = *static_cast(this); tmp -= n; return tmp; @@ -60,9 +79,15 @@ struct iterator_facade_base return tmp; } DerivedT &operator--() { + static_assert( + IsBidirectional, + "The decrement operator is only defined for bidirectional iterators."); return static_cast(this)->operator-=(1); } DerivedT operator--(int) { + static_assert( + IsBidirectional, + "The decrement operator is only defined for bidirectional iterators."); DerivedT tmp = *static_cast(this); --*static_cast(this); return tmp; @@ -73,13 +98,22 @@ struct iterator_facade_base } bool operator>(const DerivedT &RHS) const { + static_assert( + IsRandomAccess, + "Relational operators are only defined for random access iterators."); return !static_cast(this)->operator<(RHS) && !static_cast(this)->operator==(RHS); } bool operator<=(const DerivedT &RHS) const { + static_assert( + IsRandomAccess, + "Relational operators are only defined for random access iterators."); return !static_cast(this)->operator>(RHS); } bool operator>=(const DerivedT &RHS) const { + static_assert( + IsRandomAccess, + "Relational operators are only defined for random access iterators."); return !static_cast(this)->operator<(RHS); } @@ -87,6 +121,8 @@ struct iterator_facade_base return &static_cast(this)->operator*(); } ReferenceT operator[](DifferenceTypeT n) const { + static_assert(IsRandomAccess, + "Subscripting is only defined for random access iterators."); return *static_cast(this)->operator+(n); } }; @@ -96,14 +132,19 @@ struct iterator_facade_base /// This class can be used through CRTP to adapt one iterator into another. /// Typically this is done through providing in the derived class a custom \c /// operator* implementation. Other methods can be overridden as well. -template > +template < + typename DerivedT, typename WrappedIteratorT, + typename IteratorCategoryT = + typename std::iterator_traits::iterator_category, + typename T = typename std::iterator_traits::value_type, + typename DifferenceTypeT = + typename std::iterator_traits::difference_type, + typename PointerT = T *, typename ReferenceT = T &, + // Don't provide these, they are mostly to act as aliases below. + typename WrappedTraitsT = std::iterator_traits> class iterator_adaptor_base - : public iterator_facade_base< - DerivedT, typename WrappedTraitsT::iterator_category, T, - typename WrappedTraitsT::difference_type, PointerT, ReferenceT> { + : public iterator_facade_base { typedef typename iterator_adaptor_base::iterator_facade_base BaseT; protected: @@ -122,18 +163,29 @@ protected: : I(std::forward(u)) {} public: - typedef typename WrappedTraitsT::difference_type difference_type; + typedef DifferenceTypeT difference_type; DerivedT &operator+=(difference_type n) { + static_assert( + BaseT::IsRandomAccess, + "The '+=' operator is only defined for random access iterators."); I += n; return *static_cast(this); } DerivedT &operator-=(difference_type n) { + static_assert( + BaseT::IsRandomAccess, + "The '-=' operator is only defined for random access iterators."); I -= n; return *static_cast(this); } using BaseT::operator-; - difference_type operator-(const DerivedT &RHS) const { return I - RHS.I; } + difference_type operator-(const DerivedT &RHS) const { + static_assert( + BaseT::IsRandomAccess, + "The '-' operator is only defined for random access iterators."); + return I - RHS.I; + } // We have to explicitly provide ++ and -- rather than letting the facade // forward to += because WrappedIteratorT might not support +=. @@ -144,12 +196,20 @@ public: } using BaseT::operator--; DerivedT &operator--() { + static_assert( + BaseT::IsBidirectional, + "The decrement operator is only defined for bidirectional iterators."); --I; return *static_cast(this); } bool operator==(const DerivedT &RHS) const { return I == RHS.I; } - bool operator<(const DerivedT &RHS) const { return I < RHS.I; } + bool operator<(const DerivedT &RHS) const { + static_assert( + BaseT::IsRandomAccess, + "Relational operators are only defined for random access iterators."); + return I < RHS.I; + } ReferenceT operator*() const { return *I; } }; @@ -163,13 +223,14 @@ public: /// \code /// typedef pointee_iterator::iterator> iterator; /// \endcode -template < - typename WrappedIteratorT, - typename T = typename std::remove_pointer< - typename std::iterator_traits::value_type>::type> +template ())>::type> struct pointee_iterator - : iterator_adaptor_base, - WrappedIteratorT, T> { + : iterator_adaptor_base< + pointee_iterator, WrappedIteratorT, + typename std::iterator_traits::iterator_category, + T> { pointee_iterator() {} template pointee_iterator(U &&u)