Skip to content

Commit 2ed780b

Browse files
committed
fix(user_db): pointer cast error caused by multiple inheritance
1 parent debc2c0 commit 2ed780b

File tree

7 files changed

+24
-23
lines changed

7 files changed

+24
-23
lines changed

src/rime/dict/db.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class Recoverable {
9393

9494
class ResourceResolver;
9595

96-
class DbComponentBase : virtual public ComponentBase {
96+
class DbComponentBase {
9797
public:
9898
DbComponentBase();
9999
virtual ~DbComponentBase();
@@ -108,7 +108,7 @@ template <class DbClass>
108108
class DbComponent : public DbClass::Component,
109109
protected DbComponentBase {
110110
public:
111-
virtual string extension() const { return string(); }
111+
virtual string extension() const;
112112

113113
DbClass* Create(const string& name) override {
114114
return new DbClass(DbFilePath(name, extension()), name);

src/rime/dict/level_db.cc

-5
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,6 @@ string UserDbComponent<LevelDb>::extension() const {
349349
return ".userdb";
350350
}
351351

352-
template <>
353-
string UserDbComponent<LevelDb>::snapshot_extension() const {
354-
return ".userdb.txt";
355-
}
356-
357352
template <>
358353
UserDbWrapper<LevelDb>::UserDbWrapper(const string& file_name,
359354
const string& db_name)

src/rime/dict/table_db.cc

+10-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ TableDb::TableDb(const string& file_name, const string& db_name)
6969
: TextDb(file_name, db_name, "tabledb", TableDb::format) {
7070
}
7171

72-
// StableDb
73-
7472
StableDb::StableDb(const string& file_name, const string& db_name)
7573
: TableDb(file_name, db_name) {}
7674

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

84+
template <>
85+
string DbComponent<TableDb>::extension() const {
86+
return ".txt";
87+
}
88+
89+
template <>
90+
string DbComponent<StableDb>::extension() const {
91+
return ".txt";
92+
}
93+
8694
} // namespace rime

src/rime/dict/user_db.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ string UserDbComponent<TextDb>::extension() const {
6060
return plain_userdb_extension;
6161
}
6262

63-
template <>
64-
string UserDbComponent<TextDb>::snapshot_extension() const {
63+
string UserDb::snapshot_extension() {
6564
return plain_userdb_extension;
6665
}
6766

src/rime/dict/user_db.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,21 @@ struct UserDbValue {
3838
*/
3939
class UserDb {
4040
public:
41+
static string snapshot_extension();
42+
4143
/// Abstract class for a user db component.
4244
class Component : public Db::Component {
4345
public:
4446
virtual string extension() const = 0;
45-
virtual string snapshot_extension() const = 0;
4647
};
4748

4849
/// Requires a registered component for a user db class.
4950
static Component* Require(const string& name) {
50-
return static_cast<Component*>(Db::Require(name));
51+
return dynamic_cast<Component*>(Db::Require(name));
5152
}
5253

5354
UserDb() = delete;
55+
5456
};
5557

5658
/// A helper class to provide extra functionalities related to user db.
@@ -102,16 +104,14 @@ class UserDbWrapper : public BaseDb {
102104
/// Implements a component that serves as a factory for a user db class.
103105
template <class BaseDb>
104106
class UserDbComponent : public UserDb::Component,
105-
protected DbComponent<UserDbWrapper<BaseDb>> {
107+
protected DbComponentBase {
106108
public:
107109
using UserDbImpl = UserDbWrapper<BaseDb>;
108-
109-
UserDbImpl* Create(const string& name) override {
110-
return DbComponent<UserDbImpl>::Create(name);
110+
Db* Create(const string& name) override {
111+
return new UserDbImpl(DbFilePath(name, extension()), name);
111112
}
112113

113114
string extension() const override;
114-
string snapshot_extension() const override;
115115
};
116116

117117
class UserDbMerger : public Sink {

src/rime/dict/user_db_recovery_task.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ void UserDbRecoveryTask::RestoreUserDataFromSnapshot(Deployer* deployer) {
6767
// locate snapshot file
6868
boost::filesystem::path dir(deployer->user_data_sync_dir());
6969
// try *.userdb.txt
70-
fs::path snapshot_path =
71-
dir / (dict_name + component->snapshot_extension());
70+
fs::path snapshot_path = dir / (dict_name + UserDb::snapshot_extension());
7271
if (!fs::exists(snapshot_path)) {
7372
// try *.userdb.*.snapshot
7473
string legacy_snapshot_file =

src/rime/lever/user_dict_manager.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ bool UserDictManager::Backup(const string& dict_name) {
6767
return false;
6868
}
6969
}
70-
string snapshot_file = dict_name + user_db_component_->snapshot_extension();
70+
string snapshot_file = dict_name + UserDb::snapshot_extension();
7171
return db->Backup((dir / snapshot_file).string());
7272
}
7373

@@ -177,7 +177,7 @@ bool UserDictManager::UpgradeUserDict(const string& dict_name) {
177177
return false;
178178
}
179179
}
180-
string snapshot_file = dict_name + user_db_component_->snapshot_extension();
180+
string snapshot_file = dict_name + UserDb::snapshot_extension();
181181
fs::path snapshot_path = trash / snapshot_file;
182182
return legacy_db->Backup(snapshot_path.string()) &&
183183
legacy_db->Close() &&
@@ -197,7 +197,7 @@ bool UserDictManager::Synchronize(const string& dict_name) {
197197
}
198198
}
199199
// *.userdb.txt
200-
string snapshot_file = dict_name + user_db_component_->snapshot_extension();
200+
string snapshot_file = dict_name + UserDb::snapshot_extension();
201201
for (fs::directory_iterator it(sync_dir), end; it != end; ++it) {
202202
if (!fs::is_directory(it->path()))
203203
continue;

0 commit comments

Comments
 (0)