mirror of
https://github.com/RPCS3/llvm.git
synced 2025-01-27 05:32:22 +00:00
Add an ArrayRef upcasting constructor from ArrayRef<U*> -> ArrayRef<T*> where T is a base of U.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225053 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
28650b8ec2
commit
735f1df049
@ -112,6 +112,16 @@ namespace llvm {
|
|||||||
std::is_convertible<U *const *, T const *>::value>::type* = 0)
|
std::is_convertible<U *const *, T const *>::value>::type* = 0)
|
||||||
: Data(A.data()), Length(A.size()) {}
|
: Data(A.data()), Length(A.size()) {}
|
||||||
|
|
||||||
|
/// Construct an ArrayRef<T*> from an ArrayRef<U*> where T is a super class
|
||||||
|
/// of U. This uses SFINAE to ensure that only ArrayRefs with this property
|
||||||
|
/// can be converted. This is an upcasting constructor.
|
||||||
|
template <typename U>
|
||||||
|
ArrayRef(const ArrayRef<U> &A,
|
||||||
|
typename std::enable_if<std::is_base_of<
|
||||||
|
typename std::remove_pointer<T>::type,
|
||||||
|
typename std::remove_pointer<U>::type>::value>::type * = 0)
|
||||||
|
: Data(reinterpret_cast<T const *>(A.data())), Length(A.size()) {}
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
/// @name Simple Operations
|
/// @name Simple Operations
|
||||||
/// @{
|
/// @{
|
||||||
|
@ -90,4 +90,39 @@ TEST(ArrayRefTest, ConstConvert) {
|
|||||||
a = ArrayRef<int *>(A);
|
a = ArrayRef<int *>(A);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct A {
|
||||||
|
int data;
|
||||||
|
|
||||||
|
A() : data(0) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct B : A {
|
||||||
|
int data2;
|
||||||
|
|
||||||
|
B() : A(), data2(0) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST(ArrayRefTest, UpcastConvert) {
|
||||||
|
B Data[5];
|
||||||
|
|
||||||
|
for (unsigned i = 0, e = 5; i != e; ++i) {
|
||||||
|
Data[i].data = i + 5;
|
||||||
|
Data[i].data2 = i + 30;
|
||||||
|
}
|
||||||
|
|
||||||
|
B *DataPtrs[5];
|
||||||
|
for (unsigned i = 0, e = 5; i != e; ++i) {
|
||||||
|
DataPtrs[i] = &Data[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayRef<B *> BArray(DataPtrs, 5);
|
||||||
|
ArrayRef<A *> AArray(BArray);
|
||||||
|
|
||||||
|
EXPECT_TRUE(AArray.size() == 5);
|
||||||
|
for (unsigned i = 0, e = 5; i != e; ++i) {
|
||||||
|
A *a = AArray[i];
|
||||||
|
EXPECT_TRUE(a->data == int(i + 5));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // end anonymous namespace
|
} // end anonymous namespace
|
||||||
|
Loading…
x
Reference in New Issue
Block a user