1/*
2 * Copyright (C) 2005 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#ifndef ANDROID_IINTERFACE_H
19#define ANDROID_IINTERFACE_H
20
21#include <utils/Binder.h>
22
23namespace android {
24
25// ----------------------------------------------------------------------
26
27class IInterface : public virtual RefBase
28{
29public:
30            sp<IBinder>         asBinder();
31            sp<const IBinder>   asBinder() const;
32
33protected:
34    virtual IBinder*            onAsBinder() = 0;
35};
36
37// ----------------------------------------------------------------------
38
39template<typename INTERFACE>
40inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
41{
42    return INTERFACE::asInterface(obj);
43}
44
45// ----------------------------------------------------------------------
46
47template<typename INTERFACE>
48class BnInterface : public INTERFACE, public BBinder
49{
50public:
51    virtual sp<IInterface>      queryLocalInterface(const String16& _descriptor);
52    virtual String16            getInterfaceDescriptor() const;
53
54protected:
55    virtual IBinder*            onAsBinder();
56};
57
58// ----------------------------------------------------------------------
59
60template<typename INTERFACE>
61class BpInterface : public INTERFACE, public BpRefBase
62{
63public:
64                                BpInterface(const sp<IBinder>& remote);
65
66protected:
67    virtual IBinder*            onAsBinder();
68};
69
70// ----------------------------------------------------------------------
71
72#define DECLARE_META_INTERFACE(INTERFACE)                               \
73    static const String16 descriptor;                                   \
74    static sp<I##INTERFACE> asInterface(const sp<IBinder>& obj);        \
75    virtual String16 getInterfaceDescriptor() const;                    \
76
77#define IMPLEMENT_META_INTERFACE(INTERFACE, NAME)                       \
78    const String16 I##INTERFACE::descriptor(NAME);                      \
79    String16 I##INTERFACE::getInterfaceDescriptor() const {             \
80        return I##INTERFACE::descriptor;                                \
81    }                                                                   \
82    sp<I##INTERFACE> I##INTERFACE::asInterface(const sp<IBinder>& obj)  \
83    {                                                                   \
84        sp<I##INTERFACE> intr;                                          \
85        if (obj != NULL) {                                              \
86            intr = static_cast<I##INTERFACE*>(                          \
87                obj->queryLocalInterface(                               \
88                        I##INTERFACE::descriptor).get());               \
89            if (intr == NULL) {                                         \
90                intr = new Bp##INTERFACE(obj);                          \
91            }                                                           \
92        }                                                               \
93        return intr;                                                    \
94    }                                                                   \
95
96// ----------------------------------------------------------------------
97// No user-servicable parts after this...
98
99template<typename INTERFACE>
100inline sp<IInterface> BnInterface<INTERFACE>::queryLocalInterface(
101        const String16& _descriptor)
102{
103    if (_descriptor == INTERFACE::descriptor) return this;
104    return NULL;
105}
106
107template<typename INTERFACE>
108inline String16 BnInterface<INTERFACE>::getInterfaceDescriptor() const
109{
110    return INTERFACE::getInterfaceDescriptor();
111}
112
113template<typename INTERFACE>
114IBinder* BnInterface<INTERFACE>::onAsBinder()
115{
116    return this;
117}
118
119template<typename INTERFACE>
120inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote)
121    : BpRefBase(remote)
122{
123}
124
125template<typename INTERFACE>
126inline IBinder* BpInterface<INTERFACE>::onAsBinder()
127{
128    return remote();
129}
130
131// ----------------------------------------------------------------------
132
133}; // namespace android
134
135#endif // ANDROID_IINTERFACE_H
136