blob: 11b1e8eaf5651e6c2c3362c1f8b8f9d977303354 [file] [log] [blame]
Steven Moreland2648d202018-08-28 01:23:02 -07001/*
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
37namespace android {
38
39/**
40 * Represents one strong pointer to an AIBinder object.
41 */
42class AutoAIBinder {
43public:
44 /**
45 * Takes ownership of one strong refcount of binder.
46 */
47 explicit AutoAIBinder(AIBinder* binder = nullptr) : mBinder(binder) {}
48
49 /**
50 * Convenience operator for implicitly constructing an AutoAIBinder from nullptr. This is not
51 * explicit because it is not taking ownership of anything.
52 */
53 AutoAIBinder(std::nullptr_t) : AutoAIBinder() {}
54
55 /**
56 * This will delete the underlying object if it exists. See operator=.
57 */
58 AutoAIBinder(const AutoAIBinder& other) { *this = other; }
59
60 /**
61 * This deletes the underlying object if it exists. See set.
62 */
63 ~AutoAIBinder() { set(nullptr); }
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 */
69 AutoAIBinder& operator=(const AutoAIBinder& other) {
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
85 * AutoAIBinder object, it should first be incremented.
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:
95 * AutoAIBinder a; // will be nullptr
96 * SomeInitFunction(a.getR()); // value is initialized with refcount
97 *
98 * Other usecases are discouraged.
99 *
100 */
101 AIBinder** getR() { return &mBinder; }
102
103private:
104 AIBinder* mBinder = nullptr;
105};
106
107/**
108 * This baseclass owns a single object, used to make various classes RAII.
109 */
110template <typename T, void (*Destroy)(T*)>
111class AutoA {
112public:
113 /**
114 * Takes ownership of t.
115 */
116 explicit AutoA(T* t = nullptr) : mT(t) {}
117
118 /**
119 * This deletes the underlying object if it exists. See set.
120 */
121 ~AutoA() { set(nullptr); }
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:
147 * AutoA<T> a; // will be nullptr
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
156 AutoA(const AutoA&) = delete;
157 AutoA& operator=(const AutoA&) = delete;
158 AutoA& operator=(AutoA&&) = delete;
159
160 // move-constructing is okay
161 AutoA(AutoA&&) = default;
162
163private:
164 T* mT;
165};
166
167/**
168 * Convenience wrapper. See AParcel.
169 */
170class AutoAParcel : public AutoA<AParcel, AParcel_delete> {
171public:
172 /**
173 * Takes ownership of a.
174 */
175 explicit AutoAParcel(AParcel* a = nullptr) : AutoA(a) {}
176 ~AutoAParcel() {}
177 AutoAParcel(AutoAParcel&&) = default;
178};
179
180/**
181 * Convenience wrapper. See AStatus.
182 */
183class AutoAStatus : public AutoA<AStatus, AStatus_delete> {
184public:
185 /**
186 * Takes ownership of a.
187 */
188 explicit AutoAStatus(AStatus* a = nullptr) : AutoA(a) {}
189 ~AutoAStatus() {}
190 AutoAStatus(AutoAStatus&&) = default;
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 */
201class AutoAIBinder_DeathRecipient
202 : public AutoA<AIBinder_DeathRecipient, AIBinder_DeathRecipient_delete> {
203public:
204 /**
205 * Takes ownership of a.
206 */
207 explicit AutoAIBinder_DeathRecipient(AIBinder_DeathRecipient* a = nullptr) : AutoA(a) {}
208 ~AutoAIBinder_DeathRecipient() {}
209 AutoAIBinder_DeathRecipient(AutoAIBinder_DeathRecipient&&) = default;
210};
211
212/**
213 * Convenience wrapper. See AIBinder_Weak.
214 */
215class AutoAIBinder_Weak : public AutoA<AIBinder_Weak, AIBinder_Weak_delete> {
216public:
217 /**
218 * Takes ownership of a.
219 */
220 explicit AutoAIBinder_Weak(AIBinder_Weak* a = nullptr) : AutoA(a) {}
221 ~AutoAIBinder_Weak() {}
222 AutoAIBinder_Weak(AutoAIBinder_Weak&&) = default;
223
224 /**
225 * See AIBinder_Weak_promote.
226 */
227 AutoAIBinder promote() { return AutoAIBinder(AIBinder_Weak_promote(get())); }
228};
229
230} // namespace android
231
232#endif // __cplusplus
233
234/** @} */