X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fceph%2Fsrc%2Fcommon%2FPrebufferedStreambuf.h;fp=src%2Fceph%2Fsrc%2Fcommon%2FPrebufferedStreambuf.h;h=b7fa21d68b780455b754cad55d79846a07d5a3b1;hb=812ff6ca9fcd3e629e49d4328905f33eee8ca3f5;hp=0000000000000000000000000000000000000000;hpb=15280273faafb77777eab341909a3f495cf248d9;p=stor4nfv.git diff --git a/src/ceph/src/common/PrebufferedStreambuf.h b/src/ceph/src/common/PrebufferedStreambuf.h new file mode 100644 index 0000000..b7fa21d --- /dev/null +++ b/src/ceph/src/common/PrebufferedStreambuf.h @@ -0,0 +1,46 @@ +#ifndef CEPH_COMMON_PREBUFFEREDSTREAMBUF_H +#define CEPH_COMMON_PREBUFFEREDSTREAMBUF_H + +#include + +/** + * streambuf using existing buffer, overflowing into a std::string + * + * A simple streambuf that uses a preallocated buffer for small + * strings, and overflows into a std::string when necessary. If the + * preallocated buffer size is chosen well, we can optimize for the + * common case and overflow to a slower heap-allocated buffer when + * necessary. + */ +class PrebufferedStreambuf + : public std::basic_streambuf::traits_type> +{ + char *m_buf; + size_t m_buf_len; + std::string m_overflow; + + typedef std::char_traits traits_ty; + typedef traits_ty::int_type int_type; + typedef traits_ty::pos_type pos_type; + typedef traits_ty::off_type off_type; + +public: + PrebufferedStreambuf(char *buf, size_t len); + + // called when the buffer fills up + int_type overflow(int_type c) override; + + // called when we read and need more data + int_type underflow() override; + + /// return a string copy (inefficiently) + std::string get_str() const; + + // returns current size of content + size_t size() const; + + // extracts up to avail chars of content + int snprintf(char* dst, size_t avail) const; +}; + +#endif