Extract gen_output_begin_once from gen_output_begin.

This commit is contained in:
levlam 2023-06-26 13:14:26 +03:00
parent d171366faa
commit aab207cdd2
16 changed files with 66 additions and 73 deletions

View File

@ -228,16 +228,28 @@ class TlWriterCCommon final : public tl::TL_writer {
"#ifdef __cplusplus\n"
"extern \"C\" {\n"
"#endif\n"
"struct TdBytes {\n"
" unsigned char *data;\n"
" int len;\n"
"};\n"
"#define TDC_VECTOR(tdc_type_name,tdc_type) \\\n"
" struct TdVector ## tdc_type_name { \\\n"
" int len;\\\n"
" tdc_type *data;\\\n"
" };\\\n"
"\n"
"\n";
}
if (is_header_ == -1) {
return "#pragma once\n" + gen_import_declaration("td/telegram/td_tdc_api.h", false) +
gen_import_declaration("td/telegram/td_api.h", false);
}
return gen_import_declaration("td/telegram/td_tdc_api_inner.h", false) + "\n" +
gen_import_declaration("td/utils/format.h", false) + gen_import_declaration("td/utils/logging.h", false) +
gen_import_declaration("td/utils/misc.h", false) + gen_import_declaration("td/utils/Slice.h", false) + "\n";
}
std::string gen_output_begin_once() const final {
if (is_header_ == 1) {
return "struct TdBytes {\n"
" unsigned char *data;\n"
" int len;\n"
"};\n"
"TDC_VECTOR(Int,int)\n"
"TDC_VECTOR(Long,long long)\n"
"TDC_VECTOR(String,char *)\n"
@ -265,14 +277,9 @@ class TlWriterCCommon final : public tl::TL_writer {
" int (*is_nil)(void);\n"
"};\n";
}
if (is_header_ == -1) {
return "#pragma once\n" + gen_import_declaration("td/telegram/td_tdc_api.h", false) +
gen_import_declaration("td/telegram/td_api.h", false);
}
return gen_import_declaration("td/telegram/td_tdc_api_inner.h", false) + "\n" +
gen_import_declaration("td/utils/format.h", false) + gen_import_declaration("td/utils/logging.h", false) +
gen_import_declaration("td/utils/misc.h", false) + gen_import_declaration("td/utils/Slice.h", false) + "\n";
return std::string();
}
std::string gen_output_end() const final {
if (is_header_ == 1) {
return "#ifdef __cplusplus\n"

View File

@ -29,9 +29,11 @@ std::string TD_TL_writer_cpp::gen_output_begin() const {
"#include \"td/utils/TlStorerToString.h\"\n\n"
"namespace td {\n"
"namespace " +
tl_name +
" {\n\n"
"std::string to_string(const BaseObject &value) {\n"
tl_name + " {\n\n";
}
std::string TD_TL_writer_cpp::gen_output_begin_once() const {
return "std::string to_string(const BaseObject &value) {\n"
" TlStorerToString storer;\n"
" value.store(storer, \"\");\n"
" return storer.move_as_string();\n"

View File

@ -42,6 +42,7 @@ class TD_TL_writer_cpp : public TD_TL_writer {
}
std::string gen_output_begin() const override;
std::string gen_output_begin_once() const override;
std::string gen_output_end() const override;
std::string gen_forward_class_declaration(const std::string &class_name, bool is_proxy) const override;

View File

@ -180,6 +180,7 @@ class TlWriterDotNet final : public TL_writer {
return gen_main_class_name(t) + "^";
}
std::string gen_output_begin(void) const final {
return prefix_ +
"#include \"td/tl/tl_dotnet_object.h\"\n\n"
@ -187,6 +188,11 @@ class TlWriterDotNet final : public TL_writer {
"namespace Td {\n"
"namespace Api {\n";
}
std::string gen_output_begin_once(void) const final {
return std::string();
}
std::string gen_output_end() const final {
return "}\n"
"}\n"

View File

@ -53,10 +53,11 @@ std::string TD_TL_writer_h::gen_output_begin() const {
"#include <utility>\n"
"#include <vector>\n\n"
"namespace td {\n" +
ext_forward_declaration + "namespace " + tl_name +
" {\n\n"
ext_forward_declaration + "namespace " + tl_name + " {\n\n";
}
"using int32 = std::int32_t;\n"
std::string TD_TL_writer_h::gen_output_begin_once() const {
return "using int32 = std::int32_t;\n"
"using int53 = std::int64_t;\n"
"using int64 = std::int64_t;\n\n"

View File

@ -29,6 +29,7 @@ class TD_TL_writer_h : public TD_TL_writer {
}
std::string gen_output_begin() const override;
std::string gen_output_begin_once() const override;
std::string gen_output_end() const override;
std::string gen_forward_class_declaration(const std::string &class_name, bool is_proxy) const override;

View File

@ -57,6 +57,10 @@ std::string TD_TL_writer_hpp::gen_output_begin() const {
tl_name + " {\n\n";
}
std::string TD_TL_writer_hpp::gen_output_begin_once() const {
return std::string();
}
std::string TD_TL_writer_hpp::gen_output_end() const {
return "} // namespace " + tl_name +
"\n"

View File

@ -29,6 +29,7 @@ class TD_TL_writer_hpp final : public TD_TL_writer {
std::string gen_base_tl_class_name() const final;
std::string gen_output_begin() const final;
std::string gen_output_begin_once() const final;
std::string gen_output_end() const final;
std::string gen_forward_class_declaration(const std::string &class_name, bool is_proxy) const final;

View File

@ -191,10 +191,11 @@ std::string TD_TL_writer_java::gen_int_const(const tl::tl_tree *tree_c,
}
std::string TD_TL_writer_java::gen_output_begin() const {
return "package " + package_name +
";\n\n"
"public class " +
tl_name +
return "package " + package_name + ";\n\n";
}
std::string TD_TL_writer_java::gen_output_begin_once() const {
return "public class " + tl_name +
" {\n"
" static {\n"
" try {\n"

View File

@ -54,6 +54,7 @@ class TD_TL_writer_java final : public tl::TL_writer {
std::string gen_int_const(const tl::tl_tree *tree_c, const std::vector<tl::var_description> &vars) const final;
std::string gen_output_begin() const final;
std::string gen_output_begin_once() const final;
std::string gen_output_end() const final;
std::string gen_import_declaration(const std::string &name, bool is_system) const final;

View File

@ -11,8 +11,8 @@
namespace td {
std::string TD_TL_writer_jni_cpp::gen_output_begin() const {
return TD_TL_writer_cpp::gen_output_begin() +
std::string TD_TL_writer_jni_cpp::gen_output_begin_once() const {
return TD_TL_writer_cpp::gen_output_begin_once() +
"\nstatic const char *package_name = \"Call set_package_name\";\n\n"
"void set_package_name(const char *new_package_name) {\n"
" package_name = new_package_name;\n"

View File

@ -16,7 +16,7 @@
namespace td {
class TD_TL_writer_jni_cpp final : public TD_TL_writer_cpp {
std::string gen_output_begin() const final;
std::string gen_output_begin_once() const final;
std::string gen_vector_fetch(std::string field_name, const tl::tl_tree_type *t,
const std::vector<tl::var_description> &vars, int parser_type) const;

View File

@ -76,55 +76,20 @@ std::string TD_TL_writer_jni_h::gen_output_begin() const {
forward_declaration("TlStorerToString") +
"\n"
"namespace " +
tl_name +
" {\n\n"
tl_name + " {\n\n";
}
"using int32 = std::int32_t;\n"
"using int53 = std::int64_t;\n"
"using int64 = std::int64_t;\n\n"
"using string = " +
string_type +
";\n\n"
"using bytes = " +
bytes_type +
";\n\n"
"template <class Type>\n"
"using array = std::vector<Type>;\n\n"
"class " +
gen_base_tl_class_name() +
";\n"
"using BaseObject = " +
gen_base_tl_class_name() +
";\n\n"
"template <class Type>\n"
"using object_ptr = ::td::tl_object_ptr<Type>;\n\n"
"template <class Type, class... Args>\n"
"object_ptr<Type> make_object(Args &&... args) {\n"
" return object_ptr<Type>(new Type(std::forward<Args>(args)...));\n"
"}\n\n"
"template <class ToType, class FromType>\n"
"object_ptr<ToType> move_object_as(FromType &&from) {\n"
" return object_ptr<ToType>(static_cast<ToType *>(from.release()));\n"
"}\n\n"
"std::string to_string(const BaseObject &value);\n\n"
"template <class T>\n"
"std::string to_string(const object_ptr<T> &value) {\n"
" if (value == nullptr) {\n"
" return \"null\";\n"
" }\n"
"\n"
" return to_string(*value);\n"
"}\n\n"
"void set_package_name(const char *new_package_name);\n\n";
std::string TD_TL_writer_jni_h::gen_output_begin_once() const {
std::string result = TD_TL_writer_h::gen_output_begin_once();
std::string old_base_object = "using BaseObject = ::td::TlObject";
std::size_t pos = result.find(old_base_object);
assert(pos != std::string::npos);
result.replace(pos, old_base_object.size(),
"class " + gen_base_tl_class_name() +
";\n"
"using BaseObject = " +
gen_base_tl_class_name());
return result + "void set_package_name(const char *new_package_name);\n\n";
}
std::string TD_TL_writer_jni_h::gen_class_begin(const std::string &class_name, const std::string &base_class_name,

View File

@ -33,6 +33,7 @@ class TD_TL_writer_jni_h final : public TD_TL_writer_h {
std::string gen_base_tl_class_name() const final;
std::string gen_output_begin() const final;
std::string gen_output_begin_once() const final;
std::string gen_class_begin(const std::string &class_name, const std::string &base_class_name, bool is_proxy,
const tl::tl_tree *result) const final;

View File

@ -768,6 +768,7 @@ void write_tl(const tl_config &config, tl_outputer &out, const TL_writer &w) {
find_complex_types(config, w);
out.append(w.gen_output_begin());
out.append(w.gen_output_begin_once());
std::set<std::string> request_types;
std::set<std::string> result_types;

View File

@ -84,6 +84,7 @@ class TL_writer {
virtual std::string gen_int_const(const tl_tree *tree_c, const std::vector<var_description> &vars) const = 0;
virtual std::string gen_output_begin() const = 0;
virtual std::string gen_output_begin_once() const = 0;
virtual std::string gen_output_end() const = 0;
virtual std::string gen_import_declaration(const std::string &name, bool is_system) const = 0;