diff options
author | Siarhei Vishniakou <svv@google.com> | 2019-02-08 12:48:04 -0800 |
---|---|---|
committer | Siarhei Vishniakou <svv@google.com> | 2019-02-08 13:29:49 -0800 |
commit | c48d4f62f75e073f11118454c5c00b1548eeca5a (patch) | |
tree | ad1d20c930a5f61377a36c6e019339bfeec51d52 /base | |
parent | 06d58be0332f865b20cf3b0d973c1b9d22ac5c37 (diff) | |
download | libhidl-c48d4f62f75e073f11118454c5c00b1548eeca5a.tar.gz |
Allow hidl_vec = initializer list
Currently, a hidl vector can be constructed using an initializer list, but
it cannot be made equal to an initializer list.
Add the missing operator here to allow for simpler usage.
Test: atest libhidl_test
Bug: 117935272
Change-Id: Id21e9cafee754968a7300a3d7bc85481d32dcc3a
Diffstat (limited to 'base')
-rw-r--r-- | base/include/hidl/HidlSupport.h | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h index 43e84c0..55f21ea 100644 --- a/base/include/hidl/HidlSupport.h +++ b/base/include/hidl/HidlSupport.h @@ -345,19 +345,7 @@ struct hidl_vec { *this = std::move(other); } - hidl_vec(const std::initializer_list<T> list) - : mOwnsBuffer(true) { - if (list.size() > UINT32_MAX) { - details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements."); - } - mSize = static_cast<uint32_t>(list.size()); - mBuffer = new T[mSize]; - - size_t idx = 0; - for (auto it = list.begin(); it != list.end(); ++it) { - mBuffer[idx++] = *it; - } - } + hidl_vec(const std::initializer_list<T> list) : hidl_vec() { *this = list; } hidl_vec(const std::vector<T> &other) : hidl_vec() { *this = other; @@ -453,6 +441,24 @@ struct hidl_vec { return *this; } + hidl_vec& operator=(const std::initializer_list<T> list) { + if (list.size() > UINT32_MAX) { + details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements."); + } + if (mOwnsBuffer) { + delete[] mBuffer; + } + mSize = static_cast<uint32_t>(list.size()); + mBuffer = new T[mSize]; + mOwnsBuffer = true; + + size_t idx = 0; + for (auto it = list.begin(); it != list.end(); ++it) { + mBuffer[idx++] = *it; + } + return *this; + } + // cast to an std::vector. operator std::vector<T>() const { std::vector<T> v(mSize); |