精简版 rtti

    这两天看到篇文章,讲 rtti 也就是运行时类型识别的。地址如下:
    //http://vastskysun.bokee.com/3193302.html
    
        记得 <<深入浅出 mfc >> 中有关于 mfc 中 rtti 如何实现的讲解。
    该书深入分析了 mfc 中 rtti 的实现方法和那一大堆宏具体是什么含义的剖析。
    这刻想起我已不想再看 mfc 关于这部分的讲解。因为自从我认识.net,几乎
    就没再模过mfc了,也不想去分析关于mfc 关于文档,视图结构是怎么实现的,
    感兴趣的看看 observer 模式是如何实现的,基本也明白了文档,视图结构的真谛。
   
        此刻我只想实现下面的功能:
            1。如何获取对象运行时的类型信息?
            2。如何检测两对象的类型是否相符?
            3。如何遍历输出当前全部用户类型的类型信息?
        带着上述精简需求,我实现了个精简版的 rtti。基本实现方法和 mfc 中讲解是一致的,
        我没有使用那一堆精巧然令人费解的宏,因而那会增加阅读困难。写这份代码的目的就是
        简单,尽量的简单,简单到一目了然!具体实现代码如下:


结构定义代码:

  1
  2//类型信息结构
  3struct type_information
  4{
  5    //类型包含信息
  6    string name;
  7    
  8    //指针域
  9    type_information* next;
 10    type_information* parent;
 11    static type_information* first;
 12
 13    //检测两类型是否相等。
 14    bool operator== (const type_information& rhs)
 15    {
 16        return name == rhs.name;
 17    }

 18}
;
 19
 20type_information* type_information::first = 0;
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31//类类型信息结构体辅助对象。帮助串联类类型信息。
 32
 33
 34
 35
 36
 37
 38
 39
 40//类型信息辅助结构
 41struct type_information_helper
 42{
 43    //帮助实现链表结构
 44    type_information_helper(type_information* first)
 45    {
 46        type_information::first = first;
 47    }

 48}
;
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63//下面定义两个类,每个类包好自己的类型信息。
 64
 65
 66
 67//基础类型
 68class base
 69{
 70public:
 71    base()
 72    {
 73    }

 74    virtual ~base()
 75    {
 76    }

 77public:
 78    //获取类型信息。
 79    virtual type_information* gettype() const
 80    {
 81        return &base::binfo;
 82    }

 83    //测试该类型是否为指定类型 type。模版方法。
 84    bool iskindof(const type_information* type)
 85    {
 86        type_information* self = gettype();
 87        while (self)
 88        {
 89            if (*self == *type)
 90            {
 91                return true;
 92            }

 93            else
 94            {
 95                self = self->parent;
 96            }

 97        }

 98        return false;
 99    }

100public:
101    static type_information binfo;
102}
;
103
104type_information base::binfo =
105{
106    "base",
107    0,
108    0
109}
;
110
111static type_information_helper binfo_helper(&base::binfo);
112
113
114
115
116
117
118class c : public base
119{
120public:
121    c()
122    {
123
124    }

125    virtual ~c()
126    {
127
128    }

129public:
130    static type_information cinfo;
131public:
132    virtual type_information* gettype() const
133    {
134        return &c::cinfo;
135    }

136}
;
137
138
139type_information c::cinfo =
140{
141    "c",
142    &base::binfo,
143    &base::binfo
144}
;
145
146
147static type_information_helper cinfo_helper(&c::cinfo);
148
149
150
151
152
153
154
155//获取类型信息。non member function 展现多态。
156
157
158type_information* gettype(base& b)
159{
160    return b.gettype();
161}

162
163
164
165
166
167
168
169
170
171//------------------------------------------------------------------
172




测试代码:


 1    //测试类类型信息。
 2
 3    {
 4        base b1;
 5        c c1;
 6        
 7        //遍历输出类型信息。
 8        for (type_information* info = type_information::first;
 9            info != 0;
10            info = info->next)
11        {
12            cout << info->name << endl;
13        }

14
15
16        cout << "//------------------------------------------------------------------" << endl;
17        
18        //遍历输出c1的父类型信息链。
19        for (type_information* info = c1.gettype();
20            info != 0;
21            info = info->parent)
22        {
23            cout << info->name << endl;
24        }

25
26
27        cout << "//------------------------------------------------------------------" << endl;
28        
29
30        //输出 c1 的类型名。
31
32        cout << gettype(c1)->name << endl;
33
34
35        cout << "//------------------------------------------------------------------" << endl;
36
37
38        //检测 c1的类型是否为 b1 的类型。
39        cout << c1.iskindof(b1.gettype()) << endl;
40




        简要分析:
            1。每个类型有自己的类型信息结构体。这个是通过 type_info结构体定义的。
            2。类型信息结构体通过链表结构组织起来。
            3。通过遍历可以获取全部用户定义类型信息结构体。

posted on 2006-12-20 15:43  小峰  阅读(382)  评论(0)    收藏  举报

导航