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_HARDWARE_IINTERFACE_H
19 #define ANDROID_HARDWARE_IINTERFACE_H
20 
21 #include <hwbinder/Binder.h>
22 
23 namespace android {
24 namespace hardware {
25 // ----------------------------------------------------------------------
26 
27 class IInterface : public virtual RefBase
28 {
29 public:
30             IInterface();
31             static sp<IBinder>  asBinder(const IInterface*);
32             static sp<IBinder>  asBinder(const sp<IInterface>&);
33 protected:
34     virtual                     ~IInterface();
35     virtual IBinder*            onAsBinder() = 0;
36 };
37 
38 // ----------------------------------------------------------------------
39 
40 template<typename INTERFACE>
41 class BnInterface : public INTERFACE, public IInterface, public BHwBinder
42 {
43 public:
44                                 BnInterface(const sp<INTERFACE>& impl);
45 protected:
46     const sp<INTERFACE>         mImpl;
47     virtual IBinder*            onAsBinder();
48 };
49 
50 template<typename INTERFACE>
BnInterface(const sp<INTERFACE> & impl)51 inline BnInterface<INTERFACE>::BnInterface(
52         const sp<INTERFACE>& impl) : mImpl(impl)
53 {
54 }
55 // ----------------------------------------------------------------------
56 
57 template<typename INTERFACE>
58 class BpInterface : public INTERFACE, public IInterface, public BpHwRefBase
59 {
60 public:
61                                 BpInterface(const sp<IBinder>& remote);
62     virtual IBinder*            onAsBinder();
63 };
64 
65 // ----------------------------------------------------------------------
66 
67 // ----------------------------------------------------------------------
68 // No user-serviceable parts after this...
69 
70 template<typename INTERFACE>
onAsBinder()71 IBinder* BnInterface<INTERFACE>::onAsBinder()
72 {
73     return this;
74 }
75 
76 template<typename INTERFACE>
BpInterface(const sp<IBinder> & remote)77 inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote)
78     : BpHwRefBase(remote)
79 {
80 }
81 
82 template<typename INTERFACE>
onAsBinder()83 inline IBinder* BpInterface<INTERFACE>::onAsBinder()
84 {
85     return remote();
86 }
87 
88 // ----------------------------------------------------------------------
89 
90 }; // namespace hardware
91 }; // namespace android
92 
93 #endif // ANDROID_HARDWARE_IINTERFACE_H
94