Chinaunix首页 | 论坛 | 博客
  • 博客访问: 503032
  • 博文数量: 140
  • 博客积分: 461
  • 博客等级: 下士
  • 技术积分: 878
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-28 10:06
文章分类

全部博文(140)

文章存档

2016年(1)

2015年(6)

2014年(20)

2013年(1)

2012年(16)

2011年(96)

分类:

2011-08-12 22:06:44

http://vincent.blog.chinaunix.net
今天基于链表写了个对象管理器,主要是管理各种模块,组件之类的,不过感觉实现得有点那个~~~

file: manager.h
===========================================================================================
  1. /*
  2.  * author: vincent.cws2008@gmail.com
  3.  */
  4. #ifndef _MANAGER_H_
  5. #define _MANAGER_H_

  6. #include "list.h"
  7. #include "listx.h"
  8. #include "stdlib.h"

  9. #define mallocx(n) malloc(n)
  10. #define freex(p)    free(p)
  11. #define lockx()
  12. #define unlockx()

  13. typedef struct manager_s
  14. {
  15.     struct list_s list;
  16.     int (*registerx)(struct manager_s *self_p,
  17.                     char *name_p, void *new_p, bool is_malloc);
  18.     void* (*unregisterx)(struct manager_s *self_p, char *name_p);
  19.     void* (*instancex)(struct manager_s *self_p, char* name_p);
  20.     void (*queryx)(struct manager_s *self_p);
  21.     void (*clear_all)(struct manager_s *self_p);
  22. }MANAGER_T;

  23. void init_manager(MANAGER_T *mgt_p);
  24. void exit_manager(MANAGER_T *mgt_p);

  25. #define member_reg_malloc(type,name_p,mgt) {\
  26.     type *new_p = (type*)mallocx(sizeof(type)); \
  27.     if (-1==mgt.registerx(&(mgt),(name_p),(new_p),1)) freex(new_p);\
  28. }

  29. #define member_unreg_free(type,name_p,mgt) {\
  30.     type *del_p = (type*)(mgt).unregisterx(&(mgt),(name_p)); \
  31.     if (del_p) freex(del_p);\
  32. }

  33. #define member_pt(name_p, mgt) \
  34.         (mgt).instancex(&(mgt),(name_p))

  35. #define member_query(mgt) \
  36.         (mgt).queryx(&(mgt))

  37. #define member_register(name_p,new_p,is_malloc,mgt) \
  38.         (mgt).registerx(&(mgt),(name_p),(new_p),is_malloc)

  39. #define member_unregister(name_p,mgt) \
  40.         (mgt).unregisterx(&(mgt),(name_p))



  41. #endif

