mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-02-20 14:29:27 +00:00
[ADT] Provide some helpful static_asserts for using operations of the
wrong iterator category. These aren't comprehensive, but they have caught the common cases for me and produce much nicer errors. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@207601 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
1ea858937c
commit
91cc1431cb
@ -35,18 +35,36 @@ namespace llvm {
|
|||||||
template <typename DerivedT, typename IteratorCategoryT, typename T,
|
template <typename DerivedT, typename IteratorCategoryT, typename T,
|
||||||
typename DifferenceTypeT = std::ptrdiff_t, typename PointerT = T *,
|
typename DifferenceTypeT = std::ptrdiff_t, typename PointerT = T *,
|
||||||
typename ReferenceT = T &>
|
typename ReferenceT = T &>
|
||||||
struct iterator_facade_base
|
class iterator_facade_base
|
||||||
: std::iterator<IteratorCategoryT, T, DifferenceTypeT, PointerT,
|
: public std::iterator<IteratorCategoryT, T, DifferenceTypeT, PointerT,
|
||||||
ReferenceT> {
|
ReferenceT> {
|
||||||
|
protected:
|
||||||
|
enum {
|
||||||
|
IsRandomAccess =
|
||||||
|
std::is_base_of<std::random_access_iterator_tag, IteratorCategoryT>::value,
|
||||||
|
IsBidirectional =
|
||||||
|
std::is_base_of<std::bidirectional_iterator_tag, IteratorCategoryT>::value,
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
DerivedT operator+(DifferenceTypeT n) const {
|
DerivedT operator+(DifferenceTypeT n) const {
|
||||||
|
static_assert(
|
||||||
|
IsRandomAccess,
|
||||||
|
"The '+' operator is only defined for random access iterators.");
|
||||||
DerivedT tmp = *static_cast<const DerivedT *>(this);
|
DerivedT tmp = *static_cast<const DerivedT *>(this);
|
||||||
tmp += n;
|
tmp += n;
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
friend DerivedT operator+(DifferenceTypeT n, const DerivedT &i) {
|
friend DerivedT operator+(DifferenceTypeT n, const DerivedT &i) {
|
||||||
|
static_assert(
|
||||||
|
IsRandomAccess,
|
||||||
|
"The '+' operator is only defined for random access iterators.");
|
||||||
return i + n;
|
return i + n;
|
||||||
}
|
}
|
||||||
DerivedT operator-(DifferenceTypeT n) const {
|
DerivedT operator-(DifferenceTypeT n) const {
|
||||||
|
static_assert(
|
||||||
|
IsRandomAccess,
|
||||||
|
"The '-' operator is only defined for random access iterators.");
|
||||||
DerivedT tmp = *static_cast<const DerivedT *>(this);
|
DerivedT tmp = *static_cast<const DerivedT *>(this);
|
||||||
tmp -= n;
|
tmp -= n;
|
||||||
return tmp;
|
return tmp;
|
||||||
@ -61,9 +79,15 @@ struct iterator_facade_base
|
|||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
DerivedT &operator--() {
|
DerivedT &operator--() {
|
||||||
|
static_assert(
|
||||||
|
IsBidirectional,
|
||||||
|
"The decrement operator is only defined for bidirectional iterators.");
|
||||||
return static_cast<DerivedT *>(this)->operator-=(1);
|
return static_cast<DerivedT *>(this)->operator-=(1);
|
||||||
}
|
}
|
||||||
DerivedT operator--(int) {
|
DerivedT operator--(int) {
|
||||||
|
static_assert(
|
||||||
|
IsBidirectional,
|
||||||
|
"The decrement operator is only defined for bidirectional iterators.");
|
||||||
DerivedT tmp = *static_cast<DerivedT *>(this);
|
DerivedT tmp = *static_cast<DerivedT *>(this);
|
||||||
--*static_cast<DerivedT *>(this);
|
--*static_cast<DerivedT *>(this);
|
||||||
return tmp;
|
return tmp;
|
||||||
@ -74,13 +98,22 @@ struct iterator_facade_base
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool operator>(const DerivedT &RHS) const {
|
bool operator>(const DerivedT &RHS) const {
|
||||||
|
static_assert(
|
||||||
|
IsRandomAccess,
|
||||||
|
"Relational operators are only defined for random access iterators.");
|
||||||
return !static_cast<const DerivedT *>(this)->operator<(RHS) &&
|
return !static_cast<const DerivedT *>(this)->operator<(RHS) &&
|
||||||
!static_cast<const DerivedT *>(this)->operator==(RHS);
|
!static_cast<const DerivedT *>(this)->operator==(RHS);
|
||||||
}
|
}
|
||||||
bool operator<=(const DerivedT &RHS) const {
|
bool operator<=(const DerivedT &RHS) const {
|
||||||
|
static_assert(
|
||||||
|
IsRandomAccess,
|
||||||
|
"Relational operators are only defined for random access iterators.");
|
||||||
return !static_cast<const DerivedT *>(this)->operator>(RHS);
|
return !static_cast<const DerivedT *>(this)->operator>(RHS);
|
||||||
}
|
}
|
||||||
bool operator>=(const DerivedT &RHS) const {
|
bool operator>=(const DerivedT &RHS) const {
|
||||||
|
static_assert(
|
||||||
|
IsRandomAccess,
|
||||||
|
"Relational operators are only defined for random access iterators.");
|
||||||
return !static_cast<const DerivedT *>(this)->operator<(RHS);
|
return !static_cast<const DerivedT *>(this)->operator<(RHS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,6 +121,8 @@ struct iterator_facade_base
|
|||||||
return &static_cast<const DerivedT *>(this)->operator*();
|
return &static_cast<const DerivedT *>(this)->operator*();
|
||||||
}
|
}
|
||||||
ReferenceT operator[](DifferenceTypeT n) const {
|
ReferenceT operator[](DifferenceTypeT n) const {
|
||||||
|
static_assert(IsRandomAccess,
|
||||||
|
"Subscripting is only defined for random access iterators.");
|
||||||
return *static_cast<const DerivedT *>(this)->operator+(n);
|
return *static_cast<const DerivedT *>(this)->operator+(n);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -131,15 +166,26 @@ public:
|
|||||||
typedef DifferenceTypeT difference_type;
|
typedef DifferenceTypeT difference_type;
|
||||||
|
|
||||||
DerivedT &operator+=(difference_type n) {
|
DerivedT &operator+=(difference_type n) {
|
||||||
|
static_assert(
|
||||||
|
BaseT::IsRandomAccess,
|
||||||
|
"The '+=' operator is only defined for random access iterators.");
|
||||||
I += n;
|
I += n;
|
||||||
return *static_cast<DerivedT *>(this);
|
return *static_cast<DerivedT *>(this);
|
||||||
}
|
}
|
||||||
DerivedT &operator-=(difference_type n) {
|
DerivedT &operator-=(difference_type n) {
|
||||||
|
static_assert(
|
||||||
|
BaseT::IsRandomAccess,
|
||||||
|
"The '-=' operator is only defined for random access iterators.");
|
||||||
I -= n;
|
I -= n;
|
||||||
return *static_cast<DerivedT *>(this);
|
return *static_cast<DerivedT *>(this);
|
||||||
}
|
}
|
||||||
using BaseT::operator-;
|
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
|
// We have to explicitly provide ++ and -- rather than letting the facade
|
||||||
// forward to += because WrappedIteratorT might not support +=.
|
// forward to += because WrappedIteratorT might not support +=.
|
||||||
@ -150,12 +196,20 @@ public:
|
|||||||
}
|
}
|
||||||
using BaseT::operator--;
|
using BaseT::operator--;
|
||||||
DerivedT &operator--() {
|
DerivedT &operator--() {
|
||||||
|
static_assert(
|
||||||
|
BaseT::IsBidirectional,
|
||||||
|
"The decrement operator is only defined for bidirectional iterators.");
|
||||||
--I;
|
--I;
|
||||||
return *static_cast<DerivedT *>(this);
|
return *static_cast<DerivedT *>(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 { 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; }
|
ReferenceT operator*() const { return *I; }
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user