为什么 interface

为什么 interface

binder经过interface_cast转换,最终变成了BpCameraService类型
在IInterface.h文件

template
inline sp interface_cast(const sp& obj)
{
    return INTERFACE::asInterface(obj); //相当于return ICameraService::asInterface(obj);
}
然后来到ICameraService.h文件中,找到

 DECLARE_meta_INTERFACE(CameraService); //点击这里进入

截图部分代码被替换后,最终形式

类似接口
#define DECLARE_meta_INTERFACE(CameraService)                               
    static const android::String16 descriptor;                          
    static android::sp asInterface(                       
            const android::sp& obj);                  
    virtual const android::String16& getInterfaceDescriptor() const;    
    ICameraService();                                                     
    virtual ~ICameraService();                                            

类似接口的具体实现
#define IMPLEMENT_meta_INTERFACE(CameraService, NAME)                       
    const android::String16 ICameraService::descriptor(NAME);             
    const android::String16&                                            
            ICameraService::getInterfaceDescriptor() const {              
        return ICameraService::descriptor;                                
    }                                                                   
    android::sp ICameraService::asInterface(                
            const android::sp& obj)                   
    {                                                                   
        android::sp intr;                                 
        if (obj != NULL) {                                              
            intr = static_cast(                          
                obj->queryLocalInterface(                               
                        ICameraService::descriptor).get());               
            if (intr == NULL) {                                         
                intr = new BpCameraService(obj);                          
            }                                                           
        }                                                               
        return intr;                                                    
    }                                                                   
    ICameraService::ICameraService() { }                                    
    ICameraService::~ICameraService() { }                                   

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5637131.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存