summaryrefslogtreecommitdiff
path: root/libs/binder/include/binder/Value.h
blob: 735f40eb1f0d2e3fb0dfcb43af76a6f54d4eca08 (plain)
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
/*
 * 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.
 */

#ifndef ANDROID_VALUE_H
#define ANDROID_VALUE_H

#include <stdint.h>
#include <map>
#include <set>
#include <vector>
#include <string>

#include <binder/Parcelable.h>
#include <binder/PersistableBundle.h>
#include <binder/Map.h>
#include <utils/String8.h>
#include <utils/String16.h>
#include <utils/StrongPointer.h>

namespace android {

class Parcel;

namespace binder {

/**
 * A limited C++ generic type. The purpose of this class is to allow C++
 * programs to make use of (or implement) Binder interfaces which make use
 * the Java "Object" generic type (either via the use of the Map type or
 * some other mechanism).
 *
 * This class only supports a limited set of types, but additional types
 * may be easily added to this class in the future as needed---without
 * breaking binary compatability.
 *
 * This class was written in such a way as to help avoid type errors by
 * giving each type their own explicity-named accessor methods (rather than
 * overloaded methods).
 *
 * When reading or writing this class to a Parcel, use the `writeValue()`
 * and `readValue()` methods.
 */
class Value {
public:
    Value();
    virtual ~Value();

    Value& swap(Value &);

    bool empty() const;

    void clear();

#ifdef LIBBINDER_VALUE_SUPPORTS_TYPE_INFO
    const std::type_info& type() const;
#endif

    int32_t parcelType() const;

    bool operator==(const Value& rhs) const;
    bool operator!=(const Value& rhs) const { return !this->operator==(rhs); }

    Value(const Value& value);
    Value(const bool& value); // NOLINT(google-explicit-constructor)
    Value(const int8_t& value); // NOLINT(google-explicit-constructor)
    Value(const int32_t& value); // NOLINT(google-explicit-constructor)
    Value(const int64_t& value); // NOLINT(google-explicit-constructor)
    Value(const double& value); // NOLINT(google-explicit-constructor)
    Value(const String16& value); // NOLINT(google-explicit-constructor)
    Value(const std::vector<bool>& value); // NOLINT(google-explicit-constructor)
    Value(const std::vector<uint8_t>& value); // NOLINT(google-explicit-constructor)
    Value(const std::vector<int32_t>& value); // NOLINT(google-explicit-constructor)
    Value(const std::vector<int64_t>& value); // NOLINT(google-explicit-constructor)
    Value(const std::vector<double>& value); // NOLINT(google-explicit-constructor)
    Value(const std::vector<String16>& value); // NOLINT(google-explicit-constructor)
    Value(const os::PersistableBundle& value); // NOLINT(google-explicit-constructor)
    Value(const binder::Map& value); // NOLINT(google-explicit-constructor)

    Value& operator=(const Value& rhs);
    Value& operator=(const int8_t& rhs);
    Value& operator=(const bool& rhs);
    Value& operator=(const int32_t& rhs);
    Value& operator=(const int64_t& rhs);
    Value& operator=(const double& rhs);
    Value& operator=(const String16& rhs);
    Value& operator=(const std::vector<bool>& rhs);
    Value& operator=(const std::vector<uint8_t>& rhs);
    Value& operator=(const std::vector<int32_t>& rhs);
    Value& operator=(const std::vector<int64_t>& rhs);
    Value& operator=(const std::vector<double>& rhs);
    Value& operator=(const std::vector<String16>& rhs);
    Value& operator=(const os::PersistableBundle& rhs);
    Value& operator=(const binder::Map& rhs);

    void putBoolean(const bool& value);
    void putByte(const int8_t& value);
    void putInt(const int32_t& value);
    void putLong(const int64_t& value);
    void putDouble(const double& value);
    void putString(const String16& value);
    void putBooleanVector(const std::vector<bool>& value);
    void putByteVector(const std::vector<uint8_t>& value);
    void putIntVector(const std::vector<int32_t>& value);
    void putLongVector(const std::vector<int64_t>& value);
    void putDoubleVector(const std::vector<double>& value);
    void putStringVector(const std::vector<String16>& value);
    void putPersistableBundle(const os::PersistableBundle& value);
    void putMap(const binder::Map& value);

    bool getBoolean(bool* out) const;
    bool getByte(int8_t* out) const;
    bool getInt(int32_t* out) const;
    bool getLong(int64_t* out) const;
    bool getDouble(double* out) const;
    bool getString(String16* out) const;
    bool getBooleanVector(std::vector<bool>* out) const;
    bool getByteVector(std::vector<uint8_t>* out) const;
    bool getIntVector(std::vector<int32_t>* out) const;
    bool getLongVector(std::vector<int64_t>* out) const;
    bool getDoubleVector(std::vector<double>* out) const;
    bool getStringVector(std::vector<String16>* out) const;
    bool getPersistableBundle(os::PersistableBundle* out) const;
    bool getMap(binder::Map* out) const;

    bool isBoolean() const;
    bool isByte() const;
    bool isInt() const;
    bool isLong() const;
    bool isDouble() const;
    bool isString() const;
    bool isBooleanVector() const;
    bool isByteVector() const;
    bool isIntVector() const;
    bool isLongVector() const;
    bool isDoubleVector() const;
    bool isStringVector() const;
    bool isPersistableBundle() const;
    bool isMap() const;

    // String Convenience Adapters
    // ---------------------------

    explicit Value(const String8& value):               Value(String16(value)) { }
    explicit Value(const ::std::string& value):         Value(String8(value.c_str())) { }
    void putString(const String8& value)       { return putString(String16(value)); }
    void putString(const ::std::string& value) { return putString(String8(value.c_str())); }
    Value& operator=(const String8& rhs)       { return *this = String16(rhs); }
    Value& operator=(const ::std::string& rhs) { return *this = String8(rhs.c_str()); }
    bool getString(String8* out) const;
    bool getString(::std::string* out) const;

private:

    // This allows ::android::Parcel to call the two methods below.
    friend class ::android::Parcel;

    // This is called by ::android::Parcel::writeValue()
    status_t writeToParcel(Parcel* parcel) const;

    // This is called by ::android::Parcel::readValue()
    status_t readFromParcel(const Parcel* parcel);

    template<typename T> class Content;
    class ContentBase;

    ContentBase* mContent;
};

}  // namespace binder

}  // namespace android

#endif  // ANDROID_VALUE_H