Return parent type from tl_object::fetch to reduce number of unique_ptr types in header.

GitOrigin-RevId: c2bfa4b2879c8ee3a0feda50fca605a58673cc87
This commit is contained in:
levlam 2018-09-25 06:26:36 +03:00
parent f404f7799b
commit ca58d36d4d
16 changed files with 69 additions and 49 deletions

View File

@ -1028,7 +1028,8 @@ class TlWriterCCommon : public tl::TL_writer {
return "";
}
std::string gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name, int arity,
std::string gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name,
const std::string &parent_class_name, int arity,
std::vector<tl::var_description> &vars, int parser_type) const override {
return "";
}

View File

@ -506,17 +506,18 @@ std::string TD_TL_writer_cpp::gen_function_result_type(const tl::tl_tree *result
}
std::string TD_TL_writer_cpp::gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name,
int arity, std::vector<tl::var_description> &vars,
int parser_type) const {
const std::string &parent_class_name, int arity,
std::vector<tl::var_description> &vars, int parser_type) const {
for (std::size_t i = 0; i < vars.size(); i++) {
assert(vars[i].is_stored == false);
}
std::string fetched_type = "object_ptr<" + class_name + "> ";
std::string returned_type = "object_ptr<" + parent_class_name + "> ";
assert(arity == 0);
if (parser_type == 0) {
return "\n" + fetched_type + class_name + "::fetch(" + parser_name +
return "\n" + returned_type + class_name + "::fetch(" + parser_name +
" &p) {\n"
" return make_tl_object<" +
class_name +
@ -527,7 +528,7 @@ std::string TD_TL_writer_cpp::gen_fetch_function_begin(const std::string &parser
"#define FAIL(error) p.set_error(error)\n";
}
return "\n" + fetched_type + class_name + "::fetch(" + parser_name +
return "\n" + returned_type + class_name + "::fetch(" + parser_name +
" &p) {\n"
"#define FAIL(error) p.set_error(error); return nullptr;\n" +
(parser_type == -1 ? "" : " " + fetched_type + "res = make_tl_object<" + class_name + ">();\n");

View File

@ -76,7 +76,8 @@ class TD_TL_writer_cpp : public TD_TL_writer {
std::string gen_function_result_type(const tl::tl_tree *result) const override;
std::string gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name, int arity,
std::string gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name,
const std::string &parent_class_name, int arity,
std::vector<tl::var_description> &vars, int parser_type) const override;
std::string gen_fetch_function_end(int field_num, const std::vector<tl::var_description> &vars,
int parser_type) const override;

View File

@ -29,7 +29,8 @@ class TlWriterDotNet : public TL_writer {
}
bool is_built_in_simple_type(const std::string &name) const override {
return name == "Bool" || name == "Int32" || name == "Int53" || name == "Int64" || name == "Double" || name == "String" || name == "Bytes";
return name == "Bool" || name == "Int32" || name == "Int53" || name == "Int64" || name == "Double" ||
name == "String" || name == "Bytes";
}
bool is_built_in_complex_type(const std::string &name) const override {
return name == "Vector";
@ -252,7 +253,8 @@ class TlWriterDotNet : public TL_writer {
ss << "\n";
if (storer_type) {
ss << (is_header_ ? " virtual " : "") << "String^ " << (is_header_ ? "" : gen_class_name(class_name) + "::")
<< "ToString()" << (is_header_ ? " override;" : " {\n return ::Telegram::Td::Api::ToString(this);\n}") << "\n";
<< "ToString()" << (is_header_ ? " override;" : " {\n return ::Telegram::Td::Api::ToString(this);\n}")
<< "\n";
} else {
ss << (is_header_ ? " virtual " : "") << "NativeObject^ "
<< (is_header_ ? "" : gen_class_name(class_name) + "::") << "ToUnmanaged()";
@ -274,20 +276,23 @@ class TlWriterDotNet : public TL_writer {
ss << (is_header_ ? " " : gen_class_name(class_name) + "::") << gen_class_name(class_name) << "(";
return ss.str();
}
std::string gen_constructor_parameter(int field_num, const std::string &class_name, const arg &a, bool is_default) const override {
std::string gen_constructor_parameter(int field_num, const std::string &class_name, const arg &a,
bool is_default) const override {
if (is_default) {
return "";
}
std::stringstream ss;
ss << (field_num == 0 ? "" : ", ");
auto field_type = gen_field_type(a);
if (field_type.substr(0, 5) != "Array" && field_type.substr(0, 6) != "String" && to_upper(field_type[0]) == field_type[0]) {
if (field_type.substr(0, 5) != "Array" && field_type.substr(0, 6) != "String" &&
to_upper(field_type[0]) == field_type[0]) {
field_type = "::Telegram::Td::Api::" + field_type;
}
ss << field_type << " " << to_camelCase(a.name);
return ss.str();
}
std::string gen_constructor_field_init(int field_num, const std::string &class_name, const arg &a, bool is_default) const override {
std::string gen_constructor_field_init(int field_num, const std::string &class_name, const arg &a,
bool is_default) const override {
if (is_default || is_header_) {
return "";
}
@ -332,8 +337,7 @@ class TlWriterDotNet : public TL_writer {
void gen_to_unmanaged(std::stringstream &ss, const tl_combinator *t) const {
auto native_class_name = gen_native_class_name(t->name);
auto class_name = gen_class_name(t->name);
ss << "td::td_api::object_ptr<td::td_api::" << native_class_name << "> ToUnmanaged(" << class_name
<< "^ from)";
ss << "td::td_api::object_ptr<td::td_api::" << native_class_name << "> ToUnmanaged(" << class_name << "^ from)";
if (is_header_) {
ss << ";\n";
return;
@ -463,7 +467,8 @@ class TlWriterDotNet : public TL_writer {
return "";
}
std::string gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name, int arity,
std::string gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name,
const std::string &parent_class_name, int arity,
std::vector<var_description> &vars, int parser_type) const override {
return "";
}
@ -509,8 +514,7 @@ class TlWriterDotNet : public TL_writer {
auto native_class_name = gen_native_class_name(type->name);
auto class_name = gen_class_name(type->name);
if (function_name == "ToUnmanaged") {
ss << "td::td_api::object_ptr<td::td_api::" << native_class_name << "> ToUnmanaged(" << class_name
<< "^ from)";
ss << "td::td_api::object_ptr<td::td_api::" << native_class_name << "> ToUnmanaged(" << class_name << "^ from)";
if (is_header_) {
ss << ";\n";
return ss.str();
@ -519,7 +523,8 @@ class TlWriterDotNet : public TL_writer {
<< " if (!from) {\n"
<< " return nullptr;\n"
<< " }\n"
<< " return td::td_api::move_object_as<td::td_api::" << native_class_name << ">(from->ToUnmanaged()->get_object_ptr());\n}\n";
<< " return td::td_api::move_object_as<td::td_api::" << native_class_name
<< ">(from->ToUnmanaged()->get_object_ptr());\n}\n";
} else {
ss << class_name << "^ FromUnmanaged(td::td_api::" << native_class_name << " &from)";
if (is_header_) {

View File

@ -254,21 +254,21 @@ std::string TD_TL_writer_h::gen_function_result_type(const tl::tl_tree *result)
}
std::string TD_TL_writer_h::gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name,
int arity, std::vector<tl::var_description> &vars,
int parser_type) const {
std::string fetched_type = "object_ptr<" + class_name + "> ";
const std::string &parent_class_name, int arity,
std::vector<tl::var_description> &vars, int parser_type) const {
std::string returned_type = "object_ptr<" + parent_class_name + "> ";
if (parser_type == 0) {
return "\n"
" static " +
fetched_type + "fetch(" + parser_name + " &p);\n\n" + " explicit " + class_name + "(" + parser_name +
returned_type + "fetch(" + parser_name + " &p);\n\n" + " explicit " + class_name + "(" + parser_name +
" &p);\n";
}
assert(arity == 0);
return "\n"
" static " +
fetched_type + "fetch(" + parser_name + " &p);\n";
returned_type + "fetch(" + parser_name + " &p);\n";
}
std::string TD_TL_writer_h::gen_fetch_function_end(int field_num, const std::vector<tl::var_description> &vars,

View File

@ -62,7 +62,8 @@ class TD_TL_writer_h : public TD_TL_writer {
std::string gen_function_result_type(const tl::tl_tree *result) const override;
std::string gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name, int arity,
std::string gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name,
const std::string &parent_class_name, int arity,
std::vector<tl::var_description> &vars, int parser_type) const override;
std::string gen_fetch_function_end(int field_num, const std::vector<tl::var_description> &vars,
int parser_type) const override;

View File

@ -138,8 +138,8 @@ std::string TD_TL_writer_hpp::gen_function_result_type(const tl::tl_tree *result
}
std::string TD_TL_writer_hpp::gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name,
int arity, std::vector<tl::var_description> &vars,
int parser_type) const {
const std::string &parent_class_name, int arity,
std::vector<tl::var_description> &vars, int parser_type) const {
return "";
}

View File

@ -63,7 +63,8 @@ class TD_TL_writer_hpp : public TD_TL_writer {
std::string gen_function_result_type(const tl::tl_tree *result) const override;
std::string gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name, int arity,
std::string gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name,
const std::string &parent_class_name, int arity,
std::vector<tl::var_description> &vars, int parser_type) const override;
std::string gen_fetch_function_end(int field_num, const std::vector<tl::var_description> &vars,
int parser_type) const override;

View File

@ -346,8 +346,8 @@ std::string TD_TL_writer_java::gen_function_result_type(const tl::tl_tree *resul
}
std::string TD_TL_writer_java::gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name,
int arity, std::vector<tl::var_description> &vars,
int parser_type) const {
const std::string &parent_class_name, int arity,
std::vector<tl::var_description> &vars, int parser_type) const {
return "";
}

View File

@ -87,7 +87,8 @@ class TD_TL_writer_java : public tl::TL_writer {
std::string gen_function_result_type(const tl::tl_tree *result) const override;
std::string gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name, int arity,
std::string gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name,
const std::string &parent_class_name, int arity,
std::vector<tl::var_description> &vars, int parser_type) const override;
std::string gen_fetch_function_end(int field_num, const std::vector<tl::var_description> &vars,
int parser_type) const override;

View File

@ -378,7 +378,8 @@ std::string TD_TL_writer_jni_cpp::gen_get_id(const std::string &class_name, std:
}
std::string TD_TL_writer_jni_cpp::gen_fetch_function_begin(const std::string &parser_name,
const std::string &class_name, int arity,
const std::string &class_name,
const std::string &parent_class_name, int arity,
std::vector<tl::var_description> &vars,
int parser_type) const {
for (std::size_t i = 0; i < vars.size(); i++) {
@ -386,11 +387,12 @@ std::string TD_TL_writer_jni_cpp::gen_fetch_function_begin(const std::string &pa
}
std::string fetched_type = "object_ptr<" + class_name + "> ";
std::string returned_type = "object_ptr<" + parent_class_name + "> ";
assert(arity == 0);
assert(parser_type != 0);
return "\n" + fetched_type + class_name + "::fetch(" + parser_name + " &p) {\n" +
return "\n" + returned_type + class_name + "::fetch(" + parser_name + " &p) {\n" +
(parser_type == -1 ? ""
: " if (p == nullptr) return nullptr;\n"
" " +

View File

@ -73,7 +73,8 @@ class TD_TL_writer_jni_cpp : public TD_TL_writer_cpp {
std::string gen_get_id(const std::string &class_name, std::int32_t id, bool is_proxy) const override;
std::string gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name, int arity,
std::string gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name,
const std::string &parent_class_name, int arity,
std::vector<tl::var_description> &vars, int parser_type) const override;
std::string gen_fetch_function_end(int field_num, const std::vector<tl::var_description> &vars,
int parser_type) const override;

View File

@ -161,19 +161,19 @@ class unique_ptr {
template <class T>
bool operator==(std::nullptr_t, const unique_ptr<T> &p) {
return !bool(p);
return !p;
}
template <class T>
bool operator==(const unique_ptr<T> &p, std::nullptr_t) {
return !bool(p);
return !p;
}
template <class T>
bool operator!=(std::nullptr_t, const unique_ptr<T> &p) {
return bool(p);
return static_cast<bool>(p);
}
template <class T>
bool operator!=(const unique_ptr<T> &p, std::nullptr_t) {
return bool(p);
return static_cast<bool>(p);
}
} // namespace tl
template <class Type>

View File

@ -92,7 +92,7 @@ static void write_function_fetch(tl_outputer &out, const std::string &parser_nam
return;
}
out.append(w.gen_fetch_function_begin(parser_name, class_name, 0, vars, parser_type));
out.append(w.gen_fetch_function_begin(parser_name, class_name, class_name, 0, vars, parser_type));
out.append(w.gen_vars(t, NULL, vars));
int field_num = 0;
for (std::size_t i = 0; i < t->args.size(); i++) {
@ -167,7 +167,8 @@ static void write_function_result_fetch(tl_outputer &out, const std::string &par
}
static void write_constructor_fetch(tl_outputer &out, const std::string &parser_name, const tl_combinator *t,
const std::string &class_name, const tl_tree_type *result_type, bool is_flat,
const std::string &class_name, const std::string &parent_class_name,
const tl_tree_type *result_type, bool is_flat,
const std::set<std::string> &request_types,
const std::set<std::string> &result_types, const TL_writer &w) {
std::vector<var_description> vars(t->var_count);
@ -178,8 +179,8 @@ static void write_constructor_fetch(tl_outputer &out, const std::string &parser_
return;
}
out.append(w.gen_fetch_function_begin(parser_name, class_name, static_cast<int>(result_type->children.size()), vars,
parser_type));
out.append(w.gen_fetch_function_begin(parser_name, class_name, parent_class_name,
static_cast<int>(result_type->children.size()), vars, parser_type));
out.append(w.gen_vars(t, result_type, vars));
out.append(w.gen_uni(result_type, vars, true));
int field_num = 0;
@ -294,7 +295,8 @@ static void write_function(tl_outputer &out, const tl_combinator *t, const std::
out.append(w.gen_class_end());
}
static void write_constructor(tl_outputer &out, const tl_combinator *t, const std::string &base_class, bool is_proxy,
static void write_constructor(tl_outputer &out, const tl_combinator *t, const std::string &base_class,
const std::string &parent_class, bool is_proxy,
const std::set<std::string> &request_types, const std::set<std::string> &result_types,
const TL_writer &w) {
assert(w.is_combinator_supported(t));
@ -318,7 +320,7 @@ static void write_constructor(tl_outputer &out, const tl_combinator *t, const st
std::vector<std::string> parsers = w.get_parsers();
for (std::size_t i = 0; i < parsers.size(); i++) {
write_constructor_fetch(out, parsers[i], t, class_name, result_type,
write_constructor_fetch(out, parsers[i], t, class_name, parent_class, result_type,
required_args == 1 && result_type->type->simple_constructors == 1, request_types,
result_types, w);
}
@ -367,7 +369,7 @@ void write_class(tl_outputer &out, const tl_type *t, const std::set<std::string>
continue;
}
out.append(w.gen_fetch_function_begin(parsers[i], class_name, t->arity, empty_vars, -1));
out.append(w.gen_fetch_function_begin(parsers[i], class_name, class_name, t->arity, empty_vars, -1));
out.append(w.gen_fetch_switch_begin());
for (std::size_t j = 0; j < t->constructors_num; j++) {
if (w.is_combinator_supported(t->constructors[j])) {
@ -409,10 +411,11 @@ void write_class(tl_outputer &out, const tl_type *t, const std::set<std::string>
for (std::size_t i = 0; i < t->constructors_num; i++) {
if (w.is_combinator_supported(t->constructors[i])) {
if (optimize_one_constructor) {
write_constructor(out, t->constructors[i], base_class, false, request_types, result_types, w);
write_constructor(out, t->constructors[i], base_class, w.gen_class_name(t->constructors[i]->name), false,
request_types, result_types, w);
out.append(w.gen_class_alias(w.gen_class_name(t->constructors[i]->name), class_name));
} else {
write_constructor(out, t->constructors[i], class_name, false, request_types, result_types, w);
write_constructor(out, t->constructors[i], class_name, class_name, false, request_types, result_types, w);
}
written_constructors++;
} else {
@ -648,7 +651,8 @@ void write_tl(const tl_config &config, tl_outputer &out, const TL_writer &w) {
continue;
}
out.append(w.gen_fetch_function_begin(parsers[j], w.gen_base_type_class_name(i), i, empty_vars, -1));
out.append(w.gen_fetch_function_begin(parsers[j], w.gen_base_type_class_name(i), w.gen_base_type_class_name(i), i,
empty_vars, -1));
out.append(w.gen_fetch_switch_begin());
for (std::size_t type = 0; type < types_n; type++) {
tl_type *t = config.get_type_by_num(type);
@ -722,7 +726,8 @@ void write_tl(const tl_config &config, tl_outputer &out, const TL_writer &w) {
continue;
}
out.append(w.gen_fetch_function_begin(parsers[j], w.gen_base_function_class_name(), 0, empty_vars, -1));
out.append(w.gen_fetch_function_begin(parsers[j], w.gen_base_function_class_name(),
w.gen_base_function_class_name(), 0, empty_vars, -1));
out.append(w.gen_fetch_switch_begin());
for (std::size_t function = 0; function < functions_n; function++) {
tl_combinator *t = config.get_function_by_num(function);

View File

@ -118,7 +118,8 @@ class TL_writer {
virtual std::string gen_function_result_type(const tl_tree *result) const = 0;
virtual std::string gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name, int arity,
virtual std::string gen_fetch_function_begin(const std::string &parser_name, const std::string &class_name,
const std::string &parent_class_name, int arity,
std::vector<var_description> &vars, int parser_type) const = 0;
virtual std::string gen_fetch_function_end(int field_num, const std::vector<var_description> &vars,
int parser_type) const = 0;

View File

@ -643,7 +643,7 @@ class Master : public Actor {
add_event(Event::delayed_closure(&SecretChatActor::add_inbound_message, std::move(event)));
}
void send_message(tl_object_ptr<secret_api::decryptedMessage> message) {
void send_message(tl_object_ptr<secret_api::DecryptedMessage> message) {
BufferSlice serialized_message(serialize(*message));
auto resend_promise = PromiseCreator::lambda(
[actor_id = actor_id(this), serialized_message = std::move(serialized_message)](Result<> result) mutable {