file: manager.c
===========================================================================================
  1. /*
  2.  * author: vincent.cws2008@gmail.com
  3.  */

  4. #include "list.h"
  5. #include "listx.h"
  6. #include "manager.h"

  7. #define max(a,b) (((a) > (b)) ? (a) : (b))
  8. #define min(a,b) (((a) < (b)) ? (a) : (b))

  9. static int member_register__(struct manager_s *self_p,
  10.                              char *name_p, void *new_p, bool is_malloc)
  11. {
  12.     NODE_T *node_p;
  13.     int len;
  14.     assert(self_p && name_p && new_p);
  15.     lockx();
  16.     if (listx_lookup(self_p->list, name_p))
  17.     {
  18.         unlockx();
  19.         return -1;
  20.     }
  21.     unlockx();
  22.     node_p = (NODE_T*)mallocx(sizeof(NODE_T));
  23.     assert(node_p);
  24.     node_p->new_p = new_p;
  25.     len = min(strlen(name_p),MAX_NODE_NAME-1);
  26.     strncpy(node_p->name, name_p, len);
  27.     node_p->name[len]='\0';
  28.     node_p->is_malloc=is_malloc;
  29.     lockx();
  30.     listx_append(self_p->list, node_p);
  31.     unlockx();
  32.     return 0;    
  33. }

  34. static void* member_unregister__(struct manager_s *self_p, char *name_p)
  35. {
  36.     NODE_T *node_p;
  37.     void *sgt_p;
  38.     assert(self_p && name_p);
  39.     lockx();
  40.     if (!(node_p=listx_lookup(self_p->list, name_p)))
  41.     {
  42.         unlockx();
  43.         return NULL;
  44.     }
  45.     sgt_p = node_p->new_p;
  46.     listx_delete(self_p->list, node_p);
  47.     unlockx();
  48.     freex(node_p);
  49.     return sgt_p;
  50. }

  51. static void* member_instance__(struct manager_s *self_p, char* name_p)
  52. {
  53.     NODE_T *node_p;
  54.     assert(self_p && name_p);
  55.     lockx();
  56.     if (!(node_p=listx_lookup(self_p->list, name_p)))
  57.     {
  58.         unlockx();
  59.         return NULL;
  60.     }
  61.     unlockx();
  62.     return node_p->new_p;
  63. }

  64. static void member_query__(struct manager_s *self_p)
  65. {
  66.     assert(self_p);
  67.     lockx();
  68.     listx_print(self_p->list);
  69.     unlockx();
  70. }

  71. static void member_clear_all__(struct manager_s *self_p)
  72. {
  73.     struct list_head *list_p;
  74.     NODE_T *node_p;
  75.     NODE_T *temp_p;
  76.     assert(self_p);
  77.     lockx();
  78.     list_for_each_safe(list_p, temp_p, &((self_p->list).head))
  79.     {
  80.         node_p = list_entry(list_p, NODE_T, list);
  81.         if (node_p->new_p && node_p->is_malloc)
  82.         {
  83.             freex(node_p->new_p);
  84.             //list_p = list_p->next
  85.             listx_delete(self_p->list, node_p);
  86.             member_query__(self_p);
  87.         }
  88.     }
  89.     unlockx();    
  90. }


  91. extern void init_manager(MANAGER_T *mgt_p)
  92. {
  93.     assert(mgt_p);
  94.     listx_init(&mgt_p->list);
  95.     *(mgt_p->registerx) = member_register__;
  96.     *(mgt_p->unregisterx) = member_unregister__;
  97.     *(mgt_p->instancex) = member_instance__;
  98.     *(mgt_p->queryx) = member_query__;
  99.     *(mgt_p->clear_all) = member_clear_all__;
  100. }

  101. extern void exit_manager(MANAGER_T *mgt_p)
  102. {
  103.     assert(mgt_p);

  104.     mgt_p->clear_all(mgt_p);
  105. }

file: listx.h
===========================================================================================
  1. /*
  2.  * author: vincent.cws2008@gmail.com
  3.  */

  4. #ifndef _LIST_X_H_
  5. #define _LIST_X_H_

  6. #define assert(x)

  7. extern struct list_head;

  8. #define MAX_NODE_NAME 32

  9. #define bool int

  10. typedef struct node_s{
  11.     struct list_head list;
  12.     void *new_p;
  13.     char name[MAX_NODE_NAME];
  14.     bool is_malloc;
  15. }NODE_T;

  16. typedef struct list_s{
  17.     struct list_head head;
  18.     void (*prepend)(struct list_s *self_p, NODE_T *new_p);
  19.     void (*append)(struct list_s *self_p, NODE_T *new_p);
  20.     void (*delete)(struct list_s *self_p, NODE_T *node_p);
  21.     void (*replace)(struct list_s *self_p, NODE_T *old_p, NODE_T *new_p);
  22.     NODE_T* (*lookup)(struct list_s *self_p, char *name_p);
  23.     void (*print)(struct list_s *self_p);    
  24. }LIST_T;

  25. void listx_init(LIST_T *list_p);

  26. #define listx_prepend(list,node_p) ((list).prepend(&(list), node_p))
  27. #define listx_append(list,node_p) ((list).append(&(list), node_p))
  28. #define listx_print(list) ((list).print(&(list)))
  29. #define listx_replace(list,old_p,new_p) ((list).replace(&(list), old_p, new_p))
  30. #define listx_lookup(list,x) ((list).lookup(&(list), x))
  31. #define listx_delete(list,node_p) ((list).delete(&(list), node_p))

  32. #endif


