Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_HIDL_SUPPORT_H |
| 18 | #define ANDROID_HIDL_SUPPORT_H |
| 19 | |
Iliyan Malchev | 692070a | 2016-09-12 16:30:44 -0700 | [diff] [blame] | 20 | #include <algorithm> |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 21 | #include <array> |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 22 | #include <iterator> |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 23 | #include <hidl/HidlInternal.h> |
Yifan Hong | 1e265bb | 2016-11-08 12:33:44 -0800 | [diff] [blame] | 24 | #include <map> |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 25 | #include <sstream> |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 26 | #include <stddef.h> |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 27 | #include <tuple> |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 28 | #include <type_traits> |
Steven Moreland | f0dd160 | 2019-04-26 14:51:24 -0700 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
| 31 | // no requirements on types not used in scatter/gather |
| 32 | // no requirements on other libraries |
| 33 | #pragma clang diagnostic push |
| 34 | #pragma clang diagnostic ignored "-Wpadded" |
| 35 | #include <cutils/native_handle.h> |
| 36 | #include <hidl/Status.h> |
Iliyan Malchev | 692070a | 2016-09-12 16:30:44 -0700 | [diff] [blame] | 37 | #include <utils/Errors.h> |
| 38 | #include <utils/RefBase.h> |
| 39 | #include <utils/StrongPointer.h> |
Steven Moreland | f0dd160 | 2019-04-26 14:51:24 -0700 | [diff] [blame] | 40 | #pragma clang diagnostic pop |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 41 | |
| 42 | namespace android { |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 43 | |
Martijn Coenen | 9b8f9c3 | 2016-12-09 15:51:06 +0100 | [diff] [blame] | 44 | // this file is included by all hidl interface, so we must forward declare the |
| 45 | // IMemory and IBase types. |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 46 | namespace hidl { |
| 47 | namespace memory { |
| 48 | namespace V1_0 { |
Pirama Arumuga Nainar | ed4c5d0 | 2018-04-10 15:46:23 -0700 | [diff] [blame] | 49 | |
| 50 | struct IMemory; |
| 51 | |
| 52 | } // namespace V1_0 |
| 53 | } // namespace memory |
| 54 | } // namespace hidl |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 55 | |
Martijn Coenen | 9b8f9c3 | 2016-12-09 15:51:06 +0100 | [diff] [blame] | 56 | namespace hidl { |
| 57 | namespace base { |
| 58 | namespace V1_0 { |
Pirama Arumuga Nainar | ed4c5d0 | 2018-04-10 15:46:23 -0700 | [diff] [blame] | 59 | |
| 60 | struct IBase; |
| 61 | |
| 62 | } // namespace V1_0 |
| 63 | } // namespace base |
| 64 | } // namespace hidl |
Martijn Coenen | 9b8f9c3 | 2016-12-09 15:51:06 +0100 | [diff] [blame] | 65 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 66 | namespace hardware { |
| 67 | |
Yifan Hong | 24332ef | 2017-03-07 16:22:19 -0800 | [diff] [blame] | 68 | namespace details { |
| 69 | // Return true on userdebug / eng builds and false on user builds. |
| 70 | bool debuggable(); |
| 71 | } // namespace details |
| 72 | |
Martijn Coenen | 9b8f9c3 | 2016-12-09 15:51:06 +0100 | [diff] [blame] | 73 | // hidl_death_recipient is a callback interfaced that can be used with |
| 74 | // linkToDeath() / unlinkToDeath() |
| 75 | struct hidl_death_recipient : public virtual RefBase { |
| 76 | virtual void serviceDied(uint64_t cookie, |
| 77 | const ::android::wp<::android::hidl::base::V1_0::IBase>& who) = 0; |
| 78 | }; |
| 79 | |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 80 | // hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer, |
| 81 | // so that it can safely be transferred between 32-bit and 64-bit processes. |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 82 | // The ownership semantics for this are: |
| 83 | // 1) The conversion constructor and assignment operator taking a const native_handle_t* |
| 84 | // do not take ownership of the handle; this is because these operations are usually |
| 85 | // just done for IPC, and cloning by default is a waste of resources. If you want |
| 86 | // a hidl_handle to take ownership, call setTo(handle, true /*shouldOwn*/); |
| 87 | // 2) The copy constructor/assignment operator taking a hidl_handle *DO* take ownership; |
| 88 | // that is because it's not intuitive that this class encapsulates a native_handle_t |
| 89 | // which needs cloning to be valid; in particular, this allows constructs like this: |
| 90 | // hidl_handle copy; |
| 91 | // foo->someHidlCall([&](auto incoming_handle) { |
| 92 | // copy = incoming_handle; |
| 93 | // }); |
| 94 | // // copy and its enclosed file descriptors will remain valid here. |
| 95 | // 3) The move constructor does what you would expect; it only owns the handle if the |
| 96 | // original did. |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 97 | struct hidl_handle { |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 98 | hidl_handle(); |
| 99 | ~hidl_handle(); |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 100 | |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 101 | hidl_handle(const native_handle_t *handle); |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 102 | |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 103 | // copy constructor. |
| 104 | hidl_handle(const hidl_handle &other); |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 105 | |
| 106 | // move constructor. |
Martijn Coenen | e8c36e1 | 2017-05-30 16:25:07 -0700 | [diff] [blame] | 107 | hidl_handle(hidl_handle &&other) noexcept; |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 108 | |
Steven Moreland | 6ffdc2a | 2017-01-23 12:34:33 -0800 | [diff] [blame] | 109 | // assignment operators |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 110 | hidl_handle &operator=(const hidl_handle &other); |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 111 | |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 112 | hidl_handle &operator=(const native_handle_t *native_handle); |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 113 | |
Martijn Coenen | e8c36e1 | 2017-05-30 16:25:07 -0700 | [diff] [blame] | 114 | hidl_handle &operator=(hidl_handle &&other) noexcept; |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 115 | |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 116 | void setTo(native_handle_t* handle, bool shouldOwn = false); |
| 117 | |
| 118 | const native_handle_t* operator->() const; |
| 119 | |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 120 | // implicit conversion to const native_handle_t* |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 121 | operator const native_handle_t *() const; |
| 122 | |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 123 | // explicit conversion |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 124 | const native_handle_t *getNativeHandle() const; |
Nirav Atre | 564a8d2 | 2018-07-26 18:29:12 -0700 | [diff] [blame] | 125 | |
| 126 | // offsetof(hidl_handle, mHandle) exposed since mHandle is private. |
| 127 | static const size_t kOffsetOfNativeHandle; |
| 128 | |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 129 | private: |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 130 | void freeHandle(); |
| 131 | |
Steven Moreland | f0dd160 | 2019-04-26 14:51:24 -0700 | [diff] [blame] | 132 | details::hidl_pointer<const native_handle_t> mHandle; |
| 133 | bool mOwnsHandle; |
| 134 | uint8_t mPad[7]; |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 135 | }; |
| 136 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 137 | struct hidl_string { |
| 138 | hidl_string(); |
| 139 | ~hidl_string(); |
| 140 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 141 | // copy constructor. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 142 | hidl_string(const hidl_string &); |
Steven Moreland | a21d84f | 2017-02-16 09:23:45 -0800 | [diff] [blame] | 143 | // copy from a C-style string. nullptr will create an empty string |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 144 | hidl_string(const char *); |
Steven Moreland | 53120f7 | 2017-01-12 09:39:26 -0800 | [diff] [blame] | 145 | // copy the first length characters from a C-style string. |
| 146 | hidl_string(const char *, size_t length); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 147 | // copy from an std::string. |
| 148 | hidl_string(const std::string &); |
| 149 | |
| 150 | // move constructor. |
Martijn Coenen | e8c36e1 | 2017-05-30 16:25:07 -0700 | [diff] [blame] | 151 | hidl_string(hidl_string &&) noexcept; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 152 | |
| 153 | const char *c_str() const; |
| 154 | size_t size() const; |
| 155 | bool empty() const; |
| 156 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 157 | // copy assignment operator. |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 158 | hidl_string &operator=(const hidl_string &); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 159 | // copy from a C-style string. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 160 | hidl_string &operator=(const char *s); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 161 | // copy from an std::string. |
| 162 | hidl_string &operator=(const std::string &); |
| 163 | // move assignment operator. |
Martijn Coenen | e8c36e1 | 2017-05-30 16:25:07 -0700 | [diff] [blame] | 164 | hidl_string &operator=(hidl_string &&other) noexcept; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 165 | // cast to std::string. |
| 166 | operator std::string() const; |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 167 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 168 | void clear(); |
| 169 | |
| 170 | // Reference an external char array. Ownership is _not_ transferred. |
| 171 | // Caller is responsible for ensuring that underlying memory is valid |
| 172 | // for the lifetime of this hidl_string. |
Steven Moreland | 57c6fcb | 2018-03-06 11:31:33 -0800 | [diff] [blame] | 173 | // |
| 174 | // size == strlen(data) |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 175 | void setToExternal(const char *data, size_t size); |
| 176 | |
Andreas Huber | ebfeb36 | 2016-08-25 13:39:05 -0700 | [diff] [blame] | 177 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 178 | static const size_t kOffsetOfBuffer; |
| 179 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 180 | private: |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 181 | details::hidl_pointer<const char> mBuffer; |
| 182 | uint32_t mSize; // NOT including the terminating '\0'. |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 183 | bool mOwnsBuffer; // if true then mBuffer is a mutable char * |
Steven Moreland | f0dd160 | 2019-04-26 14:51:24 -0700 | [diff] [blame] | 184 | uint8_t mPad[3]; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 185 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 186 | // copy from data with size. Assume that my memory is freed |
| 187 | // (through clear(), for example) |
| 188 | void copyFrom(const char *data, size_t size); |
| 189 | // move from another hidl_string |
| 190 | void moveFrom(hidl_string &&); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 191 | }; |
| 192 | |
Chih-Hung Hsieh | b30feca | 2017-08-01 15:40:48 -0700 | [diff] [blame] | 193 | // Use NOLINT to suppress missing parentheses warnings around OP. |
Janis Danisevskis | 62a38df | 2017-11-22 13:47:33 -0800 | [diff] [blame] | 194 | #define HIDL_STRING_OPERATOR(OP) \ |
| 195 | inline bool operator OP(const hidl_string& hs1, const hidl_string& hs2) { \ |
| 196 | return strcmp(hs1.c_str(), hs2.c_str()) OP 0; /* NOLINT */ \ |
| 197 | } \ |
| 198 | inline bool operator OP(const hidl_string& hs, const char* s) { \ |
| 199 | return strcmp(hs.c_str(), s) OP 0; /* NOLINT */ \ |
| 200 | } \ |
| 201 | inline bool operator OP(const char* s, const hidl_string& hs) { \ |
| 202 | return strcmp(s, hs.c_str()) OP 0; /* NOLINT */ \ |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 203 | } |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 204 | |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 205 | HIDL_STRING_OPERATOR(==) |
| 206 | HIDL_STRING_OPERATOR(!=) |
| 207 | HIDL_STRING_OPERATOR(<) |
| 208 | HIDL_STRING_OPERATOR(<=) |
| 209 | HIDL_STRING_OPERATOR(>) |
| 210 | HIDL_STRING_OPERATOR(>=) |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 211 | |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 212 | #undef HIDL_STRING_OPERATOR |
Yifan Hong | 5708fb4 | 2016-10-26 17:50:29 -0700 | [diff] [blame] | 213 | |
Scott Randolph | 0c84ab4 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 214 | // Send our content to the output stream |
| 215 | std::ostream& operator<<(std::ostream& os, const hidl_string& str); |
| 216 | |
| 217 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 218 | // hidl_memory is a structure that can be used to transfer |
| 219 | // pieces of shared memory between processes. The assumption |
| 220 | // of this object is that the memory remains accessible as |
| 221 | // long as the file descriptors in the enclosed mHandle |
| 222 | // - as well as all of its cross-process dups() - remain opened. |
| 223 | struct hidl_memory { |
| 224 | |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 225 | hidl_memory() : mHandle(nullptr), mSize(0), mName("") { |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /** |
Steven Moreland | 3f8c416 | 2017-10-11 13:47:00 -0700 | [diff] [blame] | 229 | * Creates a hidl_memory object whose handle has the same lifetime |
| 230 | * as the handle moved into it. |
| 231 | */ |
| 232 | hidl_memory(const hidl_string& name, hidl_handle&& handle, size_t size) |
| 233 | : mHandle(std::move(handle)), mSize(size), mName(name) {} |
| 234 | |
| 235 | /** |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 236 | * Creates a hidl_memory object, but doesn't take ownership of |
| 237 | * the passed in native_handle_t; callers are responsible for |
| 238 | * making sure the handle remains valid while this object is |
| 239 | * used. |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 240 | */ |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 241 | hidl_memory(const hidl_string &name, const native_handle_t *handle, size_t size) |
| 242 | : mHandle(handle), |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 243 | mSize(size), |
| 244 | mName(name) |
| 245 | {} |
| 246 | |
| 247 | // copy constructor |
| 248 | hidl_memory(const hidl_memory& other) { |
| 249 | *this = other; |
| 250 | } |
| 251 | |
| 252 | // copy assignment |
| 253 | hidl_memory &operator=(const hidl_memory &other) { |
| 254 | if (this != &other) { |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 255 | mHandle = other.mHandle; |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 256 | mSize = other.mSize; |
| 257 | mName = other.mName; |
| 258 | } |
| 259 | |
| 260 | return *this; |
| 261 | } |
| 262 | |
Hridya Valsaraju | 0126889 | 2017-02-27 08:48:38 -0800 | [diff] [blame] | 263 | // move constructor |
Martijn Coenen | e8c36e1 | 2017-05-30 16:25:07 -0700 | [diff] [blame] | 264 | hidl_memory(hidl_memory&& other) noexcept { |
Hridya Valsaraju | 0126889 | 2017-02-27 08:48:38 -0800 | [diff] [blame] | 265 | *this = std::move(other); |
| 266 | } |
| 267 | |
| 268 | // move assignment |
Martijn Coenen | e8c36e1 | 2017-05-30 16:25:07 -0700 | [diff] [blame] | 269 | hidl_memory &operator=(hidl_memory &&other) noexcept { |
Hridya Valsaraju | 0126889 | 2017-02-27 08:48:38 -0800 | [diff] [blame] | 270 | if (this != &other) { |
| 271 | mHandle = std::move(other.mHandle); |
| 272 | mSize = other.mSize; |
| 273 | mName = std::move(other.mName); |
| 274 | other.mSize = 0; |
| 275 | } |
| 276 | |
| 277 | return *this; |
| 278 | } |
| 279 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 280 | |
| 281 | ~hidl_memory() { |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | const native_handle_t* handle() const { |
| 285 | return mHandle; |
| 286 | } |
| 287 | |
| 288 | const hidl_string &name() const { |
| 289 | return mName; |
| 290 | } |
| 291 | |
Hridya Valsaraju | 6d4acb1 | 2017-03-03 14:24:43 -0800 | [diff] [blame] | 292 | uint64_t size() const { |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 293 | return mSize; |
| 294 | } |
| 295 | |
Howard Chen | 9bc35c4 | 2017-10-13 14:21:46 +0800 | [diff] [blame] | 296 | // @return true if it's valid |
| 297 | inline bool valid() const { return handle() != nullptr; } |
| 298 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 299 | // offsetof(hidl_memory, mHandle) exposed since mHandle is private. |
| 300 | static const size_t kOffsetOfHandle; |
| 301 | // offsetof(hidl_memory, mName) exposed since mHandle is private. |
| 302 | static const size_t kOffsetOfName; |
Jeff Tinker | 0f3461d | 2017-01-03 10:40:55 -0800 | [diff] [blame] | 303 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 304 | private: |
Steven Moreland | f0dd160 | 2019-04-26 14:51:24 -0700 | [diff] [blame] | 305 | hidl_handle mHandle; |
| 306 | uint64_t mSize; |
| 307 | hidl_string mName; |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 308 | }; |
| 309 | |
Howard Chen | 9bc35c4 | 2017-10-13 14:21:46 +0800 | [diff] [blame] | 310 | // HidlMemory is a wrapper class to support sp<> for hidl_memory. It also |
| 311 | // provides factory methods to create an instance from hidl_memory or |
| 312 | // from a opened file descriptor. The number of factory methods can be increase |
| 313 | // to support other type of hidl_memory without break the ABI. |
| 314 | class HidlMemory : public virtual hidl_memory, public virtual ::android::RefBase { |
| 315 | public: |
Howard Chen | 8e42f5a | 2017-11-09 13:58:28 +0800 | [diff] [blame] | 316 | static sp<HidlMemory> getInstance(const hidl_memory& mem); |
| 317 | |
Howard Chen | 9bc35c4 | 2017-10-13 14:21:46 +0800 | [diff] [blame] | 318 | static sp<HidlMemory> getInstance(hidl_memory&& mem); |
| 319 | |
| 320 | static sp<HidlMemory> getInstance(const hidl_string& name, hidl_handle&& handle, uint64_t size); |
| 321 | // @param fd, shall be opened and points to the resource. |
| 322 | // @note this method takes the ownership of the fd and will close it in |
| 323 | // destructor |
Howard Chen | 8e42f5a | 2017-11-09 13:58:28 +0800 | [diff] [blame] | 324 | // @return nullptr in failure with the fd closed |
Howard Chen | 9bc35c4 | 2017-10-13 14:21:46 +0800 | [diff] [blame] | 325 | static sp<HidlMemory> getInstance(const hidl_string& name, int fd, uint64_t size); |
| 326 | |
Howard Chen | 280af0a | 2017-10-20 13:58:36 +0800 | [diff] [blame] | 327 | virtual ~HidlMemory(); |
Howard Chen | 8e42f5a | 2017-11-09 13:58:28 +0800 | [diff] [blame] | 328 | |
Howard Chen | 9bc35c4 | 2017-10-13 14:21:46 +0800 | [diff] [blame] | 329 | protected: |
Howard Chen | 8e42f5a | 2017-11-09 13:58:28 +0800 | [diff] [blame] | 330 | HidlMemory(); |
| 331 | HidlMemory(const hidl_string& name, hidl_handle&& handle, size_t size); |
Howard Chen | 9bc35c4 | 2017-10-13 14:21:46 +0800 | [diff] [blame] | 332 | }; |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 333 | //////////////////////////////////////////////////////////////////////////////// |
| 334 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 335 | template<typename T> |
Hridya Valsaraju | b737030 | 2017-03-08 14:39:23 -0800 | [diff] [blame] | 336 | struct hidl_vec { |
Steven Moreland | b37de4a | 2019-05-06 09:39:47 -0700 | [diff] [blame] | 337 | using value_type = T; |
| 338 | |
Steven Moreland | e953ef3 | 2019-09-05 14:37:35 -0700 | [diff] [blame] | 339 | hidl_vec() : mBuffer(nullptr), mSize(0), mOwnsBuffer(false) { |
Hridya Valsaraju | 6d4acb1 | 2017-03-03 14:24:43 -0800 | [diff] [blame] | 340 | static_assert(hidl_vec<T>::kOffsetOfBuffer == 0, "wrong offset"); |
Steven Moreland | 5cc1691 | 2019-04-18 12:54:56 -0700 | [diff] [blame] | 341 | |
Steven Moreland | f0dd160 | 2019-04-26 14:51:24 -0700 | [diff] [blame] | 342 | memset(mPad, 0, sizeof(mPad)); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 343 | } |
| 344 | |
Eran Messeri | 6329c3e | 2019-02-26 11:34:32 +0000 | [diff] [blame] | 345 | hidl_vec(size_t size) : hidl_vec() { resize(size); } |
Steven Moreland | 96e9de0 | 2017-09-29 14:11:20 -0700 | [diff] [blame] | 346 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 347 | hidl_vec(const hidl_vec<T> &other) : hidl_vec() { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 348 | *this = other; |
| 349 | } |
| 350 | |
Steven Moreland | 5cc1691 | 2019-04-18 12:54:56 -0700 | [diff] [blame] | 351 | hidl_vec(hidl_vec<T> &&other) noexcept : hidl_vec() { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 352 | *this = std::move(other); |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Siarhei Vishniakou | c48d4f6 | 2019-02-08 12:48:04 -0800 | [diff] [blame] | 355 | hidl_vec(const std::initializer_list<T> list) : hidl_vec() { *this = list; } |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 356 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 357 | hidl_vec(const std::vector<T> &other) : hidl_vec() { |
| 358 | *this = other; |
| 359 | } |
| 360 | |
Tomasz Wasilczyk | a9f6073 | 2017-06-30 10:53:22 -0700 | [diff] [blame] | 361 | template <typename InputIterator, |
| 362 | typename = typename std::enable_if<std::is_convertible< |
| 363 | typename std::iterator_traits<InputIterator>::iterator_category, |
| 364 | std::input_iterator_tag>::value>::type> |
Steven Moreland | 5cc1691 | 2019-04-18 12:54:56 -0700 | [diff] [blame] | 365 | hidl_vec(InputIterator first, InputIterator last) : hidl_vec() { |
Tomasz Wasilczyk | a9f6073 | 2017-06-30 10:53:22 -0700 | [diff] [blame] | 366 | auto size = std::distance(first, last); |
| 367 | if (size > static_cast<int64_t>(UINT32_MAX)) { |
| 368 | details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements."); |
| 369 | } |
| 370 | if (size < 0) { |
| 371 | details::logAlwaysFatal("size can't be negative."); |
| 372 | } |
| 373 | mSize = static_cast<uint32_t>(size); |
Steven Moreland | 97e2d17 | 2019-04-30 17:56:45 -0700 | [diff] [blame] | 374 | mBuffer = new T[mSize](); |
Steven Moreland | 5cc1691 | 2019-04-18 12:54:56 -0700 | [diff] [blame] | 375 | mOwnsBuffer = true; |
Tomasz Wasilczyk | a9f6073 | 2017-06-30 10:53:22 -0700 | [diff] [blame] | 376 | |
| 377 | size_t idx = 0; |
| 378 | for (; first != last; ++first) { |
| 379 | mBuffer[idx++] = static_cast<T>(*first); |
| 380 | } |
| 381 | } |
| 382 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 383 | ~hidl_vec() { |
| 384 | if (mOwnsBuffer) { |
| 385 | delete[] mBuffer; |
| 386 | } |
Stephen Hines | fd9ecee | 2017-09-27 18:52:52 -0700 | [diff] [blame] | 387 | mBuffer = nullptr; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 388 | } |
| 389 | |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 390 | // Reference an existing array, optionally taking ownership. It is the |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 391 | // caller's responsibility to ensure that the underlying memory stays |
| 392 | // valid for the lifetime of this hidl_vec. |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 393 | void setToExternal(T *data, size_t size, bool shouldOwn = false) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 394 | if (mOwnsBuffer) { |
| 395 | delete [] mBuffer; |
| 396 | } |
| 397 | mBuffer = data; |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 398 | if (size > UINT32_MAX) { |
Hridya Valsaraju | b737030 | 2017-03-08 14:39:23 -0800 | [diff] [blame] | 399 | details::logAlwaysFatal("external vector size exceeds 2^32 elements."); |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 400 | } |
| 401 | mSize = static_cast<uint32_t>(size); |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 402 | mOwnsBuffer = shouldOwn; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 403 | } |
| 404 | |
Alexey Polyudov | 6f0c9a1 | 2016-10-18 09:37:35 -0700 | [diff] [blame] | 405 | T *data() { |
| 406 | return mBuffer; |
| 407 | } |
| 408 | |
| 409 | const T *data() const { |
| 410 | return mBuffer; |
| 411 | } |
| 412 | |
Alexey Polyudov | c98a99c | 2016-10-18 09:43:56 -0700 | [diff] [blame] | 413 | T *releaseData() { |
Steven Moreland | e873cf3 | 2020-04-03 10:55:12 -0700 | [diff] [blame] | 414 | if (!mOwnsBuffer && mBuffer != nullptr) { |
Alexey Polyudov | c98a99c | 2016-10-18 09:43:56 -0700 | [diff] [blame] | 415 | resize(mSize); |
| 416 | } |
| 417 | mOwnsBuffer = false; |
| 418 | return mBuffer; |
| 419 | } |
| 420 | |
Martijn Coenen | e8c36e1 | 2017-05-30 16:25:07 -0700 | [diff] [blame] | 421 | hidl_vec &operator=(hidl_vec &&other) noexcept { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 422 | if (mOwnsBuffer) { |
| 423 | delete[] mBuffer; |
| 424 | } |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 425 | mBuffer = other.mBuffer; |
| 426 | mSize = other.mSize; |
| 427 | mOwnsBuffer = other.mOwnsBuffer; |
| 428 | other.mOwnsBuffer = false; |
| 429 | return *this; |
| 430 | } |
| 431 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 432 | hidl_vec &operator=(const hidl_vec &other) { |
| 433 | if (this != &other) { |
| 434 | if (mOwnsBuffer) { |
| 435 | delete[] mBuffer; |
| 436 | } |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 437 | copyFrom(other, other.mSize); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | return *this; |
| 441 | } |
| 442 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 443 | // copy from an std::vector. |
| 444 | hidl_vec &operator=(const std::vector<T> &other) { |
| 445 | if (mOwnsBuffer) { |
| 446 | delete[] mBuffer; |
| 447 | } |
| 448 | copyFrom(other, other.size()); |
| 449 | return *this; |
| 450 | } |
| 451 | |
Siarhei Vishniakou | c48d4f6 | 2019-02-08 12:48:04 -0800 | [diff] [blame] | 452 | hidl_vec& operator=(const std::initializer_list<T> list) { |
| 453 | if (list.size() > UINT32_MAX) { |
| 454 | details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements."); |
| 455 | } |
| 456 | if (mOwnsBuffer) { |
| 457 | delete[] mBuffer; |
| 458 | } |
| 459 | mSize = static_cast<uint32_t>(list.size()); |
Steven Moreland | 97e2d17 | 2019-04-30 17:56:45 -0700 | [diff] [blame] | 460 | mBuffer = new T[mSize](); |
Siarhei Vishniakou | c48d4f6 | 2019-02-08 12:48:04 -0800 | [diff] [blame] | 461 | mOwnsBuffer = true; |
| 462 | |
| 463 | size_t idx = 0; |
| 464 | for (auto it = list.begin(); it != list.end(); ++it) { |
| 465 | mBuffer[idx++] = *it; |
| 466 | } |
| 467 | return *this; |
| 468 | } |
| 469 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 470 | // cast to an std::vector. |
| 471 | operator std::vector<T>() const { |
| 472 | std::vector<T> v(mSize); |
| 473 | for (size_t i = 0; i < mSize; ++i) { |
| 474 | v[i] = mBuffer[i]; |
| 475 | } |
| 476 | return v; |
| 477 | } |
| 478 | |
Yifan Hong | 9fcbb36 | 2016-12-20 16:46:41 -0800 | [diff] [blame] | 479 | // equality check, assuming that T::operator== is defined. |
| 480 | bool operator==(const hidl_vec &other) const { |
| 481 | if (mSize != other.size()) { |
| 482 | return false; |
| 483 | } |
| 484 | for (size_t i = 0; i < mSize; ++i) { |
| 485 | if (!(mBuffer[i] == other.mBuffer[i])) { |
| 486 | return false; |
| 487 | } |
| 488 | } |
| 489 | return true; |
| 490 | } |
| 491 | |
| 492 | // inequality check, assuming that T::operator== is defined. |
| 493 | inline bool operator!=(const hidl_vec &other) const { |
| 494 | return !((*this) == other); |
| 495 | } |
| 496 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 497 | size_t size() const { |
| 498 | return mSize; |
| 499 | } |
| 500 | |
| 501 | T &operator[](size_t index) { |
| 502 | return mBuffer[index]; |
| 503 | } |
| 504 | |
| 505 | const T &operator[](size_t index) const { |
| 506 | return mBuffer[index]; |
| 507 | } |
| 508 | |
Steven Moreland | b4c3aad | 2020-04-10 16:10:42 -0700 | [diff] [blame] | 509 | // Copies over old elements fitting in new size. Value initializes the rest. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 510 | void resize(size_t size) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 511 | if (size > UINT32_MAX) { |
Hridya Valsaraju | b737030 | 2017-03-08 14:39:23 -0800 | [diff] [blame] | 512 | details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements."); |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 513 | } |
Steven Moreland | 97e2d17 | 2019-04-30 17:56:45 -0700 | [diff] [blame] | 514 | T* newBuffer = new T[size](); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 515 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 516 | for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) { |
Steven Moreland | dfbedb8 | 2019-06-19 14:51:11 -0700 | [diff] [blame] | 517 | newBuffer[i] = std::move(mBuffer[i]); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | if (mOwnsBuffer) { |
| 521 | delete[] mBuffer; |
| 522 | } |
| 523 | mBuffer = newBuffer; |
| 524 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 525 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 526 | mOwnsBuffer = true; |
| 527 | } |
| 528 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 529 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 530 | static const size_t kOffsetOfBuffer; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 531 | |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 532 | private: |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 533 | // Define std interator interface for walking the array contents |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 534 | template<bool is_const> |
Ryan Prichard | 3197026 | 2024-04-11 23:49:11 -0700 | [diff] [blame^] | 535 | class iter { |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 536 | public: |
Ryan Prichard | 3197026 | 2024-04-11 23:49:11 -0700 | [diff] [blame^] | 537 | using iterator_category = std::random_access_iterator_tag; |
| 538 | using value_type = T; |
| 539 | using difference_type = ptrdiff_t; |
| 540 | using pointer = std::conditional_t<is_const, const T *, T *>; |
| 541 | using reference = std::conditional_t<is_const, const T &, T &>; |
| 542 | iter(pointer ptr) : mPtr(ptr) { } |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 543 | inline iter &operator++() { mPtr++; return *this; } |
| 544 | inline iter operator++(int) { iter i = *this; mPtr++; return i; } |
| 545 | inline iter &operator--() { mPtr--; return *this; } |
| 546 | inline iter operator--(int) { iter i = *this; mPtr--; return i; } |
Ryan Prichard | 3197026 | 2024-04-11 23:49:11 -0700 | [diff] [blame^] | 547 | inline friend iter operator+(difference_type n, const iter &it) { return it.mPtr + n; } |
| 548 | inline iter operator+(difference_type n) const { return mPtr + n; } |
| 549 | inline iter operator-(difference_type n) const { return mPtr - n; } |
| 550 | inline difference_type operator-(const iter &other) const { return mPtr - other.mPtr; } |
| 551 | inline iter &operator+=(difference_type n) { mPtr += n; return *this; } |
| 552 | inline iter &operator-=(difference_type n) { mPtr -= n; return *this; } |
| 553 | inline reference operator*() const { return *mPtr; } |
| 554 | inline pointer operator->() const { return mPtr; } |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 555 | inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; } |
| 556 | inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; } |
| 557 | inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; } |
| 558 | inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; } |
| 559 | inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; } |
| 560 | inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; } |
Ryan Prichard | 3197026 | 2024-04-11 23:49:11 -0700 | [diff] [blame^] | 561 | inline reference operator[](size_t n) const { return mPtr[n]; } |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 562 | private: |
Ryan Prichard | 3197026 | 2024-04-11 23:49:11 -0700 | [diff] [blame^] | 563 | pointer mPtr; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 564 | }; |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 565 | public: |
| 566 | using iterator = iter<false /* is_const */>; |
| 567 | using const_iterator = iter<true /* is_const */>; |
| 568 | |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 569 | iterator begin() { return data(); } |
| 570 | iterator end() { return data()+mSize; } |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 571 | const_iterator begin() const { return data(); } |
| 572 | const_iterator end() const { return data()+mSize; } |
Tomasz Wasilczyk | 93f7592 | 2019-07-16 08:44:15 -0700 | [diff] [blame] | 573 | iterator find(const T& v) { return std::find(begin(), end(), v); } |
| 574 | const_iterator find(const T& v) const { return std::find(begin(), end(), v); } |
| 575 | bool contains(const T& v) const { return find(v) != end(); } |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 576 | |
Tomasz Wasilczyk | 93f7592 | 2019-07-16 08:44:15 -0700 | [diff] [blame] | 577 | private: |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 578 | details::hidl_pointer<T> mBuffer; |
| 579 | uint32_t mSize; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 580 | bool mOwnsBuffer; |
Steven Moreland | f0dd160 | 2019-04-26 14:51:24 -0700 | [diff] [blame] | 581 | uint8_t mPad[3]; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 582 | |
| 583 | // copy from an array-like object, assuming my resources are freed. |
| 584 | template <typename Array> |
| 585 | void copyFrom(const Array &data, size_t size) { |
Chia-I Wu | 4b48edc | 2016-12-07 22:31:17 +0800 | [diff] [blame] | 586 | mSize = static_cast<uint32_t>(size); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 587 | mOwnsBuffer = true; |
| 588 | if (mSize > 0) { |
Steven Moreland | 97e2d17 | 2019-04-30 17:56:45 -0700 | [diff] [blame] | 589 | mBuffer = new T[size](); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 590 | for (size_t i = 0; i < size; ++i) { |
| 591 | mBuffer[i] = data[i]; |
| 592 | } |
| 593 | } else { |
Stephen Hines | fd9ecee | 2017-09-27 18:52:52 -0700 | [diff] [blame] | 594 | mBuffer = nullptr; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 595 | } |
| 596 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 597 | }; |
| 598 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 599 | template <typename T> |
| 600 | const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer); |
| 601 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 602 | //////////////////////////////////////////////////////////////////////////////// |
| 603 | |
| 604 | namespace details { |
| 605 | |
| 606 | template<size_t SIZE1, size_t... SIZES> |
| 607 | struct product { |
| 608 | static constexpr size_t value = SIZE1 * product<SIZES...>::value; |
| 609 | }; |
| 610 | |
| 611 | template<size_t SIZE1> |
| 612 | struct product<SIZE1> { |
| 613 | static constexpr size_t value = SIZE1; |
| 614 | }; |
| 615 | |
| 616 | template<typename T, size_t SIZE1, size_t... SIZES> |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 617 | struct std_array { |
| 618 | using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>; |
| 619 | }; |
| 620 | |
| 621 | template<typename T, size_t SIZE1> |
| 622 | struct std_array<T, SIZE1> { |
| 623 | using type = std::array<T, SIZE1>; |
| 624 | }; |
| 625 | |
| 626 | template<typename T, size_t SIZE1, size_t... SIZES> |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 627 | struct accessor { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 628 | |
| 629 | using std_array_type = typename std_array<T, SIZE1, SIZES...>::type; |
| 630 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 631 | explicit accessor(T *base) |
| 632 | : mBase(base) { |
| 633 | } |
| 634 | |
| 635 | accessor<T, SIZES...> operator[](size_t index) { |
| 636 | return accessor<T, SIZES...>( |
| 637 | &mBase[index * product<SIZES...>::value]); |
| 638 | } |
| 639 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 640 | accessor &operator=(const std_array_type &other) { |
| 641 | for (size_t i = 0; i < SIZE1; ++i) { |
| 642 | (*this)[i] = other[i]; |
| 643 | } |
| 644 | return *this; |
| 645 | } |
| 646 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 647 | private: |
| 648 | T *mBase; |
| 649 | }; |
| 650 | |
| 651 | template<typename T, size_t SIZE1> |
| 652 | struct accessor<T, SIZE1> { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 653 | |
| 654 | using std_array_type = typename std_array<T, SIZE1>::type; |
| 655 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 656 | explicit accessor(T *base) |
| 657 | : mBase(base) { |
| 658 | } |
| 659 | |
| 660 | T &operator[](size_t index) { |
| 661 | return mBase[index]; |
| 662 | } |
| 663 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 664 | accessor &operator=(const std_array_type &other) { |
| 665 | for (size_t i = 0; i < SIZE1; ++i) { |
| 666 | (*this)[i] = other[i]; |
| 667 | } |
| 668 | return *this; |
| 669 | } |
| 670 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 671 | private: |
| 672 | T *mBase; |
| 673 | }; |
| 674 | |
| 675 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 676 | struct const_accessor { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 677 | |
| 678 | using std_array_type = typename std_array<T, SIZE1, SIZES...>::type; |
| 679 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 680 | explicit const_accessor(const T *base) |
| 681 | : mBase(base) { |
| 682 | } |
| 683 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 684 | const_accessor<T, SIZES...> operator[](size_t index) const { |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 685 | return const_accessor<T, SIZES...>( |
| 686 | &mBase[index * product<SIZES...>::value]); |
| 687 | } |
| 688 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 689 | operator std_array_type() { |
| 690 | std_array_type array; |
| 691 | for (size_t i = 0; i < SIZE1; ++i) { |
| 692 | array[i] = (*this)[i]; |
| 693 | } |
| 694 | return array; |
| 695 | } |
| 696 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 697 | private: |
| 698 | const T *mBase; |
| 699 | }; |
| 700 | |
| 701 | template<typename T, size_t SIZE1> |
| 702 | struct const_accessor<T, SIZE1> { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 703 | |
| 704 | using std_array_type = typename std_array<T, SIZE1>::type; |
| 705 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 706 | explicit const_accessor(const T *base) |
| 707 | : mBase(base) { |
| 708 | } |
| 709 | |
| 710 | const T &operator[](size_t index) const { |
| 711 | return mBase[index]; |
| 712 | } |
| 713 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 714 | operator std_array_type() { |
| 715 | std_array_type array; |
| 716 | for (size_t i = 0; i < SIZE1; ++i) { |
| 717 | array[i] = (*this)[i]; |
| 718 | } |
| 719 | return array; |
| 720 | } |
| 721 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 722 | private: |
| 723 | const T *mBase; |
| 724 | }; |
| 725 | |
| 726 | } // namespace details |
| 727 | |
| 728 | //////////////////////////////////////////////////////////////////////////////// |
| 729 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 730 | // A multidimensional array of T's. Assumes that T::operator=(const T &) is defined. |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 731 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 732 | struct hidl_array { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 733 | |
| 734 | using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type; |
| 735 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 736 | hidl_array() = default; |
Steven Moreland | dfbedb8 | 2019-06-19 14:51:11 -0700 | [diff] [blame] | 737 | hidl_array(const hidl_array&) noexcept = default; |
| 738 | hidl_array(hidl_array&&) noexcept = default; |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 739 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 740 | // Copies the data from source, using T::operator=(const T &). |
| 741 | hidl_array(const T *source) { |
| 742 | for (size_t i = 0; i < elementCount(); ++i) { |
| 743 | mBuffer[i] = source[i]; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | // Copies the data from the given std::array, using T::operator=(const T &). |
| 748 | hidl_array(const std_array_type &array) { |
| 749 | details::accessor<T, SIZE1, SIZES...> modifier(mBuffer); |
| 750 | modifier = array; |
| 751 | } |
| 752 | |
Steven Moreland | dfbedb8 | 2019-06-19 14:51:11 -0700 | [diff] [blame] | 753 | hidl_array& operator=(const hidl_array&) noexcept = default; |
| 754 | hidl_array& operator=(hidl_array&&) noexcept = default; |
| 755 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 756 | T *data() { return mBuffer; } |
| 757 | const T *data() const { return mBuffer; } |
| 758 | |
| 759 | details::accessor<T, SIZES...> operator[](size_t index) { |
| 760 | return details::accessor<T, SIZES...>( |
| 761 | &mBuffer[index * details::product<SIZES...>::value]); |
| 762 | } |
| 763 | |
| 764 | details::const_accessor<T, SIZES...> operator[](size_t index) const { |
| 765 | return details::const_accessor<T, SIZES...>( |
| 766 | &mBuffer[index * details::product<SIZES...>::value]); |
| 767 | } |
| 768 | |
Yifan Hong | 9fcbb36 | 2016-12-20 16:46:41 -0800 | [diff] [blame] | 769 | // equality check, assuming that T::operator== is defined. |
| 770 | bool operator==(const hidl_array &other) const { |
| 771 | for (size_t i = 0; i < elementCount(); ++i) { |
| 772 | if (!(mBuffer[i] == other.mBuffer[i])) { |
| 773 | return false; |
| 774 | } |
| 775 | } |
| 776 | return true; |
| 777 | } |
| 778 | |
| 779 | inline bool operator!=(const hidl_array &other) const { |
| 780 | return !((*this) == other); |
| 781 | } |
| 782 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 783 | using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>; |
| 784 | |
| 785 | static constexpr size_tuple_type size() { |
| 786 | return std::make_tuple(SIZE1, SIZES...); |
| 787 | } |
| 788 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 789 | static constexpr size_t elementCount() { |
| 790 | return details::product<SIZE1, SIZES...>::value; |
| 791 | } |
| 792 | |
| 793 | operator std_array_type() const { |
| 794 | return details::const_accessor<T, SIZE1, SIZES...>(mBuffer); |
| 795 | } |
| 796 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 797 | private: |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 798 | T mBuffer[elementCount()]; |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 799 | }; |
| 800 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 801 | // An array of T's. Assumes that T::operator=(const T &) is defined. |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 802 | template<typename T, size_t SIZE1> |
| 803 | struct hidl_array<T, SIZE1> { |
Steven Moreland | b37de4a | 2019-05-06 09:39:47 -0700 | [diff] [blame] | 804 | using value_type = T; |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 805 | using std_array_type = typename details::std_array<T, SIZE1>::type; |
| 806 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 807 | hidl_array() = default; |
Steven Moreland | dfbedb8 | 2019-06-19 14:51:11 -0700 | [diff] [blame] | 808 | hidl_array(const hidl_array&) noexcept = default; |
| 809 | hidl_array(hidl_array&&) noexcept = default; |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 810 | |
| 811 | // Copies the data from source, using T::operator=(const T &). |
Sasha Levitskiy | 3da6848 | 2016-11-17 16:48:43 -0800 | [diff] [blame] | 812 | hidl_array(const T *source) { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 813 | for (size_t i = 0; i < elementCount(); ++i) { |
| 814 | mBuffer[i] = source[i]; |
| 815 | } |
Sasha Levitskiy | 3da6848 | 2016-11-17 16:48:43 -0800 | [diff] [blame] | 816 | } |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 817 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 818 | // Copies the data from the given std::array, using T::operator=(const T &). |
| 819 | hidl_array(const std_array_type &array) : hidl_array(array.data()) {} |
| 820 | |
Steven Moreland | dfbedb8 | 2019-06-19 14:51:11 -0700 | [diff] [blame] | 821 | hidl_array& operator=(const hidl_array&) noexcept = default; |
| 822 | hidl_array& operator=(hidl_array&&) noexcept = default; |
| 823 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 824 | T *data() { return mBuffer; } |
| 825 | const T *data() const { return mBuffer; } |
| 826 | |
| 827 | T &operator[](size_t index) { |
| 828 | return mBuffer[index]; |
| 829 | } |
| 830 | |
| 831 | const T &operator[](size_t index) const { |
| 832 | return mBuffer[index]; |
| 833 | } |
| 834 | |
Yifan Hong | 9fcbb36 | 2016-12-20 16:46:41 -0800 | [diff] [blame] | 835 | // equality check, assuming that T::operator== is defined. |
| 836 | bool operator==(const hidl_array &other) const { |
| 837 | for (size_t i = 0; i < elementCount(); ++i) { |
| 838 | if (!(mBuffer[i] == other.mBuffer[i])) { |
| 839 | return false; |
| 840 | } |
| 841 | } |
| 842 | return true; |
| 843 | } |
| 844 | |
| 845 | inline bool operator!=(const hidl_array &other) const { |
| 846 | return !((*this) == other); |
| 847 | } |
| 848 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 849 | static constexpr size_t size() { return SIZE1; } |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 850 | static constexpr size_t elementCount() { return SIZE1; } |
| 851 | |
| 852 | // Copies the data to an std::array, using T::operator=(T). |
| 853 | operator std_array_type() const { |
| 854 | std_array_type array; |
| 855 | for (size_t i = 0; i < SIZE1; ++i) { |
| 856 | array[i] = mBuffer[i]; |
| 857 | } |
| 858 | return array; |
| 859 | } |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 860 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 861 | private: |
| 862 | T mBuffer[SIZE1]; |
| 863 | }; |
| 864 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 865 | // ---------------------------------------------------------------------- |
| 866 | // Version functions |
| 867 | struct hidl_version { |
| 868 | public: |
Steven Moreland | 5cc1691 | 2019-04-18 12:54:56 -0700 | [diff] [blame] | 869 | constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) { |
| 870 | static_assert(sizeof(*this) == 4, "wrong size"); |
| 871 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 872 | |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 873 | bool operator==(const hidl_version& other) const { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 874 | return (mMajor == other.get_major() && mMinor == other.get_minor()); |
| 875 | } |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 876 | |
Steven Moreland | b328f01 | 2018-06-25 16:07:22 -0700 | [diff] [blame] | 877 | bool operator!=(const hidl_version& other) const { |
| 878 | return !(*this == other); |
| 879 | } |
| 880 | |
Eino-Ville Talvala | 19f4db5 | 2016-12-14 15:19:28 -0800 | [diff] [blame] | 881 | bool operator<(const hidl_version& other) const { |
| 882 | return (mMajor < other.get_major() || |
| 883 | (mMajor == other.get_major() && mMinor < other.get_minor())); |
| 884 | } |
| 885 | |
| 886 | bool operator>(const hidl_version& other) const { |
| 887 | return other < *this; |
| 888 | } |
| 889 | |
| 890 | bool operator<=(const hidl_version& other) const { |
| 891 | return !(*this > other); |
| 892 | } |
| 893 | |
| 894 | bool operator>=(const hidl_version& other) const { |
| 895 | return !(*this < other); |
| 896 | } |
| 897 | |
Martijn Coenen | 097a767 | 2016-09-08 16:56:41 +0200 | [diff] [blame] | 898 | constexpr uint16_t get_major() const { return mMajor; } |
| 899 | constexpr uint16_t get_minor() const { return mMinor; } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 900 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 901 | private: |
| 902 | uint16_t mMajor; |
| 903 | uint16_t mMinor; |
| 904 | }; |
| 905 | |
| 906 | inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) { |
| 907 | return hidl_version(major,minor); |
| 908 | } |
| 909 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 910 | ///////////////////// toString functions |
| 911 | |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 912 | std::string toString(const void *t); |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 913 | |
| 914 | // toString alias for numeric types |
| 915 | template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type> |
| 916 | inline std::string toString(T t) { |
| 917 | return std::to_string(t); |
| 918 | } |
| 919 | |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 920 | namespace details { |
| 921 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 922 | template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type> |
| 923 | inline std::string toHexString(T t, bool prefix = true) { |
| 924 | std::ostringstream os; |
| 925 | if (prefix) { os << std::showbase; } |
| 926 | os << std::hex << t; |
| 927 | return os.str(); |
| 928 | } |
| 929 | |
| 930 | template<> |
| 931 | inline std::string toHexString(uint8_t t, bool prefix) { |
| 932 | return toHexString(static_cast<int32_t>(t), prefix); |
| 933 | } |
| 934 | |
| 935 | template<> |
| 936 | inline std::string toHexString(int8_t t, bool prefix) { |
| 937 | return toHexString(static_cast<int32_t>(t), prefix); |
| 938 | } |
| 939 | |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 940 | template<typename Array> |
| 941 | std::string arrayToString(const Array &a, size_t size); |
| 942 | |
| 943 | template<size_t SIZE1> |
| 944 | std::string arraySizeToString() { |
| 945 | return std::string{"["} + toString(SIZE1) + "]"; |
| 946 | } |
| 947 | |
| 948 | template<size_t SIZE1, size_t SIZE2, size_t... SIZES> |
| 949 | std::string arraySizeToString() { |
| 950 | return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>(); |
| 951 | } |
| 952 | |
| 953 | template<typename T, size_t SIZE1> |
| 954 | std::string toString(details::const_accessor<T, SIZE1> a) { |
| 955 | return arrayToString(a, SIZE1); |
| 956 | } |
| 957 | |
| 958 | template<typename Array> |
| 959 | std::string arrayToString(const Array &a, size_t size) { |
| 960 | using android::hardware::toString; |
| 961 | std::string os; |
| 962 | os += "{"; |
| 963 | for (size_t i = 0; i < size; ++i) { |
| 964 | if (i > 0) { |
| 965 | os += ", "; |
| 966 | } |
| 967 | os += toString(a[i]); |
| 968 | } |
| 969 | os += "}"; |
| 970 | return os; |
| 971 | } |
| 972 | |
| 973 | template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES> |
| 974 | std::string toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...> a) { |
| 975 | return arrayToString(a, SIZE1); |
| 976 | } |
| 977 | |
| 978 | } //namespace details |
| 979 | |
| 980 | inline std::string toString(const void *t) { |
| 981 | return details::toHexString(reinterpret_cast<uintptr_t>(t)); |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | // debug string dump. There will be quotes around the string! |
| 985 | inline std::string toString(const hidl_string &hs) { |
| 986 | return std::string{"\""} + hs.c_str() + "\""; |
| 987 | } |
| 988 | |
| 989 | // debug string dump |
| 990 | inline std::string toString(const hidl_handle &hs) { |
| 991 | return toString(hs.getNativeHandle()); |
| 992 | } |
| 993 | |
| 994 | inline std::string toString(const hidl_memory &mem) { |
| 995 | return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = " |
| 996 | + toString(mem.size()) |
| 997 | + ", .handle = " + toString(mem.handle()) + "}"; |
| 998 | } |
| 999 | |
| 1000 | inline std::string toString(const sp<hidl_death_recipient> &dr) { |
| 1001 | return std::string{"death_recipient@"} + toString(dr.get()); |
| 1002 | } |
| 1003 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 1004 | // debug string dump, assuming that toString(T) is defined. |
| 1005 | template<typename T> |
| 1006 | std::string toString(const hidl_vec<T> &a) { |
| 1007 | std::string os; |
| 1008 | os += "[" + toString(a.size()) + "]"; |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 1009 | os += details::arrayToString(a, a.size()); |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 1010 | return os; |
| 1011 | } |
| 1012 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 1013 | template<typename T, size_t SIZE1> |
| 1014 | std::string toString(const hidl_array<T, SIZE1> &a) { |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 1015 | return details::arraySizeToString<SIZE1>() |
| 1016 | + details::toString(details::const_accessor<T, SIZE1>(a.data())); |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES> |
| 1020 | std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) { |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 1021 | return details::arraySizeToString<SIZE1, SIZE2, SIZES...>() |
| 1022 | + details::toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data())); |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 1023 | } |
| 1024 | |
Steven Moreland | 39d9f88 | 2018-10-26 15:34:01 -0700 | [diff] [blame] | 1025 | namespace details { |
| 1026 | // Never instantiated. Used as a placeholder for template variables. |
| 1027 | template <typename T> |
| 1028 | struct hidl_invalid_type; |
| 1029 | |
| 1030 | // HIDL generates specializations of this for enums. See hidl_enum_range. |
| 1031 | template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>> |
| 1032 | constexpr hidl_invalid_type<T> hidl_enum_values; |
| 1033 | } // namespace details |
| 1034 | |
Steven Moreland | abce0c7 | 2017-11-17 15:37:09 -0800 | [diff] [blame] | 1035 | /** |
Steven Moreland | 39d9f88 | 2018-10-26 15:34:01 -0700 | [diff] [blame] | 1036 | * Every HIDL generated enum supports this function. |
Steven Moreland | 5f3a8cf | 2018-04-25 11:57:35 -0700 | [diff] [blame] | 1037 | * E.x.: for(const auto v : hidl_enum_range<Enum>) { ... } |
Steven Moreland | abce0c7 | 2017-11-17 15:37:09 -0800 | [diff] [blame] | 1038 | */ |
Steven Moreland | 2f2c963 | 2018-05-01 12:24:14 -0700 | [diff] [blame] | 1039 | template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>> |
Steven Moreland | 39d9f88 | 2018-10-26 15:34:01 -0700 | [diff] [blame] | 1040 | struct hidl_enum_range { |
Hunter Knepshield | 5529b82 | 2020-02-20 13:17:42 -0800 | [diff] [blame] | 1041 | // Container-like associated type. |
| 1042 | using value_type = T; |
| 1043 | |
Steven Moreland | 39d9f88 | 2018-10-26 15:34:01 -0700 | [diff] [blame] | 1044 | constexpr auto begin() const { return std::begin(details::hidl_enum_values<T>); } |
| 1045 | constexpr auto cbegin() const { return begin(); } |
| 1046 | constexpr auto rbegin() const { return std::rbegin(details::hidl_enum_values<T>); } |
| 1047 | constexpr auto crbegin() const { return rbegin(); } |
| 1048 | constexpr auto end() const { return std::end(details::hidl_enum_values<T>); } |
| 1049 | constexpr auto cend() const { return end(); } |
| 1050 | constexpr auto rend() const { return std::rend(details::hidl_enum_values<T>); } |
| 1051 | constexpr auto crend() const { return rend(); } |
| 1052 | }; |
Steven Moreland | 5f3a8cf | 2018-04-25 11:57:35 -0700 | [diff] [blame] | 1053 | |
Steven Moreland | 2f2c963 | 2018-05-01 12:24:14 -0700 | [diff] [blame] | 1054 | template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>> |
| 1055 | struct hidl_enum_iterator { |
| 1056 | static_assert(!std::is_enum<T>::value, |
| 1057 | "b/78573628: hidl_enum_iterator was renamed to hidl_enum_range because it is not " |
| 1058 | "actually an iterator. Please use that type instead."); |
| 1059 | }; |
Steven Moreland | abce0c7 | 2017-11-17 15:37:09 -0800 | [diff] [blame] | 1060 | |
Steven Moreland | a675212 | 2017-12-28 13:35:20 -0800 | [diff] [blame] | 1061 | /** |
| 1062 | * Bitfields in HIDL are the underlying type of the enumeration. |
| 1063 | */ |
| 1064 | template <typename Enum> |
| 1065 | using hidl_bitfield = typename std::underlying_type<Enum>::type; |
| 1066 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 1067 | } // namespace hardware |
| 1068 | } // namespace android |
| 1069 | |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 1070 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 1071 | #endif // ANDROID_HIDL_SUPPORT_H |