【代碼清單】 typedef.h [html] #ifndef __TYPEDEF_H__ #define __TYPEDEF_H__ #include <stdio.h> #include <stdlib.h> #ifdef __cplusplus extern "C" { #endif typedef enum _Ret { RET_OK, RET_FAIL }Ret; #define return_if_fail(p)\ if(!(p)){\ printf("%s:%d Warning:"#p"Failed\n",__func__,__LINE__);\ return;} #define return_val_if_fail(p, ret)\ if(!(p)){\ printf("%s:%d Warning:"#p"Failed\n",__func__,__LINE__);\ return (ret);} #define SAFE_FREE(p) if(p != NULL){free(p); p = NULL;} #ifdef __cplusplus } #endif #endif #ifndef __TYPEDEF_H__ #define __TYPEDEF_H__ #include <stdio.h> #include <stdlib.h> #ifdef __cplusplus extern "C" { #endif typedef enum _Ret { RET_OK, RET_FAIL }Ret; #define return_if_fail(p)\ if(!(p)){\ printf("%s:%d Warning:"#p"Failed\n",__func__,__LINE__);\ return;} #define return_val_if_fail(p, ret)\ if(!(p)){\ printf("%s:%d Warning:"#p"Failed\n",__func__,__LINE__);\ return (ret);} #define SAFE_FREE(p) if(p != NULL){free(p); p = NULL;} #ifdef __cplusplus } #endif #endif player.h [html] #ifndef __PLAYER_H__ #define __PLAYER_H__ #include "typedef.h" #ifdef __cplusplus extern "C" { #endif struct _Player; typedef struct _Player Player; struct _Player { char *name; void (*attack)(void *player); void (*defend)(void *player); void (*destroy)(void *player); }; static inline void player_attack(Player *thiz) { return_if_fail(thiz != NULL); if(thiz->attack != NULL) { thiz->attack(thiz); } } static inline void player_defend(Player *thiz) { return_if_fail(thiz != NULL); if(thiz->defend != NULL) { thiz->defend(thiz); } } static inline void player_destroy(Player *thiz) { return_if_fail(thiz != NULL); if(thiz->destroy != NULL) { thiz->destroy(thiz); } } #ifdef __cplusplus } #endif #endif #ifndef __PLAYER_H__ #define __PLAYER_H__ #include "typedef.h" #ifdef __cplusplus extern "C" { #endif struct _Player; typedef struct _Player Player; struct _Player { char *name; void (*attack)(void *player); void (*defend)(void *player); void (*destroy)(void *player); }; static inline void player_attack(Player *thiz) { return_if_fail(thiz != NULL); if(thiz->attack != NULL) { thiz->attack(thiz); } } static inline void player_defend(Player *thiz) { return_if_fail(thiz != NULL); if(thiz->defend != NULL) { thiz->defend(thiz); } } static inline void player_destroy(Player *thiz) { return_if_fail(thiz != NULL); if(thiz->destroy != NULL) { thiz->destroy(thiz); } } #ifdef __cplusplus } #endif #endif forwardsplayer.h [html] #ifndef __FORWARDSPLAYER_H__ #define __FORWARDSPLAYER_H__ #include "player.h" #ifdef __cplusplus extern "C" { #endif struct _Forwards; typedef struct _Forwards Forwards; struct _Forwards { Player player; }; Forwards *ForwardsCreate(char *name); #ifdef __cplusplus } #endif #endif #ifndef __FORWARDSPLAYER_H__ #define __FORWARDSPLAYER_H__ #include "player.h" #ifdef __cplusplus extern "C" { #endif struct _Forwards; typedef struct _Forwards Forwards; struct _Forwards { Player player; }; Forwards *ForwardsCreate(char *name); #ifdef __cplusplus } #endif #endif forwardsplayer.c [html] #include <stdio.h> #include <stdlib.h> #include "forwardsplayer.h" static void forwards_attack(void *thiz) { printf("前鋒 %s 進攻\n", ((Forwards *)thiz)->player.name); } static void forwards_defend(void *thiz) { printf("前鋒 %s 防守\n", ((Forwards *)thiz)->player.name); } static void forwards_destroy(void *thiz) { return_if_fail(thiz != NULL); SAFE_FREE(thiz); } Forwards *ForwardsCreate(char *name) { Forwards *thiz = malloc(sizeof(Forwards)); if(thiz != NULL) { thiz->player.name = name; thiz->player.attack = forwards_attack; thiz->player.defend = forwards_defend; thiz->player.destroy = forwards_destroy; } //printf("%s\n", thiz->player->name); return thiz; } #include <stdio.h> #include <stdlib.h> #include "forwardsplayer.h" static void forwards_attack(void *thiz) { printf("前鋒 %s 進攻\n", ((Forwards *)thiz)->player.name); } static void forwards_defend(void *thiz) { printf("前鋒 %s 防守\n", ((Forwards *)thiz)->player.name); } static void forwards_destroy(void *thiz) { return_if_fail(thiz != NULL); SAFE_FREE(thiz); } Forwards *ForwardsCreate(char *name) { Forwards *thiz = malloc(sizeof(Forwards)); if(thiz != NULL) { thiz->player.name = name; thiz->player.attack = forwards_attack; thiz->player.defend = forwards_defend; thiz->player.destroy = forwards_destroy; } //printf("%s\n", thiz->player->name); return thiz; } centerplayer.h [html] #ifndef __CENTERPLAYER_H__ #define __CENTERPLAYER_H__ #include "player.h" #ifdef __cplusplus extern "C" { #endif struct _Center; typedef struct _Center Center; struct _Center { Player player; }; Center *CenterCreate(char *name); #ifdef __cplusplus } #endif #endif #ifndef __CENTERPLAYER_H__ #define __CENTERPLAYER_H__ #include "player.h" #ifdef __cplusplus extern "C" { #endif struct _Center; typedef struct _Center Center; struct _Center { Player player; }; Center *CenterCreate(char *name); #ifdef __cplusplus } #endif #endif centerplayer.c [html] #include <stdio.h> #include <stdlib.h> #include "centerplayer.h" static void center_attack(void *thiz) { printf("中鋒 %s 進攻\n", ((Center *)thiz)->player.name); } static void center_defend(void *thiz) { printf("中鋒 %s 防守\n", ((Center *)thiz)->player.name); } static void center_destroy(void *thiz) { return_if_fail(thiz != NULL); SAFE_FREE(thiz); } Center *CenterCreate(char *name) { Center *thiz = malloc(sizeof(Center)); if(thiz != NULL) { thiz->player.name = name; thiz->player.attack = center_attack; thiz->player.defend = center_defend; thiz->player.destroy = center_destroy; } return thiz; } #include <stdio.h> #include <stdlib.h> #include "centerplayer.h" static void center_attack(void *thiz) { printf("中鋒 %s 進攻\n", ((Center *)thiz)->player.name); } static void center_defend(void *thiz) { printf("中鋒 %s 防守\n", ((Center *)thiz)->player.name); } static void center_destroy(void *thiz) { return_if_fail(thiz != NULL); SAFE_FREE(thiz); } Center *CenterCreate(char *name) { Center *thiz = malloc(sizeof(Center)); if(thiz != NULL) { thiz->player.name = name; thiz->player.attack = center_attack; thiz->player.defend = center_defend; thiz->player.destroy = center_destroy; } return thiz; } guardsplayer.h [html] v #ifndef __GUARDSPLAYER_H__ #define __GUARDSPLAYER_H__ #include "player.h" #ifdef __cplusplus extern "C" { #endif struct _Guards; typedef struct _Guards Guards; struct _Guards { Player player; }; Guards *GuardsCreate(char *name); #ifdef __cplusplus } #endif #endif #ifndef __GUARDSPLAYER_H__ #define __GUARDSPLAYER_H__ #include "player.h" #ifdef __cplusplus extern "C" { #endif struct _Guards; typedef struct _Guards Guards; struct _Guards { Player player; }; Guards *GuardsCreate(char *name); #ifdef __cplusplus } #endif #endif guardsplayer.c [html] #include <stdio.h> #include <stdlib.h> #include "guardsplayer.h" static void guards_attack(void *thiz) { printf("後衛 %s 進攻\n", ((Guards *)thiz)->player.name); } static void guards_defend(void *thiz) { printf("後衛 %s 防守\n", ((Guards *)thiz)->player.name); } static void guards_destroy(void *thiz) { return_if_fail(thiz != NULL); SAFE_FREE(thiz); } Guards *GuardsCreate(char *name) { Guards *thiz = malloc(sizeof(Guards)); if(thiz != NULL) { thiz->player.name = name; thiz->player.attack = guards_attack; thiz->player.defend = guards_defend; thiz->player.destroy = guards_destroy; } return thiz; } #include <stdio.h> #include <stdlib.h> #include "guardsplayer.h" static void guards_attack(void *thiz) { printf("後衛 %s 進攻\n", ((Guards *)thiz)->player.name); } static void guards_defend(void *thiz) { printf("後衛 %s 防守\n", ((Guards *)thiz)->player.name); } static void guards_destroy(void *thiz) { return_if_fail(thiz != NULL); SAFE_FREE(thiz); } Guards *GuardsCreate(char *name) { Guards *thiz = malloc(sizeof(Guards)); if(thiz != NULL) { thiz->player.name = name; thiz->player.attack = guards_attack; thiz->player.defend = guards_defend; thiz->player.destroy = guards_destroy; } return thiz; } foreigncenterplayer.h [html] #ifndef __FORIGNCENTERPLAYER_H__ #define __FORIGNCENTERPLAYER_H__ #include "typedef.h" #ifdef __cplusplus extern "C" { #endif struct _ForeignCenter; typedef struct _ForeignCenter ForeignCenter; struct _ForeignCenter { char *name; void (*fattack)(void *player); void (*fdefend)(void *player); void (*fdestroy)(void *player); }; void foreign_center_attack(void *thiz); void foreign_center_defend(void *thiz); void foreign_center_destroy(void *thiz); #ifdef __cplusplus } #endif #endif #ifndef __FORIGNCENTERPLAYER_H__ #define __FORIGNCENTERPLAYER_H__ #include "typedef.h" #ifdef __cplusplus extern "C" { #endif struct _ForeignCenter; typedef struct _ForeignCenter ForeignCenter; struct _ForeignCenter { char *name; void (*fattack)(void *player); void (*fdefend)(void *player); void (*fdestroy)(void *player); }; void foreign_center_attack(void *thiz); void foreign_center_defend(void *thiz); void foreign_center_destroy(void *thiz); #ifdef __cplusplus } #endif #endif foreigncenterplayer.c [html] #include <stdio.h> #include <stdlib.h> #include "foreigncenterplayer.h" void foreign_center_attack(void *thiz) { printf("外籍中鋒 %s 進攻\n", ((ForeignCenter *)thiz)->name); } void foreign_center_defend(void *thiz) { printf("外籍中鋒 %s 防守\n", ((ForeignCenter *)thiz)->name); } void foreign_center_destroy(void *thiz) { return_if_fail(thiz != NULL); SAFE_FREE(thiz); } static ForeignCenter *ForeignCenterCreate(char *name) { ForeignCenter *thiz = malloc(sizeof(ForeignCenter)); if(thiz != NULL) { thiz->name = name; thiz->fattack = foreign_center_attack; thiz->fdefend = foreign_center_defend; thiz->fdestroy = foreign_center_destroy; } return thiz; } #include <stdio.h> #include <stdlib.h> #include "foreigncenterplayer.h" void foreign_center_attack(void *thiz) { printf("外籍中鋒 %s 進攻\n", ((ForeignCenter *)thiz)->name); } void foreign_center_defend(void *thiz) { printf("外籍中鋒 %s 防守\n", ((ForeignCenter *)thiz)->name); } void foreign_center_destroy(void *thiz) { return_if_fail(thiz != NULL); SAFE_FREE(thiz); } static ForeignCenter *ForeignCenterCreate(char *name) { ForeignCenter *thiz = malloc(sizeof(ForeignCenter)); if(thiz != NULL) { thiz->name = name; thiz->fattack = foreign_center_attack; thiz->fdefend = foreign_center_defend; thiz->fdestroy = foreign_center_destroy; } return thiz; } foreigncenteradapter.h [html] #ifndef __FORIGNCENTERADAPTER_H__ #define __FORIGNCENTERADAPTER_H__ #include "player.h" #ifdef __cplusplus extern "C" { #endif struct _ForeignCenterAdapter; typedef struct _ForeignCenterAdapter ForeignCenterAdapter; struct _ForeignCenterAdapter { Player player; }; ForeignCenterAdapter *ForeignCenterAdapterCreate(char *name); #ifdef __cplusplus } #endif #endif #ifndef __FORIGNCENTERADAPTER_H__ #define __FORIGNCENTERADAPTER_H__ #include "player.h" #ifdef __cplusplus extern "C" { #endif struct _ForeignCenterAdapter; typedef struct _ForeignCenterAdapter ForeignCenterAdapter; struct _ForeignCenterAdapter { Player player; }; ForeignCenterAdapter *ForeignCenterAdapterCreate(char *name); #ifdef __cplusplus } #endif #endif foreigncenteradapter.c [html] #include <stdio.h> #include <stdlib.h> #include "foreigncenterplayer.h" #include "foreigncenteradapter.h" static void foreign_center_adapter_attack(void *thiz) { foreign_center_attack(thiz); } static void foreign_center_adapter_defend(void *thiz) { foreign_center_defend(thiz); } static void foreign_center_adapter_destroy(void *thiz) { foreign_center_destroy(thiz); } ForeignCenterAdapter *ForeignCenterAdapterCreate(char *name) { ForeignCenterAdapter *thiz = malloc(sizeof(ForeignCenterAdapter)); if(thiz != NULL) { thiz->player.name = name; thiz->player.attack = foreign_center_adapter_attack; thiz->player.defend = foreign_center_adapter_defend; thiz->player.destroy = foreign_center_adapter_destroy; } return thiz; } #include <stdio.h> #include <stdlib.h> #include "foreigncenterplayer.h" #include "foreigncenteradapter.h" static void foreign_center_adapter_attack(void *thiz) { foreign_center_attack(thiz); } static void foreign_center_adapter_defend(void *thiz) { foreign_center_defend(thiz); } static void foreign_center_adapter_destroy(void *thiz) { foreign_center_destroy(thiz); } ForeignCenterAdapter *ForeignCenterAdapterCreate(char *name) { ForeignCenterAdapter *thiz = malloc(sizeof(ForeignCenterAdapter)); if(thiz != NULL) { thiz->player.name = name; thiz->player.attack = foreign_center_adapter_attack; thiz->player.defend = foreign_center_adapter_defend; thiz->player.destroy = foreign_center_adapter_destroy; } return thiz; } test.c [html] #include <stdio.h> #include <stdlib.h> #include "player.h" #include "forwardsplayer.h" #include "centerplayer.h" #include "guardsplayer.h" #include "foreigncenteradapter.h" int main(int argc, char **argv) { Player *a = (Player *)CenterCreate("姚明"); player_attack(a); Player *b = (Player *)ForeignCenterAdapterCreate("巴蒂爾"); player_defend(b); player_attack(b); player_destroy(a); player_destroy(b); return 0; } #include <stdio.h> #include <stdlib.h> #include "player.h" #include "forwardsplayer.h" #include "centerplayer.h" #include "guardsplayer.h" #include "foreigncenteradapter.h" int main(int argc, char **argv) { Player *a = (Player *)CenterCreate("姚明"); player_attack(a); Player *b = (Player *)ForeignCenterAdapterCreate("巴蒂爾"); player_defend(b); player_attack(b); player_destroy(a); player_destroy(b); return 0; } Makefile [html] exe:=test temp := $(wildcard test *~) all:test.c player.h foreigncenteradapter.h foreigncenteradapter.c foreigncenterplayer.h foreigncenterplayer.c forwardsplayer.h forwardsplayer.c centerplayer.h centerplayer.c guardsplayer.h guardsplayer.c typedef.h gcc -g $^ -o $(exe) clean: rm $(temp) exe:=test temp := $(wildcard test *~) all:test.c player.h foreigncenteradapter.h foreigncenteradapter.c foreigncenterplayer.h foreigncenterplayer.c forwardsplayer.h forwardsplayer.c centerplayer.h centerplayer.c guardsplayer.h guardsplayer.c typedef.h gcc -g $^ -o $(exe) clean: rm $(temp)