diff --git a/td/telegram/files/FileDb.cpp b/td/telegram/files/FileDb.cpp index fa129a4e8..9290d48b7 100644 --- a/td/telegram/files/FileDb.cpp +++ b/td/telegram/files/FileDb.cpp @@ -185,7 +185,7 @@ class FileDb : public FileDbInterface { } string generate_key; if (file_data.generate_.type_ == GenerateFileLocation::Type::Full) { - generate_key = as_key(file_data.generate_.full_); + generate_key = as_key(file_data.generate_.full()); } send_closure(file_db_actor_, &FileDbActor::clear_file_data, id, remote_key, local_key, generate_key); } @@ -200,7 +200,7 @@ class FileDb : public FileDbInterface { } string generate_key; if (file_data.generate_.type_ == GenerateFileLocation::Type::Full && new_generate) { - generate_key = as_key(file_data.generate_.full_); + generate_key = as_key(file_data.generate_.full()); } LOG(DEBUG) << "SAVE " << id << " -> " << file_data << " " << tag("remote", format::as_hex_dump<4>(Slice(remote_key))) diff --git a/td/telegram/files/FileLoaderActor.h b/td/telegram/files/FileLoaderActor.h index 9121c8a4e..d802f589c 100644 --- a/td/telegram/files/FileLoaderActor.h +++ b/td/telegram/files/FileLoaderActor.h @@ -11,7 +11,7 @@ namespace td { -struct LocalFileLocation; +class LocalFileLocation; class ResourceManager; class FileLoaderActor : public NetQueryCallback { diff --git a/td/telegram/files/FileLocation.h b/td/telegram/files/FileLocation.h index de46f6d9e..cfe25c8f7 100644 --- a/td/telegram/files/FileLocation.h +++ b/td/telegram/files/FileLocation.h @@ -695,8 +695,10 @@ inline StringBuilder &operator<<(StringBuilder &string_builder, return string_builder << "]"; } -struct RemoteFileLocation { - enum class Type : int32 { Empty, Partial, Full } type_; +class RemoteFileLocation { + public: + enum class Type : int32 { Empty, Partial, Full }; + Type type_; Variant variant_; template @@ -710,18 +712,12 @@ struct RemoteFileLocation { }); CHECK(ok); } - EmptyRemoteFileLocation &empty_location() { - return variant_.get<0>(); - } PartialRemoteFileLocation &partial() { return variant_.get<1>(); } FullRemoteFileLocation &full() { return variant_.get<2>(); } - const EmptyRemoteFileLocation &empty_location() const { - return variant_.get<0>(); - } const PartialRemoteFileLocation &partial() const { return variant_.get<1>(); } @@ -734,7 +730,7 @@ struct RemoteFileLocation { switch (type_) { case Type::Empty: { variant_ = EmptyRemoteFileLocation(); - return empty_location().parse(parser); + return; } case Type::Partial: { variant_ = PartialRemoteFileLocation(); @@ -792,8 +788,8 @@ inline bool operator!=(const EmptyLocalFileLocation &lhs, const EmptyLocalFileLo struct PartialLocalFileLocation { FileType type_; string path_; - int part_size_; - int ready_part_count_; + int32 part_size_; + int32 ready_part_count_; string iv_; template @@ -880,23 +876,18 @@ inline StringBuilder &operator<<(StringBuilder &sb, const FullLocalFileLocation return sb << tag("path", location.path_); } -struct LocalFileLocation { +class LocalFileLocation { + public: enum class Type : int32 { Empty, Partial, Full }; Type type_; Variant variant_; - EmptyLocalFileLocation &empty_location() { - return variant_.get<0>(); - } PartialLocalFileLocation &partial() { return variant_.get<1>(); } FullLocalFileLocation &full() { return variant_.get<2>(); } - const EmptyLocalFileLocation &empty_location() const { - return variant_.get<0>(); - } const PartialLocalFileLocation &partial() const { return variant_.get<1>(); } @@ -920,7 +911,7 @@ struct LocalFileLocation { switch (type_) { case Type::Empty: variant_ = EmptyLocalFileLocation(); - return parse(empty_location(), parser); + return; case Type::Partial: variant_ = PartialLocalFileLocation(); return parse(partial(), parser); @@ -950,23 +941,6 @@ inline bool operator!=(const LocalFileLocation &lhs, const LocalFileLocation &rh return !(lhs == rhs); } -struct EmptyGenerateFileLocation { - template - void store(StorerT &storer) const { - } - template - void parse(ParserT &parser) { - } -}; - -inline bool operator==(const EmptyGenerateFileLocation &lhs, const EmptyGenerateFileLocation &rhs) { - return true; -} - -inline bool operator!=(const EmptyGenerateFileLocation &lhs, const EmptyGenerateFileLocation &rhs) { - return !(lhs == rhs); -} - struct FullGenerateFileLocation { FileType type_{FileType::None}; string original_path_; @@ -1019,33 +993,41 @@ inline StringBuilder &operator<<(StringBuilder &string_builder, << tag("conversion", full_generated_file_location.conversion_) << "]"; } -struct GenerateFileLocation { +class GenerateFileLocation { + public: enum class Type : int32 { Empty, Full }; Type type_; - EmptyGenerateFileLocation empty_; - FullGenerateFileLocation full_; + + FullGenerateFileLocation &full() { + CHECK(type_ == Type::Full); + return full_; + } + const FullGenerateFileLocation &full() const { + CHECK(type_ == Type::Full); + return full_; + } template void store(StorerT &storer) const { - storer.store_int(static_cast(type_)); + td::store(type_, storer); switch (type_) { case Type::Empty: - return empty_.store(storer); + return; case Type::Full: - return full_.store(storer); + return td::store(full_, storer); } - UNREACHABLE(); } + template void parse(ParserT &parser) { - type_ = static_cast(parser.fetch_int()); + td::parse(type_, parser); switch (type_) { case Type::Empty: - return empty_.parse(parser); + return; case Type::Full: - return full_.parse(parser); + return td::parse(full_, parser); } - return parser.set_error("Invalid type in LocalFileLocation"); + return parser.set_error("Invalid type in GenerateFileLocation"); } GenerateFileLocation() : type_(Type::Empty) { @@ -1053,9 +1035,13 @@ struct GenerateFileLocation { explicit GenerateFileLocation(const FullGenerateFileLocation &full) : type_(Type::Full), full_(full) { } + GenerateFileLocation(FileType file_type, string original_path, string conversion) - : type_(Type::Full), full_(file_type, std::move(original_path), std::move(conversion)) { + : type_(Type::Full), full_{file_type, std::move(original_path), std::move(conversion)} { } + + private: + FullGenerateFileLocation full_; }; inline bool operator==(const GenerateFileLocation &lhs, const GenerateFileLocation &rhs) { @@ -1064,9 +1050,9 @@ inline bool operator==(const GenerateFileLocation &lhs, const GenerateFileLocati } switch (lhs.type_) { case GenerateFileLocation::Type::Empty: - return lhs.empty_ == rhs.empty_; + return true; case GenerateFileLocation::Type::Full: - return lhs.full_ == rhs.full_; + return lhs.full() == rhs.full(); } UNREACHABLE(); return false; diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index dcfe61c77..16633d724 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -184,7 +184,7 @@ bool FileNode::need_pmc_flush() const { bool has_generate_location = generate_.type_ == GenerateFileLocation::Type::Full; // Do not save "#file_id#" conversion. - if (has_generate_location && begins_with(generate_.full_.conversion_, "#file_id#")) { + if (has_generate_location && begins_with(generate_.full().conversion_, "#file_id#")) { has_generate_location = false; } @@ -229,7 +229,7 @@ bool FileView::has_generate_location() const { } const FullGenerateFileLocation &FileView::generate_location() const { CHECK(has_generate_location()); - return node_->generate_.full_; + return node_->generate_.full(); } int64 FileView::size() const { @@ -1112,7 +1112,7 @@ void FileManager::flush_to_pmc(FileNode *node, bool new_remote, bool new_local, data.generate_ = node->generate_; if (data.generate_.type_ == GenerateFileLocation::Type::Full && - begins_with(data.generate_.full_.conversion_, "#file_id#")) { + begins_with(data.generate_.full().conversion_, "#file_id#")) { data.generate_ = GenerateFileLocation(); } @@ -1542,7 +1542,7 @@ void FileManager::run_generate(FileNode *node) { QueryId id = queries_container_.create(Query{file_id, Query::Generate}); node->generate_id_ = id; - send_closure(file_generate_manager_, &FileGenerateManager::generate_file, id, node->generate_.full_, node->local_, + send_closure(file_generate_manager_, &FileGenerateManager::generate_file, id, node->generate_.full(), node->local_, node->name_, [file_manager = this, id] { class Callback : public FileGenerateCallback { ActorId actor_;