当前位置:编程学习 > C/C++ >>

C语言实现《大话设计模式》中的观察者模式中的委托例程

 1. #ifndef __ENTRUSTOBSERVER_H__ 
2. #define __ENTRUSTOBSERVER_H__ 
3. #include "rtthread.h" 
4. #include "finsh.h" 
5. //根据类名和类里面项的名,获得类的入口句柄 
6. #define ClassEntry(node, type, member) \ 
7.     ((type *)((char *)(node) - (unsigned long)(&((type *)0)->member))) 
8. //LIST数组 
9. typedef struct _List List; 
10. struct _List 
11. { 
12.     void **pListPointArray;                         //LIST数组指针 
13.     int Total;                                      //元素个数 
14.     void (*Add)(List *pList, void *pListPoint);     //添加 
15.     void (*Remove)(List *pList, void *pListPoint);  //移除 
16.     void (*Delete)(void *pList);                    //析构 
17. }; 
18. //List类的析构函数 
19. static void ListDelete(void *pList) 
20. { 
21.     if(((List *)pList)->pListPointArray != RT_NULL)     //先释放指针数组 
22.     { 
23.         rt_free(((List *)pList)->pListPointArray); 
24.     } 
25.     rt_free(pList);                                     //再释放整个List类 
26. } 
27. //元素增加函数 
28. static void ListAdd(List *pList, void *pListPoint) 
29. { 
30.     void **tListPointArray = rt_malloc(sizeof(int *) * (pList->Total + 1));     //申请比原来大一个存储单元的内存 
31.     int pListIndex; 
32.     for(pListIndex = 0; pListIndex < pList->Total; pListIndex++)        //拷贝 
33.     { 
34.         if(pList->pListPointArray[pListIndex] == pListPoint)                 //判断,如果有相同的元素存在 
35.         {    
36.             rt_free(tListPointArray);                                        //释放现申请的内存 
37.             return;                                                     //返回 
38.         } 
39.         tListPointArray[pListIndex] = pList->pListPointArray[pListIndex];         //拷贝 
40.     } 
41.     tListPointArray[pList->Total] = pListPoint;                              //将添加的元素放到最后一个存储单元中 
42.     pList->Total += 1;                                                  //总数加1 
43.     if(pList->pListPointArray != RT_NULL) rt_free(pList->pListPointArray);                      //释放原来的内存 
44.     pList->pListPointArray = tListPointArray;                                            //将新的句柄替换原句柄 
45. } 
46. //元素移除函数 
47. static void ListRemove(List *pList, void *pListPoint) 
48. { 
49.     int pListIndex, tListIndex; 
50.     void **tListPointArray; 
51.     void **FreePointArray; 
52.     void **SavePointArray; 
53.     if(pList->Total == 0) return;                                       //总数为0时退出 
54.     tListPointArray = rt_malloc(sizeof(int) * (pList->Total - 1));    //申请比原来小一个存储单元的内存 
55.     FreePointArray = tListPointArray;                                  //将刚申请的内存空间作为默认的释放空间 
56.     SavePointArray = pList->pListPointArray;                           //将已有的内存空间作为默认的存储空间 
57.     for(pListIndex = 0, tListIndex= 0; pListIndex < pList->Total; pListIndex++) //查找移除点 
58.     { 
59.         if(pList->pListPointArray[pListIndex] == pListPoint)         //当前点是移除点 
60.         { 
61.             FreePointArray = pList->pListPointArray;            //改变释放内存指针 
62.             SavePointArray = tListPointArray;                   //改变保留内存指针 
63.             continue; &nb

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,