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" "#ifdef __cplusplus\n"
"extern \"C\" {\n" "extern \"C\" {\n"
"#endif\n" "#endif\n"
"struct TdBytes {\n"
" unsigned char *data;\n"
" int len;\n"
"};\n"
"#define TDC_VECTOR(tdc_type_name,tdc_type) \\\n" "#define TDC_VECTOR(tdc_type_name,tdc_type) \\\n"
" struct TdVector ## tdc_type_name { \\\n" " struct TdVector ## tdc_type_name { \\\n"
" int len;\\\n" " int len;\\\n"
" tdc_type *data;\\\n" " tdc_type *data;\\\n"
" };\\\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(Int,int)\n"
"TDC_VECTOR(Long,long long)\n" "TDC_VECTOR(Long,long long)\n"
"TDC_VECTOR(String,char *)\n" "TDC_VECTOR(String,char *)\n"
@ -265,14 +277,9 @@ class TlWriterCCommon final : public tl::TL_writer {
" int (*is_nil)(void);\n" " int (*is_nil)(void);\n"
"};\n"; "};\n";
} }
if (is_header_ == -1) { return std::string();
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_end() const final { std::string gen_output_end() const final {
if (is_header_ == 1) { if (is_header_ == 1) {
return "#ifdef __cplusplus\n" 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" "#include \"td/utils/TlStorerToString.h\"\n\n"
"namespace td {\n" "namespace td {\n"
"namespace " + "namespace " +
tl_name + tl_name + " {\n\n";
" {\n\n" }
"std::string to_string(const BaseObject &value) {\n"
std::string TD_TL_writer_cpp::gen_output_begin_once() const {
return "std::string to_string(const BaseObject &value) {\n"
" TlStorerToString storer;\n" " TlStorerToString storer;\n"
" value.store(storer, \"\");\n" " value.store(storer, \"\");\n"
" return storer.move_as_string();\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() const override;
std::string gen_output_begin_once() const override;
std::string gen_output_end() 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; 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) + "^"; return gen_main_class_name(t) + "^";
} }
std::string gen_output_begin(void) const final { std::string gen_output_begin(void) const final {
return prefix_ + return prefix_ +
"#include \"td/tl/tl_dotnet_object.h\"\n\n" "#include \"td/tl/tl_dotnet_object.h\"\n\n"
@ -187,6 +188,11 @@ class TlWriterDotNet final : public TL_writer {
"namespace Td {\n" "namespace Td {\n"
"namespace Api {\n"; "namespace Api {\n";
} }
std::string gen_output_begin_once(void) const final {
return std::string();
}
std::string gen_output_end() const final { std::string gen_output_end() const final {
return "}\n" return "}\n"
"}\n" "}\n"

View File

@ -53,10 +53,11 @@ std::string TD_TL_writer_h::gen_output_begin() const {
"#include <utility>\n" "#include <utility>\n"
"#include <vector>\n\n" "#include <vector>\n\n"
"namespace td {\n" + "namespace td {\n" +
ext_forward_declaration + "namespace " + tl_name + ext_forward_declaration + "namespace " + tl_name + " {\n\n";
" {\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 int53 = std::int64_t;\n"
"using int64 = std::int64_t;\n\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() const override;
std::string gen_output_begin_once() const override;
std::string gen_output_end() 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; 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"; 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 { std::string TD_TL_writer_hpp::gen_output_end() const {
return "} // namespace " + tl_name + return "} // namespace " + tl_name +
"\n" "\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_base_tl_class_name() const final;
std::string gen_output_begin() 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_output_end() const final;
std::string gen_forward_class_declaration(const std::string &class_name, bool is_proxy) 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 { std::string TD_TL_writer_java::gen_output_begin() const {
return "package " + package_name + return "package " + package_name + ";\n\n";
";\n\n" }
"public class " +
tl_name + std::string TD_TL_writer_java::gen_output_begin_once() const {
return "public class " + tl_name +
" {\n" " {\n"
" static {\n" " static {\n"
" try {\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_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() const final;
std::string gen_output_begin_once() const final;
std::string gen_output_end() const final; std::string gen_output_end() const final;
std::string gen_import_declaration(const std::string &name, bool is_system) const final; std::string gen_import_declaration(const std::string &name, bool is_system) const final;

View File

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

View File

@ -16,7 +16,7 @@
namespace td { namespace td {
class TD_TL_writer_jni_cpp final : public TD_TL_writer_cpp { 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, 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; 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") + forward_declaration("TlStorerToString") +
"\n" "\n"
"namespace " + "namespace " +
tl_name + tl_name + " {\n\n";
" {\n\n" }
"using int32 = std::int32_t;\n" std::string TD_TL_writer_jni_h::gen_output_begin_once() const {
"using int53 = std::int64_t;\n" std::string result = TD_TL_writer_h::gen_output_begin_once();
"using int64 = std::int64_t;\n\n" std::string old_base_object = "using BaseObject = ::td::TlObject";
std::size_t pos = result.find(old_base_object);
"using string = " + assert(pos != std::string::npos);
string_type + result.replace(pos, old_base_object.size(),
";\n\n" "class " + gen_base_tl_class_name() +
";\n"
"using bytes = " + "using BaseObject = " +
bytes_type + gen_base_tl_class_name());
";\n\n" return result + "void set_package_name(const char *new_package_name);\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_class_begin(const std::string &class_name, const std::string &base_class_name, 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_base_tl_class_name() const final;
std::string gen_output_begin() 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, 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; 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); find_complex_types(config, w);
out.append(w.gen_output_begin()); out.append(w.gen_output_begin());
out.append(w.gen_output_begin_once());
std::set<std::string> request_types; std::set<std::string> request_types;
std::set<std::string> result_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_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() const = 0;
virtual std::string gen_output_begin_once() const = 0;
virtual std::string gen_output_end() 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; virtual std::string gen_import_declaration(const std::string &name, bool is_system) const = 0;