庖丁解牛——代码段分析

下面这段代码来源于opencv的头文件。

#define CV_TREE_NODE_FIELDS(node_type) \

    int       flags;             /* Miscellaneous flags.     */      \
    int       header_size;       /* Size of sequence header. */      \
    struct    node_type* h_prev; /* Previous sequence.       */      \
    struct    node_type* h_next; /* Next sequence.           */      \
    struct    node_type* v_prev; /* 2nd previous sequence.   */      \
    struct    node_type* v_next  /* 2nd next sequence.       */

/*
   Read/Write sequence.
   Elements can be dynamically inserted to or deleted from the sequence.
*/
#define CV_SEQUENCE_FIELDS()                                              \
    CV_TREE_NODE_FIELDS(CvSeq);                                           \
    int       total;          /* Total number of elements.            */  \
    int       elem_size;      /* Size of sequence element in bytes.   */  \
    schar*    block_max;      /* Maximal bound of the last block.     */  \
    schar*    ptr;            /* Current write pointer.               */  \
    int       delta_elems;    /* Grow seq this many at a time.        */  \
    CvMemStorage* storage;    /* Where the seq is stored.             */  \
    CvSeqBlock* free_blocks;  /* Free blocks list.                    */  \
    CvSeqBlock* first;        /* Pointer to the first sequence block. */

typedef struct CvSeq
{
    CV_SEQUENCE_FIELDS()
}
CvSeq;

这段代码的精妙之处在于将指针进行了封装  CV_TREE_NODE_FIELDS(node_type)  从而让指针的定义无区别化,只要更改node_type就能将这个指针节点用在别的定义中。

 

 

 

阅读更多
个人分类: c/c++
上一篇openssl工作笔记
下一篇openGL在win8上的开发环境搭建
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