1/*
2 * Copyright (C) 2015 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_VALUE_H
18#define ANDROID_VALUE_H
19
20#include <stdint.h>
21#include <map>
22#include <set>
23#include <vector>
24#include <string>
25
26#include <binder/Parcelable.h>
27#include <binder/PersistableBundle.h>
28#include <binder/Map.h>
29#include <utils/String8.h>
30#include <utils/String16.h>
31#include <utils/StrongPointer.h>
32
33namespace android {
34
35class Parcel;
36
37namespace binder {
38
39/**
40 * A limited C++ generic type. The purpose of this class is to allow C++
41 * programs to make use of (or implement) Binder interfaces which make use
42 * the Java "Object" generic type (either via the use of the Map type or
43 * some other mechanism).
44 *
45 * This class only supports a limited set of types, but additional types
46 * may be easily added to this class in the future as needed---without
47 * breaking binary compatability.
48 *
49 * This class was written in such a way as to help avoid type errors by
50 * giving each type their own explicity-named accessor methods (rather than
51 * overloaded methods).
52 *
53 * When reading or writing this class to a Parcel, use the `writeValue()`
54 * and `readValue()` methods.
55 */
56class Value {
57public:
58    Value();
59    virtual ~Value();
60
61    Value& swap(Value &);
62
63    bool empty() const;
64
65    void clear();
66
67#ifdef LIBBINDER_VALUE_SUPPORTS_TYPE_INFO
68    const std::type_info& type() const;
69#endif
70
71    int32_t parcelType() const;
72
73    bool operator==(const Value& rhs) const;
74    bool operator!=(const Value& rhs) const { return !this->operator==(rhs); }
75
76    Value(const Value& value);
77    Value(const bool& value);
78    Value(const int8_t& value);
79    Value(const int32_t& value);
80    Value(const int64_t& value);
81    Value(const double& value);
82    Value(const String16& value);
83    Value(const std::vector<bool>& value);
84    Value(const std::vector<uint8_t>& value);
85    Value(const std::vector<int32_t>& value);
86    Value(const std::vector<int64_t>& value);
87    Value(const std::vector<double>& value);
88    Value(const std::vector<String16>& value);
89    Value(const os::PersistableBundle& value);
90    Value(const binder::Map& value);
91
92    Value& operator=(const Value& rhs);
93    Value& operator=(const int8_t& rhs);
94    Value& operator=(const bool& rhs);
95    Value& operator=(const int32_t& rhs);
96    Value& operator=(const int64_t& rhs);
97    Value& operator=(const double& rhs);
98    Value& operator=(const String16& rhs);
99    Value& operator=(const std::vector<bool>& rhs);
100    Value& operator=(const std::vector<uint8_t>& rhs);
101    Value& operator=(const std::vector<int32_t>& rhs);
102    Value& operator=(const std::vector<int64_t>& rhs);
103    Value& operator=(const std::vector<double>& rhs);
104    Value& operator=(const std::vector<String16>& rhs);
105    Value& operator=(const os::PersistableBundle& rhs);
106    Value& operator=(const binder::Map& rhs);
107
108    void putBoolean(const bool& value);
109    void putByte(const int8_t& value);
110    void putInt(const int32_t& value);
111    void putLong(const int64_t& value);
112    void putDouble(const double& value);
113    void putString(const String16& value);
114    void putBooleanVector(const std::vector<bool>& value);
115    void putByteVector(const std::vector<uint8_t>& value);
116    void putIntVector(const std::vector<int32_t>& value);
117    void putLongVector(const std::vector<int64_t>& value);
118    void putDoubleVector(const std::vector<double>& value);
119    void putStringVector(const std::vector<String16>& value);
120    void putPersistableBundle(const os::PersistableBundle& value);
121    void putMap(const binder::Map& value);
122
123    bool getBoolean(bool* out) const;
124    bool getByte(int8_t* out) const;
125    bool getInt(int32_t* out) const;
126    bool getLong(int64_t* out) const;
127    bool getDouble(double* out) const;
128    bool getString(String16* out) const;
129    bool getBooleanVector(std::vector<bool>* out) const;
130    bool getByteVector(std::vector<uint8_t>* out) const;
131    bool getIntVector(std::vector<int32_t>* out) const;
132    bool getLongVector(std::vector<int64_t>* out) const;
133    bool getDoubleVector(std::vector<double>* out) const;
134    bool getStringVector(std::vector<String16>* out) const;
135    bool getPersistableBundle(os::PersistableBundle* out) const;
136    bool getMap(binder::Map* out) const;
137
138    bool isBoolean() const;
139    bool isByte() const;
140    bool isInt() const;
141    bool isLong() const;
142    bool isDouble() const;
143    bool isString() const;
144    bool isBooleanVector() const;
145    bool isByteVector() const;
146    bool isIntVector() const;
147    bool isLongVector() const;
148    bool isDoubleVector() const;
149    bool isStringVector() const;
150    bool isPersistableBundle() const;
151    bool isMap() const;
152
153    // String Convenience Adapters
154    // ---------------------------
155
156    Value(const String8& value):               Value(String16(value)) { }
157    Value(const ::std::string& value):         Value(String8(value.c_str())) { }
158    void putString(const String8& value)       { return putString(String16(value)); }
159    void putString(const ::std::string& value) { return putString(String8(value.c_str())); }
160    Value& operator=(const String8& rhs)       { return *this = String16(rhs); }
161    Value& operator=(const ::std::string& rhs) { return *this = String8(rhs.c_str()); }
162    bool getString(String8* out) const;
163    bool getString(::std::string* out) const;
164
165private:
166
167    // This allows ::android::Parcel to call the two methods below.
168    friend class ::android::Parcel;
169
170    // This is called by ::android::Parcel::writeValue()
171    status_t writeToParcel(Parcel* parcel) const;
172
173    // This is called by ::android::Parcel::readValue()
174    status_t readFromParcel(const Parcel* parcel);
175
176    template<typename T> class Content;
177    class ContentBase;
178
179    ContentBase* mContent;
180};
181
182}  // namespace binder
183
184}  // namespace android
185
186#endif  // ANDROID_VALUE_H
187