Less const_casts [#METR-2944].

This commit is contained in:
Alexey Milovidov 2017-01-03 04:42:17 +03:00
parent 9dc44c1a3a
commit 604bd6c5a3
10 changed files with 77 additions and 46 deletions

View File

@ -8,6 +8,7 @@
#include <DB/IO/WriteBufferFromVector.h>
#include <DB/IO/ReadBufferFromString.h>
#include <DB/IO/Operators.h>
#include <DB/IO/ReadBufferFromMemory.h>
#include <DB/DataTypes/DataTypeFactory.h>
#include <DB/DataTypes/DataTypesNumberFixed.h>
#include <DB/DataTypes/DataTypeString.h>
@ -785,8 +786,7 @@ struct ConvertImpl<DataTypeString, ToDataType, Name>
for (size_t i = 0; i < size; ++i)
{
ReadBuffer read_buffer(const_cast<char *>(reinterpret_cast<const char *>(
&chars[current_offset])), offsets[i] - current_offset - 1, 0);
ReadBufferFromMemory read_buffer(&chars[current_offset], offsets[i] - current_offset - 1);
parseImpl<ToDataType>(vec_to[i], read_buffer);
@ -860,8 +860,7 @@ struct ConvertOrZeroImpl
for (size_t i = 0; i < size; ++i)
{
ReadBuffer read_buffer(const_cast<char *>(reinterpret_cast<const char *>(
&chars[current_offset])), offsets[i] - current_offset - 1, 0);
ReadBufferFromMemory read_buffer(&chars[current_offset], offsets[i] - current_offset - 1);
/// NOTE Need to implement for Date and DateTime too.
if (!tryParseImpl<ToDataType>(vec_to[i], read_buffer) || !read_buffer.eof())
@ -915,8 +914,7 @@ struct ConvertImplGenericFromString
for (size_t i = 0; i < size; ++i)
{
ReadBuffer read_buffer(const_cast<char *>(reinterpret_cast<const char *>(
&chars[current_offset])), offsets[i] - current_offset - 1, 0);
ReadBufferFromMemory read_buffer(&chars[current_offset], offsets[i] - current_offset - 1);
data_type_to.deserializeTextEscaped(column_to, read_buffer);
@ -959,7 +957,7 @@ struct StringToTimestampConverter
const ColumnString::Offsets_t & offsets, PaddedPODArray<ToFieldType> & vec_to)
{
const auto & local_date_lut = DateLUT::instance();
ReadBuffer read_buffer(const_cast<char *>(reinterpret_cast<const char *>(&vec_from[0])), vec_from.size(), 0);
ReadBufferFromMemory read_buffer(&vec_from[0], vec_from.size());
ColumnString::Offset_t prev_offset = 0;
@ -989,7 +987,7 @@ struct StringToTimestampConverter
{
const auto & local_date_lut = DateLUT::instance();
const auto & remote_date_lut = DateLUT::instance(data);
ReadBuffer read_buffer(const_cast<char *>(reinterpret_cast<const char *>(&vec_from[0])), vec_from.size(), 0);
ReadBufferFromMemory read_buffer(&vec_from[0], vec_from.size());
char zero = 0;
for (size_t i = 0; i < vec_to.size(); ++i)
@ -1008,7 +1006,7 @@ struct StringToTimestampConverter
static void vector_constant(const ColumnString::Chars_t & vec_from, PaddedPODArray<ToFieldType> & vec_to)
{
ReadBuffer read_buffer(const_cast<char *>(reinterpret_cast<const char *>(&vec_from[0])), vec_from.size(), 0);
ReadBufferFromMemory read_buffer(&vec_from[0], vec_from.size());
char zero = 0;
for (size_t i = 0; i < vec_to.size(); ++i)
@ -1203,9 +1201,8 @@ struct ConvertImpl<DataTypeFixedString, ToDataType, Name>
for (size_t i = 0; i < size; ++i)
{
char * begin = const_cast<char *>(reinterpret_cast<const char *>(&data_from[i * n]));
char * end = begin + n;
ReadBuffer read_buffer(begin, n, 0);
ReadBufferFromMemory read_buffer(&data_from[i * n], n, 0);
const char * end = read_buffer.buffer().end();
parseImpl<ToDataType>(vec_to[i], read_buffer);
if (!read_buffer.eof())

View File

@ -13,6 +13,7 @@
#include <DB/Common/Volnitsky.h>
#include <DB/Functions/IFunction.h>
#include <DB/Functions/FunctionsStringSearch.h>
#include <DB/IO/ReadBufferFromMemory.h>
/** Функции для извлечения параметров визитов.
* Реализованы через шаблоны из FunctionsStringSearch.h.
@ -51,7 +52,7 @@ struct ExtractNumericType
static ResultType extract(const UInt8 * begin, const UInt8 * end)
{
ReadBuffer in(const_cast<char *>(reinterpret_cast<const char *>(begin)), end - begin, 0);
ReadBufferFromMemory in(begin, end - begin);
/// Учимся читать числа в двойных кавычках
if (!in.eof() && *in.position() == '"')

View File

@ -0,0 +1,26 @@
#pragma once
#include <DB/IO/ReadBuffer.h>
namespace DB
{
/** Allows to read from memory range.
* In comparison with just ReadBuffer, it only adds convenient constructors, that do const_cast.
* In fact, ReadBuffer will not modify data in buffer, but it requires non-const pointer.
*/
class ReadBufferFromMemory : public ReadBuffer
{
public:
ReadBufferFromMemory(const char * buf, size_t size)
: ReadBuffer(const_cast<char *>(buf), size, 0) {}
ReadBufferFromMemory(const unsigned char * buf, size_t size)
: ReadBuffer(const_cast<char *>(reinterpret_cast<const char *>(buf)), size, 0) {}
ReadBufferFromMemory(const signed char * buf, size_t size)
: ReadBuffer(const_cast<char *>(reinterpret_cast<const char *>(buf)), size, 0) {}
};
}

View File

@ -1,19 +1,19 @@
#pragma once
#include <DB/IO/ReadBuffer.h>
#include <DB/IO/ReadBufferFromMemory.h>
namespace DB
{
/** Позволяет читать из строки.
/** Allows to read from std::string-like object.
*/
class ReadBufferFromString : public ReadBuffer
class ReadBufferFromString : public ReadBufferFromMemory
{
public:
/// std::string или mysqlxx::Value
/// std::string or mysqlxx::Value
template <typename S>
ReadBufferFromString(const S & s) : ReadBuffer(const_cast<char *>(s.data()), s.size(), 0) {}
ReadBufferFromString(const S & s) : ReadBufferFromMemory(s.data(), s.size()) {}
};
}

View File

@ -22,6 +22,7 @@
#include <DB/Common/Arena.h>
#include <DB/IO/ReadBuffer.h>
#include <DB/IO/ReadBufferFromMemory.h>
#include <DB/IO/VarInt.h>
#include <city.h>
@ -936,7 +937,7 @@ template <typename T>
inline T parse(const char * data, size_t size)
{
T res;
ReadBuffer buf(const_cast<char *>(data), size, 0);
ReadBufferFromMemory buf(data, size);
readText(res, buf);
return res;
}

View File

@ -26,6 +26,7 @@
#include <DB/IO/ReadBufferFromFileDescriptor.h>
#include <DB/IO/WriteBufferFromFileDescriptor.h>
#include <DB/IO/WriteBufferFromString.h>
#include <DB/IO/ReadBufferFromMemory.h>
#include <DB/IO/ReadHelpers.h>
#include <DB/IO/WriteHelpers.h>
@ -795,7 +796,7 @@ private:
if (parsed_insert_query->data)
{
/// Отправляем данные из запроса.
ReadBuffer data_in(const_cast<char *>(parsed_insert_query->data), parsed_insert_query->end - parsed_insert_query->data, 0);
ReadBufferFromMemory data_in(parsed_insert_query->data, parsed_insert_query->end - parsed_insert_query->data);
sendDataFrom(data_in, sample);
}
else if (!is_interactive)

View File

@ -1,5 +1,7 @@
#include <DB/IO/ReadBufferFromMemory.h>
#include <DB/DataStreams/InputStreamFromASTInsertQuery.h>
namespace DB
{
@ -17,8 +19,8 @@ InputStreamFromASTInsertQuery::InputStreamFromASTInsertQuery(
/// Data could be in parsed (ast_insert_query.data) and in not parsed yet (input_buffer_tail_part) part of query.
input_buffer_ast_part = std::make_unique<ReadBuffer>(
const_cast<char *>(ast_insert_query->data), ast_insert_query->data ? ast_insert_query->end - ast_insert_query->data : 0, 0);
input_buffer_ast_part = std::make_unique<ReadBufferFromMemory>(
ast_insert_query->data, ast_insert_query->data ? ast_insert_query->end - ast_insert_query->data : 0);
ConcatReadBuffer::ReadBuffers buffers;
if (ast_insert_query->data)

View File

@ -1,6 +1,7 @@
#include <DB/DataTypes/DataTypeNullable.h>
#include <DB/Columns/ColumnNullable.h>
#include <DB/IO/ReadBuffer.h>
#include <DB/IO/ReadBufferFromMemory.h>
#include <DB/IO/ReadHelpers.h>
#include <DB/IO/WriteBuffer.h>
#include <DB/IO/WriteHelpers.h>
@ -131,7 +132,7 @@ void DataTypeNullable::deserializeTextEscaped(IColumn & column, ReadBuffer & ist
else
{
/// Otherwise, we need to place backslash back in front of istr.
ReadBuffer prefix(const_cast<char *>("\\"), 1, 0);
ReadBufferFromMemory prefix("\\", 1);
ConcatReadBuffer prepended_istr(prefix, istr);
nested_data_type->deserializeTextEscaped(nested, prepended_istr);

View File

@ -4,36 +4,37 @@
#include <DB/Core/Types.h>
#include <DB/IO/ReadHelpers.h>
#include <DB/IO/ReadBufferFromMemory.h>
#include <DB/IO/ConcatReadBuffer.h>
using namespace DB;
int main(int argc, char ** argv)
try
{
try
{
std::string s1 = "abc\\x\n";
std::string s2 = "\tdef";
std::string s1 = "abc\\x\n";
std::string s2 = "\tdef";
DB::ReadBuffer rb1(const_cast<char *>(s1.data()), 3, 0);
DB::ReadBuffer rb2(const_cast<char *>(s2.data()), s2.size(), 0);
ReadBufferFromMemory rb1(s1.data(), 3);
ReadBufferFromMemory rb2(s2.data(), s2.size());
DB::ConcatReadBuffer rb3(rb1, rb2);
ConcatReadBuffer rb3(rb1, rb2);
std::string read_s1;
std::string read_s2;
std::string read_s1;
std::string read_s2;
DB::readEscapedString(read_s1, rb3);
DB::assertChar('\t', rb3);
DB::readEscapedString(read_s2, rb3);
readEscapedString(read_s1, rb3);
assertChar('\t', rb3);
readEscapedString(read_s2, rb3);
std::cerr << read_s1 << ", " << read_s2 << std::endl;
std::cerr << ((read_s1 == "abc" && read_s2 == "def") ? "Ok." : "Fail.") << std::endl;
}
catch (const DB::Exception & e)
{
std::cerr << e.what() << ", " << e.displayText() << std::endl;
return 1;
}
std::cerr << read_s1 << ", " << read_s2 << std::endl;
std::cerr << ((read_s1 == "abc" && read_s2 == "def") ? "Ok." : "Fail.") << std::endl;
return 0;
}
catch (const Exception & e)
{
std::cerr << e.what() << ", " << e.displayText() << std::endl;
return 1;
}

View File

@ -2,6 +2,7 @@
#include <cstdlib>
#include <DB/IO/ReadHelpers.h>
#include <DB/IO/ReadBufferFromMemory.h>
#include <DB/Parsers/IAST.h>
#include <DB/Parsers/ASTExpressionList.h>
@ -138,7 +139,7 @@ bool ParserIdentifier::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_pa
/// Идентификатор в обратных кавычках
if (pos != end && *pos == '`')
{
ReadBuffer buf(const_cast<char *>(pos), end - pos, 0);
ReadBufferFromMemory buf(pos, end - pos);
String s;
readBackQuotedString(s, buf);
@ -497,7 +498,7 @@ bool ParserUnsignedInteger::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & m
return false;
UInt64 x = 0;
ReadBuffer in(const_cast<char *>(pos), end - pos, 0);
ReadBufferFromMemory in(pos, end - pos);
if (!tryReadIntText(x, in) || in.count() == 0)
{
expected = "unsigned integer";
@ -522,7 +523,7 @@ bool ParserStringLiteral::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max
return false;
}
ReadBuffer in(const_cast<char *>(pos), end - pos, 0);
ReadBufferFromMemory in(pos, end - pos);
try
{