Skip to content

Commit cdafab0

Browse files
Kacperos155SRombauts
authored andcommitted
Small improvements & code cleanup
# Re-introduce the unique_ptr with custom deleter needed to avoid including sqlite.h in Backup.h
1 parent 36a2cb3 commit cdafab0

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

include/SQLiteCpp/Backup.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ class Backup
9696
Backup(const Backup&) = delete;
9797
Backup& operator=(const Backup&) = delete;
9898

99-
/// Release the SQLite Backup resource.
100-
~Backup();
101-
10299
/**
103100
* @brief Execute a step of backup with a given number of source pages to be copied
104101
*
@@ -121,7 +118,13 @@ class Backup
121118
int getTotalPageCount() const;
122119

123120
private:
124-
sqlite3_backup* mpSQLiteBackup = nullptr; ///< Pointer to SQLite Database Backup Handle
121+
// Deleter functor to use with smart pointers to close the SQLite database backup in an RAII fashion.
122+
struct Deleter
123+
{
124+
void operator()(sqlite3_backup* apBackup);
125+
};
126+
127+
std::unique_ptr<sqlite3_backup, Deleter> mpSQLiteBackup{}; ///< Pointer to SQLite Database Backup Handle
125128
};
126129

127130
} // namespace SQLite

src/Backup.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ Backup::Backup(Database& aDestDatabase,
2424
Database& aSrcDatabase,
2525
const char* apSrcDatabaseName)
2626
{
27-
mpSQLiteBackup = sqlite3_backup_init(aDestDatabase.getHandle(),
27+
mpSQLiteBackup.reset(sqlite3_backup_init(aDestDatabase.getHandle(),
2828
apDestDatabaseName,
2929
aSrcDatabase.getHandle(),
30-
apSrcDatabaseName);
30+
apSrcDatabaseName));
3131
if (nullptr == mpSQLiteBackup)
3232
{
3333
// If an error occurs, the error code and message are attached to the destination database connection.
@@ -48,19 +48,10 @@ Backup::Backup(Database &aDestDatabase, Database &aSrcDatabase) :
4848
{
4949
}
5050

51-
// Release resource for SQLite database backup
52-
Backup::~Backup()
53-
{
54-
if (mpSQLiteBackup)
55-
{
56-
sqlite3_backup_finish(mpSQLiteBackup);
57-
}
58-
}
59-
6051
// Execute backup step with a given number of source pages to be copied
6152
int Backup::executeStep(const int aNumPage /* = -1 */)
6253
{
63-
const int res = sqlite3_backup_step(mpSQLiteBackup, aNumPage);
54+
const int res = sqlite3_backup_step(mpSQLiteBackup.get(), aNumPage);
6455
if (SQLITE_OK != res && SQLITE_DONE != res && SQLITE_BUSY != res && SQLITE_LOCKED != res)
6556
{
6657
throw SQLite::Exception(sqlite3_errstr(res), res);
@@ -71,13 +62,22 @@ int Backup::executeStep(const int aNumPage /* = -1 */)
7162
// Get the number of remaining source pages to be copied in this backup process
7263
int Backup::getRemainingPageCount() const
7364
{
74-
return sqlite3_backup_remaining(mpSQLiteBackup);
65+
return sqlite3_backup_remaining(mpSQLiteBackup.get());
7566
}
7667

7768
// Get the number of total source pages to be copied in this backup process
7869
int Backup::getTotalPageCount() const
7970
{
80-
return sqlite3_backup_pagecount(mpSQLiteBackup);
71+
return sqlite3_backup_pagecount(mpSQLiteBackup.get());
72+
}
73+
74+
// Release resource for SQLite database backup
75+
void SQLite::Backup::Deleter::operator()(sqlite3_backup* apBackup)
76+
{
77+
if (apBackup)
78+
{
79+
sqlite3_backup_finish(apBackup);
80+
}
8181
}
8282

8383

0 commit comments

Comments
 (0)