适配器模式C语言实现
【代码清单】typedef.h[html]#ifndef __TYPEDEF_H__#define __TYPEDEF_H__#include <stdio.h>#include <stdlib.h>#ifdef __cplusplusextern "C" {#endiftypedef 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 __cplusplusextern "C" {#endiftypedef 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#endifplayer.h[html]#ifndef __PLAYER_H__#define __PLAYER_H__#include "typedef.h"#ifdef __cplusplusextern "C" {#endifstruct _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 __cplusplusextern "C" {#endifstruct _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#endifforwardsplayer.h[html]#ifndef __FORWARDSPLAYER_H__#define __FORWARDSPLAYER_H__#include "player.h"#ifdef __cplusplusextern "C" {#endifstruct _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 __cplusplusextern "C" {#endifstruct _Forwards;typedef struct _Forwards Forwards;struct _Forwards{Player player;};Forwards *ForwardsCreate(char *name);#ifdef __cplusplus}#endif#endifforwardsplayer.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 v补充:软件开发 , C语言 ,
上一个:关于C语言指针
下一个:C语言教案——基础知识
- 更多C/C++疑问解答:
- 关于c++的cout输出的问题。
- 在学校里学过C和C++,不过学的很一般,现在自学C#,会不会很难?
- 全国计算机二级C语言笔试题
- 已知某树有2个2度结点,3个3度结点,4个4度结点,问有几个叶子结点?
- c++数据结构内部排序问题,整数排序
- 2012九月计算机二级C语言全国题库,,急求急求
- 如果assert只有一个字符串作为参数,是什么意思呢?
- C语言中,哪些运算符具有左结合性,哪些具有右结合性,帮忙总结下,谢谢了!
- 为什么用结构体编写的程序输入是,0输不出来啊~~~
- 将IEEE—754的十六进制转化为十进制浮点类型,用C或C++都行,多谢各位大侠啊,非常感谢!
- 为什么这个程序求不出公式?
- 这个链表倒置的算法请大家分析下
- c语言函数库调用
- C语言unsigned int纠错
- C语言快排求解啊