From 2ed780bcbcab203a6ff3745eac6eaaf36676b3eb Mon Sep 17 00:00:00 2001 From: Chen Gong Date: Wed, 26 Aug 2020 01:00:00 +0800 Subject: [PATCH] fix(user_db): pointer cast error caused by multiple inheritance --- src/rime/dict/db.h | 4 ++-- src/rime/dict/level_db.cc | 5 ----- src/rime/dict/table_db.cc | 12 ++++++++++-- src/rime/dict/user_db.cc | 3 +-- src/rime/dict/user_db.h | 14 +++++++------- src/rime/dict/user_db_recovery_task.cc | 3 +-- src/rime/lever/user_dict_manager.cc | 6 +++--- 7 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/rime/dict/db.h b/src/rime/dict/db.h index 111eac923a..1cade68f59 100644 --- a/src/rime/dict/db.h +++ b/src/rime/dict/db.h @@ -93,7 +93,7 @@ class Recoverable { class ResourceResolver; -class DbComponentBase : virtual public ComponentBase { +class DbComponentBase { public: DbComponentBase(); virtual ~DbComponentBase(); @@ -108,7 +108,7 @@ template class DbComponent : public DbClass::Component, protected DbComponentBase { public: - virtual string extension() const { return string(); } + virtual string extension() const; DbClass* Create(const string& name) override { return new DbClass(DbFilePath(name, extension()), name); diff --git a/src/rime/dict/level_db.cc b/src/rime/dict/level_db.cc index 1ab591f99c..859605e570 100644 --- a/src/rime/dict/level_db.cc +++ b/src/rime/dict/level_db.cc @@ -349,11 +349,6 @@ string UserDbComponent::extension() const { return ".userdb"; } -template <> -string UserDbComponent::snapshot_extension() const { - return ".userdb.txt"; -} - template <> UserDbWrapper::UserDbWrapper(const string& file_name, const string& db_name) diff --git a/src/rime/dict/table_db.cc b/src/rime/dict/table_db.cc index 7366e16fcc..611a551619 100644 --- a/src/rime/dict/table_db.cc +++ b/src/rime/dict/table_db.cc @@ -69,8 +69,6 @@ TableDb::TableDb(const string& file_name, const string& db_name) : TextDb(file_name, db_name, "tabledb", TableDb::format) { } -// StableDb - StableDb::StableDb(const string& file_name, const string& db_name) : TableDb(file_name, db_name) {} @@ -83,4 +81,14 @@ bool StableDb::Open() { return TableDb::OpenReadOnly(); } +template <> +string DbComponent::extension() const { + return ".txt"; +} + +template <> +string DbComponent::extension() const { + return ".txt"; +} + } // namespace rime diff --git a/src/rime/dict/user_db.cc b/src/rime/dict/user_db.cc index d104950424..c35efac333 100644 --- a/src/rime/dict/user_db.cc +++ b/src/rime/dict/user_db.cc @@ -60,8 +60,7 @@ string UserDbComponent::extension() const { return plain_userdb_extension; } -template <> -string UserDbComponent::snapshot_extension() const { +string UserDb::snapshot_extension() { return plain_userdb_extension; } diff --git a/src/rime/dict/user_db.h b/src/rime/dict/user_db.h index 4aa7398b75..8823cea0b2 100644 --- a/src/rime/dict/user_db.h +++ b/src/rime/dict/user_db.h @@ -38,19 +38,21 @@ struct UserDbValue { */ class UserDb { public: + static string snapshot_extension(); + /// Abstract class for a user db component. class Component : public Db::Component { public: virtual string extension() const = 0; - virtual string snapshot_extension() const = 0; }; /// Requires a registered component for a user db class. static Component* Require(const string& name) { - return static_cast(Db::Require(name)); + return dynamic_cast(Db::Require(name)); } UserDb() = delete; + }; /// A helper class to provide extra functionalities related to user db. @@ -102,16 +104,14 @@ class UserDbWrapper : public BaseDb { /// Implements a component that serves as a factory for a user db class. template class UserDbComponent : public UserDb::Component, - protected DbComponent> { + protected DbComponentBase { public: using UserDbImpl = UserDbWrapper; - - UserDbImpl* Create(const string& name) override { - return DbComponent::Create(name); + Db* Create(const string& name) override { + return new UserDbImpl(DbFilePath(name, extension()), name); } string extension() const override; - string snapshot_extension() const override; }; class UserDbMerger : public Sink { diff --git a/src/rime/dict/user_db_recovery_task.cc b/src/rime/dict/user_db_recovery_task.cc index 83699b7ca9..b50ae8afac 100644 --- a/src/rime/dict/user_db_recovery_task.cc +++ b/src/rime/dict/user_db_recovery_task.cc @@ -67,8 +67,7 @@ void UserDbRecoveryTask::RestoreUserDataFromSnapshot(Deployer* deployer) { // locate snapshot file boost::filesystem::path dir(deployer->user_data_sync_dir()); // try *.userdb.txt - fs::path snapshot_path = - dir / (dict_name + component->snapshot_extension()); + fs::path snapshot_path = dir / (dict_name + UserDb::snapshot_extension()); if (!fs::exists(snapshot_path)) { // try *.userdb.*.snapshot string legacy_snapshot_file = diff --git a/src/rime/lever/user_dict_manager.cc b/src/rime/lever/user_dict_manager.cc index 2a23f049da..c5258266e6 100644 --- a/src/rime/lever/user_dict_manager.cc +++ b/src/rime/lever/user_dict_manager.cc @@ -67,7 +67,7 @@ bool UserDictManager::Backup(const string& dict_name) { return false; } } - string snapshot_file = dict_name + user_db_component_->snapshot_extension(); + string snapshot_file = dict_name + UserDb::snapshot_extension(); return db->Backup((dir / snapshot_file).string()); } @@ -177,7 +177,7 @@ bool UserDictManager::UpgradeUserDict(const string& dict_name) { return false; } } - string snapshot_file = dict_name + user_db_component_->snapshot_extension(); + string snapshot_file = dict_name + UserDb::snapshot_extension(); fs::path snapshot_path = trash / snapshot_file; return legacy_db->Backup(snapshot_path.string()) && legacy_db->Close() && @@ -197,7 +197,7 @@ bool UserDictManager::Synchronize(const string& dict_name) { } } // *.userdb.txt - string snapshot_file = dict_name + user_db_component_->snapshot_extension(); + string snapshot_file = dict_name + UserDb::snapshot_extension(); for (fs::directory_iterator it(sync_dir), end; it != end; ++it) { if (!fs::is_directory(it->path())) continue;