http://
vincent.blog.chinaunix.net今天基于链表写了个对象管理器,主要是管理各种模块,组件之类的,不过感觉实现得有点那个~~~
file: manager.h
===========================================================================================
- /*
- * author: vincent.cws2008@gmail.com
- */
- #ifndef _MANAGER_H_
-
#define _MANAGER_H_
-
-
#include "list.h"
-
#include "listx.h"
-
#include "stdlib.h"
-
-
#define mallocx(n) malloc(n)
-
#define freex(p) free(p)
-
#define lockx()
-
#define unlockx()
-
-
typedef struct manager_s
-
{
-
struct list_s list;
-
int (*registerx)(struct manager_s *self_p,
-
char *name_p, void *new_p, bool is_malloc);
-
void* (*unregisterx)(struct manager_s *self_p, char *name_p);
-
void* (*instancex)(struct manager_s *self_p, char* name_p);
-
void (*queryx)(struct manager_s *self_p);
-
void (*clear_all)(struct manager_s *self_p);
-
}MANAGER_T;
-
-
void init_manager(MANAGER_T *mgt_p);
-
void exit_manager(MANAGER_T *mgt_p);
-
-
#define member_reg_malloc(type,name_p,mgt) {\
-
type *new_p = (type*)mallocx(sizeof(type)); \
-
if (-1==mgt.registerx(&(mgt),(name_p),(new_p),1)) freex(new_p);\
-
}
-
-
#define member_unreg_free(type,name_p,mgt) {\
-
type *del_p = (type*)(mgt).unregisterx(&(mgt),(name_p)); \
-
if (del_p) freex(del_p);\
-
}
-
-
#define member_pt(name_p, mgt) \
-
(mgt).instancex(&(mgt),(name_p))
-
-
#define member_query(mgt) \
-
(mgt).queryx(&(mgt))
-
-
#define member_register(name_p,new_p,is_malloc,mgt) \
-
(mgt).registerx(&(mgt),(name_p),(new_p),is_malloc)
-
-
#define member_unregister(name_p,mgt) \
-
(mgt).unregisterx(&(mgt),(name_p))
-
-
-
-
#endif
file: manager.c
===========================================================================================
- /*
- * author: vincent.cws2008@gmail.com
- */
- #include "list.h"
-
#include "listx.h"
-
#include "manager.h"
-
-
#define max(a,b) (((a) > (b)) ? (a) : (b))
-
#define min(a,b) (((a) < (b)) ? (a) : (b))
-
-
static int member_register__(struct manager_s *self_p,
-
char *name_p, void *new_p, bool is_malloc)
-
{
-
NODE_T *node_p;
-
int len;
-
assert(self_p && name_p && new_p);
-
lockx();
-
if (listx_lookup(self_p->list, name_p))
-
{
-
unlockx();
-
return -1;
-
}
-
unlockx();
-
node_p = (NODE_T*)mallocx(sizeof(NODE_T));
-
assert(node_p);
-
node_p->new_p = new_p;
-
len = min(strlen(name_p),MAX_NODE_NAME-1);
-
strncpy(node_p->name, name_p, len);
-
node_p->name[len]='\0';
-
node_p->is_malloc=is_malloc;
-
lockx();
-
listx_append(self_p->list, node_p);
-
unlockx();
-
return 0;
-
}
-
-
static void* member_unregister__(struct manager_s *self_p, char *name_p)
-
{
-
NODE_T *node_p;
-
void *sgt_p;
-
assert(self_p && name_p);
-
lockx();
-
if (!(node_p=listx_lookup(self_p->list, name_p)))
-
{
-
unlockx();
-
return NULL;
-
}
-
sgt_p = node_p->new_p;
-
listx_delete(self_p->list, node_p);
-
unlockx();
-
freex(node_p);
-
return sgt_p;
-
}
-
-
static void* member_instance__(struct manager_s *self_p, char* name_p)
-
{
-
NODE_T *node_p;
-
assert(self_p && name_p);
-
lockx();
-
if (!(node_p=listx_lookup(self_p->list, name_p)))
-
{
-
unlockx();
-
return NULL;
-
}
-
unlockx();
-
return node_p->new_p;
-
}
-
-
static void member_query__(struct manager_s *self_p)
-
{
-
assert(self_p);
-
lockx();
-
listx_print(self_p->list);
-
unlockx();
-
}
-
-
static void member_clear_all__(struct manager_s *self_p)
-
{
-
struct list_head *list_p;
-
NODE_T *node_p;
-
NODE_T *temp_p;
-
assert(self_p);
-
lockx();
-
list_for_each_safe(list_p, temp_p, &((self_p->list).head))
-
{
-
node_p = list_entry(list_p, NODE_T, list);
-
if (node_p->new_p && node_p->is_malloc)
-
{
-
freex(node_p->new_p);
-
//list_p = list_p->next
-
listx_delete(self_p->list, node_p);
-
member_query__(self_p);
-
}
-
}
-
unlockx();
-
}
-
-
-
extern void init_manager(MANAGER_T *mgt_p)
-
{
-
assert(mgt_p);
-
listx_init(&mgt_p->list);
-
*(mgt_p->registerx) = member_register__;
-
*(mgt_p->unregisterx) = member_unregister__;
-
*(mgt_p->instancex) = member_instance__;
-
*(mgt_p->queryx) = member_query__;
-
*(mgt_p->clear_all) = member_clear_all__;
-
}
-
-
extern void exit_manager(MANAGER_T *mgt_p)
-
{
-
assert(mgt_p);
-
-
mgt_p->clear_all(mgt_p);
-
}
file: listx.h
===========================================================================================
- /*
- * author: vincent.cws2008@gmail.com
- */
- #ifndef _LIST_X_H_
-
#define _LIST_X_H_
-
-
#define assert(x)
-
-
extern struct list_head;
-
-
#define MAX_NODE_NAME 32
-
-
#define bool int
-
-
typedef struct node_s{
-
struct list_head list;
-
void *new_p;
-
char name[MAX_NODE_NAME];
-
bool is_malloc;
-
}NODE_T;
-
-
typedef struct list_s{
-
struct list_head head;
-
void (*prepend)(struct list_s *self_p, NODE_T *new_p);
-
void (*append)(struct list_s *self_p, NODE_T *new_p);
-
void (*delete)(struct list_s *self_p, NODE_T *node_p);
-
void (*replace)(struct list_s *self_p, NODE_T *old_p, NODE_T *new_p);
-
NODE_T* (*lookup)(struct list_s *self_p, char *name_p);
-
void (*print)(struct list_s *self_p);
-
}LIST_T;
-
-
void listx_init(LIST_T *list_p);
-
-
#define listx_prepend(list,node_p) ((list).prepend(&(list), node_p))
-
#define listx_append(list,node_p) ((list).append(&(list), node_p))
-
#define listx_print(list) ((list).print(&(list)))
-
#define listx_replace(list,old_p,new_p) ((list).replace(&(list), old_p, new_p))
-
#define listx_lookup(list,x) ((list).lookup(&(list), x))
-
#define listx_delete(list,node_p) ((list).delete(&(list), node_p))
-
-
#endif
file: listx.c
===========================================================================================
- /*
- * author: vincent.cws2008@gmail.com
- */
- #include "list.h"
-
#include "listx.h"
-
-
static void node_prepend(struct list_s *self_p, NODE_T *new_p);
-
static void node_append(struct list_s *self_p, NODE_T *new_p);
-
static void node_delete(struct list_s *self_p, NODE_T *node_p);
-
static void node_replace(struct list_s *self_p, NODE_T *old_p, NODE_T *new_p);
-
static NODE_T* node_lookup(struct list_s *self_p, char *name_p);
-
static void node_print(struct list_s *self_p);
-
-
static void node_prepend(struct list_s *self_p, NODE_T *new_p)
-
{
-
assert(self_p && new_p);
-
list_add(&new_p->list, &(self_p->head));
-
}
-
-
static void node_append(struct list_s *self_p, NODE_T *new_p)
-
{
-
assert(self_p && new_p);
-
list_add_tail(&new_p->list, &(self_p->head));
-
}
-
-
static void node_delete(struct list_s *self_p, NODE_T *node_p)
-
{
-
assert(self_p && node_p);
-
list_del(&node_p->list);
-
}
-
-
static void node_replace(struct list_s *self_p, NODE_T *old_p, NODE_T *new_p)
-
{
-
assert(self_p && old_p && new_p);
-
list_replace(&old_p->list, &new_p->list);
-
}
-
-
static NODE_T* node_lookup(struct list_s *self_p, char *name_p)
-
{
-
struct list_head *list_p;
-
NODE_T *node_p;
-
assert(self_p && name_p);
-
list_for_each(list_p, &(self_p->head))
-
{
-
node_p = list_entry(list_p, NODE_T, list);
-
if (!strcmp(node_p->name, name_p))
-
return node_p;
-
}
-
return NULL;
-
}
-
-
static void node_print(struct list_s *self_p)
-
{
-
struct list_head *list_p;
-
NODE_T *node_p;
-
list_for_each(list_p, &(self_p->head))
-
{
-
node_p = list_entry(list_p, NODE_T, list);
-
printf("node name=%s --> ", node_p->name);
-
}
-
printf("\r\n");
-
}
-
-
extern void listx_init(LIST_T *list_p)
-
{
-
assert(list_p);
-
INIT_LIST_HEAD(&list_p->head);
-
*(list_p->prepend) = node_prepend;
-
*(list_p->append) = node_append;
-
*(list_p->delete) = node_delete;
-
*(list_p->replace) = node_replace;
-
*(list_p->lookup) = node_lookup;
-
*(list_p->print) = node_print;
-
}
file: main.c
===========================================================================================
- /*
- * author: vincent.cws2008@gmail.com
- */
- #include "stdio.h"
-
#include "manager.h"
-
-
#ifndef MAX_PATH
-
#define MAX_PATH 260
-
#endif
-
-
static int read()
-
{
-
printf("read ok!\r\n");
-
return 0;
-
}
-
static int write()
-
{
-
printf("write ok!\r\n");
-
return 0;
-
}
-
static int open()
-
{
-
printf("open ok!\r\n");
-
return 0;
-
}
-
static int close()
-
{
-
printf("close ok!\r\n");
-
return 0;
-
}
-
-
typedef struct common_opt_s {
-
int (*read)();
-
int (*write)();
-
int (*open)();
-
int (*close)();
-
}COMMON_OPT_T;
-
-
typedef struct printer_s{
-
char name[MAX_PATH];
-
COMMON_OPT_T common_opt;
-
void *other_p;
-
}PRINTER_T;
-
-
typedef struct smartcard_s{
-
char name[MAX_PATH];
-
COMMON_OPT_T common_opt;
-
void *other_p;
-
}SMARTCART_T;
-
-
typedef struct pinpad_s{
-
char name[MAX_PATH];
-
COMMON_OPT_T common_opt;
-
void *other_p;
-
}PINPAD_T;
-
-
typedef struct screen_s{
-
char name[MAX_PATH];
-
COMMON_OPT_T common_opt;
-
void *other_p;
-
}SCREEN_T;
-
-
typedef struct communicate_s{
-
char name[MAX_PATH];
-
COMMON_OPT_T common_opt;
-
void *other_p;
-
}COMMUNICATE_T;
-
-
-
MANAGER_T g_mgt;
-
-
#define mbr_reg_malloc(type,name_p) \
-
member_reg_malloc(type,name_p,g_mgt,)
-
#define mbr_unreg_free(type,name_p) \
-
member_unreg_free(type,name_p,g_mgt)
-
#define mbr_pt(name_p) \
-
member_pt(name_p, g_mgt)
-
#define mbr_query() \
-
member_query(g_mgt)
-
#define mbr_reg(name_p,new_p,is_malloc) \
-
member_register(name_p,new_p,is_malloc,g_mgt)
-
#define mbr_unreg(name_p) \
-
member_unregister(name_p,g_mgt)
-
-
-
void opt_print(const char* prompt_p, COMMON_OPT_T *comom_opt_p)
-
{
-
assert(comom_opt_p && prompt_p);
-
printf("===%s===\r\n", prompt_p);
-
comom_opt_p->open();
-
comom_opt_p->read();
-
comom_opt_p->write();
-
comom_opt_p->close();
-
}
-
-
#define ID_PRINTER "printer"
-
#define ID_SMARTCARD "smartcard"
-
#define ID_PINPAD "pinpad"
-
#define ID_SCREEN "screen"
-
#define ID_COMMUNICATE "communicate"
-
-
#define OPTS_INIT {read,write,open,close}
-
-
void main()
-
{
-
PRINTER_T* printer_p;
-
SMARTCART_T* smartcard_p;
-
PINPAD_T* pinpad_p;
-
SCREEN_T* screen_p;
-
COMMUNICATE_T* communicate_p;
-
-
static PRINTER_T s_printer={ID_PRINTER, OPTS_INIT, 0};
-
static SMARTCART_T s_smartcard={ID_SMARTCARD, OPTS_INIT, 0};
-
static PINPAD_T s_pinpad={ID_PINPAD, OPTS_INIT, 0};
-
static SCREEN_T s_screen={ID_SCREEN, OPTS_INIT, 0};
-
static COMMUNICATE_T s_communicate={ID_COMMUNICATE, OPTS_INIT, 0};
-
-
/* init manager */
-
init_manager(&g_mgt);
-
-
/* register all devices (static) */
-
mbr_reg(s_printer.name, &s_printer, 0);
-
printer_p = mbr_pt(ID_PRINTER);
-
if (printer_p) opt_print(ID_PRINTER, &printer_p->common_opt);
-
mbr_query();
-
-
mbr_reg(s_smartcard.name, &s_smartcard, 0);
-
smartcard_p = mbr_pt(ID_SMARTCARD);
-
if (smartcard_p) opt_print(ID_SMARTCARD, &smartcard_p->common_opt);
-
mbr_query();
-
-
mbr_reg(s_pinpad.name, &s_pinpad, 0);
-
pinpad_p = mbr_pt(ID_PINPAD);
-
if (pinpad_p) opt_print(ID_PINPAD, &pinpad_p->common_opt);
-
mbr_query();
-
-
mbr_reg(s_screen.name, &s_screen, 0);
-
screen_p = mbr_pt(ID_SCREEN);
-
if (screen_p) opt_print(ID_SCREEN, &screen_p->common_opt);
-
mbr_query();
-
-
mbr_reg(s_communicate.name, &s_communicate, 0);
-
communicate_p = mbr_pt(ID_COMMUNICATE);
-
if (communicate_p) opt_print(ID_COMMUNICATE, &communicate_p->common_opt);
-
mbr_query();
-
-
/* unregister all devices */
-
printf("===unregister all devices!===\r\n");
-
mbr_unreg(ID_PRINTER);
-
mbr_query();
-
-
mbr_unreg(ID_SMARTCARD);
-
mbr_query();
-
-
mbr_unreg(ID_PINPAD);
-
mbr_query();
-
-
mbr_unreg(ID_PINPAD);
-
mbr_query();
-
-
mbr_unreg(ID_SCREEN);
-
mbr_query();
-
-
mbr_unreg(ID_COMMUNICATE);
-
mbr_query();
-
-
/* register all devices (malloc and free) */
-
printf("===register all devices (malloc and free)===\r\n");
-
-
mbr_reg_malloc(PRINTER_T, ID_PRINTER);
-
printer_p = mbr_pt(ID_PRINTER);
-
if (printer_p) *printer_p=s_printer;
-
if (printer_p) opt_print(ID_PRINTER, &printer_p->common_opt);
-
mbr_query();
-
-
mbr_reg_malloc(SMARTCART_T, ID_SMARTCARD);
-
smartcard_p = mbr_pt(ID_SMARTCARD);
-
if (smartcard_p) *smartcard_p=s_smartcard;
-
if (smartcard_p) opt_print(ID_SMARTCARD, &smartcard_p->common_opt);
-
mbr_query();
-
-
mbr_reg_malloc(PINPAD_T, ID_PINPAD);
-
pinpad_p = mbr_pt(ID_PINPAD);
-
if (pinpad_p) *pinpad_p=s_pinpad;
-
if (pinpad_p) opt_print(ID_PINPAD, &pinpad_p->common_opt);
-
mbr_query();
-
-
mbr_reg_malloc(SCREEN_T, ID_SCREEN);
-
screen_p = mbr_pt(ID_SCREEN);
-
if (screen_p) *screen_p=s_screen;
-
if (screen_p) opt_print(ID_SCREEN, &screen_p->common_opt);
-
mbr_query();
-
-
mbr_reg_malloc(COMMUNICATE_T, ID_COMMUNICATE);
-
communicate_p = mbr_pt(ID_COMMUNICATE);
-
if (communicate_p) *communicate_p=s_communicate;
-
if (communicate_p) opt_print(ID_COMMUNICATE, &communicate_p->common_opt);
-
mbr_query();
-
-
mbr_unreg_free(PRINTER_T, ID_PRINTER);
-
mbr_unreg_free(SMARTCART_T, ID_SMARTCARD);
-
mbr_unreg_free(PINPAD_T, ID_PINPAD);
-
mbr_unreg_free(SCREEN_T, ID_SCREEN);
-
mbr_unreg_free(COMMUNICATE_T, ID_COMMUNICATE);
-
mbr_query();
-
-
exit_manager(&g_mgt);
-
}
附件:
manager.rar ===========================================================================================
阅读(444) | 评论(0) | 转发(0) |