Use array instead of std::vector in the generated C++ classes.

This commit is contained in:
levlam 2020-11-15 19:57:03 +03:00
parent f693874c50
commit 78e45da6b9
9 changed files with 26 additions and 13 deletions

View File

@ -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

View File

@ -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<Type>;', <<<EOT
/**
* This type is used to store a list of objects of any type and is represented as Array in JSON.
*/
EOT
);
$this->addDocumentation('using BaseObject', <<<EOT
@ -225,7 +232,7 @@ EOT
* \\code
* auto get_authorization_state_request = td::td_api::make_object<td::td_api::getAuthorizationState>();
* auto message_text = td::td_api::make_object<td::td_api::formattedText>("Hello, world!!!",
* std::vector<td::td_api::object_ptr<td::td_api::textEntity>>());
* td::td_api::array<td::td_api::object_ptr<td::td_api::textEntity>>());
* auto send_message_request = td::td_api::make_object<td::td_api::sendMessage>(chat_id, 0, 0, nullptr, nullptr,
* td::td_api::make_object<td::td_api::inputMessageText>(std::move(message_text), false, true));
* \\endcode

View File

@ -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<tl::var_description> &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<std::uint32_t>(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(";

View File

@ -68,6 +68,9 @@ std::string TD_TL_writer_h::gen_output_begin() const {
bytes_type +
";\n\n"
"template <class Type>\n"
"using array = std::vector<Type>;\n\n"
"using BaseObject = ::td::TlObject;\n\n"
"template <class Type>\n"

View File

@ -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<const tl::tl_tree_type *>(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, " +

View File

@ -91,6 +91,9 @@ std::string TD_TL_writer_jni_h::gen_output_begin() const {
bytes_type +
";\n\n"
"template <class Type>\n"
"using array = std::vector<Type>;\n\n"
"class " +
gen_base_tl_class_name() +
";\n"

View File

@ -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<const tl::tl_tree_type *>(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 + "&&";

View File

@ -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.
*

View File

@ -189,7 +189,7 @@ using tl_object_ptr = tl::unique_ptr<Type>;
* \code
* auto get_authorization_state_request = td::make_tl_object<td::td_api::getAuthorizationState>();
* auto message_text = td::make_tl_object<td::td_api::formattedText>("Hello, world!!!",
* std::vector<td::tl_object_ptr<td::td_api::textEntity>>());
* td::td_api::array<td::tl_object_ptr<td::td_api::textEntity>>());
* auto send_message_request = td::make_tl_object<td::td_api::sendMessage>(chat_id, 0, 0, nullptr, nullptr,
* td::make_tl_object<td::td_api::inputMessageText>(std::move(message_text), false, true));
* \endcode