Skip to content

Commit

Permalink
fix(user_db): pointer cast error caused by multiple inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Aug 26, 2020
1 parent debc2c0 commit 2ed780b
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/rime/dict/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Recoverable {

class ResourceResolver;

class DbComponentBase : virtual public ComponentBase {
class DbComponentBase {
public:
DbComponentBase();
virtual ~DbComponentBase();
Expand All @@ -108,7 +108,7 @@ template <class DbClass>
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);
Expand Down
5 changes: 0 additions & 5 deletions src/rime/dict/level_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,6 @@ string UserDbComponent<LevelDb>::extension() const {
return ".userdb";
}

template <>
string UserDbComponent<LevelDb>::snapshot_extension() const {
return ".userdb.txt";
}

template <>
UserDbWrapper<LevelDb>::UserDbWrapper(const string& file_name,
const string& db_name)
Expand Down
12 changes: 10 additions & 2 deletions src/rime/dict/table_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

Expand All @@ -83,4 +81,14 @@ bool StableDb::Open() {
return TableDb::OpenReadOnly();
}

template <>
string DbComponent<TableDb>::extension() const {
return ".txt";
}

template <>
string DbComponent<StableDb>::extension() const {
return ".txt";
}

} // namespace rime
3 changes: 1 addition & 2 deletions src/rime/dict/user_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ string UserDbComponent<TextDb>::extension() const {
return plain_userdb_extension;
}

template <>
string UserDbComponent<TextDb>::snapshot_extension() const {
string UserDb::snapshot_extension() {
return plain_userdb_extension;
}

Expand Down
14 changes: 7 additions & 7 deletions src/rime/dict/user_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Component*>(Db::Require(name));
return dynamic_cast<Component*>(Db::Require(name));
}

UserDb() = delete;

};

/// A helper class to provide extra functionalities related to user db.
Expand Down Expand Up @@ -102,16 +104,14 @@ class UserDbWrapper : public BaseDb {
/// Implements a component that serves as a factory for a user db class.
template <class BaseDb>
class UserDbComponent : public UserDb::Component,
protected DbComponent<UserDbWrapper<BaseDb>> {
protected DbComponentBase {
public:
using UserDbImpl = UserDbWrapper<BaseDb>;

UserDbImpl* Create(const string& name) override {
return DbComponent<UserDbImpl>::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 {
Expand Down
3 changes: 1 addition & 2 deletions src/rime/dict/user_db_recovery_task.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
6 changes: 3 additions & 3 deletions src/rime/lever/user_dict_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -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() &&
Expand All @@ -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;
Expand Down

0 comments on commit 2ed780b

Please sign in to comment.