本文最后更新于 2024-10-12,文章内容可能已经过时。

#include <stdio.h>
#include <stdbool.h>
#include <malloc.h>
#include <string.h>

//队列
typedef struct tagSBSPQueueBase
{
    int save;
    int get;
    int num;
    int data[1024];
}SBSPQueueBase;

static struct tagSBSPQueueBase * creatBSPQueueBase(void) {
    struct tagSBSPQueueBase *pQueue = (SBSPQueueBase *)malloc(sizeof(SBSPQueueBase));
    pQueue->get = 0;
    pQueue->save = 0;
    pQueue->num = 0;
    memset(pQueue->data, 0xEE ,sizeof ((pQueue->data)));
    return pQueue;
}

typedef enum {
    EPT_INVALID = 0,
    EPT_HEX = 1,
    EPT_JSON = 2,
}EProtocolType;

/*****************************************************
 *
 *                      封装
 *
 ***************************************************/
struct tagSProtocol;
typedef bool (*function)(struct tagSProtocol *me);
typedef struct tagSProtocol
{
    EProtocolType   type;
    SBSPQueueBase*  pQueue;

    function        parse;
}SProtocol;

//初始化
void ProtocolBaseInit(struct tagSProtocol * me, EProtocolType type, function parseFun, struct tagSBSPQueueBase *pQueue) {
    (me->type != EPT_INVALID) ? \
        (me->type = type) : (printf("Error:Type input(%d) Error!\r\n", type));
    me->parse = parseFun;
    (pQueue != NULL) ? (me->pQueue = pQueue) : ((void)0);
}

//构造
struct tagSProtocol * creatProtocolBase(EProtocolType type, function parseFun) {

    struct tagSProtocol *me = (struct tagSProtocol *)malloc(sizeof(struct tagSProtocol));
    SBSPQueueBase *pQueue = creatBSPQueueBase();
    if (me == NULL) {
        printf("Error: nullptr!\r\n");
    }

    (me->type != EPT_INVALID) ? \
        (me->type = type) : (printf("Error:Type input(%d) Error!\r\n", type));
    me->parse = parseFun;

    me->pQueue = pQueue;
    return me;
}

//析构
bool deleteProtocolBase(struct tagSProtocol *me) {
    if (me == NULL) {
        printf("delete target is a NULL pointer!\r\n");
        return true;
    }
    free(me->pQueue);
    me->pQueue = NULL;

    free(me);
    me = NULL;

    return true;
}

/*****************************************************
 *
 *                    继承+多态
 *
 ***************************************************/
typedef struct tagSProtocolJSON
{
    SProtocol base;

    bool protocolState;
} SProtocolJSON;

bool parseJson(struct tagSProtocol *me) {
    printf("Log: in JSON Parse,type=%d\r\n", me->type);
    return true;
}

//构造
struct tagSProtocolJSON * creatProtocolJSON(EProtocolType type, function parseFun) {
    struct tagSProtocolJSON *me = (struct tagSProtocolJSON *)malloc(sizeof(struct tagSProtocolJSON));
    ProtocolBaseInit(&me->base, type, parseFun, creatBSPQueueBase());

    me->protocolState = 1;
    return me;
}

void deletcProtocolJSON(struct tagSProtocolJSON **me) {
    free((*me)->base.pQueue);
    (*me)->base.pQueue = NULL;
    free(*me);
    (*me) = NULL;
}


typedef struct tagSProtocolHex
{
    SProtocol base;

    bool heartTime;
} SProtocolHex;

bool parseHex(struct tagSProtocol *me) {
    printf("Log: in HEX Parse,type=%d\r\n", me->type);
    return true;
}

//构造
struct tagSProtocolHex * creatProtocolHex(EProtocolType type, function parseFun) {
    struct tagSProtocolHex *me = (struct tagSProtocolHex *)malloc(sizeof(struct tagSProtocolHex));
    ProtocolBaseInit(&me->base, type, parseFun, creatBSPQueueBase());

    me->heartTime = 0;
    return me;
}

void deletcProtocolHex(struct tagSProtocolHex **me) {
    free((*me)->base.pQueue);
    (*me)->base.pQueue = NULL;
    free(*me);
    (*me) = NULL;
}

int main(int argc,char **argv){
    printf(" start argc=%d argv0=%s\r\n", argc, argv[0]);

    //封装演示,创建协议基类(成员变量赋值、成员方法指定具体实现)
    SProtocol *base = creatProtocolBase(EPT_HEX, NULL);
    printf("base type is %d\r\n", base->type);
    deleteProtocolBase(base);

    //继承演示,SProtocolJSON和SProtocolHex继承自SProtocol,除均拥有SProtocol的属性外,还拥有各自独立的属性
    SProtocolJSON *pJson = creatProtocolJSON(EPT_JSON, parseJson);
    pJson->base.parse(&pJson->base);


    SProtocolHex *pHex = creatProtocolHex(EPT_HEX, parseHex);
    pHex->base.parse(&pHex->base);


    //多态演示
    SProtocol *pBase = (SProtocol *)pJson;
    printf(" type1=%d\r\n",pBase->type);

    pBase = (SProtocol *)pHex;
    printf(" type1=%d\r\n",pBase->type);

    deletcProtocolJSON(&pJson);
    deletcProtocolHex(&pHex);
    printf(" end \r\n");
}