Move TlStorerToString to TlStorerToString.h.

This commit is contained in:
levlam 2021-11-01 19:21:24 +03:00
parent 5e43075d3b
commit f743c782bf
6 changed files with 176 additions and 160 deletions

View File

@ -276,7 +276,6 @@ class TlWriterCCommon final : public tl::TL_writer {
"#include \"td/utils/logging.h\"\n" "#include \"td/utils/logging.h\"\n"
"#include \"td/utils/misc.h\"\n" "#include \"td/utils/misc.h\"\n"
"#include \"td/utils/Slice.h\"\n" "#include \"td/utils/Slice.h\"\n"
"#include \"td/utils/tl_storers.h\"\n"
"\n"; "\n";
} }
std::string gen_output_end() const final { std::string gen_output_end() const final {

View File

@ -26,6 +26,7 @@ std::string TD_TL_writer_cpp::gen_output_begin() const {
"#include \"td/utils/SliceBuilder.h\"\n" "#include \"td/utils/SliceBuilder.h\"\n"
"#include \"td/utils/tl_parsers.h\"\n" "#include \"td/utils/tl_parsers.h\"\n"
"#include \"td/utils/tl_storers.h\"\n\n" "#include \"td/utils/tl_storers.h\"\n\n"
"#include \"td/utils/TlStorerToString.h\"\n\n"
"namespace td {\n" "namespace td {\n"
"namespace " + "namespace " +
tl_name + tl_name +
@ -33,7 +34,7 @@ std::string TD_TL_writer_cpp::gen_output_begin() const {
"std::string to_string(const BaseObject &value) {\n" "std::string to_string(const BaseObject &value) {\n"
" TlStorerToString storer;\n" " TlStorerToString storer;\n"
" value.store(storer, \"\");\n" " value.store(storer, \"\");\n"
" return storer.move_as_str();\n" " return storer.move_as_string();\n"
"}\n"; "}\n";
} }

View File

@ -23,6 +23,7 @@
#include "td/utils/logging.h" #include "td/utils/logging.h"
#include "td/utils/Status.h" #include "td/utils/Status.h"
#include "td/utils/tl_storers.h" #include "td/utils/tl_storers.h"
#include "td/utils/TlStorerToString.h"
#include <map> #include <map>
#include <unordered_set> #include <unordered_set>

View File

@ -7,7 +7,7 @@
#pragma once #pragma once
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/tl_storers.h" #include "td/utils/TlStorerToString.h"
namespace td { namespace td {

View File

@ -0,0 +1,171 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#include "td/utils/common.h"
#include "td/utils/Slice.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/UInt.h"
namespace td {
class TlStorerToString {
string result;
size_t shift = 0;
void store_field_begin(const char *name) {
result.append(shift, ' ');
if (name && name[0]) {
result += name;
result += " = ";
}
}
void store_field_end() {
result += '\n';
}
void store_long(int64 value) {
result += (PSLICE() << value).c_str();
}
void store_binary(Slice data) {
static const char *hex = "0123456789ABCDEF";
result.append("{ ", 2);
for (auto c : data) {
unsigned char byte = c;
result += hex[byte >> 4];
result += hex[byte & 15];
result += ' ';
}
result += '}';
}
public:
TlStorerToString() = default;
TlStorerToString(const TlStorerToString &other) = delete;
TlStorerToString &operator=(const TlStorerToString &other) = delete;
void store_field(const char *name, bool value) {
store_field_begin(name);
result += (value ? "true" : "false");
store_field_end();
}
void store_field(const char *name, int32 value) {
store_field(name, static_cast<int64>(value));
}
void store_field(const char *name, int64 value) {
store_field_begin(name);
store_long(value);
store_field_end();
}
void store_field(const char *name, double value) {
store_field_begin(name);
result += (PSLICE() << value).c_str();
store_field_end();
}
void store_field(const char *name, const char *value) {
store_field_begin(name);
result += value;
store_field_end();
}
void store_field(const char *name, const string &value) {
store_field_begin(name);
result += '"';
result += value;
result += '"';
store_field_end();
}
void store_field(const char *name, const SecureString &value) {
store_field_begin(name);
result.append("<secret>");
store_field_end();
}
template <class T>
void store_field(const char *name, const T &value) {
store_field_begin(name);
result.append(value.data(), value.size());
store_field_end();
}
void store_bytes_field(const char *name, const SecureString &value) {
store_field_begin(name);
result.append("<secret>");
store_field_end();
}
template <class BytesT>
void store_bytes_field(const char *name, const BytesT &value) {
static const char *hex = "0123456789ABCDEF";
store_field_begin(name);
result.append("bytes [");
store_long(static_cast<int64>(value.size()));
result.append("] { ");
size_t len = min(static_cast<size_t>(64), value.size());
for (size_t i = 0; i < len; i++) {
int b = value[static_cast<int>(i)] & 0xff;
result += hex[b >> 4];
result += hex[b & 15];
result += ' ';
}
if (len < value.size()) {
result.append("...");
}
result += '}';
store_field_end();
}
template <class ObjectT>
void store_object_field(const char *name, const ObjectT *value) {
if (value == nullptr) {
store_field(name, "null");
} else {
value->store(*this, name);
}
}
void store_field(const char *name, const UInt128 &value) {
store_field_begin(name);
store_binary(as_slice(value));
store_field_end();
}
void store_field(const char *name, const UInt256 &value) {
store_field_begin(name);
store_binary(as_slice(value));
store_field_end();
}
void store_class_begin(const char *field_name, const char *class_name) {
store_field_begin(field_name);
result += class_name;
result += " {\n";
shift += 2;
}
void store_class_end() {
CHECK(shift >= 2);
shift -= 2;
result.append(shift, ' ');
result += "}\n";
}
string move_as_string() {
return std::move(result);
}
};
} // namespace td

View File

@ -10,9 +10,7 @@
#include "td/utils/logging.h" #include "td/utils/logging.h"
#include "td/utils/SharedSlice.h" #include "td/utils/SharedSlice.h"
#include "td/utils/Slice.h" #include "td/utils/Slice.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/StorerBase.h" #include "td/utils/StorerBase.h"
#include "td/utils/UInt.h"
#include <cstring> #include <cstring>
@ -46,6 +44,7 @@ class TlStorerUnsafe {
std::memcpy(buf_, slice.begin(), slice.size()); std::memcpy(buf_, slice.begin(), slice.size());
buf_ += slice.size(); buf_ += slice.size();
} }
void store_storer(const Storer &storer) { void store_storer(const Storer &storer) {
size_t size = storer.store(buf_); size_t size = storer.store(buf_);
buf_ += size; buf_ += size;
@ -142,161 +141,6 @@ class TlStorerCalcLength {
} }
}; };
class TlStorerToString {
std::string result;
size_t shift = 0;
void store_field_begin(const char *name) {
result.append(shift, ' ');
if (name && name[0]) {
result += name;
result += " = ";
}
}
void store_field_end() {
result += '\n';
}
void store_long(int64 value) {
result += (PSLICE() << value).c_str();
}
void store_binary(Slice data) {
static const char *hex = "0123456789ABCDEF";
result.append("{ ", 2);
for (auto c : data) {
unsigned char byte = c;
result += hex[byte >> 4];
result += hex[byte & 15];
result += ' ';
}
result += '}';
}
public:
TlStorerToString() = default;
TlStorerToString(const TlStorerToString &other) = delete;
TlStorerToString &operator=(const TlStorerToString &other) = delete;
void store_field(const char *name, bool value) {
store_field_begin(name);
result += (value ? "true" : "false");
store_field_end();
}
void store_field(const char *name, int32 value) {
store_field(name, static_cast<int64>(value));
}
void store_field(const char *name, int64 value) {
store_field_begin(name);
store_long(value);
store_field_end();
}
void store_field(const char *name, double value) {
store_field_begin(name);
result += (PSLICE() << value).c_str();
store_field_end();
}
void store_field(const char *name, const char *value) {
store_field_begin(name);
result += value;
store_field_end();
}
void store_field(const char *name, const string &value) {
store_field_begin(name);
result += '"';
result += value;
result += '"';
store_field_end();
}
void store_field(const char *name, const SecureString &value) {
store_field_begin(name);
result.append("<secret>");
store_field_end();
}
template <class T>
void store_field(const char *name, const T &value) {
store_field_begin(name);
result.append(value.data(), value.size());
store_field_end();
}
void store_bytes_field(const char *name, const SecureString &value) {
store_field_begin(name);
result.append("<secret>");
store_field_end();
}
template <class BytesT>
void store_bytes_field(const char *name, const BytesT &value) {
static const char *hex = "0123456789ABCDEF";
store_field_begin(name);
result.append("bytes [");
store_long(static_cast<int64>(value.size()));
result.append("] { ");
size_t len = min(static_cast<size_t>(64), value.size());
for (size_t i = 0; i < len; i++) {
int b = value[static_cast<int>(i)] & 0xff;
result += hex[b >> 4];
result += hex[b & 15];
result += ' ';
}
if (len < value.size()) {
result.append("...");
}
result += '}';
store_field_end();
}
template <class ObjectT>
void store_object_field(const char *name, const ObjectT *value) {
if (value == nullptr) {
store_field(name, "null");
} else {
value->store(*this, name);
}
}
void store_field(const char *name, const UInt128 &value) {
store_field_begin(name);
store_binary(as_slice(value));
store_field_end();
}
void store_field(const char *name, const UInt256 &value) {
store_field_begin(name);
store_binary(as_slice(value));
store_field_end();
}
void store_class_begin(const char *field_name, const char *class_name) {
store_field_begin(field_name);
result += class_name;
result += " {\n";
shift += 2;
}
void store_class_end() {
CHECK(shift >= 2);
shift -= 2;
result.append(shift, ' ');
result += "}\n";
}
std::string move_as_str() {
return std::move(result);
}
};
template <class T> template <class T>
size_t tl_calc_length(const T &data) { size_t tl_calc_length(const T &data) {
TlStorerCalcLength storer_calc_length; TlStorerCalcLength storer_calc_length;