Skip to content

Commit

Permalink
Remove remaining C-Style casts.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent bf24ff9 commit 85b8a00
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 24 deletions.
64 changes: 44 additions & 20 deletions impeller/archivist/archive_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,80 @@

namespace impeller {

#define DB_HANDLE reinterpret_cast<sqlite3*>(database_)
struct ArchiveDatabase::Handle {
Handle(const std::string& filename) {
if (::sqlite3_initialize() != SQLITE_OK) {
VALIDATION_LOG << "Could not initialize sqlite.";
return;
}

ArchiveDatabase::ArchiveDatabase(const std::string& filename) {
if (::sqlite3_initialize() != SQLITE_OK) {
VALIDATION_LOG << "Could not initialize sqlite.";
return;
sqlite3* db = nullptr;
auto res = ::sqlite3_open(filename.c_str(), &db);

if (res != SQLITE_OK || db == nullptr) {
return;
}

handle_ = db;
}

sqlite3* db = nullptr;
auto res = ::sqlite3_open(filename.c_str(), &db);
database_ = db;
~Handle() {
if (handle_ == nullptr) {
return;
}
::sqlite3_close(handle_);
}

::sqlite3* Get() const { return handle_; }

bool IsValid() const { return handle_ != nullptr; }

if (res != SQLITE_OK || database_ == nullptr) {
private:
::sqlite3* handle_ = nullptr;

FML_DISALLOW_COPY_AND_ASSIGN(Handle);
};

ArchiveDatabase::ArchiveDatabase(const std::string& filename)
: handle_(std::make_unique<Handle>(filename)) {
if (!handle_->IsValid()) {
handle_.reset();
return;
}

begin_transaction_stmt_ = std::unique_ptr<ArchiveStatement>(
new ArchiveStatement(database_, "BEGIN TRANSACTION;"));
new ArchiveStatement(handle_->Get(), "BEGIN TRANSACTION;"));

if (!begin_transaction_stmt_->IsValid()) {
return;
}

end_transaction_stmt_ = std::unique_ptr<ArchiveStatement>(
new ArchiveStatement(database_, "END TRANSACTION;"));
new ArchiveStatement(handle_->Get(), "END TRANSACTION;"));

if (!end_transaction_stmt_->IsValid()) {
return;
}

rollback_transaction_stmt_ = std::unique_ptr<ArchiveStatement>(
new ArchiveStatement(database_, "ROLLBACK TRANSACTION;"));
new ArchiveStatement(handle_->Get(), "ROLLBACK TRANSACTION;"));

if (!rollback_transaction_stmt_->IsValid()) {
return;
}

ready_ = true;
}

ArchiveDatabase::~ArchiveDatabase() {
::sqlite3_close(DB_HANDLE);
}
ArchiveDatabase::~ArchiveDatabase() = default;

bool ArchiveDatabase::IsValid() const {
return ready_;
return handle_ != nullptr;
}

int64_t ArchiveDatabase::GetLastInsertRowID() {
return ::sqlite3_last_insert_rowid(DB_HANDLE);
if (!IsValid()) {
return 0u;
}
return ::sqlite3_last_insert_rowid(handle_->Get());
}

static inline const ArchiveClassRegistration* RegistrationIfReady(
Expand Down Expand Up @@ -103,7 +127,7 @@ const ArchiveClassRegistration* ArchiveDatabase::GetRegistrationForDefinition(

ArchiveStatement ArchiveDatabase::CreateStatement(
const std::string& statementString) const {
return ArchiveStatement{database_, statementString};
return ArchiveStatement{handle_ ? handle_->Get() : nullptr, statementString};
}

ArchiveTransaction ArchiveDatabase::CreateTransaction(
Expand Down
4 changes: 2 additions & 2 deletions impeller/archivist/archive_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class ArchiveDatabase {
ArchiveTransaction CreateTransaction(int64_t& transactionCount);

private:
void* database_ = nullptr;
bool ready_ = false;
struct Handle;
std::unique_ptr<Handle> handle_;
std::map<std::string, std::unique_ptr<ArchiveClassRegistration>>
registrations_;
std::unique_ptr<ArchiveStatement> begin_transaction_stmt_;
Expand Down
40 changes: 38 additions & 2 deletions impeller/archivist/archive_statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ namespace impeller {

struct ArchiveStatement::Handle {
Handle(void* db, const std::string& statememt) {
if (db == nullptr) {
return;
}
::sqlite3_stmt* handle = nullptr;
if (::sqlite3_prepare_v2(reinterpret_cast<sqlite3*>(db), //
statememt.c_str(), //
Expand All @@ -31,12 +34,12 @@ struct ArchiveStatement::Handle {
FML_CHECK(res == SQLITE_OK) << "Unable to finalize the archive.";
}

bool IsValid() { return handle_ != nullptr; }
bool IsValid() const { return handle_ != nullptr; }

::sqlite3_stmt* Get() const { return handle_; }

private:
::sqlite3_stmt* handle_;
::sqlite3_stmt* handle_ = nullptr;

FML_DISALLOW_COPY_AND_ASSIGN(Handle);
};
Expand All @@ -55,6 +58,9 @@ bool ArchiveStatement::IsValid() const {
}

bool ArchiveStatement::Reset() {
if (!IsValid()) {
return false;
}
if (::sqlite3_reset(statement_handle_->Get()) != SQLITE_OK) {
return false;
}
Expand All @@ -81,13 +87,19 @@ static constexpr int ToColumn(size_t index) {
}

size_t ArchiveStatement::GetColumnCount() {
if (!IsValid()) {
return 0u;
}
return ::sqlite3_column_count(statement_handle_->Get());
}

/*
* Bind Variants
*/
bool ArchiveStatement::WriteValue(size_t index, const std::string& item) {
if (!IsValid()) {
return false;
}
return ::sqlite3_bind_text(statement_handle_->Get(), //
ToParam(index), //
item.data(), //
Expand All @@ -96,18 +108,27 @@ bool ArchiveStatement::WriteValue(size_t index, const std::string& item) {
}

bool ArchiveStatement::BindIntegral(size_t index, int64_t item) {
if (!IsValid()) {
return false;
}
return ::sqlite3_bind_int64(statement_handle_->Get(), //
ToParam(index), //
item) == SQLITE_OK;
}

bool ArchiveStatement::WriteValue(size_t index, double item) {
if (!IsValid()) {
return false;
}
return ::sqlite3_bind_double(statement_handle_->Get(), //
ToParam(index), //
item) == SQLITE_OK;
}

bool ArchiveStatement::WriteValue(size_t index, const Allocation& item) {
if (!IsValid()) {
return false;
}
return ::sqlite3_bind_blob(statement_handle_->Get(), //
ToParam(index), //
item.GetBuffer(), //
Expand All @@ -119,11 +140,17 @@ bool ArchiveStatement::WriteValue(size_t index, const Allocation& item) {
* Column Variants
*/
bool ArchiveStatement::ColumnIntegral(size_t index, int64_t& item) {
if (!IsValid()) {
return false;
}
item = ::sqlite3_column_int64(statement_handle_->Get(), ToColumn(index));
return true;
}

bool ArchiveStatement::ReadValue(size_t index, double& item) {
if (!IsValid()) {
return false;
}
item = ::sqlite3_column_double(statement_handle_->Get(), ToColumn(index));
return true;
}
Expand All @@ -137,6 +164,9 @@ bool ArchiveStatement::ReadValue(size_t index, double& item) {
*/

bool ArchiveStatement::ReadValue(size_t index, std::string& item) {
if (!IsValid()) {
return false;
}
/*
* Get the character data
*/
Expand All @@ -156,6 +186,9 @@ bool ArchiveStatement::ReadValue(size_t index, std::string& item) {
}

bool ArchiveStatement::ReadValue(size_t index, Allocation& item) {
if (!IsValid()) {
return false;
}
/*
* Get a blob pointer
*/
Expand All @@ -180,6 +213,9 @@ bool ArchiveStatement::ReadValue(size_t index, Allocation& item) {
}

ArchiveStatement::Result ArchiveStatement::Execute() {
if (!IsValid()) {
return Result::kFailure;
}
switch (::sqlite3_step(statement_handle_->Get())) {
case SQLITE_DONE:
return Result::kDone;
Expand Down

0 comments on commit 85b8a00

Please sign in to comment.