michael@0: /* michael@0: * Copyright (C) 2005 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: // michael@0: #ifndef ANDROID_IINTERFACE_H michael@0: #define ANDROID_IINTERFACE_H michael@0: michael@0: #include michael@0: michael@0: namespace android { michael@0: michael@0: // ---------------------------------------------------------------------- michael@0: michael@0: class IInterface : public virtual RefBase michael@0: { michael@0: public: michael@0: IInterface(); michael@0: sp asBinder(); michael@0: sp asBinder() const; michael@0: michael@0: protected: michael@0: virtual ~IInterface(); michael@0: virtual IBinder* onAsBinder() = 0; michael@0: }; michael@0: michael@0: // ---------------------------------------------------------------------- michael@0: michael@0: template michael@0: inline sp interface_cast(const sp& obj) michael@0: { michael@0: return INTERFACE::asInterface(obj); michael@0: } michael@0: michael@0: // ---------------------------------------------------------------------- michael@0: michael@0: template michael@0: class BnInterface : public INTERFACE, public BBinder michael@0: { michael@0: public: michael@0: virtual sp queryLocalInterface(const String16& _descriptor); michael@0: virtual const String16& getInterfaceDescriptor() const; michael@0: michael@0: protected: michael@0: virtual IBinder* onAsBinder(); michael@0: }; michael@0: michael@0: // ---------------------------------------------------------------------- michael@0: michael@0: template michael@0: class BpInterface : public INTERFACE, public BpRefBase michael@0: { michael@0: public: michael@0: BpInterface(const sp& remote); michael@0: michael@0: protected: michael@0: virtual IBinder* onAsBinder(); michael@0: }; michael@0: michael@0: // ---------------------------------------------------------------------- michael@0: michael@0: #define DECLARE_META_INTERFACE(INTERFACE) \ michael@0: static const android::String16 descriptor; \ michael@0: static android::sp asInterface( \ michael@0: const android::sp& obj); \ michael@0: virtual const android::String16& getInterfaceDescriptor() const; \ michael@0: I##INTERFACE(); \ michael@0: virtual ~I##INTERFACE(); \ michael@0: michael@0: michael@0: #define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \ michael@0: const android::String16 I##INTERFACE::descriptor(NAME); \ michael@0: const android::String16& \ michael@0: I##INTERFACE::getInterfaceDescriptor() const { \ michael@0: return I##INTERFACE::descriptor; \ michael@0: } \ michael@0: android::sp I##INTERFACE::asInterface( \ michael@0: const android::sp& obj) \ michael@0: { \ michael@0: android::sp intr; \ michael@0: if (obj != NULL) { \ michael@0: intr = static_cast( \ michael@0: obj->queryLocalInterface( \ michael@0: I##INTERFACE::descriptor).get()); \ michael@0: if (intr == NULL) { \ michael@0: intr = new Bp##INTERFACE(obj); \ michael@0: } \ michael@0: } \ michael@0: return intr; \ michael@0: } \ michael@0: I##INTERFACE::I##INTERFACE() { } \ michael@0: I##INTERFACE::~I##INTERFACE() { } \ michael@0: michael@0: michael@0: #define CHECK_INTERFACE(interface, data, reply) \ michael@0: if (!data.checkInterface(this)) { return PERMISSION_DENIED; } \ michael@0: michael@0: michael@0: // ---------------------------------------------------------------------- michael@0: // No user-serviceable parts after this... michael@0: michael@0: template michael@0: inline sp BnInterface::queryLocalInterface( michael@0: const String16& _descriptor) michael@0: { michael@0: if (_descriptor == INTERFACE::descriptor) return this; michael@0: return NULL; michael@0: } michael@0: michael@0: template michael@0: inline const String16& BnInterface::getInterfaceDescriptor() const michael@0: { michael@0: return INTERFACE::getInterfaceDescriptor(); michael@0: } michael@0: michael@0: template michael@0: IBinder* BnInterface::onAsBinder() michael@0: { michael@0: return this; michael@0: } michael@0: michael@0: template michael@0: inline BpInterface::BpInterface(const sp& remote) michael@0: : BpRefBase(remote) michael@0: { michael@0: } michael@0: michael@0: template michael@0: inline IBinder* BpInterface::onAsBinder() michael@0: { michael@0: return remote(); michael@0: } michael@0: michael@0: // ---------------------------------------------------------------------- michael@0: michael@0: }; // namespace android michael@0: michael@0: #endif // ANDROID_IINTERFACE_H