1
- #include < string.h >
1
+ #include < cstring >
2
2
#include < napi.h>
3
-
4
3
#include " macros.h"
5
4
#include " database.h"
6
5
#include " backup.h"
@@ -11,9 +10,9 @@ Napi::Object Backup::Init(Napi::Env env, Napi::Object exports) {
11
10
Napi::HandleScope scope (env);
12
11
13
12
// declare napi_default_method here as it is only available in Node v14.12.0+
14
- napi_property_attributes napi_default_method = static_cast <napi_property_attributes>(napi_writable | napi_configurable);
13
+ auto napi_default_method = static_cast <napi_property_attributes>(napi_writable | napi_configurable);
15
14
16
- Napi::Function t = DefineClass (env, " Backup" , {
15
+ auto t = DefineClass (env, " Backup" , {
17
16
InstanceMethod (" step" , &Backup::Step, napi_default_method),
18
17
InstanceMethod (" finish" , &Backup::Finish, napi_default_method),
19
18
InstanceAccessor (" idle" , &Backup::IdleGetter, nullptr ),
@@ -34,7 +33,7 @@ void Backup::Process() {
34
33
}
35
34
36
35
while (inited && !locked && !queue.empty ()) {
37
- std::unique_ptr<Call> call (queue.front ());
36
+ auto call = std::move (queue.front ());
38
37
queue.pop ();
39
38
40
39
call->callback (call->baton );
@@ -43,19 +42,19 @@ void Backup::Process() {
43
42
44
43
void Backup::Schedule (Work_Callback callback, Baton* baton) {
45
44
if (finished) {
46
- queue.push (new Call (callback, baton));
45
+ queue.emplace (new Call (callback, baton));
47
46
CleanQueue ();
48
47
}
49
48
else if (!inited || locked || !queue.empty ()) {
50
- queue.push (new Call (callback, baton));
49
+ queue.emplace (new Call (callback, baton));
51
50
}
52
51
else {
53
52
callback (baton);
54
53
}
55
54
}
56
55
57
56
template <class T > void Backup::Error (T* baton) {
58
- Napi::Env env = baton->backup ->Env ();
57
+ auto env = baton->backup ->Env ();
59
58
Napi::HandleScope scope (env);
60
59
61
60
Backup* backup = baton->backup ;
@@ -76,7 +75,7 @@ template <class T> void Backup::Error(T* baton) {
76
75
}
77
76
78
77
void Backup::CleanQueue () {
79
- Napi::Env env = this ->Env ();
78
+ auto env = this ->Env ();
80
79
Napi::HandleScope scope (env);
81
80
82
81
if (inited && !queue.empty ()) {
@@ -88,7 +87,7 @@ void Backup::CleanQueue() {
88
87
89
88
// Clear out the queue so that this object can get GC'ed.
90
89
while (!queue.empty ()) {
91
- std::unique_ptr<Call> call (queue.front ());
90
+ auto call = std::move (queue.front ());
92
91
queue.pop ();
93
92
94
93
std::unique_ptr<Baton> baton (call->baton );
@@ -111,7 +110,7 @@ void Backup::CleanQueue() {
111
110
else while (!queue.empty ()) {
112
111
// Just delete all items in the queue; we already fired an event when
113
112
// initializing the backup failed.
114
- std::unique_ptr<Call> call (queue.front ());
113
+ auto call = std::move (queue.front ());
115
114
queue.pop ();
116
115
117
116
// We don't call the actual callback, so we have to make sure that
@@ -121,13 +120,13 @@ void Backup::CleanQueue() {
121
120
}
122
121
123
122
Backup::Backup (const Napi::CallbackInfo& info) : Napi::ObjectWrap<Backup>(info) {
124
- Napi::Env env = info.Env ();
123
+ auto env = info.Env ();
125
124
if (!info.IsConstructCall ()) {
126
125
Napi::TypeError::New (env, " Use the new operator to create new Backup objects" ).ThrowAsJavaScriptException ();
127
126
return ;
128
127
}
129
128
130
- int length = info.Length ();
129
+ auto length = info.Length ();
131
130
132
131
if (length <= 0 || !Database::HasInstance (info[0 ])) {
133
132
Napi::TypeError::New (env, " Database object expected" ).ThrowAsJavaScriptException ();
@@ -154,32 +153,32 @@ Backup::Backup(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Backup>(info)
154
153
return ;
155
154
}
156
155
157
- Database* db = Napi::ObjectWrap<Database>::Unwrap (info[0 ].As <Napi::Object>());
158
- Napi::String filename = info[1 ].As <Napi::String>();
159
- Napi::String sourceName = info[2 ].As <Napi::String>();
160
- Napi::String destName = info[3 ].As <Napi::String>();
161
- Napi:: Boolean filenameIsDest = info[4 ].As <Napi::Boolean >();
156
+ auto * database = Napi::ObjectWrap<Database>::Unwrap (info[0 ].As <Napi::Object>());
157
+ auto filename = info[1 ].As <Napi::String>();
158
+ auto sourceName = info[2 ].As <Napi::String>();
159
+ auto destName = info[3 ].As <Napi::String>();
160
+ auto filenameIsDest = info[4 ].As <Napi::Boolean >();
162
161
163
162
info.This ().As <Napi::Object>().DefineProperty (Napi::PropertyDescriptor::Value (" filename" , filename));
164
163
info.This ().As <Napi::Object>().DefineProperty (Napi::PropertyDescriptor::Value (" sourceName" , sourceName));
165
164
info.This ().As <Napi::Object>().DefineProperty (Napi::PropertyDescriptor::Value (" destName" , destName));
166
165
info.This ().As <Napi::Object>().DefineProperty (Napi::PropertyDescriptor::Value (" filenameIsDest" , filenameIsDest));
167
166
168
- init (db );
167
+ init (database );
169
168
170
- InitializeBaton * baton = new InitializeBaton (db , info[5 ].As <Napi::Function>(), this );
169
+ auto * baton = new InitializeBaton (database , info[5 ].As <Napi::Function>(), this );
171
170
baton->filename = filename.Utf8Value ();
172
171
baton->sourceName = sourceName.Utf8Value ();
173
172
baton->destName = destName.Utf8Value ();
174
173
baton->filenameIsDest = filenameIsDest.Value ();
175
- db ->Schedule (Work_BeginInitialize, baton);
174
+ database ->Schedule (Work_BeginInitialize, baton);
176
175
}
177
176
178
177
void Backup::Work_BeginInitialize (Database::Baton* baton) {
179
178
assert (baton->db ->open );
180
179
baton->db ->pending ++;
181
- Napi::Env env = baton->db ->Env ();
182
- int status = napi_create_async_work (
180
+ auto env = baton->db ->Env ();
181
+ int UNUSED ( status) = napi_create_async_work (
183
182
env, NULL , Napi::String::New (env, " sqlite3.Backup.Initialize" ),
184
183
Work_Initialize, Work_AfterInitialize, baton, &baton->request
185
184
);
@@ -192,7 +191,7 @@ void Backup::Work_Initialize(napi_env e, void* data) {
192
191
193
192
// In case stepping fails, we use a mutex to make sure we get the associated
194
193
// error message.
195
- sqlite3_mutex * mtx = sqlite3_db_mutex (baton->db ->_handle );
194
+ auto * mtx = sqlite3_db_mutex (baton->db ->_handle );
196
195
sqlite3_mutex_enter (mtx);
197
196
198
197
backup->status = sqlite3_open (baton->filename .c_str (), &backup->_otherDb );
@@ -218,9 +217,9 @@ void Backup::Work_Initialize(napi_env e, void* data) {
218
217
219
218
void Backup::Work_AfterInitialize (napi_env e, napi_status status, void * data) {
220
219
std::unique_ptr<InitializeBaton> baton (static_cast <InitializeBaton*>(data));
221
- Backup * backup = baton->backup ;
220
+ auto * backup = baton->backup ;
222
221
223
- Napi::Env env = backup->Env ();
222
+ auto env = backup->Env ();
224
223
Napi::HandleScope scope (env);
225
224
226
225
if (backup->status != SQLITE_OK) {
@@ -239,13 +238,13 @@ void Backup::Work_AfterInitialize(napi_env e, napi_status status, void* data) {
239
238
}
240
239
241
240
Napi::Value Backup::Step (const Napi::CallbackInfo& info) {
242
- Backup * backup = this ;
243
- Napi::Env env = backup->Env ();
241
+ auto * backup = this ;
242
+ auto env = backup->Env ();
244
243
245
244
REQUIRE_ARGUMENT_INTEGER (0 , pages);
246
245
OPTIONAL_ARGUMENT_FUNCTION (1 , callback);
247
246
248
- StepBaton * baton = new StepBaton (backup, callback, pages);
247
+ auto * baton = new StepBaton (backup, callback, pages);
249
248
backup->GetRetryErrors (baton->retryErrorsSet );
250
249
backup->Schedule (Work_BeginStep, baton);
251
250
return info.This ();
@@ -281,9 +280,9 @@ void Backup::Work_Step(napi_env e, void* data) {
281
280
282
281
void Backup::Work_AfterStep (napi_env e, napi_status status, void * data) {
283
282
std::unique_ptr<StepBaton> baton (static_cast <StepBaton*>(data));
284
- Backup * backup = baton->backup ;
283
+ auto * backup = baton->backup ;
285
284
286
- Napi::Env env = backup->Env ();
285
+ auto env = backup->Env ();
287
286
Napi::HandleScope scope (env);
288
287
289
288
if (backup->status == SQLITE_DONE) {
@@ -308,12 +307,12 @@ void Backup::Work_AfterStep(napi_env e, napi_status status, void* data) {
308
307
}
309
308
310
309
Napi::Value Backup::Finish (const Napi::CallbackInfo& info) {
311
- Backup * backup = this ;
312
- Napi::Env env = backup->Env ();
310
+ auto * backup = this ;
311
+ auto env = backup->Env ();
313
312
314
313
OPTIONAL_ARGUMENT_FUNCTION (0 , callback);
315
314
316
- Baton * baton = new Baton (backup, callback);
315
+ auto * baton = new Baton (backup, callback);
317
316
backup->Schedule (Work_BeginFinish, baton);
318
317
return info.This ();
319
318
}
@@ -329,9 +328,9 @@ void Backup::Work_Finish(napi_env e, void* data) {
329
328
330
329
void Backup::Work_AfterFinish (napi_env e, napi_status status, void * data) {
331
330
std::unique_ptr<Baton> baton (static_cast <Baton*>(data));
332
- Backup * backup = baton->backup ;
331
+ auto * backup = baton->backup ;
333
332
334
- Napi::Env env = backup->Env ();
333
+ auto env = backup->Env ();
335
334
Napi::HandleScope scope (env);
336
335
337
336
backup->FinishAll ();
@@ -369,39 +368,39 @@ void Backup::FinishSqlite() {
369
368
}
370
369
371
370
Napi::Value Backup::IdleGetter (const Napi::CallbackInfo& info) {
372
- Backup * backup = this ;
371
+ auto * backup = this ;
373
372
bool idle = backup->inited && !backup->locked && backup->queue .empty ();
374
373
return Napi::Boolean::New (this ->Env (), idle);
375
374
}
376
375
377
376
Napi::Value Backup::CompletedGetter (const Napi::CallbackInfo& info) {
378
- Backup * backup = this ;
377
+ auto * backup = this ;
379
378
return Napi::Boolean::New (this ->Env (), backup->completed );
380
379
}
381
380
382
381
Napi::Value Backup::FailedGetter (const Napi::CallbackInfo& info) {
383
- Backup * backup = this ;
382
+ auto * backup = this ;
384
383
return Napi::Boolean::New (this ->Env (), backup->failed );
385
384
}
386
385
387
386
Napi::Value Backup::RemainingGetter (const Napi::CallbackInfo& info) {
388
- Backup * backup = this ;
387
+ auto * backup = this ;
389
388
return Napi::Number::New (this ->Env (), backup->remaining );
390
389
}
391
390
392
391
Napi::Value Backup::PageCountGetter (const Napi::CallbackInfo& info) {
393
- Backup * backup = this ;
392
+ auto * backup = this ;
394
393
return Napi::Number::New (this ->Env (), backup->pageCount );
395
394
}
396
395
397
396
Napi::Value Backup::RetryErrorGetter (const Napi::CallbackInfo& info) {
398
- Backup * backup = this ;
397
+ auto * backup = this ;
399
398
return backup->retryErrors .Value ();
400
399
}
401
400
402
401
void Backup::RetryErrorSetter (const Napi::CallbackInfo& info, const Napi::Value& value) {
403
- Backup * backup = this ;
404
- Napi::Env env = backup->Env ();
402
+ auto * backup = this ;
403
+ auto env = backup->Env ();
405
404
if (!value.IsArray ()) {
406
405
Napi::Error::New (env, " retryErrors must be an array" ).ThrowAsJavaScriptException ();
407
406
return ;
@@ -413,9 +412,9 @@ void Backup::RetryErrorSetter(const Napi::CallbackInfo& info, const Napi::Value&
413
412
void Backup::GetRetryErrors (std::set<int >& retryErrorsSet) {
414
413
retryErrorsSet.clear ();
415
414
Napi::Array array = retryErrors.Value ();
416
- int length = array.Length ();
417
- for (int i = 0 ; i < length; i++) {
418
- Napi::Value code = (array).Get (i );
415
+ auto length = array.Length ();
416
+ for (size_t i = 0 ; i < length; i++) {
417
+ Napi::Value code = (array).Get (static_cast < uint32_t >(i) );
419
418
if (code.IsNumber ()) {
420
419
retryErrorsSet.insert (code.As <Napi::Number>().Int32Value ());
421
420
}
0 commit comments