C面向对象编程

    本文地址:http://www.tongxinmao.com/Article/Detail/id/113

    #ifndef _STACK_H_
    #define _STACK_H_
    #include <stddef.h>
    #ifdef __cplusplus
    extern "C" {
    #endif
    typedef struct _Validator {
        bool (* const validate)(struct _Validator *pThis, int val);
    } Validator;
    typedef struct {
        Validator base;
        const int min;
        const int max;
    } RangeValidator;
    typedef struct {
        Validator base;
        int previousValue;
    } PreviousValueValidator;
    typedef struct {
        int top;
        const size_t size;
        int * const pBuf;
        Validator * const pValidator;
    } Stack;
    bool validateRange(Validator *pThis, int val);
    bool validatePrevious(Validator *pThis, int val);
    #define newRangeValidator(min, max) \
        {{validateRange}, (min), (max)}
    #define newPreviousValueValidator \
        {{validatePrevious}, 0}
    bool push(Stack *p, int val);
    bool pop(Stack *p, int *pRet);
    #define newStack(buf) {                  \
        0, sizeof(buf) / sizeof(int), (buf), \
        NULL                                 \
    } 
    #define newStackWithValidator(buf, pValidator) { \
        0, sizeof(buf) / sizeof(int), (buf),         \
        pValidator                                   \
    }
    #ifdef __cplusplus
    }
    #endif
    #endif
    #include <stdbool.h>
    #include "stack.h"
    static bool isStackFull(const Stack *p) {
        return p->top == p->size;
    }
    static bool isStackEmpty(const Stack *p) {
        return p->top == 0;
    }
    bool validateRange(Validator *p, int val) {
        RangeValidator *pThis = (RangeValidator *)p;
        return pThis->min <= val && val <= pThis->max;
    }
    bool validatePrevious(Validator *p, int val) {
        PreviousValueValidator *pThis = (PreviousValueValidator *)p;
        if (val < pThis->previousValue) return false;
        pThis->previousValue = val;
        return true;
    }
    bool validate(Validator *p, int val) {
        if (! p) return true;
        return p->validate(p, val);
    }
    // true: 成功, false: 失敗
    bool push(Stack *p, int val) {
        if (! validate(p->pValidator, val) || isStackFull(p)) return false;
        p->pBuf[p->top++] = val;
        return true;
    }
    // true: 成功, false: 失敗
    bool pop(Stack *p, int *pRet) {
        if (isStackEmpty(p)) return false;
        *pRet = p->pBuf[--p->top];
        return true;
    }


    上一篇:C语言 FIFO(宏)
    下一篇:USB复合设备(CDC+HID)描述符