Windows Mobile下native c++ typeid的使用

背景

我在开发Mobile Sensors API - Native unified APIs for Windows Mobile Sensors Unit Test的过程中,想把Sensor对象的类打印出来,所以需要使用typeid来实现。

简介

本文讲述在Windows Mobile下如何使用c++的typeid操作符。

实现

IGSensor* GSensorFactory::CreateGSensor()
{
try
{
return HTCGSensor::Create();
}
catch(std::runtime_error& err)
{
printf("%s\n", err.what());
}

try
{
return SamsungGSensor::GetInstance();
}
catch(std::runtime_error& err)
{
printf("%s\n", err.what());
}
return NULL;
}

上面是Sensor工厂类,理想状况下,程序可以自动检测设备类型,生成相应的Sensor处理类。

所以我在Unit Test的时候,可以把运行时的类信息打印出来,方便测试。

TEST(IGSensor, GSensorTest )
{
IGSensor* gSensor = GSensorFactory::CreateGSensor();

CHECK(gSensor != NULL);
FAIL(typeid(*gSensor).name());
}

gSensor 定义为父类IGSensor,但是GSensorFactory返回的是其子类HTCGSensor或者SamsungGSensor。typeid可以把运行时的对象的类型信息打印出来。

下面是用wikipedia的例子简单解释一下typeid。原文见Typeid

#include <iostream>
#include <typeinfo> //for 'typeid' to work

class Person {
public:
// ... Person members ...
virtual ~Person() {}
};

class Employee : public Person {
// ... Employee members ...
};

int main () {
Person person;
Employee employee;
Person *ptr = &employee;

std::cout << typeid(person).name() << std::endl; // Person (statically known at compile-time)
std::cout << typeid(employee).name() << std::endl; // Employee (statically known at compile-time)
std::cout << typeid(ptr).name() << std::endl; // Person * (statically known at compile-time)
std::cout << typeid(*ptr).name() << std::endl; // Employee (looked up dynamically at run-time
// because it is the dereference of a pointer to a polymorphic class)
}

typeid的功能是在运行的时候判断(determine)对象的类信息。其返回type_info的对象,但是type_info的构造函数,==操作符都是私有的,所以不可能产生新的type_info的对象,每次使用都类似typeid(person).name() ,直接返回类信息。

虽然说typeid的功能是运行时判断,但是有写情况下,编译器是编译时判断的,例如上述例子中的前三个如下:

std::cout << typeid(person).name() << std::endl;   // Person (statically known at compile-time)
std::cout << typeid(employee).name() << std::endl; // Employee (statically known at compile-time)
std::cout << typeid(ptr).name() << std::endl; // Person * (statically known at compile-time)

这些都是编译时判断的。只有多态对象的指针才会运行时判断。如下:

std::cout << typeid(*ptr).name() << std::endl;     // Employee (looked up dynamically at run-time
// because it is the dereference of a pointer to a polymorphic class)

 

在我的程序中IGSensor的指针gSensor也使用了运行时判断。在Visual Studio下开发Windows Mobile,需要注意的是如果使用了运行时判断,需要修改项目编译选项。Configuration Properties -> C/C++ -> Language -> Enable Run-Time Type Info -> Yes。否则程序运行时会出错。

typeid1 

关于Mobile Sensors API项目

这个项目还是在起步阶段,当前实现了samsung的重力感应器,我把项目host到 Mobile Sensors API - Native unified APIs for Windows Mobile Sensors 了,我会持续改进,把各种sensors的实现到这个项目中。

由于我手头上没有HTC的机器,如果谁有兴趣可以加入到项目中帮我测试HTC设备,由于加入了Unit Test,测试变得很简单,只需要执行程序,参考测试输出文件就可以了,不需要调试。当然这个测试过程是一个不断迭代的过程,只是Unit Test把子过程简单化了。

源代码:http://mobilesensor.codeplex.com/SourceControl/ListDownloadableCommits.aspx

环境:VS2008 + WM 6 professional SDK + Samsung Windows Mobile SDK

posted @ 2009-09-01 09:28  Jake Lin  阅读(1041)  评论(0编辑  收藏  举报