1 | #pragma once |
---|---|
2 | |
3 | #include <string> |
4 | #include <IO/WriteBufferFromVector.h> |
5 | #include <common/StringRef.h> |
6 | |
7 | |
8 | namespace DB |
9 | { |
10 | |
11 | /** Writes the data to a string. |
12 | * Note: before using the resulting string, destroy this object. |
13 | */ |
14 | using WriteBufferFromString = WriteBufferFromVector<std::string>; |
15 | |
16 | |
17 | namespace detail |
18 | { |
19 | /// For correct order of initialization. |
20 | class StringHolder |
21 | { |
22 | protected: |
23 | std::string value; |
24 | }; |
25 | } |
26 | |
27 | /// Creates the string by itself and allows to get it. |
28 | class WriteBufferFromOwnString : public detail::StringHolder, public WriteBufferFromString |
29 | { |
30 | public: |
31 | WriteBufferFromOwnString() : WriteBufferFromString(value) {} |
32 | |
33 | StringRef stringRef() const { return isFinished() ? StringRef(value) : StringRef(value.data(), pos - value.data()); } |
34 | |
35 | std::string & str() |
36 | { |
37 | finish(); |
38 | return value; |
39 | } |
40 | }; |
41 | |
42 | } |
43 |