diff --git a/td/generate/tl_writer_c.h b/td/generate/tl_writer_c.h index 6a1090a67..98bc31e77 100644 --- a/td/generate/tl_writer_c.h +++ b/td/generate/tl_writer_c.h @@ -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" diff --git a/td/generate/tl_writer_cpp.cpp b/td/generate/tl_writer_cpp.cpp index ac5e8b76f..456964aed 100644 --- a/td/generate/tl_writer_cpp.cpp +++ b/td/generate/tl_writer_cpp.cpp @@ -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" diff --git a/td/generate/tl_writer_cpp.h b/td/generate/tl_writer_cpp.h index 8d983ca1b..71a072ce7 100644 --- a/td/generate/tl_writer_cpp.h +++ b/td/generate/tl_writer_cpp.h @@ -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; diff --git a/td/generate/tl_writer_dotnet.h b/td/generate/tl_writer_dotnet.h index fd9d840c9..e240a2f34 100644 --- a/td/generate/tl_writer_dotnet.h +++ b/td/generate/tl_writer_dotnet.h @@ -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" diff --git a/td/generate/tl_writer_h.cpp b/td/generate/tl_writer_h.cpp index 19585d69e..37ace6b7d 100644 --- a/td/generate/tl_writer_h.cpp +++ b/td/generate/tl_writer_h.cpp @@ -53,10 +53,11 @@ std::string TD_TL_writer_h::gen_output_begin() const { "#include \n" "#include \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" diff --git a/td/generate/tl_writer_h.h b/td/generate/tl_writer_h.h index 273087e9c..a32b832c4 100644 --- a/td/generate/tl_writer_h.h +++ b/td/generate/tl_writer_h.h @@ -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; diff --git a/td/generate/tl_writer_hpp.cpp b/td/generate/tl_writer_hpp.cpp index 6f14a6a7e..020064eb4 100644 --- a/td/generate/tl_writer_hpp.cpp +++ b/td/generate/tl_writer_hpp.cpp @@ -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" diff --git a/td/generate/tl_writer_hpp.h b/td/generate/tl_writer_hpp.h index 4c38187b2..cfd1c13a0 100644 --- a/td/generate/tl_writer_hpp.h +++ b/td/generate/tl_writer_hpp.h @@ -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; diff --git a/td/generate/tl_writer_java.cpp b/td/generate/tl_writer_java.cpp index 339fefce2..79e6da3c6 100644 --- a/td/generate/tl_writer_java.cpp +++ b/td/generate/tl_writer_java.cpp @@ -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" diff --git a/td/generate/tl_writer_java.h b/td/generate/tl_writer_java.h index 666486cc0..1ef943cf7 100644 --- a/td/generate/tl_writer_java.h +++ b/td/generate/tl_writer_java.h @@ -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 &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; diff --git a/td/generate/tl_writer_jni_cpp.cpp b/td/generate/tl_writer_jni_cpp.cpp index cc531f54d..5656db787 100644 --- a/td/generate/tl_writer_jni_cpp.cpp +++ b/td/generate/tl_writer_jni_cpp.cpp @@ -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" diff --git a/td/generate/tl_writer_jni_cpp.h b/td/generate/tl_writer_jni_cpp.h index 7ceda54b9..853af8cd4 100644 --- a/td/generate/tl_writer_jni_cpp.h +++ b/td/generate/tl_writer_jni_cpp.h @@ -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 &vars, int parser_type) const; diff --git a/td/generate/tl_writer_jni_h.cpp b/td/generate/tl_writer_jni_h.cpp index 095cfed52..cb93b6637 100644 --- a/td/generate/tl_writer_jni_h.cpp +++ b/td/generate/tl_writer_jni_h.cpp @@ -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 \n" - "using array = std::vector;\n\n" - - "class " + - gen_base_tl_class_name() + - ";\n" - "using BaseObject = " + - gen_base_tl_class_name() + - ";\n\n" - - "template \n" - "using object_ptr = ::td::tl_object_ptr;\n\n" - "template \n" - "object_ptr make_object(Args &&... args) {\n" - " return object_ptr(new Type(std::forward(args)...));\n" - "}\n\n" - - "template \n" - "object_ptr move_object_as(FromType &&from) {\n" - " return object_ptr(static_cast(from.release()));\n" - "}\n\n" - - "std::string to_string(const BaseObject &value);\n\n" - - "template \n" - "std::string to_string(const object_ptr &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, diff --git a/td/generate/tl_writer_jni_h.h b/td/generate/tl_writer_jni_h.h index 78479756a..f9662a344 100644 --- a/td/generate/tl_writer_jni_h.h +++ b/td/generate/tl_writer_jni_h.h @@ -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; diff --git a/tdtl/td/tl/tl_generate.cpp b/tdtl/td/tl/tl_generate.cpp index 66d0dcee6..a3d0fd5d5 100644 --- a/tdtl/td/tl/tl_generate.cpp +++ b/tdtl/td/tl/tl_generate.cpp @@ -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 request_types; std::set result_types; diff --git a/tdtl/td/tl/tl_writer.h b/tdtl/td/tl/tl_writer.h index 66dbdd1f5..cce63bf8e 100644 --- a/tdtl/td/tl/tl_writer.h +++ b/tdtl/td/tl/tl_writer.h @@ -84,6 +84,7 @@ class TL_writer { virtual std::string gen_int_const(const tl_tree *tree_c, const std::vector &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;