file: listx.c
===========================================================================================
  1. /*
  2.  * author: vincent.cws2008@gmail.com
  3.  */

  4. #include "list.h"
  5. #include "listx.h"

  6. static void node_prepend(struct list_s *self_p, NODE_T *new_p);
  7. static void node_append(struct list_s *self_p, NODE_T *new_p);
  8. static void node_delete(struct list_s *self_p, NODE_T *node_p);
  9. static void node_replace(struct list_s *self_p, NODE_T *old_p, NODE_T *new_p);
  10. static NODE_T* node_lookup(struct list_s *self_p, char *name_p);
  11. static void node_print(struct list_s *self_p);

  12. static void node_prepend(struct list_s *self_p, NODE_T *new_p)
  13. {
  14.     assert(self_p && new_p);
  15.     list_add(&new_p->list, &(self_p->head));
  16. }

  17. static void node_append(struct list_s *self_p, NODE_T *new_p)
  18. {
  19.     assert(self_p && new_p);
  20.     list_add_tail(&new_p->list, &(self_p->head));
  21. }

  22. static void node_delete(struct list_s *self_p, NODE_T *node_p)
  23. {
  24.     assert(self_p && node_p);
  25.     list_del(&node_p->list);
  26. }

  27. static void node_replace(struct list_s *self_p, NODE_T *old_p, NODE_T *new_p)
  28. {
  29.     assert(self_p && old_p && new_p);
  30.     list_replace(&old_p->list, &new_p->list);
  31. }

  32. static NODE_T* node_lookup(struct list_s *self_p, char *name_p)
  33. {
  34.     struct list_head *list_p;
  35.     NODE_T *node_p;    
  36.     assert(self_p && name_p);
  37.     list_for_each(list_p, &(self_p->head))
  38.     {
  39.         node_p = list_entry(list_p, NODE_T, list);
  40.         if (!strcmp(node_p->name, name_p))
  41.             return node_p;
  42.     }
  43.     return NULL;
  44. }

  45. static void node_print(struct list_s *self_p)
  46. {
  47.     struct list_head *list_p;
  48.     NODE_T *node_p;
  49.     list_for_each(list_p, &(self_p->head))
  50.     {
  51.         node_p = list_entry(list_p, NODE_T, list);
  52.         printf("node name=%s --> ", node_p->name);
  53.     }
  54.     printf("\r\n");
  55. }

  56. extern void listx_init(LIST_T *list_p)
  57. {
  58.     assert(list_p);
  59.     INIT_LIST_HEAD(&list_p->head);
  60.     *(list_p->prepend) = node_prepend;
  61.     *(list_p->append) = node_append;
  62.     *(list_p->delete) = node_delete;
  63.     *(list_p->replace) = node_replace;
  64.     *(list_p->lookup) = node_lookup;
  65.     *(list_p->print) = node_print;
  66. }

