ClickHouse/dbms/include/DB/IO/WriteBufferFromString.h

51 lines
1006 B
C++
Raw Normal View History

2011-11-01 17:57:37 +00:00
#pragma once
#include <iostream>
#include <DB/IO/WriteBuffer.h>
2011-11-03 20:30:12 +00:00
#define WRITE_BUFFER_FROM_STRING_INITIAL_SIZE_IF_EMPTY 32
2011-11-01 17:57:37 +00:00
namespace DB
{
/** Пишет данные в строку.
2011-11-29 18:53:44 +00:00
* Замечение: перед использованием полученной строки, уничтожте этот объект.
2011-11-01 17:57:37 +00:00
*/
class WriteBufferFromString : public WriteBuffer
{
private:
std::string & s;
void nextImpl()
{
size_t old_size = s.size();
s.resize(old_size * 2);
internal_buffer = Buffer(reinterpret_cast<Position>(&s[old_size]), reinterpret_cast<Position>(&*s.end()));
working_buffer = internal_buffer;
}
2012-06-18 06:19:13 +00:00
void finish()
{
s.resize(count());
}
2011-11-01 17:57:37 +00:00
public:
WriteBufferFromString(std::string & s_)
: WriteBuffer(reinterpret_cast<Position>(&s_[0]), s_.size()), s(s_)
{
2011-11-03 20:30:12 +00:00
if (s.empty())
{
s.resize(WRITE_BUFFER_FROM_STRING_INITIAL_SIZE_IF_EMPTY);
set(reinterpret_cast<Position>(&s[0]), s.size());
}
}
virtual ~WriteBufferFromString()
{
2012-06-18 06:19:13 +00:00
finish();
2011-11-01 17:57:37 +00:00
}
};
}