Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | /** |
| 18 | * @addtogroup NdkBinder |
| 19 | * @{ |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * @file binder_auto_utils.h |
| 24 | * @brief These objects provide a more C++-like thin interface to the . |
| 25 | */ |
| 26 | |
| 27 | #pragma once |
| 28 | |
| 29 | #include <android/binder_ibinder.h> |
| 30 | #include <android/binder_parcel.h> |
| 31 | #include <android/binder_status.h> |
| 32 | |
| 33 | #ifdef __cplusplus |
| 34 | |
| 35 | #include <cstddef> |
| 36 | |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 37 | namespace ndk { |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Represents one strong pointer to an AIBinder object. |
| 41 | */ |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 42 | class SpAIBinder { |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 43 | public: |
| 44 | /** |
| 45 | * Takes ownership of one strong refcount of binder. |
| 46 | */ |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 47 | explicit SpAIBinder(AIBinder* binder = nullptr) : mBinder(binder) {} |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 50 | * Convenience operator for implicitly constructing an SpAIBinder from nullptr. This is not |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 51 | * explicit because it is not taking ownership of anything. |
| 52 | */ |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 53 | SpAIBinder(std::nullptr_t) : SpAIBinder() {} |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * This will delete the underlying object if it exists. See operator=. |
| 57 | */ |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 58 | SpAIBinder(const SpAIBinder& other) { *this = other; } |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * This deletes the underlying object if it exists. See set. |
| 62 | */ |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 63 | ~SpAIBinder() { set(nullptr); } |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * This takes ownership of a binder from another AIBinder object but it does not affect the |
| 67 | * ownership of that other object. |
| 68 | */ |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 69 | SpAIBinder& operator=(const SpAIBinder& other) { |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 70 | AIBinder_incStrong(other.mBinder); |
| 71 | set(other.mBinder); |
| 72 | return *this; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Takes ownership of one strong refcount of binder |
| 77 | */ |
| 78 | void set(AIBinder* binder) { |
| 79 | if (mBinder != nullptr) AIBinder_decStrong(mBinder); |
| 80 | mBinder = binder; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * This returns the underlying binder object for transactions. If it is used to create another |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 85 | * SpAIBinder object, it should first be incremented. |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 86 | */ |
| 87 | AIBinder* get() const { return mBinder; } |
| 88 | |
| 89 | /** |
| 90 | * This allows the value in this class to be set from beneath it. If you call this method and |
| 91 | * then change the value of T*, you must take ownership of the value you are replacing and add |
| 92 | * ownership to the object that is put in here. |
| 93 | * |
| 94 | * Recommended use is like this: |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 95 | * SpAIBinder a; // will be nullptr |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 96 | * SomeInitFunction(a.getR()); // value is initialized with refcount |
| 97 | * |
| 98 | * Other usecases are discouraged. |
| 99 | * |
| 100 | */ |
| 101 | AIBinder** getR() { return &mBinder; } |
| 102 | |
| 103 | private: |
| 104 | AIBinder* mBinder = nullptr; |
| 105 | }; |
| 106 | |
| 107 | /** |
| 108 | * This baseclass owns a single object, used to make various classes RAII. |
| 109 | */ |
| 110 | template <typename T, void (*Destroy)(T*)> |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame^] | 111 | class ScopedAResource { |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 112 | public: |
| 113 | /** |
| 114 | * Takes ownership of t. |
| 115 | */ |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame^] | 116 | explicit ScopedAResource(T* t = nullptr) : mT(t) {} |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * This deletes the underlying object if it exists. See set. |
| 120 | */ |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame^] | 121 | ~ScopedAResource() { set(nullptr); } |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * Takes ownership of t. |
| 125 | */ |
| 126 | void set(T* t) { |
| 127 | Destroy(mT); |
| 128 | mT = t; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * This returns the underlying object to be modified but does not affect ownership. |
| 133 | */ |
| 134 | T* get() { return mT; } |
| 135 | |
| 136 | /** |
| 137 | * This returns the const underlying object but does not affect ownership. |
| 138 | */ |
| 139 | const T* get() const { return mT; } |
| 140 | |
| 141 | /** |
| 142 | * This allows the value in this class to be set from beneath it. If you call this method and |
| 143 | * then change the value of T*, you must take ownership of the value you are replacing and add |
| 144 | * ownership to the object that is put in here. |
| 145 | * |
| 146 | * Recommended use is like this: |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame^] | 147 | * ScopedAResource<T> a; // will be nullptr |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 148 | * SomeInitFunction(a.getR()); // value is initialized with refcount |
| 149 | * |
| 150 | * Other usecases are discouraged. |
| 151 | * |
| 152 | */ |
| 153 | T** getR() { return &mT; } |
| 154 | |
| 155 | // copy-constructing, or move/copy assignment is disallowed |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame^] | 156 | ScopedAResource(const ScopedAResource&) = delete; |
| 157 | ScopedAResource& operator=(const ScopedAResource&) = delete; |
| 158 | ScopedAResource& operator=(ScopedAResource&&) = delete; |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 159 | |
| 160 | // move-constructing is okay |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame^] | 161 | ScopedAResource(ScopedAResource&&) = default; |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 162 | |
| 163 | private: |
| 164 | T* mT; |
| 165 | }; |
| 166 | |
| 167 | /** |
| 168 | * Convenience wrapper. See AParcel. |
| 169 | */ |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame^] | 170 | class ScopedAParcel : public ScopedAResource<AParcel, AParcel_delete> { |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 171 | public: |
| 172 | /** |
| 173 | * Takes ownership of a. |
| 174 | */ |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame^] | 175 | explicit ScopedAParcel(AParcel* a = nullptr) : ScopedAResource(a) {} |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 176 | ~ScopedAParcel() {} |
| 177 | ScopedAParcel(ScopedAParcel&&) = default; |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | /** |
| 181 | * Convenience wrapper. See AStatus. |
| 182 | */ |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame^] | 183 | class ScopedAStatus : public ScopedAResource<AStatus, AStatus_delete> { |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 184 | public: |
| 185 | /** |
| 186 | * Takes ownership of a. |
| 187 | */ |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame^] | 188 | explicit ScopedAStatus(AStatus* a = nullptr) : ScopedAResource(a) {} |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 189 | ~ScopedAStatus() {} |
| 190 | ScopedAStatus(ScopedAStatus&&) = default; |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 191 | |
| 192 | /** |
| 193 | * See AStatus_isOk. |
| 194 | */ |
| 195 | bool isOk() { return get() != nullptr && AStatus_isOk(get()); } |
| 196 | }; |
| 197 | |
| 198 | /** |
| 199 | * Convenience wrapper. See AIBinder_DeathRecipient. |
| 200 | */ |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 201 | class ScopedAIBinder_DeathRecipient |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame^] | 202 | : public ScopedAResource<AIBinder_DeathRecipient, AIBinder_DeathRecipient_delete> { |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 203 | public: |
| 204 | /** |
| 205 | * Takes ownership of a. |
| 206 | */ |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame^] | 207 | explicit ScopedAIBinder_DeathRecipient(AIBinder_DeathRecipient* a = nullptr) |
| 208 | : ScopedAResource(a) {} |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 209 | ~ScopedAIBinder_DeathRecipient() {} |
| 210 | ScopedAIBinder_DeathRecipient(ScopedAIBinder_DeathRecipient&&) = default; |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | /** |
| 214 | * Convenience wrapper. See AIBinder_Weak. |
| 215 | */ |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame^] | 216 | class ScopedAIBinder_Weak : public ScopedAResource<AIBinder_Weak, AIBinder_Weak_delete> { |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 217 | public: |
| 218 | /** |
| 219 | * Takes ownership of a. |
| 220 | */ |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame^] | 221 | explicit ScopedAIBinder_Weak(AIBinder_Weak* a = nullptr) : ScopedAResource(a) {} |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 222 | ~ScopedAIBinder_Weak() {} |
| 223 | ScopedAIBinder_Weak(ScopedAIBinder_Weak&&) = default; |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 224 | |
| 225 | /** |
| 226 | * See AIBinder_Weak_promote. |
| 227 | */ |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 228 | SpAIBinder promote() { return SpAIBinder(AIBinder_Weak_promote(get())); } |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 229 | }; |
| 230 | |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 231 | } // namespace ndk |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 232 | |
| 233 | #endif // __cplusplus |
| 234 | |
| 235 | /** @} */ |