file: main.c
===========================================================================================
  1. /*
  2.  * author: vincent.cws2008@gmail.com
  3.  */

  4. #include "stdio.h"
  5. #include "manager.h"

  6. #ifndef MAX_PATH
  7. #define MAX_PATH 260
  8. #endif

  9. static int read()
  10. {
  11.     printf("read ok!\r\n");
  12.     return 0;
  13. }
  14. static int write()
  15. {
  16.     printf("write ok!\r\n");
  17.     return 0;
  18. }
  19. static int open()
  20. {
  21.     printf("open ok!\r\n");
  22.     return 0;
  23. }
  24. static int close()
  25. {
  26.     printf("close ok!\r\n");
  27.     return 0;
  28. }

  29. typedef struct common_opt_s {
  30.     int (*read)();
  31.     int (*write)();
  32.     int (*open)();
  33.     int (*close)();
  34. }COMMON_OPT_T;

  35. typedef struct printer_s{
  36.     char name[MAX_PATH];
  37.     COMMON_OPT_T common_opt;
  38.     void *other_p;
  39. }PRINTER_T;

  40. typedef struct smartcard_s{
  41.     char name[MAX_PATH];
  42.     COMMON_OPT_T common_opt;
  43.     void *other_p;
  44. }SMARTCART_T;

  45. typedef struct pinpad_s{
  46.     char name[MAX_PATH];
  47.     COMMON_OPT_T common_opt;
  48.     void *other_p;
  49. }PINPAD_T;

  50. typedef struct screen_s{
  51.     char name[MAX_PATH];
  52.     COMMON_OPT_T common_opt;
  53.     void *other_p;
  54. }SCREEN_T;

  55. typedef struct communicate_s{
  56.     char name[MAX_PATH];
  57.     COMMON_OPT_T common_opt;
  58.     void *other_p;
  59. }COMMUNICATE_T;


  60. MANAGER_T g_mgt;

  61. #define mbr_reg_malloc(type,name_p) \
  62.         member_reg_malloc(type,name_p,g_mgt,)
  63. #define mbr_unreg_free(type,name_p) \
  64.         member_unreg_free(type,name_p,g_mgt)
  65. #define mbr_pt(name_p) \
  66.         member_pt(name_p, g_mgt)
  67. #define mbr_query() \
  68.         member_query(g_mgt)
  69. #define mbr_reg(name_p,new_p,is_malloc) \
  70.         member_register(name_p,new_p,is_malloc,g_mgt)
  71. #define mbr_unreg(name_p) \
  72.         member_unregister(name_p,g_mgt)


  73. void opt_print(const char* prompt_p, COMMON_OPT_T *comom_opt_p)
  74. {
  75.     assert(comom_opt_p && prompt_p);
  76.     printf("===%s===\r\n", prompt_p);
  77.     comom_opt_p->open();
  78.     comom_opt_p->read();
  79.     comom_opt_p->write();
  80.     comom_opt_p->close();
  81. }

  82. #define ID_PRINTER "printer"
  83. #define ID_SMARTCARD "smartcard"
  84. #define ID_PINPAD "pinpad"
  85. #define ID_SCREEN "screen"
  86. #define ID_COMMUNICATE "communicate"

  87. #define OPTS_INIT {read,write,open,close}

  88. void main()
  89. {
  90.     PRINTER_T* printer_p;
  91.     SMARTCART_T* smartcard_p;
  92.     PINPAD_T* pinpad_p;
  93.     SCREEN_T* screen_p;
  94.     COMMUNICATE_T* communicate_p;

  95.     static PRINTER_T s_printer={ID_PRINTER, OPTS_INIT, 0};
  96.     static SMARTCART_T s_smartcard={ID_SMARTCARD, OPTS_INIT, 0};
  97.     static PINPAD_T s_pinpad={ID_PINPAD, OPTS_INIT, 0};
  98.     static SCREEN_T s_screen={ID_SCREEN, OPTS_INIT, 0};
  99.     static COMMUNICATE_T s_communicate={ID_COMMUNICATE, OPTS_INIT, 0};

  100.     /* init manager */
  101.     init_manager(&g_mgt);
  102.     
  103.     /* register all devices (static) */
  104.     mbr_reg(s_printer.name, &s_printer, 0);
  105.     printer_p = mbr_pt(ID_PRINTER);
  106.     if (printer_p) opt_print(ID_PRINTER, &printer_p->common_opt);
  107.     mbr_query();

  108.     mbr_reg(s_smartcard.name, &s_smartcard, 0);
  109.     smartcard_p = mbr_pt(ID_SMARTCARD);
  110.     if (smartcard_p) opt_print(ID_SMARTCARD, &smartcard_p->common_opt);
  111.     mbr_query();

  112.     mbr_reg(s_pinpad.name, &s_pinpad, 0);
  113.     pinpad_p = mbr_pt(ID_PINPAD);
  114.     if (pinpad_p) opt_print(ID_PINPAD, &pinpad_p->common_opt);
  115.     mbr_query();

  116.     mbr_reg(s_screen.name, &s_screen, 0);
  117.     screen_p = mbr_pt(ID_SCREEN);
  118.     if (screen_p) opt_print(ID_SCREEN, &screen_p->common_opt);
  119.     mbr_query();

  120.     mbr_reg(s_communicate.name, &s_communicate, 0);
  121.     communicate_p = mbr_pt(ID_COMMUNICATE);
  122.     if (communicate_p) opt_print(ID_COMMUNICATE, &communicate_p->common_opt);
  123.     mbr_query();

  124.     /* unregister all devices */
  125.     printf("===unregister all devices!===\r\n");
  126.     mbr_unreg(ID_PRINTER);
  127.     mbr_query();
  128.     
  129.     mbr_unreg(ID_SMARTCARD);
  130.     mbr_query();
  131.     
  132.     mbr_unreg(ID_PINPAD);
  133.     mbr_query();

  134.     mbr_unreg(ID_PINPAD);
  135.     mbr_query();

  136.     mbr_unreg(ID_SCREEN);
  137.     mbr_query();

  138.     mbr_unreg(ID_COMMUNICATE);
  139.     mbr_query();

  140.     /* register all devices (malloc and free) */
  141.     printf("===register all devices (malloc and free)===\r\n");
  142.     
  143.     mbr_reg_malloc(PRINTER_T, ID_PRINTER);
  144.     printer_p = mbr_pt(ID_PRINTER);
  145.     if (printer_p) *printer_p=s_printer;
  146.     if (printer_p) opt_print(ID_PRINTER, &printer_p->common_opt);
  147.     mbr_query();
  148.     
  149.     mbr_reg_malloc(SMARTCART_T, ID_SMARTCARD);
  150.     smartcard_p = mbr_pt(ID_SMARTCARD);
  151.     if (smartcard_p) *smartcard_p=s_smartcard;
  152.     if (smartcard_p) opt_print(ID_SMARTCARD, &smartcard_p->common_opt);
  153.     mbr_query();    
  154.     
  155.     mbr_reg_malloc(PINPAD_T, ID_PINPAD);
  156.     pinpad_p = mbr_pt(ID_PINPAD);
  157.     if (pinpad_p) *pinpad_p=s_pinpad;
  158.     if (pinpad_p) opt_print(ID_PINPAD, &pinpad_p->common_opt);
  159.     mbr_query();
  160.     
  161.     mbr_reg_malloc(SCREEN_T, ID_SCREEN);
  162.     screen_p = mbr_pt(ID_SCREEN);
  163.     if (screen_p) *screen_p=s_screen;
  164.     if (screen_p) opt_print(ID_SCREEN, &screen_p->common_opt);
  165.     mbr_query();
  166.     
  167.     mbr_reg_malloc(COMMUNICATE_T, ID_COMMUNICATE);
  168.     communicate_p = mbr_pt(ID_COMMUNICATE);
  169.     if (communicate_p) *communicate_p=s_communicate;
  170.     if (communicate_p) opt_print(ID_COMMUNICATE, &communicate_p->common_opt);
  171.     mbr_query();
  172.     
  173.     mbr_unreg_free(PRINTER_T, ID_PRINTER);
  174.     mbr_unreg_free(SMARTCART_T, ID_SMARTCARD);
  175.     mbr_unreg_free(PINPAD_T, ID_PINPAD);
  176.     mbr_unreg_free(SCREEN_T, ID_SCREEN);
  177.     mbr_unreg_free(COMMUNICATE_T, ID_COMMUNICATE);
  178.     mbr_query();

  179.     exit_manager(&g_mgt);
  180. }

附件: manager.rar  
===========================================================================================
阅读(444) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~