Commit dd82d95a authored by Clemens Backes's avatar Clemens Backes Committed by Commit Bot

[utils] Fix construction of constant OwnedVector

I hit this issue in an unrelated CL and it took me a while to figure out
what's happening.
This CL will allow the creation of constant OwnedVectors via
{OwnedVector<const T>::Of(collection)}.

R=tebbi@chromium.org

Change-Id: I337077a6c3960a2a2a8d857bec7450f664b87a3b
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2010109Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#65868}
parent 7763a926
......@@ -195,7 +195,7 @@ class OwnedVector {
// Allocates a new vector of the specified size via the default allocator.
static OwnedVector<T> New(size_t size) {
if (size == 0) return {};
return OwnedVector<T>(std::unique_ptr<T[]>(new T[size]), size);
return OwnedVector<T>(std::make_unique<T[]>(size), size);
}
// Allocates a new vector containing the specified collection of values.
......@@ -207,7 +207,8 @@ class OwnedVector {
static OwnedVector<T> Of(const U& collection) {
Iterator begin = std::begin(collection);
Iterator end = std::end(collection);
OwnedVector<T> vec = New(std::distance(begin, end));
using non_const_t = typename std::remove_const<T>::type;
auto vec = OwnedVector<non_const_t>::New(std::distance(begin, end));
std::copy(begin, end, vec.start());
return vec;
}
......
......@@ -57,5 +57,21 @@ TEST(VectorTest, Equals) {
CHECK(vec3_char != vec1_const_char);
}
TEST(OwnedVectorConstruction, Equals) {
auto int_vec = OwnedVector<int>::New(4);
CHECK_EQ(4, int_vec.size());
auto find_non_zero = [](int i) { return i != 0; };
CHECK_EQ(int_vec.end(),
std::find_if(int_vec.begin(), int_vec.end(), find_non_zero));
constexpr int kInit[] = {4, 11, 3};
auto init_vec1 = OwnedVector<int>::Of(kInit);
// Note: {const int} should also work: We initialize the owned vector, but
// afterwards it's non-modifyable.
auto init_vec2 = OwnedVector<const int>::Of(ArrayVector(kInit));
CHECK_EQ(init_vec1.as_vector(), ArrayVector(kInit));
CHECK_EQ(init_vec1.as_vector(), init_vec2.as_vector());
}
} // namespace internal
} // namespace v8
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment