今天看camera服务注册的时候遇到一个小问题
camera是利用binder机制进行通信的,那么必须将其service注册到ServiceManager中
我们可以从framework/base/mediaserver/mediaserver/main_mediaserver.cpp中查看相关源码
int main(int argc, char** argv)
{
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm = defaultServiceManager();
.........
CameraService::instantiate();
.............
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
}
进而追踪CameraService类,查看其instantiate方法,发现定义CameraService类的文件
frameworks/base/services/camera/libcameraservice/CameraService.h中没有找到相关方法
而在此文件中发现class CameraService :public BinderService<CameraService>,public BnCameraService
找到其父类BinderService,它定义在frameworks/base/include/binder/BinderService.h中,果然找到了instantiate方法
template<typename SERVICE>
class BinderService
{
public:
static status_t publish() {
sp<IServiceManager> sm(defaultServiceManager());
return sm->addService(String16(SERVICE::getServiceName()), new SERVICE());
}
static void publishAndJoinThreadPool() {
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm(defaultServiceManager());
sm->addService(String16(SERVICE::getServiceName()), new SERVICE());
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
}
static void instantiate() { publish(); }
static status_t shutdown() {
return NO_ERROR;
}
};
由此可见,先调用publish,再调用sm->addService(String16(SERVICE::getServiceName()), new SERVICE());
其中的SERVICE是怎么来的咧?
再回到CameraService.h 发现有这么一段class CameraService :public BinderService<CameraService>, public BnCameraService,是否意味着
将CameraService传递给SERVICE了呢?
为此,做了一个测试用例
以下是C++代码
#include<iostream>
using namespace std;
template<typename a, typename b>
class A
{
public:
void find()
{
cout<<sizeof(a)<<endl;
cout<<sizeof(b)<<endl;
}
};
class B : public A<int,char>
{
};
void main()
{
B bb;
bb.find();
}
运行结果如下
至此,明白了,可以用class A :public B<typename t>传递模板参数到父类中。
记录下,以免以后忘记