Skip to content

Commit

Permalink
Use array instead of std::vector in the generated C++ classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
levlam committed Nov 15, 2020
1 parent f693874 commit 78e45da
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion example/web/tdweb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions td/generate/DoxygenTlDocumentationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private function getParameterTypeName($type)
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)) {
Expand Down Expand Up @@ -99,7 +99,7 @@ protected function getTypeName($type)
$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)) {
Expand Down Expand Up @@ -202,6 +202,13 @@ protected function addGlobalDocumentation()
* 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
Expand All @@ -225,7 +232,7 @@ protected function addGlobalDocumentation()
* \\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
Expand Down
4 changes: 2 additions & 2 deletions td/generate/tl_writer_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(\"" +
Expand Down Expand Up @@ -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(";
Expand Down
3 changes: 3 additions & 0 deletions td/generate/tl_writer_h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions td/generate/tl_writer_jni_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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, " +
Expand Down
3 changes: 3 additions & 0 deletions td/generate/tl_writer_jni_h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions td/generate/tl_writer_td.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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 + "&&";
Expand Down
2 changes: 1 addition & 1 deletion td/telegram/td_json_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion td/tl/TlObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 78e45da

Please sign in to comment.