-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathValue.cpp
420 lines (346 loc) · 14.4 KB
/
Value.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "Value"
#include <binder/Value.h>
#include <limits>
#include <binder/IBinder.h>
#include <binder/Parcel.h>
#include <binder/Map.h>
#include <private/binder/ParcelValTypes.h>
#include <log/log.h>
#include <utils/Errors.h>
using android::BAD_TYPE;
using android::BAD_VALUE;
using android::NO_ERROR;
using android::UNEXPECTED_NULL;
using android::Parcel;
using android::sp;
using android::status_t;
using std::map;
using std::set;
using std::vector;
using android::binder::Value;
using android::IBinder;
using android::os::PersistableBundle;
using namespace android::binder;
// ====================================================================
#define RETURN_IF_FAILED(calledOnce) \
do { \
status_t returnStatus = calledOnce; \
if (returnStatus) { \
ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
return returnStatus; \
} \
} while(false)
// ====================================================================
/* These `internal_type_ptr()` functions allow this
* class to work without C++ RTTI support. This technique
* only works properly when called directly from this file,
* but that is OK because that is the only place we will
* be calling them from. */
template<class T> const void* internal_type_ptr()
{
static const T *marker;
return (void*)▮
}
/* Allows the type to be specified by the argument
* instead of inside angle brackets. */
template<class T> const void* internal_type_ptr(const T&)
{
return internal_type_ptr<T>();
}
// ====================================================================
namespace android {
namespace binder {
class Value::ContentBase {
public:
virtual ~ContentBase() = default;
virtual const void* type_ptr() const = 0;
virtual ContentBase * clone() const = 0;
virtual bool operator==(const ContentBase& rhs) const = 0;
#ifdef LIBBINDER_VALUE_SUPPORTS_TYPE_INFO
virtual const std::type_info &type() const = 0;
#endif
template<typename T> bool get(T* out) const;
};
/* This is the actual class that holds the value. */
template<typename T> class Value::Content : public Value::ContentBase {
public:
Content() = default;
explicit Content(const T & value) : mValue(value) { }
virtual ~Content() = default;
#ifdef LIBBINDER_VALUE_SUPPORTS_TYPE_INFO
virtual const std::type_info &type() const override
{
return typeid(T);
}
#endif
virtual const void* type_ptr() const override
{
return internal_type_ptr<T>();
}
virtual ContentBase * clone() const override
{
return new Content(mValue);
};
virtual bool operator==(const ContentBase& rhs) const override
{
if (type_ptr() != rhs.type_ptr()) {
return false;
}
return mValue == static_cast<const Content<T>* >(&rhs)->mValue;
}
T mValue;
};
template<typename T> bool Value::ContentBase::get(T* out) const
{
if (internal_type_ptr(*out) != type_ptr())
{
return false;
}
*out = static_cast<const Content<T>*>(this)->mValue;
return true;
}
// ====================================================================
Value::Value() : mContent(nullptr)
{
}
Value::Value(const Value& value)
: mContent(value.mContent ? value.mContent->clone() : nullptr)
{
}
Value::~Value()
{
delete mContent;
}
bool Value::operator==(const Value& rhs) const
{
const Value& lhs(*this);
if (lhs.empty() && rhs.empty()) {
return true;
}
if ( (lhs.mContent == nullptr)
|| (rhs.mContent == nullptr)
) {
return false;
}
return *lhs.mContent == *rhs.mContent;
}
Value& Value::swap(Value &rhs)
{
std::swap(mContent, rhs.mContent);
return *this;
}
Value& Value::operator=(const Value& rhs)
{
if (this != &rhs) {
delete mContent;
mContent = rhs.mContent
? rhs.mContent->clone()
: nullptr;
}
return *this;
}
bool Value::empty() const
{
return mContent == nullptr;
}
void Value::clear()
{
delete mContent;
mContent = nullptr;
}
int32_t Value::parcelType() const
{
const void* t_info(mContent ? mContent->type_ptr() : nullptr);
if (t_info == internal_type_ptr<bool>()) return VAL_BOOLEAN;
if (t_info == internal_type_ptr<uint8_t>()) return VAL_BYTE;
if (t_info == internal_type_ptr<int32_t>()) return VAL_INTEGER;
if (t_info == internal_type_ptr<int64_t>()) return VAL_LONG;
if (t_info == internal_type_ptr<double>()) return VAL_DOUBLE;
if (t_info == internal_type_ptr<String16>()) return VAL_STRING;
if (t_info == internal_type_ptr<vector<bool>>()) return VAL_BOOLEANARRAY;
if (t_info == internal_type_ptr<vector<uint8_t>>()) return VAL_BYTEARRAY;
if (t_info == internal_type_ptr<vector<int32_t>>()) return VAL_INTARRAY;
if (t_info == internal_type_ptr<vector<int64_t>>()) return VAL_LONGARRAY;
if (t_info == internal_type_ptr<vector<double>>()) return VAL_DOUBLEARRAY;
if (t_info == internal_type_ptr<vector<String16>>()) return VAL_STRINGARRAY;
if (t_info == internal_type_ptr<Map>()) return VAL_MAP;
if (t_info == internal_type_ptr<PersistableBundle>()) return VAL_PERSISTABLEBUNDLE;
return VAL_NULL;
}
#ifdef LIBBINDER_VALUE_SUPPORTS_TYPE_INFO
const std::type_info& Value::type() const
{
return mContent != nullptr
? mContent->type()
: typeid(void);
}
#endif // ifdef LIBBINDER_VALUE_SUPPORTS_TYPE_INFO
#define DEF_TYPE_ACCESSORS(T, TYPENAME) \
bool Value::is ## TYPENAME() const \
{ \
return mContent \
? internal_type_ptr<T>() == mContent->type_ptr() \
: false; \
} \
bool Value::get ## TYPENAME(T* out) const \
{ \
return mContent \
? mContent->get(out) \
: false; \
} \
void Value::put ## TYPENAME(const T& in) \
{ \
*this = in; \
} \
Value& Value::operator=(const T& rhs) \
{ \
delete mContent; \
mContent = new Content< T >(rhs); \
return *this; \
} \
Value::Value(const T& value) \
: mContent(new Content< T >(value)) \
{ }
DEF_TYPE_ACCESSORS(bool, Boolean)
DEF_TYPE_ACCESSORS(int8_t, Byte)
DEF_TYPE_ACCESSORS(int32_t, Int)
DEF_TYPE_ACCESSORS(int64_t, Long)
DEF_TYPE_ACCESSORS(double, Double)
DEF_TYPE_ACCESSORS(String16, String)
DEF_TYPE_ACCESSORS(std::vector<bool>, BooleanVector)
DEF_TYPE_ACCESSORS(std::vector<uint8_t>, ByteVector)
DEF_TYPE_ACCESSORS(std::vector<int32_t>, IntVector)
DEF_TYPE_ACCESSORS(std::vector<int64_t>, LongVector)
DEF_TYPE_ACCESSORS(std::vector<double>, DoubleVector)
DEF_TYPE_ACCESSORS(std::vector<String16>, StringVector)
DEF_TYPE_ACCESSORS(::android::binder::Map, Map)
DEF_TYPE_ACCESSORS(PersistableBundle, PersistableBundle)
bool Value::getString(String8* out) const
{
String16 val;
bool ret = getString(&val);
if (ret) {
*out = String8(val);
}
return ret;
}
bool Value::getString(::std::string* out) const
{
String8 val;
bool ret = getString(&val);
if (ret) {
*out = val.string();
}
return ret;
}
status_t Value::writeToParcel(Parcel* parcel) const
{
// This implementation needs to be kept in sync with the writeValue
// implementation in frameworks/base/core/java/android/os/Parcel.java
#define BEGIN_HANDLE_WRITE() \
do { \
const void* t_info(mContent?mContent->type_ptr():nullptr); \
if (false) { }
#define HANDLE_WRITE_TYPE(T, TYPEVAL, TYPEMETHOD) \
else if (t_info == internal_type_ptr<T>()) { \
RETURN_IF_FAILED(parcel->writeInt32(TYPEVAL)); \
RETURN_IF_FAILED(parcel->TYPEMETHOD(static_cast<const Content<T>*>(mContent)->mValue)); \
}
#define HANDLE_WRITE_PARCELABLE(T, TYPEVAL) \
else if (t_info == internal_type_ptr<T>()) { \
RETURN_IF_FAILED(parcel->writeInt32(TYPEVAL)); \
RETURN_IF_FAILED(static_cast<const Content<T>*>(mContent)->mValue.writeToParcel(parcel)); \
}
#define END_HANDLE_WRITE() \
else { \
ALOGE("writeToParcel: Type not supported"); \
return BAD_TYPE; \
} \
} while (false);
BEGIN_HANDLE_WRITE()
HANDLE_WRITE_TYPE(bool, VAL_BOOLEAN, writeBool)
HANDLE_WRITE_TYPE(int8_t, VAL_BYTE, writeByte)
HANDLE_WRITE_TYPE(int8_t, VAL_BYTE, writeByte)
HANDLE_WRITE_TYPE(int32_t, VAL_INTEGER, writeInt32)
HANDLE_WRITE_TYPE(int64_t, VAL_LONG, writeInt64)
HANDLE_WRITE_TYPE(double, VAL_DOUBLE, writeDouble)
HANDLE_WRITE_TYPE(String16, VAL_STRING, writeString16)
HANDLE_WRITE_TYPE(vector<bool>, VAL_BOOLEANARRAY, writeBoolVector)
HANDLE_WRITE_TYPE(vector<uint8_t>, VAL_BYTEARRAY, writeByteVector)
HANDLE_WRITE_TYPE(vector<int8_t>, VAL_BYTEARRAY, writeByteVector)
HANDLE_WRITE_TYPE(vector<int32_t>, VAL_INTARRAY, writeInt32Vector)
HANDLE_WRITE_TYPE(vector<int64_t>, VAL_LONGARRAY, writeInt64Vector)
HANDLE_WRITE_TYPE(vector<double>, VAL_DOUBLEARRAY, writeDoubleVector)
HANDLE_WRITE_TYPE(vector<String16>, VAL_STRINGARRAY, writeString16Vector)
HANDLE_WRITE_PARCELABLE(PersistableBundle, VAL_PERSISTABLEBUNDLE)
END_HANDLE_WRITE()
return NO_ERROR;
#undef BEGIN_HANDLE_WRITE
#undef HANDLE_WRITE_TYPE
#undef HANDLE_WRITE_PARCELABLE
#undef END_HANDLE_WRITE
}
status_t Value::readFromParcel(const Parcel* parcel)
{
// This implementation needs to be kept in sync with the readValue
// implementation in frameworks/base/core/java/android/os/Parcel.javai
#define BEGIN_HANDLE_READ() \
switch(value_type) { \
default: \
ALOGE("readFromParcel: Parcel type %d is not supported", value_type); \
return BAD_TYPE;
#define HANDLE_READ_TYPE(T, TYPEVAL, TYPEMETHOD) \
case TYPEVAL: \
mContent = new Content<T>(); \
RETURN_IF_FAILED(parcel->TYPEMETHOD(&static_cast<Content<T>*>(mContent)->mValue)); \
break;
#define HANDLE_READ_PARCELABLE(T, TYPEVAL) \
case TYPEVAL: \
mContent = new Content<T>(); \
RETURN_IF_FAILED(static_cast<Content<T>*>(mContent)->mValue.readFromParcel(parcel)); \
break;
#define END_HANDLE_READ() \
}
int32_t value_type = VAL_NULL;
delete mContent;
mContent = nullptr;
RETURN_IF_FAILED(parcel->readInt32(&value_type));
BEGIN_HANDLE_READ()
HANDLE_READ_TYPE(bool, VAL_BOOLEAN, readBool)
HANDLE_READ_TYPE(int8_t, VAL_BYTE, readByte)
HANDLE_READ_TYPE(int32_t, VAL_INTEGER, readInt32)
HANDLE_READ_TYPE(int64_t, VAL_LONG, readInt64)
HANDLE_READ_TYPE(double, VAL_DOUBLE, readDouble)
HANDLE_READ_TYPE(String16, VAL_STRING, readString16)
HANDLE_READ_TYPE(vector<bool>, VAL_BOOLEANARRAY, readBoolVector)
HANDLE_READ_TYPE(vector<uint8_t>, VAL_BYTEARRAY, readByteVector)
HANDLE_READ_TYPE(vector<int32_t>, VAL_INTARRAY, readInt32Vector)
HANDLE_READ_TYPE(vector<int64_t>, VAL_LONGARRAY, readInt64Vector)
HANDLE_READ_TYPE(vector<double>, VAL_DOUBLEARRAY, readDoubleVector)
HANDLE_READ_TYPE(vector<String16>, VAL_STRINGARRAY, readString16Vector)
HANDLE_READ_PARCELABLE(PersistableBundle, VAL_PERSISTABLEBUNDLE)
END_HANDLE_READ()
return NO_ERROR;
#undef BEGIN_HANDLE_READ
#undef HANDLE_READ_TYPE
#undef HANDLE_READ_PARCELABLE
#undef END_HANDLE_READ
}
} // namespace binder
} // namespace android
/* vim: set ts=4 sw=4 tw=0 et :*/