The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 21 | #include <binder/Binder.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | // ---------------------------------------------------------------------- |
| 26 | |
| 27 | class IInterface : public virtual RefBase |
| 28 | { |
| 29 | public: |
| 30 | sp<IBinder> asBinder(); |
| 31 | sp<const IBinder> asBinder() const; |
| 32 | |
| 33 | protected: |
| 34 | virtual IBinder* onAsBinder() = 0; |
| 35 | }; |
| 36 | |
| 37 | // ---------------------------------------------------------------------- |
| 38 | |
| 39 | template<typename INTERFACE> |
| 40 | inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj) |
| 41 | { |
| 42 | return INTERFACE::asInterface(obj); |
| 43 | } |
| 44 | |
| 45 | // ---------------------------------------------------------------------- |
| 46 | |
| 47 | template<typename INTERFACE> |
| 48 | class BnInterface : public INTERFACE, public BBinder |
| 49 | { |
| 50 | public: |
| 51 | virtual sp<IInterface> queryLocalInterface(const String16& _descriptor); |
| 52 | virtual String16 getInterfaceDescriptor() const; |
| 53 | |
| 54 | protected: |
| 55 | virtual IBinder* onAsBinder(); |
| 56 | }; |
| 57 | |
| 58 | // ---------------------------------------------------------------------- |
| 59 | |
| 60 | template<typename INTERFACE> |
| 61 | class BpInterface : public INTERFACE, public BpRefBase |
| 62 | { |
| 63 | public: |
| 64 | BpInterface(const sp<IBinder>& remote); |
| 65 | |
| 66 | protected: |
| 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 | |
| 99 | template<typename INTERFACE> |
| 100 | inline sp<IInterface> BnInterface<INTERFACE>::queryLocalInterface( |
| 101 | const String16& _descriptor) |
| 102 | { |
| 103 | if (_descriptor == INTERFACE::descriptor) return this; |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | template<typename INTERFACE> |
| 108 | inline String16 BnInterface<INTERFACE>::getInterfaceDescriptor() const |
| 109 | { |
| 110 | return INTERFACE::getInterfaceDescriptor(); |
| 111 | } |
| 112 | |
| 113 | template<typename INTERFACE> |
| 114 | IBinder* BnInterface<INTERFACE>::onAsBinder() |
| 115 | { |
| 116 | return this; |
| 117 | } |
| 118 | |
| 119 | template<typename INTERFACE> |
| 120 | inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote) |
| 121 | : BpRefBase(remote) |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | template<typename INTERFACE> |
| 126 | inline IBinder* BpInterface<INTERFACE>::onAsBinder() |
| 127 | { |
| 128 | return remote(); |
| 129 | } |
| 130 | |
| 131 | // ---------------------------------------------------------------------- |
| 132 | |
| 133 | }; // namespace android |
| 134 | |
| 135 | #endif // ANDROID_IINTERFACE_H |