ClickHouse/dbms/src/IO/WriteBufferFromVector.h

62 lines
1.6 KiB
C++
Raw Normal View History

2011-10-16 01:57:10 +00:00
#pragma once
2011-11-18 20:17:27 +00:00
#include <vector>
2011-10-16 01:57:10 +00:00
#include <IO/WriteBuffer.h>
2011-10-16 01:57:10 +00:00
namespace DB
{
/** Writes data to existing std::vector or similar type. When not enough space, it doubles vector size.
*
* In destructor, vector is cutted to the size of written data.
* You can call to 'finish' to resize earlier.
*
* The vector should live until this object is destroyed or until the 'finish' method is called.
2011-10-16 01:57:10 +00:00
*/
template <typename VectorType>
2011-10-16 01:57:10 +00:00
class WriteBufferFromVector : public WriteBuffer
{
private:
VectorType & vector;
bool is_finished = false;
2011-10-16 01:57:10 +00:00
void nextImpl() override
{
size_t old_size = vector.size();
vector.resize(old_size * 2);
internal_buffer = Buffer(reinterpret_cast<Position>(&vector[old_size]), reinterpret_cast<Position>(vector.data() + vector.size()));
working_buffer = internal_buffer;
}
2011-10-16 01:57:10 +00:00
public:
WriteBufferFromVector(VectorType & vector_)
2017-11-15 18:24:48 +00:00
: WriteBuffer(reinterpret_cast<Position>(vector_.data()), vector_.size()), vector(vector_)
{
if (vector.empty())
{
static constexpr size_t initial_size = 32;
vector.resize(initial_size);
set(reinterpret_cast<Position>(vector.data()), vector.size());
}
}
void finish()
{
is_finished = true;
vector.resize(
((position() - reinterpret_cast<Position>(vector.data()))
+ sizeof(typename VectorType::value_type) - 1) /// Align up.
/ sizeof(typename VectorType::value_type));
}
~WriteBufferFromVector() override
{
if (!is_finished)
finish();
}
2011-10-16 01:57:10 +00:00
};
}