]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | #ifndef CEPH_COMMON_PREBUFFEREDSTREAMBUF_H |
2 | #define CEPH_COMMON_PREBUFFEREDSTREAMBUF_H | |
3 | ||
7c673cae FG |
4 | #include <streambuf> |
5 | ||
6 | /** | |
7 | * streambuf using existing buffer, overflowing into a std::string | |
8 | * | |
9 | * A simple streambuf that uses a preallocated buffer for small | |
10 | * strings, and overflows into a std::string when necessary. If the | |
11 | * preallocated buffer size is chosen well, we can optimize for the | |
12 | * common case and overflow to a slower heap-allocated buffer when | |
13 | * necessary. | |
14 | */ | |
15 | class PrebufferedStreambuf | |
16 | : public std::basic_streambuf<char, std::basic_string<char>::traits_type> | |
17 | { | |
18 | char *m_buf; | |
19 | size_t m_buf_len; | |
20 | std::string m_overflow; | |
21 | ||
22 | typedef std::char_traits<char> traits_ty; | |
23 | typedef traits_ty::int_type int_type; | |
24 | typedef traits_ty::pos_type pos_type; | |
25 | typedef traits_ty::off_type off_type; | |
26 | ||
27 | public: | |
28 | PrebufferedStreambuf(char *buf, size_t len); | |
29 | ||
30 | // called when the buffer fills up | |
31 | int_type overflow(int_type c) override; | |
32 | ||
33 | // called when we read and need more data | |
34 | int_type underflow() override; | |
35 | ||
36 | /// return a string copy (inefficiently) | |
37 | std::string get_str() const; | |
38 | ||
39 | // returns current size of content | |
40 | size_t size() const; | |
41 | ||
42 | // extracts up to avail chars of content | |
43 | int snprintf(char* dst, size_t avail) const; | |
44 | }; | |
45 | ||
46 | #endif |