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