summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomasz Wasilczyk <twasilczyk@google.com>2017-07-20 03:34:03 +0000
committerandroid-build-merger <android-build-merger@google.com>2017-07-20 03:34:03 +0000
commit9a964e3ebafb4f675f664b394f3ed828f0613556 (patch)
tree7404bc6c4319d1758257ed36dfc2898336d6fc6f
parent7b3ed882bc89c58df638b04dabc8727a74a290db (diff)
parent59d63aa8aab423525a21e4fb6c8582fa4726c478 (diff)
downloadlibhidl-9a964e3ebafb4f675f664b394f3ed828f0613556.tar.gz
Add range constructor to hidl_vec. am: a9f6073068 am: 2801531033 am: 66562e3a27
am: 59d63aa8aa Change-Id: I33e750e1abc58a5f4291186411cf216fdb89faa4
-rw-r--r--base/include/hidl/HidlSupport.h21
-rw-r--r--test_main.cpp24
2 files changed, 45 insertions, 0 deletions
diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h
index 6807860..f25cdc4 100644
--- a/base/include/hidl/HidlSupport.h
+++ b/base/include/hidl/HidlSupport.h
@@ -316,6 +316,27 @@ struct hidl_vec {
*this = other;
}
+ template <typename InputIterator,
+ typename = typename std::enable_if<std::is_convertible<
+ typename std::iterator_traits<InputIterator>::iterator_category,
+ std::input_iterator_tag>::value>::type>
+ hidl_vec(InputIterator first, InputIterator last) : mOwnsBuffer(true) {
+ auto size = std::distance(first, last);
+ if (size > static_cast<int64_t>(UINT32_MAX)) {
+ details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
+ }
+ if (size < 0) {
+ details::logAlwaysFatal("size can't be negative.");
+ }
+ mSize = static_cast<uint32_t>(size);
+ mBuffer = new T[mSize];
+
+ size_t idx = 0;
+ for (; first != last; ++first) {
+ mBuffer[idx++] = static_cast<T>(*first);
+ }
+ }
+
~hidl_vec() {
if (mOwnsBuffer) {
delete[] mBuffer;
diff --git a/test_main.cpp b/test_main.cpp
index bce9294..1f2f845 100644
--- a/test_main.cpp
+++ b/test_main.cpp
@@ -262,6 +262,30 @@ TEST_F(LibHidlTest, VecEqTest) {
EXPECT_TRUE(hv1 != hv3);
}
+TEST_F(LibHidlTest, VecRangeCtorTest) {
+ struct ConvertibleType {
+ int val;
+
+ explicit ConvertibleType(int val) : val(val) {}
+ explicit operator int() const { return val; }
+ bool operator==(const int& other) const { return val == other; }
+ };
+
+ std::vector<ConvertibleType> input{
+ ConvertibleType(1), ConvertibleType(2), ConvertibleType(3),
+ };
+
+ android::hardware::hidl_vec<int> hv(input.begin(), input.end());
+
+ EXPECT_EQ(input.size(), hv.size());
+ int sum = 0;
+ for (unsigned i = 0; i < input.size(); i++) {
+ EXPECT_EQ(input[i], hv[i]);
+ sum += hv[i];
+ }
+ EXPECT_EQ(sum, 1 + 2 + 3);
+}
+
TEST_F(LibHidlTest, ArrayTest) {
using android::hardware::hidl_array;
int32_t array[] = {5, 6, 7};