Remove remaining C-Style casts.

This commit is contained in:
Chinmay Garde
2021-12-12 14:27:47 -08:00
committed by Dan Field
parent 9d4b8c3658
commit 4d53e2a928
3 changed files with 84 additions and 24 deletions

View File

@@ -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_);
}
if (res != SQLITE_OK || database_ == nullptr) {
::sqlite3* Get() const { return handle_; }
bool IsValid() const { return handle_ != 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(
@@ -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(

View File

@@ -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_;

View File

@@ -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(), //
@@ -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);
};
@@ -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;
}
@@ -81,6 +87,9 @@ static constexpr int ToColumn(size_t index) {
}
size_t ArchiveStatement::GetColumnCount() {
if (!IsValid()) {
return 0u;
}
return ::sqlite3_column_count(statement_handle_->Get());
}
@@ -88,6 +97,9 @@ size_t ArchiveStatement::GetColumnCount() {
* 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(), //
@@ -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(), //
@@ -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;
}
@@ -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
*/
@@ -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
*/
@@ -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;