Skip to content

Commit 85b8a00

Browse files
chinmaygardednfield
authored andcommitted
Remove remaining C-Style casts.
1 parent bf24ff9 commit 85b8a00

File tree

3 files changed

+84
-24
lines changed

3 files changed

+84
-24
lines changed

impeller/archivist/archive_database.cc

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,56 +16,80 @@
1616

1717
namespace impeller {
1818

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

21-
ArchiveDatabase::ArchiveDatabase(const std::string& filename) {
22-
if (::sqlite3_initialize() != SQLITE_OK) {
23-
VALIDATION_LOG << "Could not initialize sqlite.";
24-
return;
26+
sqlite3* db = nullptr;
27+
auto res = ::sqlite3_open(filename.c_str(), &db);
28+
29+
if (res != SQLITE_OK || db == nullptr) {
30+
return;
31+
}
32+
33+
handle_ = db;
2534
}
2635

27-
sqlite3* db = nullptr;
28-
auto res = ::sqlite3_open(filename.c_str(), &db);
29-
database_ = db;
36+
~Handle() {
37+
if (handle_ == nullptr) {
38+
return;
39+
}
40+
::sqlite3_close(handle_);
41+
}
42+
43+
::sqlite3* Get() const { return handle_; }
44+
45+
bool IsValid() const { return handle_ != nullptr; }
3046

31-
if (res != SQLITE_OK || database_ == nullptr) {
47+
private:
48+
::sqlite3* handle_ = nullptr;
49+
50+
FML_DISALLOW_COPY_AND_ASSIGN(Handle);
51+
};
52+
53+
ArchiveDatabase::ArchiveDatabase(const std::string& filename)
54+
: handle_(std::make_unique<Handle>(filename)) {
55+
if (!handle_->IsValid()) {
56+
handle_.reset();
3257
return;
3358
}
3459

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

3863
if (!begin_transaction_stmt_->IsValid()) {
3964
return;
4065
}
4166

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

4570
if (!end_transaction_stmt_->IsValid()) {
4671
return;
4772
}
4873

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

5277
if (!rollback_transaction_stmt_->IsValid()) {
5378
return;
5479
}
55-
56-
ready_ = true;
5780
}
5881

59-
ArchiveDatabase::~ArchiveDatabase() {
60-
::sqlite3_close(DB_HANDLE);
61-
}
82+
ArchiveDatabase::~ArchiveDatabase() = default;
6283

6384
bool ArchiveDatabase::IsValid() const {
64-
return ready_;
85+
return handle_ != nullptr;
6586
}
6687

6788
int64_t ArchiveDatabase::GetLastInsertRowID() {
68-
return ::sqlite3_last_insert_rowid(DB_HANDLE);
89+
if (!IsValid()) {
90+
return 0u;
91+
}
92+
return ::sqlite3_last_insert_rowid(handle_->Get());
6993
}
7094

7195
static inline const ArchiveClassRegistration* RegistrationIfReady(
@@ -103,7 +127,7 @@ const ArchiveClassRegistration* ArchiveDatabase::GetRegistrationForDefinition(
103127

104128
ArchiveStatement ArchiveDatabase::CreateStatement(
105129
const std::string& statementString) const {
106-
return ArchiveStatement{database_, statementString};
130+
return ArchiveStatement{handle_ ? handle_->Get() : nullptr, statementString};
107131
}
108132

109133
ArchiveTransaction ArchiveDatabase::CreateTransaction(

impeller/archivist/archive_database.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class ArchiveDatabase {
3535
ArchiveTransaction CreateTransaction(int64_t& transactionCount);
3636

3737
private:
38-
void* database_ = nullptr;
39-
bool ready_ = false;
38+
struct Handle;
39+
std::unique_ptr<Handle> handle_;
4040
std::map<std::string, std::unique_ptr<ArchiveClassRegistration>>
4141
registrations_;
4242
std::unique_ptr<ArchiveStatement> begin_transaction_stmt_;

impeller/archivist/archive_statement.cc

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ namespace impeller {
1313

1414
struct ArchiveStatement::Handle {
1515
Handle(void* db, const std::string& statememt) {
16+
if (db == nullptr) {
17+
return;
18+
}
1619
::sqlite3_stmt* handle = nullptr;
1720
if (::sqlite3_prepare_v2(reinterpret_cast<sqlite3*>(db), //
1821
statememt.c_str(), //
@@ -31,12 +34,12 @@ struct ArchiveStatement::Handle {
3134
FML_CHECK(res == SQLITE_OK) << "Unable to finalize the archive.";
3235
}
3336

34-
bool IsValid() { return handle_ != nullptr; }
37+
bool IsValid() const { return handle_ != nullptr; }
3538

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

3841
private:
39-
::sqlite3_stmt* handle_;
42+
::sqlite3_stmt* handle_ = nullptr;
4043

4144
FML_DISALLOW_COPY_AND_ASSIGN(Handle);
4245
};
@@ -55,6 +58,9 @@ bool ArchiveStatement::IsValid() const {
5558
}
5659

5760
bool ArchiveStatement::Reset() {
61+
if (!IsValid()) {
62+
return false;
63+
}
5864
if (::sqlite3_reset(statement_handle_->Get()) != SQLITE_OK) {
5965
return false;
6066
}
@@ -81,13 +87,19 @@ static constexpr int ToColumn(size_t index) {
8187
}
8288

8389
size_t ArchiveStatement::GetColumnCount() {
90+
if (!IsValid()) {
91+
return 0u;
92+
}
8493
return ::sqlite3_column_count(statement_handle_->Get());
8594
}
8695

8796
/*
8897
* Bind Variants
8998
*/
9099
bool ArchiveStatement::WriteValue(size_t index, const std::string& item) {
100+
if (!IsValid()) {
101+
return false;
102+
}
91103
return ::sqlite3_bind_text(statement_handle_->Get(), //
92104
ToParam(index), //
93105
item.data(), //
@@ -96,18 +108,27 @@ bool ArchiveStatement::WriteValue(size_t index, const std::string& item) {
96108
}
97109

98110
bool ArchiveStatement::BindIntegral(size_t index, int64_t item) {
111+
if (!IsValid()) {
112+
return false;
113+
}
99114
return ::sqlite3_bind_int64(statement_handle_->Get(), //
100115
ToParam(index), //
101116
item) == SQLITE_OK;
102117
}
103118

104119
bool ArchiveStatement::WriteValue(size_t index, double item) {
120+
if (!IsValid()) {
121+
return false;
122+
}
105123
return ::sqlite3_bind_double(statement_handle_->Get(), //
106124
ToParam(index), //
107125
item) == SQLITE_OK;
108126
}
109127

110128
bool ArchiveStatement::WriteValue(size_t index, const Allocation& item) {
129+
if (!IsValid()) {
130+
return false;
131+
}
111132
return ::sqlite3_bind_blob(statement_handle_->Get(), //
112133
ToParam(index), //
113134
item.GetBuffer(), //
@@ -119,11 +140,17 @@ bool ArchiveStatement::WriteValue(size_t index, const Allocation& item) {
119140
* Column Variants
120141
*/
121142
bool ArchiveStatement::ColumnIntegral(size_t index, int64_t& item) {
143+
if (!IsValid()) {
144+
return false;
145+
}
122146
item = ::sqlite3_column_int64(statement_handle_->Get(), ToColumn(index));
123147
return true;
124148
}
125149

126150
bool ArchiveStatement::ReadValue(size_t index, double& item) {
151+
if (!IsValid()) {
152+
return false;
153+
}
127154
item = ::sqlite3_column_double(statement_handle_->Get(), ToColumn(index));
128155
return true;
129156
}
@@ -137,6 +164,9 @@ bool ArchiveStatement::ReadValue(size_t index, double& item) {
137164
*/
138165

139166
bool ArchiveStatement::ReadValue(size_t index, std::string& item) {
167+
if (!IsValid()) {
168+
return false;
169+
}
140170
/*
141171
* Get the character data
142172
*/
@@ -156,6 +186,9 @@ bool ArchiveStatement::ReadValue(size_t index, std::string& item) {
156186
}
157187

158188
bool ArchiveStatement::ReadValue(size_t index, Allocation& item) {
189+
if (!IsValid()) {
190+
return false;
191+
}
159192
/*
160193
* Get a blob pointer
161194
*/
@@ -180,6 +213,9 @@ bool ArchiveStatement::ReadValue(size_t index, Allocation& item) {
180213
}
181214

182215
ArchiveStatement::Result ArchiveStatement::Execute() {
216+
if (!IsValid()) {
217+
return Result::kFailure;
218+
}
183219
switch (::sqlite3_step(statement_handle_->Get())) {
184220
case SQLITE_DONE:
185221
return Result::kDone;

0 commit comments

Comments
 (0)