AMessage.h revision 14acc736e336cbd6026df781d4f411e908831815
1/*
2 * Copyright (C) 2010 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 A_MESSAGE_H_
18
19#define A_MESSAGE_H_
20
21#include <media/stagefright/foundation/ABase.h>
22#include <media/stagefright/foundation/ALooper.h>
23#include <utils/KeyedVector.h>
24#include <utils/RefBase.h>
25
26namespace android {
27
28struct AString;
29struct Parcel;
30
31struct AMessage : public RefBase {
32    AMessage(uint32_t what = 0, ALooper::handler_id target = 0);
33
34    static sp<AMessage> FromParcel(const Parcel &parcel);
35    void writeToParcel(Parcel *parcel) const;
36
37    void setWhat(uint32_t what);
38    uint32_t what() const;
39
40    void setTarget(ALooper::handler_id target);
41    ALooper::handler_id target() const;
42
43    void setInt32(const char *name, int32_t value);
44    void setInt64(const char *name, int64_t value);
45    void setSize(const char *name, size_t value);
46    void setFloat(const char *name, float value);
47    void setDouble(const char *name, double value);
48    void setPointer(const char *name, void *value);
49    void setString(const char *name, const char *s, ssize_t len = -1);
50    void setObject(const char *name, const sp<RefBase> &obj);
51    void setMessage(const char *name, const sp<AMessage> &obj);
52
53    bool findInt32(const char *name, int32_t *value) const;
54    bool findInt64(const char *name, int64_t *value) const;
55    bool findSize(const char *name, size_t *value) const;
56    bool findFloat(const char *name, float *value) const;
57    bool findDouble(const char *name, double *value) const;
58    bool findPointer(const char *name, void **value) const;
59    bool findString(const char *name, AString *value) const;
60    bool findObject(const char *name, sp<RefBase> *obj) const;
61    bool findMessage(const char *name, sp<AMessage> *obj) const;
62
63    void post(int64_t delayUs = 0);
64
65    sp<AMessage> dup() const;
66
67    AString debugString(int32_t indent = 0) const;
68
69protected:
70    virtual ~AMessage();
71
72private:
73    enum Type {
74        kTypeInt32,
75        kTypeInt64,
76        kTypeSize,
77        kTypeFloat,
78        kTypeDouble,
79        kTypePointer,
80        kTypeString,
81        kTypeObject,
82        kTypeMessage,
83    };
84
85    uint32_t mWhat;
86    ALooper::handler_id mTarget;
87
88    struct Item {
89        union {
90            int32_t int32Value;
91            int64_t int64Value;
92            size_t sizeValue;
93            float floatValue;
94            double doubleValue;
95            void *ptrValue;
96            RefBase *refValue;
97            AString *stringValue;
98        } u;
99        const char *mName;
100        Type mType;
101    };
102
103    enum {
104        kMaxNumItems = 16
105    };
106    Item mItems[kMaxNumItems];
107    size_t mNumItems;
108
109    void clear();
110    Item *allocateItem(const char *name);
111    void freeItem(Item *item);
112    const Item *findItem(const char *name, Type type) const;
113
114    DISALLOW_EVIL_CONSTRUCTORS(AMessage);
115};
116
117}  // namespace android
118
119#endif  // A_MESSAGE_H_
120