From 78e45da6b9014f8efd96cee0ad5701464d911cc7 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 15 Nov 2020 19:57:03 +0300 Subject: [PATCH] Use array instead of std::vector in the generated C++ classes. --- example/web/tdweb/README.md | 2 +- td/generate/DoxygenTlDocumentationGenerator.php | 13 ++++++++++--- td/generate/tl_writer_cpp.cpp | 4 ++-- td/generate/tl_writer_h.cpp | 3 +++ td/generate/tl_writer_jni_cpp.cpp | 6 +++--- td/generate/tl_writer_jni_h.cpp | 3 +++ td/generate/tl_writer_td.cpp | 4 ++-- td/telegram/td_json_client.h | 2 +- td/tl/TlObject.h | 2 +- 9 files changed, 26 insertions(+), 13 deletions(-) diff --git a/example/web/tdweb/README.md b/example/web/tdweb/README.md index 4d8d545ca..591bc941f 100644 --- a/example/web/tdweb/README.md +++ b/example/web/tdweb/README.md @@ -15,7 +15,7 @@ The JSON representation of TDLib API objects is straightforward: all API objects [td_api.tl](https://github.com/tdlib/td/blob/master/td/generate/scheme/td_api.tl) scheme. Note that in the automatically generated C++ documentation all fields have an additional terminating underscore which shouldn't be used in the JSON interface. The object type name is stored in the special field '@type' which is optional in places where type is uniquely determined by the context. Fields of Bool type are stored as Boolean, fields of int32, int53, and double types are stored as Number, fields of int64 and string types are stored as String, -fields of bytes type are base64 encoded and then stored as String, fields of vector type are stored as Array. +fields of bytes type are base64 encoded and then stored as String, fields of array type are stored as Array. You can also add the field '@extra' to any query to TDLib and the response will contain the field '@extra' with exactly the same value. ## Installation diff --git a/td/generate/DoxygenTlDocumentationGenerator.php b/td/generate/DoxygenTlDocumentationGenerator.php index 99675fb7c..8f727a047 100644 --- a/td/generate/DoxygenTlDocumentationGenerator.php +++ b/td/generate/DoxygenTlDocumentationGenerator.php @@ -27,7 +27,7 @@ class DoxygenTlDocumentationGenerator extends TlDocumentationGenerator if ($type[6] !== '<' || $type[strlen($type) - 1] !== '>') { return ''; } - return 'std::vector<'.$this->getTypeName(substr($type, 7, -1)).'> &&'; + return 'array<'.$this->getTypeName(substr($type, 7, -1)).'> &&'; } if (preg_match('/[^A-Za-z0-9.]/', $type)) { @@ -99,7 +99,7 @@ class DoxygenTlDocumentationGenerator extends TlDocumentationGenerator $this->printError("Wrong vector subtype in $type"); return ''; } - return 'std::vector<'.$this->getTypeName(substr($type, 7, -1)).'>'; + return 'array<'.$this->getTypeName(substr($type, 7, -1)).'>'; } if (preg_match('/[^A-Za-z0-9.]/', $type)) { @@ -202,6 +202,13 @@ EOT * This type is used to store arbitrary sequences of bytes. In JSON interface the bytes are base64-encoded. */ EOT +); + + $this->addDocumentation('using array = std::vector;', <<addDocumentation('using BaseObject', <<(); * auto message_text = td::td_api::make_object("Hello, world!!!", - * std::vector>()); + * td::td_api::array>()); * auto send_message_request = td::td_api::make_object(chat_id, 0, 0, nullptr, nullptr, * td::td_api::make_object(std::move(message_text), false, true)); * \\endcode diff --git a/td/generate/tl_writer_cpp.cpp b/td/generate/tl_writer_cpp.cpp index f9c5029a7..044a228d7 100644 --- a/td/generate/tl_writer_cpp.cpp +++ b/td/generate/tl_writer_cpp.cpp @@ -305,7 +305,7 @@ std::string TD_TL_writer_cpp::get_pretty_class_name(std::string class_name) cons std::string TD_TL_writer_cpp::gen_vector_store(const std::string &field_name, const tl::tl_tree_type *t, const std::vector &vars, int storer_type) const { std::string num = field_name.back() == ']' ? "2" : ""; - return "{ const std::vector<" + gen_type_name(t) + "> &v" + num + " = " + field_name + + return "{ const array<" + gen_type_name(t) + "> &v" + num + " = " + field_name + "; const std::uint32_t multiplicity" + num + " = static_cast(v" + num + ".size()); const auto vector_name" + num + " = \"" + get_pretty_class_name("vector") + "[\" + td::to_string(multiplicity" + num + ")+ \"]\"; s.store_class_begin(\"" + @@ -662,7 +662,7 @@ std::string TD_TL_writer_cpp::gen_constructor_field_init(int field_num, const st } std::string move_begin; std::string move_end; - if ((field_type == "bytes" || field_type.compare(0, 11, "std::vector") == 0 || + if ((field_type == "bytes" || field_type.compare(0, 5, "array") == 0 || field_type.compare(0, 10, "object_ptr") == 0) && !is_default) { move_begin = "std::move("; diff --git a/td/generate/tl_writer_h.cpp b/td/generate/tl_writer_h.cpp index cc3951958..d7e701cbe 100644 --- a/td/generate/tl_writer_h.cpp +++ b/td/generate/tl_writer_h.cpp @@ -68,6 +68,9 @@ std::string TD_TL_writer_h::gen_output_begin() const { bytes_type + ";\n\n" + "template \n" + "using array = std::vector;\n\n" + "using BaseObject = ::td::TlObject;\n\n" "template \n" diff --git a/td/generate/tl_writer_jni_cpp.cpp b/td/generate/tl_writer_jni_cpp.cpp index 645903a65..a0f2137bc 100644 --- a/td/generate/tl_writer_jni_cpp.cpp +++ b/td/generate/tl_writer_jni_cpp.cpp @@ -100,13 +100,13 @@ std::string TD_TL_writer_jni_cpp::gen_vector_fetch(std::string field_name, const std::string template_type; if (vector_type == "string") { template_type = "string"; - } else if (vector_type.compare(0, 11, "std::vector") == 0) { + } else if (vector_type.compare(0, 5, "array") == 0) { const tl::tl_tree_type *child = static_cast(t->children[0]); template_type = gen_type_name(child); if (template_type.compare(0, 10, "object_ptr") == 0) { template_type = gen_main_class_name(child->type); } - template_type = "std::vector<" + template_type + ">"; + template_type = "array<" + template_type + ">"; } else if (vector_type == "bytes") { template_type = "jbyteArray"; } else { @@ -248,7 +248,7 @@ std::string TD_TL_writer_jni_cpp::gen_vector_store(const std::string &field_name assert(false); // TODO } if (vector_type == "int32" || vector_type == "int53" || vector_type == "int64" || vector_type == "double" || - vector_type == "string" || vector_type.compare(0, 11, "std::vector") == 0 || + vector_type == "string" || vector_type.compare(0, 5, "array") == 0 || vector_type.compare(0, 10, "object_ptr") == 0) { return "{ " "auto arr_tmp_ = jni::store_vector(env, " + diff --git a/td/generate/tl_writer_jni_h.cpp b/td/generate/tl_writer_jni_h.cpp index d050edae6..42aa7cd60 100644 --- a/td/generate/tl_writer_jni_h.cpp +++ b/td/generate/tl_writer_jni_h.cpp @@ -91,6 +91,9 @@ std::string TD_TL_writer_jni_h::gen_output_begin() const { bytes_type + ";\n\n" + "template \n" + "using array = std::vector;\n\n" + "class " + gen_base_tl_class_name() + ";\n" diff --git a/td/generate/tl_writer_td.cpp b/td/generate/tl_writer_td.cpp index 1a9cdb431..3d5447c46 100644 --- a/td/generate/tl_writer_td.cpp +++ b/td/generate/tl_writer_td.cpp @@ -200,7 +200,7 @@ std::string TD_TL_writer::gen_type_name(const tl::tl_tree_type *tree_type) const assert(tree_type->children[0]->get_type() == tl::NODE_TYPE_TYPE); const tl::tl_tree_type *child = static_cast(tree_type->children[0]); - return "std::vector<" + gen_type_name(child) + ">"; + return "array<" + gen_type_name(child) + ">"; } assert(!is_built_in_simple_type(name) && !is_built_in_complex_type(name)); @@ -248,7 +248,7 @@ std::string TD_TL_writer::gen_constructor_parameter(int field_num, const std::st } else if (field_type == "UInt128 " || field_type == "UInt256 " || field_type == "string " || (string_type == bytes_type && field_type == "bytes ")) { res += field_type + "const &"; - } else if (field_type.compare(0, 11, "std::vector") == 0 || field_type == "bytes ") { + } else if (field_type.compare(0, 5, "array") == 0 || field_type == "bytes ") { res += field_type + "&&"; } else if (field_type.compare(0, 10, "object_ptr") == 0) { res += field_type + "&&"; diff --git a/td/telegram/td_json_client.h b/td/telegram/td_json_client.h index 6e667536c..a5562dd20 100644 --- a/td/telegram/td_json_client.h +++ b/td/telegram/td_json_client.h @@ -17,7 +17,7 @@ * optional in places where type is uniquely determined by the context. * Fields of Bool type are stored as Boolean, fields of int32, int53, and double types are stored as Number, fields of * int64 and string types are stored as String, fields of bytes type are base64 encoded and then stored as String, - * fields of vector type are stored as Array. + * fields of array type are stored as Array. * The main TDLib interface is asynchronous. To match requests with a corresponding response a field "@extra" can * be added to the request object. The corresponding response will have an "@extra" field with exactly the same value. * diff --git a/td/tl/TlObject.h b/td/tl/TlObject.h index 9e954d0bc..d6a5819aa 100644 --- a/td/tl/TlObject.h +++ b/td/tl/TlObject.h @@ -189,7 +189,7 @@ using tl_object_ptr = tl::unique_ptr; * \code * auto get_authorization_state_request = td::make_tl_object(); * auto message_text = td::make_tl_object("Hello, world!!!", - * std::vector>()); + * td::td_api::array>()); * auto send_message_request = td::make_tl_object(chat_id, 0, 0, nullptr, nullptr, * td::make_tl_object(std::move(message_text), false, true)); * \endcode