visualfc

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  42 随笔 :: 0 文章 :: 119 评论 :: 0 Trackbacks
原创 visualfc

介绍一个通用c++ typeid实现.

主要功能:
    通用的typeid实现,可以在VS60/VS2005以及mingw32下保证相同的类型名输出.
    程序使用boost::type_traits来实现,没有使用内置的typeid.不支持RTTI操作.

项目地址:
    http://code.google.com/p/typeid

程序例子:
  1.  vfc::type_id(100).name();    // output "int"
  2.  vfc::type_id_t<int>::name(); // output "int"
  3.  vfc::type_id_t<const int &>::name(); // output "int const &"

  4.  struct a 
  5.  {
  6.      const int * test(intconst char *);
  7.  };
  8.  VFC_MAKE_ID(a,0x2003);

  9.  vfc::type_id(&a::test).name(); //output  "int const * (a::*)(int,char const *)"
主要类介绍:
   template<typename T>  vfc::type_id_t<>  获取类型的typeid名
   template<typename T>  vfc::type_id(const T & t) 获取变量的类型的type_id_t类型.

主要函数:
   type_id(100).std_name() 输出 std::string 类型的name
   type_id(100).name()     输出 const char * 类型的name

定义类型:
   VFC_TYPE_ID(type,id) 实现类型的定义,type为类型,id为类型自定义数值标识
例: VFC_TYPE_ID(std::string,0x2001)


下面给出一个完整的例子:  

  1. #include "vfc_type.h"
  2. struct a
  3. {
  4.     const int * test(int,int)
  5.     {
  6.         return NULL;
  7.     }

  8.     void test1() const
  9.     {
  10.     }
  11.     const char * test2(intintvolatile
  12.     {
  13.         return NULL;
  14.     }
  15.     const int & test3(int *, const char *) const volatile
  16.     {
  17.         static int i = 100;
  18.         return i;
  19.     }
  20. };

  21. void test(const char *, int k)
  22. {
  23. };

  24. VFC_TYPE_ID(a,0x2000);



  25. int main(int argc, char* argv[])
  26. {
  27.     typedef void (*T1)(void);
  28.     typedef int (*T2)(void);
  29.     typedef int (*T3)(int,int,int,int,int,int,int,int,int);
  30.     typedef const char * (*T4)(int &,int *,char **,char &,const int &);

  31.     typedef int U1;
  32.     typedef int U2[5];
  33.     typedef const int & U3;
  34.     typedef char U4[5][6][7];

  35.     printf("T1 type %s\n",vfc::type_id_t<T1>::name());
  36.     printf("T2 type %s\n",vfc::type_id_t<T2>::name());
  37.     printf("T3 type %s\n",vfc::type_id_t<T3>::name());
  38.     printf("T4 type %s\n",vfc::type_id_t<T4>::name());

  39.     printf("U1 type %s\n",vfc::type_id_t<U1>::name());
  40.     printf("U2 type %s\n",vfc::type_id_t<U2>::name());
  41.     printf("U3 type %s\n",vfc::type_id_t<U3>::name());
  42.     printf("U4 type %s\n",vfc::type_id_t<U4>::name());

  43.     printf("\'d\' type %s\n",vfc::type_id('d').name());
  44.     printf("100 type %s\n",vfc::type_id(100).name());
  45.     printf("100.0 type %s\n",vfc::type_id(100.0).name());
  46.     printf("\"str\" type %s\n",vfc::type_id("str").name());

  47.     const char * str = "ok";
  48.     printf("const char * str type %s\n",vfc::type_id(str).name());

  49.     a * pa;
  50.     printf("a type %s\n",vfc::type_id_t<a>::name());
  51.     printf("pa type %s\n",vfc::type_id(pa).name());
  52.     printf("a::test type %s\n",vfc::type_id(&a::test).name());
  53.     printf("a::test type %s\n",vfc::type_id(&a::test1).name());
  54.     printf("a::test type %s\n",vfc::type_id(&a::test2).name());
  55.     printf("a::test type %s\n",vfc::type_id(&a::test3).name());
  56.     printf("test %s\n",vfc::type_id(&test).name());

  57.     return 0;
  58. }
在VS60,VS2005和mingw32下编译得到相同输出结果如下:
  1. T1 type void (*)()
  2. T2 type int (*)()
  3. T3 type int (*)(int,int,int,int,int,int,int,int,int)
  4. T4 type char const * (*)(int &,int *,char * *,char &,int const &)
  5. U1 type int
  6. U2 type int [5]
  7. U3 type int const &
  8. U4 type char [7] [6] [5]
  9. 'd' type char
  10. 100 type int
  11. 100.0 type double
  12. "str" type char [4]
  13. const char * str type char const *
  14. a type a
  15. pa type a *
  16. a::test type int const * (a::*)(int,int)
  17. a::test type void (a::*)()const
  18. a::test type char const * (a::*)(int,int)volatile
  19. a::test type int const & (a::*)(int *,char const *)const volatile
  20. test void (*)(char const *,int)
posted on 2008-11-03 22:22 visualfc 阅读(2216) 评论(2)  编辑 收藏 引用 所属分类: C++

评论

# re: 通用C++ typeid实现(不支持RTTI) 2008-11-04 00:06 踏雪赤兔
看了一下代码,貌似不是线程安全的,呵呵~
继续努力!  回复  更多评论
  

# re: 通用C++ typeid实现(不支持RTTI)[未登录] 2008-11-04 07:58 visualfc
谢谢提醒.
看来为了线程安全, 可能得将type_id_t<T>::name()接口改为type_id_t<T>().name()了.  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理