From cdafab002459346f0e3e0ff3ce6209d506823336 Mon Sep 17 00:00:00 2001 From: Kacperos155 Date: Sat, 23 Jul 2022 16:07:53 +0200 Subject: [PATCH] Small improvements & code cleanup # Re-introduce the unique_ptr with custom deleter needed to avoid including sqlite.h in Backup.h --- include/SQLiteCpp/Backup.h | 11 +++++++---- src/Backup.cpp | 28 ++++++++++++++-------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/include/SQLiteCpp/Backup.h b/include/SQLiteCpp/Backup.h index ea6f71f1..ac940cb6 100644 --- a/include/SQLiteCpp/Backup.h +++ b/include/SQLiteCpp/Backup.h @@ -96,9 +96,6 @@ class Backup Backup(const Backup&) = delete; Backup& operator=(const Backup&) = delete; - /// Release the SQLite Backup resource. - ~Backup(); - /** * @brief Execute a step of backup with a given number of source pages to be copied * @@ -121,7 +118,13 @@ class Backup int getTotalPageCount() const; private: - sqlite3_backup* mpSQLiteBackup = nullptr; ///< Pointer to SQLite Database Backup Handle + // Deleter functor to use with smart pointers to close the SQLite database backup in an RAII fashion. + struct Deleter + { + void operator()(sqlite3_backup* apBackup); + }; + + std::unique_ptr mpSQLiteBackup{}; ///< Pointer to SQLite Database Backup Handle }; } // namespace SQLite diff --git a/src/Backup.cpp b/src/Backup.cpp index 4c91e694..8aa43df6 100644 --- a/src/Backup.cpp +++ b/src/Backup.cpp @@ -24,10 +24,10 @@ Backup::Backup(Database& aDestDatabase, Database& aSrcDatabase, const char* apSrcDatabaseName) { - mpSQLiteBackup = sqlite3_backup_init(aDestDatabase.getHandle(), + mpSQLiteBackup.reset(sqlite3_backup_init(aDestDatabase.getHandle(), apDestDatabaseName, aSrcDatabase.getHandle(), - apSrcDatabaseName); + apSrcDatabaseName)); if (nullptr == mpSQLiteBackup) { // 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) : { } -// Release resource for SQLite database backup -Backup::~Backup() -{ - if (mpSQLiteBackup) - { - sqlite3_backup_finish(mpSQLiteBackup); - } -} - // Execute backup step with a given number of source pages to be copied int Backup::executeStep(const int aNumPage /* = -1 */) { - const int res = sqlite3_backup_step(mpSQLiteBackup, aNumPage); + const int res = sqlite3_backup_step(mpSQLiteBackup.get(), aNumPage); if (SQLITE_OK != res && SQLITE_DONE != res && SQLITE_BUSY != res && SQLITE_LOCKED != res) { throw SQLite::Exception(sqlite3_errstr(res), res); @@ -71,13 +62,22 @@ int Backup::executeStep(const int aNumPage /* = -1 */) // Get the number of remaining source pages to be copied in this backup process int Backup::getRemainingPageCount() const { - return sqlite3_backup_remaining(mpSQLiteBackup); + return sqlite3_backup_remaining(mpSQLiteBackup.get()); } // Get the number of total source pages to be copied in this backup process int Backup::getTotalPageCount() const { - return sqlite3_backup_pagecount(mpSQLiteBackup); + return sqlite3_backup_pagecount(mpSQLiteBackup.get()); +} + +// Release resource for SQLite database backup +void SQLite::Backup::Deleter::operator()(sqlite3_backup* apBackup) +{ + if (apBackup) + { + sqlite3_backup_finish(apBackup); + } }