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> |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 23 | #include <cutils/native_handle.h> |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 24 | #include <hidl/HidlInternal.h> |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 25 | #include <hidl/Status.h> |
Yifan Hong | 1e265bb | 2016-11-08 12:33:44 -0800 | [diff] [blame] | 26 | #include <map> |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 27 | #include <sstream> |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 28 | #include <stddef.h> |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 29 | #include <tuple> |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 30 | #include <type_traits> |
Iliyan Malchev | 692070a | 2016-09-12 16:30:44 -0700 | [diff] [blame] | 31 | #include <utils/Errors.h> |
| 32 | #include <utils/RefBase.h> |
| 33 | #include <utils/StrongPointer.h> |
Yifan Hong | 44c0e57 | 2017-01-20 15:41:52 -0800 | [diff] [blame] | 34 | #include <vintf/Transport.h> |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 35 | #include <vector> |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 36 | |
| 37 | namespace android { |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 38 | |
Martijn Coenen | 9b8f9c3 | 2016-12-09 15:51:06 +0100 | [diff] [blame] | 39 | // this file is included by all hidl interface, so we must forward declare the |
| 40 | // IMemory and IBase types. |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 41 | namespace hidl { |
| 42 | namespace memory { |
| 43 | namespace V1_0 { |
| 44 | struct IMemory; |
| 45 | }; // namespace V1_0 |
| 46 | }; // namespace manager |
| 47 | }; // namespace hidl |
| 48 | |
Martijn Coenen | 9b8f9c3 | 2016-12-09 15:51:06 +0100 | [diff] [blame] | 49 | namespace hidl { |
| 50 | namespace base { |
| 51 | namespace V1_0 { |
| 52 | struct IBase; |
| 53 | }; // namespace V1_0 |
| 54 | }; // namespace base |
| 55 | }; // namespace hidl |
| 56 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 57 | namespace hardware { |
| 58 | |
Yifan Hong | 44c0e57 | 2017-01-20 15:41:52 -0800 | [diff] [blame] | 59 | // Get transport method from vendor interface manifest. |
Yifan Hong | 37b3620 | 2017-02-28 16:04:22 -0800 | [diff] [blame] | 60 | // interfaceName has the format "android.hardware.foo@1.0::IFoo" |
| 61 | // instanceName is "default", "ashmem", etc. |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 62 | // If it starts with "android.hidl.", a static map is looked up instead. |
Yifan Hong | 37b3620 | 2017-02-28 16:04:22 -0800 | [diff] [blame] | 63 | vintf::Transport getTransport(const std::string &interfaceName, |
| 64 | const std::string &instanceName); |
Yifan Hong | 44c0e57 | 2017-01-20 15:41:52 -0800 | [diff] [blame] | 65 | |
Yifan Hong | 24332ef | 2017-03-07 16:22:19 -0800 | [diff] [blame] | 66 | namespace details { |
| 67 | // Return true on userdebug / eng builds and false on user builds. |
| 68 | bool debuggable(); |
| 69 | } // namespace details |
| 70 | |
Martijn Coenen | 9b8f9c3 | 2016-12-09 15:51:06 +0100 | [diff] [blame] | 71 | // hidl_death_recipient is a callback interfaced that can be used with |
| 72 | // linkToDeath() / unlinkToDeath() |
| 73 | struct hidl_death_recipient : public virtual RefBase { |
| 74 | virtual void serviceDied(uint64_t cookie, |
| 75 | const ::android::wp<::android::hidl::base::V1_0::IBase>& who) = 0; |
| 76 | }; |
| 77 | |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 78 | // hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer, |
| 79 | // 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] | 80 | // The ownership semantics for this are: |
| 81 | // 1) The conversion constructor and assignment operator taking a const native_handle_t* |
| 82 | // do not take ownership of the handle; this is because these operations are usually |
| 83 | // just done for IPC, and cloning by default is a waste of resources. If you want |
| 84 | // a hidl_handle to take ownership, call setTo(handle, true /*shouldOwn*/); |
| 85 | // 2) The copy constructor/assignment operator taking a hidl_handle *DO* take ownership; |
| 86 | // that is because it's not intuitive that this class encapsulates a native_handle_t |
| 87 | // which needs cloning to be valid; in particular, this allows constructs like this: |
| 88 | // hidl_handle copy; |
| 89 | // foo->someHidlCall([&](auto incoming_handle) { |
| 90 | // copy = incoming_handle; |
| 91 | // }); |
| 92 | // // copy and its enclosed file descriptors will remain valid here. |
| 93 | // 3) The move constructor does what you would expect; it only owns the handle if the |
| 94 | // original did. |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 95 | struct hidl_handle { |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 96 | hidl_handle(); |
| 97 | ~hidl_handle(); |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 98 | |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 99 | hidl_handle(const native_handle_t *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 | // copy constructor. |
| 102 | hidl_handle(const hidl_handle &other); |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 103 | |
| 104 | // move constructor. |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 105 | hidl_handle(hidl_handle &&other); |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 106 | |
Steven Moreland | 6ffdc2a | 2017-01-23 12:34:33 -0800 | [diff] [blame] | 107 | // assignment operators |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 108 | hidl_handle &operator=(const hidl_handle &other); |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 109 | |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 110 | hidl_handle &operator=(const native_handle_t *native_handle); |
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=(hidl_handle &&other); |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 113 | |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 114 | void setTo(native_handle_t* handle, bool shouldOwn = false); |
| 115 | |
| 116 | const native_handle_t* operator->() const; |
| 117 | |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 118 | // implicit conversion to const native_handle_t* |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 119 | operator const native_handle_t *() const; |
| 120 | |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 121 | // explicit conversion |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 122 | const native_handle_t *getNativeHandle() const; |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 123 | private: |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 124 | void freeHandle(); |
| 125 | |
Andreas Huber | 6688d60 | 2017-03-30 10:58:29 -0700 | [diff] [blame] | 126 | details::hidl_pointer<const native_handle_t> mHandle __attribute__ ((aligned(8))); |
| 127 | bool mOwnsHandle __attribute ((aligned(8))); |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 128 | }; |
| 129 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 130 | struct hidl_string { |
| 131 | hidl_string(); |
| 132 | ~hidl_string(); |
| 133 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 134 | // copy constructor. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 135 | hidl_string(const hidl_string &); |
Steven Moreland | a21d84f | 2017-02-16 09:23:45 -0800 | [diff] [blame] | 136 | // copy from a C-style string. nullptr will create an empty string |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 137 | hidl_string(const char *); |
Steven Moreland | 53120f7 | 2017-01-12 09:39:26 -0800 | [diff] [blame] | 138 | // copy the first length characters from a C-style string. |
| 139 | hidl_string(const char *, size_t length); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 140 | // copy from an std::string. |
| 141 | hidl_string(const std::string &); |
| 142 | |
| 143 | // move constructor. |
| 144 | hidl_string(hidl_string &&); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 145 | |
| 146 | const char *c_str() const; |
| 147 | size_t size() const; |
| 148 | bool empty() const; |
| 149 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 150 | // copy assignment operator. |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 151 | hidl_string &operator=(const hidl_string &); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 152 | // copy from a C-style string. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 153 | hidl_string &operator=(const char *s); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 154 | // copy from an std::string. |
| 155 | hidl_string &operator=(const std::string &); |
| 156 | // move assignment operator. |
| 157 | hidl_string &operator=(hidl_string &&other); |
| 158 | // cast to std::string. |
| 159 | operator std::string() const; |
| 160 | // cast to C-style string. Caller is responsible |
| 161 | // to maintain this hidl_string alive. |
| 162 | operator const char *() const; |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 163 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 164 | void clear(); |
| 165 | |
| 166 | // Reference an external char array. Ownership is _not_ transferred. |
| 167 | // Caller is responsible for ensuring that underlying memory is valid |
| 168 | // for the lifetime of this hidl_string. |
| 169 | void setToExternal(const char *data, size_t size); |
| 170 | |
Andreas Huber | ebfeb36 | 2016-08-25 13:39:05 -0700 | [diff] [blame] | 171 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 172 | static const size_t kOffsetOfBuffer; |
| 173 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 174 | private: |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 175 | details::hidl_pointer<const char> mBuffer; |
| 176 | uint32_t mSize; // NOT including the terminating '\0'. |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 177 | bool mOwnsBuffer; // if true then mBuffer is a mutable char * |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 178 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 179 | // copy from data with size. Assume that my memory is freed |
| 180 | // (through clear(), for example) |
| 181 | void copyFrom(const char *data, size_t size); |
| 182 | // move from another hidl_string |
| 183 | void moveFrom(hidl_string &&); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 184 | }; |
| 185 | |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 186 | #define HIDL_STRING_OPERATOR(OP) \ |
| 187 | inline bool operator OP(const hidl_string &hs1, const hidl_string &hs2) { \ |
| 188 | return strcmp(hs1.c_str(), hs2.c_str()) OP 0; \ |
| 189 | } \ |
| 190 | inline bool operator OP(const hidl_string &hs, const char *s) { \ |
| 191 | return strcmp(hs.c_str(), s) OP 0; \ |
| 192 | } \ |
| 193 | inline bool operator OP(const char *s, const hidl_string &hs) { \ |
| 194 | return strcmp(hs.c_str(), s) OP 0; \ |
| 195 | } |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 196 | |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 197 | HIDL_STRING_OPERATOR(==) |
| 198 | HIDL_STRING_OPERATOR(!=) |
| 199 | HIDL_STRING_OPERATOR(<) |
| 200 | HIDL_STRING_OPERATOR(<=) |
| 201 | HIDL_STRING_OPERATOR(>) |
| 202 | HIDL_STRING_OPERATOR(>=) |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 203 | |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 204 | #undef HIDL_STRING_OPERATOR |
Yifan Hong | 5708fb4 | 2016-10-26 17:50:29 -0700 | [diff] [blame] | 205 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 206 | // hidl_memory is a structure that can be used to transfer |
| 207 | // pieces of shared memory between processes. The assumption |
| 208 | // of this object is that the memory remains accessible as |
| 209 | // long as the file descriptors in the enclosed mHandle |
| 210 | // - as well as all of its cross-process dups() - remain opened. |
| 211 | struct hidl_memory { |
| 212 | |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 213 | hidl_memory() : mHandle(nullptr), mSize(0), mName("") { |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /** |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 217 | * Creates a hidl_memory object, but doesn't take ownership of |
| 218 | * the passed in native_handle_t; callers are responsible for |
| 219 | * making sure the handle remains valid while this object is |
| 220 | * used. |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 221 | */ |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 222 | hidl_memory(const hidl_string &name, const native_handle_t *handle, size_t size) |
| 223 | : mHandle(handle), |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 224 | mSize(size), |
| 225 | mName(name) |
| 226 | {} |
| 227 | |
| 228 | // copy constructor |
| 229 | hidl_memory(const hidl_memory& other) { |
| 230 | *this = other; |
| 231 | } |
| 232 | |
| 233 | // copy assignment |
| 234 | hidl_memory &operator=(const hidl_memory &other) { |
| 235 | if (this != &other) { |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 236 | mHandle = other.mHandle; |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 237 | mSize = other.mSize; |
| 238 | mName = other.mName; |
| 239 | } |
| 240 | |
| 241 | return *this; |
| 242 | } |
| 243 | |
Hridya Valsaraju | 0126889 | 2017-02-27 08:48:38 -0800 | [diff] [blame] | 244 | // move constructor |
| 245 | hidl_memory(hidl_memory&& other) { |
| 246 | *this = std::move(other); |
| 247 | } |
| 248 | |
| 249 | // move assignment |
| 250 | hidl_memory &operator=(hidl_memory &&other) { |
| 251 | if (this != &other) { |
| 252 | mHandle = std::move(other.mHandle); |
| 253 | mSize = other.mSize; |
| 254 | mName = std::move(other.mName); |
| 255 | other.mSize = 0; |
| 256 | } |
| 257 | |
| 258 | return *this; |
| 259 | } |
| 260 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 261 | |
| 262 | ~hidl_memory() { |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | const native_handle_t* handle() const { |
| 266 | return mHandle; |
| 267 | } |
| 268 | |
| 269 | const hidl_string &name() const { |
| 270 | return mName; |
| 271 | } |
| 272 | |
Hridya Valsaraju | 6d4acb1 | 2017-03-03 14:24:43 -0800 | [diff] [blame] | 273 | uint64_t size() const { |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 274 | return mSize; |
| 275 | } |
| 276 | |
| 277 | // offsetof(hidl_memory, mHandle) exposed since mHandle is private. |
| 278 | static const size_t kOffsetOfHandle; |
| 279 | // offsetof(hidl_memory, mName) exposed since mHandle is private. |
| 280 | static const size_t kOffsetOfName; |
Jeff Tinker | 0f3461d | 2017-01-03 10:40:55 -0800 | [diff] [blame] | 281 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 282 | private: |
Hridya Valsaraju | 6d4acb1 | 2017-03-03 14:24:43 -0800 | [diff] [blame] | 283 | hidl_handle mHandle __attribute__ ((aligned(8))); |
| 284 | uint64_t mSize __attribute__ ((aligned(8))); |
| 285 | hidl_string mName __attribute__ ((aligned(8))); |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 286 | }; |
| 287 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 288 | //////////////////////////////////////////////////////////////////////////////// |
| 289 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 290 | template<typename T> |
Hridya Valsaraju | b737030 | 2017-03-08 14:39:23 -0800 | [diff] [blame] | 291 | struct hidl_vec { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 292 | hidl_vec() |
| 293 | : mBuffer(NULL), |
| 294 | mSize(0), |
| 295 | mOwnsBuffer(true) { |
Hridya Valsaraju | 6d4acb1 | 2017-03-03 14:24:43 -0800 | [diff] [blame] | 296 | static_assert(hidl_vec<T>::kOffsetOfBuffer == 0, "wrong offset"); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 299 | hidl_vec(const hidl_vec<T> &other) : hidl_vec() { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 300 | *this = other; |
| 301 | } |
| 302 | |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 303 | hidl_vec(hidl_vec<T> &&other) |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 304 | : mOwnsBuffer(false) { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 305 | *this = std::move(other); |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 308 | hidl_vec(const std::initializer_list<T> list) |
Yifan Hong | ca1d1bf | 2016-12-19 14:26:19 -0800 | [diff] [blame] | 309 | : mOwnsBuffer(true) { |
| 310 | if (list.size() > UINT32_MAX) { |
Hridya Valsaraju | b737030 | 2017-03-08 14:39:23 -0800 | [diff] [blame] | 311 | details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements."); |
Yifan Hong | ca1d1bf | 2016-12-19 14:26:19 -0800 | [diff] [blame] | 312 | } |
| 313 | mSize = static_cast<uint32_t>(list.size()); |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 314 | mBuffer = new T[mSize]; |
| 315 | |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 316 | size_t idx = 0; |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 317 | for (auto it = list.begin(); it != list.end(); ++it) { |
| 318 | mBuffer[idx++] = *it; |
| 319 | } |
| 320 | } |
| 321 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 322 | hidl_vec(const std::vector<T> &other) : hidl_vec() { |
| 323 | *this = other; |
| 324 | } |
| 325 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 326 | ~hidl_vec() { |
| 327 | if (mOwnsBuffer) { |
| 328 | delete[] mBuffer; |
| 329 | } |
| 330 | mBuffer = NULL; |
| 331 | } |
| 332 | |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 333 | // Reference an existing array, optionally taking ownership. It is the |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 334 | // caller's responsibility to ensure that the underlying memory stays |
| 335 | // valid for the lifetime of this hidl_vec. |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 336 | void setToExternal(T *data, size_t size, bool shouldOwn = false) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 337 | if (mOwnsBuffer) { |
| 338 | delete [] mBuffer; |
| 339 | } |
| 340 | mBuffer = data; |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 341 | if (size > UINT32_MAX) { |
Hridya Valsaraju | b737030 | 2017-03-08 14:39:23 -0800 | [diff] [blame] | 342 | details::logAlwaysFatal("external vector size exceeds 2^32 elements."); |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 343 | } |
| 344 | mSize = static_cast<uint32_t>(size); |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 345 | mOwnsBuffer = shouldOwn; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Alexey Polyudov | 6f0c9a1 | 2016-10-18 09:37:35 -0700 | [diff] [blame] | 348 | T *data() { |
| 349 | return mBuffer; |
| 350 | } |
| 351 | |
| 352 | const T *data() const { |
| 353 | return mBuffer; |
| 354 | } |
| 355 | |
Alexey Polyudov | c98a99c | 2016-10-18 09:43:56 -0700 | [diff] [blame] | 356 | T *releaseData() { |
| 357 | if (!mOwnsBuffer && mSize > 0) { |
| 358 | resize(mSize); |
| 359 | } |
| 360 | mOwnsBuffer = false; |
| 361 | return mBuffer; |
| 362 | } |
| 363 | |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 364 | hidl_vec &operator=(hidl_vec &&other) { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 365 | if (mOwnsBuffer) { |
| 366 | delete[] mBuffer; |
| 367 | } |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 368 | mBuffer = other.mBuffer; |
| 369 | mSize = other.mSize; |
| 370 | mOwnsBuffer = other.mOwnsBuffer; |
| 371 | other.mOwnsBuffer = false; |
| 372 | return *this; |
| 373 | } |
| 374 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 375 | hidl_vec &operator=(const hidl_vec &other) { |
| 376 | if (this != &other) { |
| 377 | if (mOwnsBuffer) { |
| 378 | delete[] mBuffer; |
| 379 | } |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 380 | copyFrom(other, other.mSize); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | return *this; |
| 384 | } |
| 385 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 386 | // copy from an std::vector. |
| 387 | hidl_vec &operator=(const std::vector<T> &other) { |
| 388 | if (mOwnsBuffer) { |
| 389 | delete[] mBuffer; |
| 390 | } |
| 391 | copyFrom(other, other.size()); |
| 392 | return *this; |
| 393 | } |
| 394 | |
| 395 | // cast to an std::vector. |
| 396 | operator std::vector<T>() const { |
| 397 | std::vector<T> v(mSize); |
| 398 | for (size_t i = 0; i < mSize; ++i) { |
| 399 | v[i] = mBuffer[i]; |
| 400 | } |
| 401 | return v; |
| 402 | } |
| 403 | |
Yifan Hong | 9fcbb36 | 2016-12-20 16:46:41 -0800 | [diff] [blame] | 404 | // equality check, assuming that T::operator== is defined. |
| 405 | bool operator==(const hidl_vec &other) const { |
| 406 | if (mSize != other.size()) { |
| 407 | return false; |
| 408 | } |
| 409 | for (size_t i = 0; i < mSize; ++i) { |
| 410 | if (!(mBuffer[i] == other.mBuffer[i])) { |
| 411 | return false; |
| 412 | } |
| 413 | } |
| 414 | return true; |
| 415 | } |
| 416 | |
| 417 | // inequality check, assuming that T::operator== is defined. |
| 418 | inline bool operator!=(const hidl_vec &other) const { |
| 419 | return !((*this) == other); |
| 420 | } |
| 421 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 422 | size_t size() const { |
| 423 | return mSize; |
| 424 | } |
| 425 | |
| 426 | T &operator[](size_t index) { |
| 427 | return mBuffer[index]; |
| 428 | } |
| 429 | |
| 430 | const T &operator[](size_t index) const { |
| 431 | return mBuffer[index]; |
| 432 | } |
| 433 | |
| 434 | void resize(size_t size) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 435 | if (size > UINT32_MAX) { |
Hridya Valsaraju | b737030 | 2017-03-08 14:39:23 -0800 | [diff] [blame] | 436 | details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements."); |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 437 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 438 | T *newBuffer = new T[size]; |
| 439 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 440 | for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 441 | newBuffer[i] = mBuffer[i]; |
| 442 | } |
| 443 | |
| 444 | if (mOwnsBuffer) { |
| 445 | delete[] mBuffer; |
| 446 | } |
| 447 | mBuffer = newBuffer; |
| 448 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 449 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 450 | mOwnsBuffer = true; |
| 451 | } |
| 452 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 453 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 454 | static const size_t kOffsetOfBuffer; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 455 | |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 456 | private: |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 457 | // Define std interator interface for walking the array contents |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 458 | template<bool is_const> |
| 459 | class iter : public std::iterator< |
| 460 | std::random_access_iterator_tag, /* Category */ |
| 461 | T, |
| 462 | ptrdiff_t, /* Distance */ |
| 463 | typename std::conditional<is_const, const T *, T *>::type /* Pointer */, |
| 464 | typename std::conditional<is_const, const T &, T &>::type /* Reference */> |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 465 | { |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 466 | using traits = std::iterator_traits<iter>; |
| 467 | using ptr_type = typename traits::pointer; |
| 468 | using ref_type = typename traits::reference; |
| 469 | using diff_type = typename traits::difference_type; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 470 | public: |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 471 | iter(ptr_type ptr) : mPtr(ptr) { } |
| 472 | inline iter &operator++() { mPtr++; return *this; } |
| 473 | inline iter operator++(int) { iter i = *this; mPtr++; return i; } |
| 474 | inline iter &operator--() { mPtr--; return *this; } |
| 475 | inline iter operator--(int) { iter i = *this; mPtr--; return i; } |
| 476 | inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; } |
| 477 | inline iter operator+(diff_type n) const { return mPtr + n; } |
| 478 | inline iter operator-(diff_type n) const { return mPtr - n; } |
| 479 | inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; } |
| 480 | inline iter &operator+=(diff_type n) { mPtr += n; return *this; } |
| 481 | inline iter &operator-=(diff_type n) { mPtr -= n; return *this; } |
| 482 | inline ref_type operator*() const { return *mPtr; } |
| 483 | inline ptr_type operator->() const { return mPtr; } |
| 484 | inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; } |
| 485 | inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; } |
| 486 | inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; } |
| 487 | inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; } |
| 488 | inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; } |
| 489 | inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; } |
| 490 | inline ref_type operator[](size_t n) const { return mPtr[n]; } |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 491 | private: |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 492 | ptr_type mPtr; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 493 | }; |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 494 | public: |
| 495 | using iterator = iter<false /* is_const */>; |
| 496 | using const_iterator = iter<true /* is_const */>; |
| 497 | |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 498 | iterator begin() { return data(); } |
| 499 | iterator end() { return data()+mSize; } |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 500 | const_iterator begin() const { return data(); } |
| 501 | const_iterator end() const { return data()+mSize; } |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 502 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 503 | private: |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 504 | details::hidl_pointer<T> mBuffer; |
| 505 | uint32_t mSize; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 506 | bool mOwnsBuffer; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 507 | |
| 508 | // copy from an array-like object, assuming my resources are freed. |
| 509 | template <typename Array> |
| 510 | void copyFrom(const Array &data, size_t size) { |
Chia-I Wu | 4b48edc | 2016-12-07 22:31:17 +0800 | [diff] [blame] | 511 | mSize = static_cast<uint32_t>(size); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 512 | mOwnsBuffer = true; |
| 513 | if (mSize > 0) { |
| 514 | mBuffer = new T[size]; |
| 515 | for (size_t i = 0; i < size; ++i) { |
| 516 | mBuffer[i] = data[i]; |
| 517 | } |
| 518 | } else { |
| 519 | mBuffer = NULL; |
| 520 | } |
| 521 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 522 | }; |
| 523 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 524 | template <typename T> |
| 525 | const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer); |
| 526 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 527 | //////////////////////////////////////////////////////////////////////////////// |
| 528 | |
| 529 | namespace details { |
| 530 | |
| 531 | template<size_t SIZE1, size_t... SIZES> |
| 532 | struct product { |
| 533 | static constexpr size_t value = SIZE1 * product<SIZES...>::value; |
| 534 | }; |
| 535 | |
| 536 | template<size_t SIZE1> |
| 537 | struct product<SIZE1> { |
| 538 | static constexpr size_t value = SIZE1; |
| 539 | }; |
| 540 | |
| 541 | template<typename T, size_t SIZE1, size_t... SIZES> |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 542 | struct std_array { |
| 543 | using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>; |
| 544 | }; |
| 545 | |
| 546 | template<typename T, size_t SIZE1> |
| 547 | struct std_array<T, SIZE1> { |
| 548 | using type = std::array<T, SIZE1>; |
| 549 | }; |
| 550 | |
| 551 | template<typename T, size_t SIZE1, size_t... SIZES> |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 552 | struct accessor { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 553 | |
| 554 | using std_array_type = typename std_array<T, SIZE1, SIZES...>::type; |
| 555 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 556 | explicit accessor(T *base) |
| 557 | : mBase(base) { |
| 558 | } |
| 559 | |
| 560 | accessor<T, SIZES...> operator[](size_t index) { |
| 561 | return accessor<T, SIZES...>( |
| 562 | &mBase[index * product<SIZES...>::value]); |
| 563 | } |
| 564 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 565 | accessor &operator=(const std_array_type &other) { |
| 566 | for (size_t i = 0; i < SIZE1; ++i) { |
| 567 | (*this)[i] = other[i]; |
| 568 | } |
| 569 | return *this; |
| 570 | } |
| 571 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 572 | private: |
| 573 | T *mBase; |
| 574 | }; |
| 575 | |
| 576 | template<typename T, size_t SIZE1> |
| 577 | struct accessor<T, SIZE1> { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 578 | |
| 579 | using std_array_type = typename std_array<T, SIZE1>::type; |
| 580 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 581 | explicit accessor(T *base) |
| 582 | : mBase(base) { |
| 583 | } |
| 584 | |
| 585 | T &operator[](size_t index) { |
| 586 | return mBase[index]; |
| 587 | } |
| 588 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 589 | accessor &operator=(const std_array_type &other) { |
| 590 | for (size_t i = 0; i < SIZE1; ++i) { |
| 591 | (*this)[i] = other[i]; |
| 592 | } |
| 593 | return *this; |
| 594 | } |
| 595 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 596 | private: |
| 597 | T *mBase; |
| 598 | }; |
| 599 | |
| 600 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 601 | struct const_accessor { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 602 | |
| 603 | using std_array_type = typename std_array<T, SIZE1, SIZES...>::type; |
| 604 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 605 | explicit const_accessor(const T *base) |
| 606 | : mBase(base) { |
| 607 | } |
| 608 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 609 | const_accessor<T, SIZES...> operator[](size_t index) const { |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 610 | return const_accessor<T, SIZES...>( |
| 611 | &mBase[index * product<SIZES...>::value]); |
| 612 | } |
| 613 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 614 | operator std_array_type() { |
| 615 | std_array_type array; |
| 616 | for (size_t i = 0; i < SIZE1; ++i) { |
| 617 | array[i] = (*this)[i]; |
| 618 | } |
| 619 | return array; |
| 620 | } |
| 621 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 622 | private: |
| 623 | const T *mBase; |
| 624 | }; |
| 625 | |
| 626 | template<typename T, size_t SIZE1> |
| 627 | struct const_accessor<T, SIZE1> { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 628 | |
| 629 | using std_array_type = typename std_array<T, SIZE1>::type; |
| 630 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 631 | explicit const_accessor(const T *base) |
| 632 | : mBase(base) { |
| 633 | } |
| 634 | |
| 635 | const T &operator[](size_t index) const { |
| 636 | return mBase[index]; |
| 637 | } |
| 638 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 639 | operator std_array_type() { |
| 640 | std_array_type array; |
| 641 | for (size_t i = 0; i < SIZE1; ++i) { |
| 642 | array[i] = (*this)[i]; |
| 643 | } |
| 644 | return array; |
| 645 | } |
| 646 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 647 | private: |
| 648 | const T *mBase; |
| 649 | }; |
| 650 | |
| 651 | } // namespace details |
| 652 | |
| 653 | //////////////////////////////////////////////////////////////////////////////// |
| 654 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 655 | // 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] | 656 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 657 | struct hidl_array { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 658 | |
| 659 | using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type; |
| 660 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 661 | hidl_array() = default; |
| 662 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 663 | // Copies the data from source, using T::operator=(const T &). |
| 664 | hidl_array(const T *source) { |
| 665 | for (size_t i = 0; i < elementCount(); ++i) { |
| 666 | mBuffer[i] = source[i]; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | // Copies the data from the given std::array, using T::operator=(const T &). |
| 671 | hidl_array(const std_array_type &array) { |
| 672 | details::accessor<T, SIZE1, SIZES...> modifier(mBuffer); |
| 673 | modifier = array; |
| 674 | } |
| 675 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 676 | T *data() { return mBuffer; } |
| 677 | const T *data() const { return mBuffer; } |
| 678 | |
| 679 | details::accessor<T, SIZES...> operator[](size_t index) { |
| 680 | return details::accessor<T, SIZES...>( |
| 681 | &mBuffer[index * details::product<SIZES...>::value]); |
| 682 | } |
| 683 | |
| 684 | details::const_accessor<T, SIZES...> operator[](size_t index) const { |
| 685 | return details::const_accessor<T, SIZES...>( |
| 686 | &mBuffer[index * details::product<SIZES...>::value]); |
| 687 | } |
| 688 | |
Yifan Hong | 9fcbb36 | 2016-12-20 16:46:41 -0800 | [diff] [blame] | 689 | // equality check, assuming that T::operator== is defined. |
| 690 | bool operator==(const hidl_array &other) const { |
| 691 | for (size_t i = 0; i < elementCount(); ++i) { |
| 692 | if (!(mBuffer[i] == other.mBuffer[i])) { |
| 693 | return false; |
| 694 | } |
| 695 | } |
| 696 | return true; |
| 697 | } |
| 698 | |
| 699 | inline bool operator!=(const hidl_array &other) const { |
| 700 | return !((*this) == other); |
| 701 | } |
| 702 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 703 | using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>; |
| 704 | |
| 705 | static constexpr size_tuple_type size() { |
| 706 | return std::make_tuple(SIZE1, SIZES...); |
| 707 | } |
| 708 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 709 | static constexpr size_t elementCount() { |
| 710 | return details::product<SIZE1, SIZES...>::value; |
| 711 | } |
| 712 | |
| 713 | operator std_array_type() const { |
| 714 | return details::const_accessor<T, SIZE1, SIZES...>(mBuffer); |
| 715 | } |
| 716 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 717 | private: |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 718 | T mBuffer[elementCount()]; |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 719 | }; |
| 720 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 721 | // 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] | 722 | template<typename T, size_t SIZE1> |
| 723 | struct hidl_array<T, SIZE1> { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 724 | |
| 725 | using std_array_type = typename details::std_array<T, SIZE1>::type; |
| 726 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 727 | hidl_array() = default; |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 728 | |
| 729 | // Copies the data from source, using T::operator=(const T &). |
Sasha Levitskiy | 3da6848 | 2016-11-17 16:48:43 -0800 | [diff] [blame] | 730 | hidl_array(const T *source) { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 731 | for (size_t i = 0; i < elementCount(); ++i) { |
| 732 | mBuffer[i] = source[i]; |
| 733 | } |
Sasha Levitskiy | 3da6848 | 2016-11-17 16:48:43 -0800 | [diff] [blame] | 734 | } |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 735 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 736 | // Copies the data from the given std::array, using T::operator=(const T &). |
| 737 | hidl_array(const std_array_type &array) : hidl_array(array.data()) {} |
| 738 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 739 | T *data() { return mBuffer; } |
| 740 | const T *data() const { return mBuffer; } |
| 741 | |
| 742 | T &operator[](size_t index) { |
| 743 | return mBuffer[index]; |
| 744 | } |
| 745 | |
| 746 | const T &operator[](size_t index) const { |
| 747 | return mBuffer[index]; |
| 748 | } |
| 749 | |
Yifan Hong | 9fcbb36 | 2016-12-20 16:46:41 -0800 | [diff] [blame] | 750 | // equality check, assuming that T::operator== is defined. |
| 751 | bool operator==(const hidl_array &other) const { |
| 752 | for (size_t i = 0; i < elementCount(); ++i) { |
| 753 | if (!(mBuffer[i] == other.mBuffer[i])) { |
| 754 | return false; |
| 755 | } |
| 756 | } |
| 757 | return true; |
| 758 | } |
| 759 | |
| 760 | inline bool operator!=(const hidl_array &other) const { |
| 761 | return !((*this) == other); |
| 762 | } |
| 763 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 764 | static constexpr size_t size() { return SIZE1; } |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 765 | static constexpr size_t elementCount() { return SIZE1; } |
| 766 | |
| 767 | // Copies the data to an std::array, using T::operator=(T). |
| 768 | operator std_array_type() const { |
| 769 | std_array_type array; |
| 770 | for (size_t i = 0; i < SIZE1; ++i) { |
| 771 | array[i] = mBuffer[i]; |
| 772 | } |
| 773 | return array; |
| 774 | } |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 775 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 776 | private: |
| 777 | T mBuffer[SIZE1]; |
| 778 | }; |
| 779 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 780 | // ---------------------------------------------------------------------- |
| 781 | // Version functions |
| 782 | struct hidl_version { |
| 783 | public: |
Chia-I Wu | 666b76b | 2016-10-06 14:15:59 +0800 | [diff] [blame] | 784 | constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {} |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 785 | |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 786 | bool operator==(const hidl_version& other) const { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 787 | return (mMajor == other.get_major() && mMinor == other.get_minor()); |
| 788 | } |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 789 | |
Eino-Ville Talvala | 19f4db5 | 2016-12-14 15:19:28 -0800 | [diff] [blame] | 790 | bool operator<(const hidl_version& other) const { |
| 791 | return (mMajor < other.get_major() || |
| 792 | (mMajor == other.get_major() && mMinor < other.get_minor())); |
| 793 | } |
| 794 | |
| 795 | bool operator>(const hidl_version& other) const { |
| 796 | return other < *this; |
| 797 | } |
| 798 | |
| 799 | bool operator<=(const hidl_version& other) const { |
| 800 | return !(*this > other); |
| 801 | } |
| 802 | |
| 803 | bool operator>=(const hidl_version& other) const { |
| 804 | return !(*this < other); |
| 805 | } |
| 806 | |
Martijn Coenen | 097a767 | 2016-09-08 16:56:41 +0200 | [diff] [blame] | 807 | constexpr uint16_t get_major() const { return mMajor; } |
| 808 | constexpr uint16_t get_minor() const { return mMinor; } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 809 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 810 | private: |
| 811 | uint16_t mMajor; |
| 812 | uint16_t mMinor; |
| 813 | }; |
| 814 | |
| 815 | inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) { |
| 816 | return hidl_version(major,minor); |
| 817 | } |
| 818 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 819 | ///////////////////// toString functions |
| 820 | |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 821 | std::string toString(const void *t); |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 822 | |
| 823 | // toString alias for numeric types |
| 824 | template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type> |
| 825 | inline std::string toString(T t) { |
| 826 | return std::to_string(t); |
| 827 | } |
| 828 | |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 829 | namespace details { |
| 830 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 831 | template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type> |
| 832 | inline std::string toHexString(T t, bool prefix = true) { |
| 833 | std::ostringstream os; |
| 834 | if (prefix) { os << std::showbase; } |
| 835 | os << std::hex << t; |
| 836 | return os.str(); |
| 837 | } |
| 838 | |
| 839 | template<> |
| 840 | inline std::string toHexString(uint8_t t, bool prefix) { |
| 841 | return toHexString(static_cast<int32_t>(t), prefix); |
| 842 | } |
| 843 | |
| 844 | template<> |
| 845 | inline std::string toHexString(int8_t t, bool prefix) { |
| 846 | return toHexString(static_cast<int32_t>(t), prefix); |
| 847 | } |
| 848 | |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 849 | template<typename Array> |
| 850 | std::string arrayToString(const Array &a, size_t size); |
| 851 | |
| 852 | template<size_t SIZE1> |
| 853 | std::string arraySizeToString() { |
| 854 | return std::string{"["} + toString(SIZE1) + "]"; |
| 855 | } |
| 856 | |
| 857 | template<size_t SIZE1, size_t SIZE2, size_t... SIZES> |
| 858 | std::string arraySizeToString() { |
| 859 | return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>(); |
| 860 | } |
| 861 | |
| 862 | template<typename T, size_t SIZE1> |
| 863 | std::string toString(details::const_accessor<T, SIZE1> a) { |
| 864 | return arrayToString(a, SIZE1); |
| 865 | } |
| 866 | |
| 867 | template<typename Array> |
| 868 | std::string arrayToString(const Array &a, size_t size) { |
| 869 | using android::hardware::toString; |
| 870 | std::string os; |
| 871 | os += "{"; |
| 872 | for (size_t i = 0; i < size; ++i) { |
| 873 | if (i > 0) { |
| 874 | os += ", "; |
| 875 | } |
| 876 | os += toString(a[i]); |
| 877 | } |
| 878 | os += "}"; |
| 879 | return os; |
| 880 | } |
| 881 | |
| 882 | template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES> |
| 883 | std::string toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...> a) { |
| 884 | return arrayToString(a, SIZE1); |
| 885 | } |
| 886 | |
| 887 | } //namespace details |
| 888 | |
| 889 | inline std::string toString(const void *t) { |
| 890 | return details::toHexString(reinterpret_cast<uintptr_t>(t)); |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | // debug string dump. There will be quotes around the string! |
| 894 | inline std::string toString(const hidl_string &hs) { |
| 895 | return std::string{"\""} + hs.c_str() + "\""; |
| 896 | } |
| 897 | |
| 898 | // debug string dump |
| 899 | inline std::string toString(const hidl_handle &hs) { |
| 900 | return toString(hs.getNativeHandle()); |
| 901 | } |
| 902 | |
| 903 | inline std::string toString(const hidl_memory &mem) { |
| 904 | return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = " |
| 905 | + toString(mem.size()) |
| 906 | + ", .handle = " + toString(mem.handle()) + "}"; |
| 907 | } |
| 908 | |
| 909 | inline std::string toString(const sp<hidl_death_recipient> &dr) { |
| 910 | return std::string{"death_recipient@"} + toString(dr.get()); |
| 911 | } |
| 912 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 913 | // debug string dump, assuming that toString(T) is defined. |
| 914 | template<typename T> |
| 915 | std::string toString(const hidl_vec<T> &a) { |
| 916 | std::string os; |
| 917 | os += "[" + toString(a.size()) + "]"; |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 918 | os += details::arrayToString(a, a.size()); |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 919 | return os; |
| 920 | } |
| 921 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 922 | template<typename T, size_t SIZE1> |
| 923 | std::string toString(const hidl_array<T, SIZE1> &a) { |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 924 | return details::arraySizeToString<SIZE1>() |
| 925 | + details::toString(details::const_accessor<T, SIZE1>(a.data())); |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES> |
| 929 | std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) { |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 930 | return details::arraySizeToString<SIZE1, SIZE2, SIZES...>() |
| 931 | + details::toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data())); |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 932 | } |
| 933 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 934 | } // namespace hardware |
| 935 | } // namespace android |
| 936 | |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 937 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 938 | #endif // ANDROID_HIDL_SUPPORT_H |