{"id":2654,"date":"2019-01-03T16:27:17","date_gmt":"2019-01-03T08:27:17","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=2654"},"modified":"2019-01-03T16:29:11","modified_gmt":"2019-01-03T08:29:11","slug":"%e7%8e%af%e5%bd%a2%e7%bc%93%e5%86%b2%e5%8c%baringbuffer-c%e7%b1%bb%e6%a8%a1%e7%89%88%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/2654","title":{"rendered":"\u73af\u5f62\u7f13\u51b2\u533aringbuffer c++\u7c7b\u6a21\u7248\u5b9e\u73b0"},"content":{"rendered":"<div class=\"article-title\"><\/div>\n<div class=\"intro\">\u672c\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u73af\u5f62\u7f13\u51b2\u533aringbuffer c++\u7c7b\u6a21\u7248\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\u73af\u5f62\u7f13\u51b2\u533aringbuffer c++\u7c7b\u6a21\u7248\u5b9e\u73b0\u4f7f\u7528\u5b9e\u4f8b\u3001\u5e94\u7528\u6280\u5de7\u3001\u57fa\u672c\u77e5\u8bc6\u70b9\u603b\u7ed3\u548c\u9700\u8981\u6ce8\u610f\u4e8b\u9879\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/div>\n<div id=\"code_example\" class=\"article-content\">\n<div id=\"article_left_top_banner\"><\/div>\n<p>@ringbuffer<\/p>\n<h1><a id=\"ringbuffer_c_2\"><\/a>\u73af\u5f62\u7f13\u51b2\u533aringbuffer c++\u7c7b\u6a21\u7248\u5b9e\u73b0<\/h1>\n<p>\u4f7f\u7528\u65b9\u6cd5\uff1a<br \/>\n1\u3001\u5c06ringBuffer.h\u6587\u4ef6\u62f7\u8d1d\u5230\u5f00\u53d1\u5de5\u7a0b\u4e2d\uff0c\u6dfb\u52a0\u76f8\u5e94\u7684\u5934\u6587\u4ef6<br \/>\n2\u3001\u9700\u8981g++\u7f16\u8bd1\u5668\uff08\u5f00\u53d1\u7684\u9879\u76ee\u5de5\u7a0b\u9700\u8981\u652f\u6301\uff09\uff0c\u7f16\u8bd1\u6b64\u7c7b\u6a21\u7248<br \/>\n3\u3001\u652f\u6301int\u3001char\u3001bool\u7b49\u7c7b\u578b<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#pragma once\r\n\r\n#include \"stdlib.h\"\r\n#include &lt;string&gt;\r\n\r\n#define MAX_ASK_FOR_CAP       (1 &lt;&lt; 16)  \/\/ 64K\r\n\r\nstruct BufferInfo{\r\n    BufferInfo(unsigned int r = 0, unsigned int w = 0, unsigned int c = 0)\r\n            : read(r)\r\n            , write(w)\r\n            , capacity(c)\r\n            , mask(c - 1)\r\n    {\r\n    }\r\n    unsigned int read;  \/\/ read index in buffer\r\n    unsigned int write;  \/\/ write index in buffer\r\n    unsigned int capacity;  \/\/ capacity of the buffer\r\n    unsigned int mask;  \/\/ mask of the capacity\r\n};\r\n\r\ntemplate &lt;typename T&gt;\r\nclass RingBuffer\r\n{\r\npublic:\r\n    RingBuffer(unsigned int bufCap = 0)\r\n        : buffer(NULL)\r\n    {\r\n        create(bufCap);\r\n    }\r\n\r\n    ~RingBuffer()\r\n    {\r\n        destroy();\r\n    }\r\n\r\n    \/**\r\n     * @brief create  create one buffer\r\n     * @param bufCap  buffer capacity\r\n     * @return  return true if successful\r\n     *\/\r\n    bool create(unsigned int bufCap)\r\n    {\r\n        if (bufCap == 0)\r\n        {\r\n            return false;\r\n        }\r\n\r\n        if (buffer != NULL)\r\n        {\r\n            delete buffer;\r\n            buffer = NULL;\r\n        }\r\n\r\n        unsigned int length = bufCap;\r\n        unsigned int n = 1;\r\n        while (length &gt; n)\r\n        {\r\n            n *= 2;\r\n            if (n &gt; MAX_ASK_FOR_CAP)\r\n            {\r\n                return false;\r\n            }\r\n        }\r\n        length = n;\r\n\r\n        buffer = new T[length];\r\n        bufferInfo.read = 0;\r\n        bufferInfo.write = 0;\r\n        bufferInfo.capacity = length;\r\n        bufferInfo.mask = bufferInfo.capacity - 1;\r\n        return true;\r\n    }\r\n\r\n    \/**\r\n     * @brief push  write one data of T type into the buffer\r\n     * @param data  one data of T type\r\n     * @return  return true if successful\r\n     *\/\r\n    bool push(T data)\r\n    {\r\n        if (isFull() == true)\r\n        {\r\n            return false;\r\n        }\r\n        int num = memcpy((buffer + bufferInfo.write), reinterpret_cast&lt;void*&gt;(&amp;data), sizeof(T));\r\n        if (num == sizeof(T))\r\n        {\r\n            bufferInfo.write += num;\r\n            return true;\r\n        }\r\n        return false;\r\n    }\r\n\r\n    \/**\r\n     * @brief push  write serival data of T type into the buffer\r\n     * @param data serival data of T type\r\n     * @param len  length of data\r\n     * @return num of writing to buffer successfully\r\n     *\/\r\n    unsigned int push(T *data, unsigned int len)\r\n    {\r\n        if (isFull() == true)\r\n        {\r\n            return 0;\r\n        }\r\n        unsigned int space = availableToWrite();\r\n        if (len &gt; space)\r\n        {\r\n            len = space;\r\n        }\r\n        int num = memcpy((buffer + bufferInfo.write), reinterpret_cast&lt;void*&gt;(data), len * sizeof(T));\r\n        bufferInfo.write += len;\r\n        return num;\r\n    }\r\n\r\n    \/**\r\n     * @brief pop  remove oldest data of the buffer\r\n     * @param num  num of the removed data\r\n     * @return  num of the successful removed data\r\n     *\/\r\n    unsigned int pop(unsigned int num)\r\n    {\r\n        if (isEmpty() == true)\r\n        {\r\n            return 0;\r\n        }\r\n        unsigned int count = availableToRead();\r\n        if (num &lt; count)\r\n        {\r\n            bufferInfo.read += num;\r\n            return num;\r\n        }\r\n        bufferInfo.read = bufferInfo.write = 0;\r\n        return count;\r\n    }\r\n\r\n    \/**\r\n     * @brief takeTail  get oldest data of the buffer and remove it\r\n     * @return the data of T type\r\n     *\/\r\n    T takeTail()\r\n    {\r\n        if (isEmpty() == true)\r\n        {\r\n            return;\r\n        }\r\n        T data = buffer[bufferInfo.read];\r\n        pop(1);\r\n        return data;\r\n    }\r\n\r\n    \/**\r\n     * @brief read  read some oldest data of the buffer and remove them\r\n     * @param buf  read the data to this buf.\r\n     * @param len  read length\r\n     * @return  read legth successfully\r\n     *\/\r\n    unsigned int read(T &amp;buf, unsigned int len)\r\n    {\r\n        if (sizeof(buf) &lt; len)\r\n        {\r\n            len = sizeof(buf);\r\n        }\r\n        unsigned int count = 0;\r\n        while (isEmpty() == false)\r\n        {\r\n            buf[count++] = this-&gt;buffer[bufferInfo.read];\r\n            pop(1);\r\n            if (count &gt;= len)\r\n            {\r\n                break;\r\n            }\r\n        }\r\n        return count;\r\n    }\r\n\r\nprivate:\r\n    \/**\r\n     * @brief destroy  destroy the buffer created\r\n     *\/\r\n    void destroy()\r\n    {\r\n        if (buffer == NULL)\r\n        {\r\n            return;\r\n        }\r\n        delete buffer;\r\n        buffer = NULL;\r\n    }\r\n\r\n    \/**\r\n     * @brief isFull  if be full of the buffer\r\n     * @return   return true if successful\r\n     *\/\r\n    inline bool isFull() const\r\n    {\r\n        if (0 == availableToWrite())\r\n        {\r\n            return true;\r\n        }\r\n        return false;\r\n    }\r\n\r\n    \/**\r\n     * @brief isEmpty  if be empty of the buffer\r\n     * @return   return true if successful\r\n     *\/\r\n    inline bool isEmpty() const\r\n    {\r\n        if (0 == availableToRead())\r\n        {\r\n            return true;\r\n        }\r\n        return false;\r\n    }\r\n\r\n    \/**\r\n     * @brief availableToRead  available buffer to read\r\n     * @return num of available buffer to read\r\n     *\/\r\n    inline unsigned int availableToRead() const\r\n    {\r\n        return (bufferInfo.write &amp; bufferInfo.mask)  - (bufferInfo.read &amp; bufferInfo.mask);\r\n    }\r\n\r\n    \/**\r\n     * @brief availableToWrite  available buffer to write\r\n     * @return num of available buffer to write\r\n     *\/\r\n    inline unsigned int availableToWrite() const\r\n    {\r\n        return bufferInfo.mask - bufferInfo.mask &amp; bufferInfo.write;\r\n    }\r\n\r\n    T *buffer;\r\n    BufferInfo bufferInfo;\r\n};<\/pre>\n<pre><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u73af\u5f62\u7f13\u51b2\u533aringbuf<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119],"class_list":["post-2654","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2654","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=2654"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2654\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=2654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=2654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=2654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}