Skip to content

Commit c8f5f4c

Browse files
committed
use std::optional<std::string> over std::string*
1 parent 081701d commit c8f5f4c

File tree

1 file changed

+44
-50
lines changed

1 file changed

+44
-50
lines changed

binding.cc

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <leveldb/filter_policy.h>
1111

1212
#include <map>
13+
#include <optional>
1314
#include <vector>
1415

1516
/**
@@ -253,19 +254,17 @@ static size_t StringOrBufferLength (napi_env env, napi_value value) {
253254
* Takes a Buffer or string property 'name' from 'opts'.
254255
* Returns null if the property does not exist or is zero-length.
255256
*/
256-
static std::string* RangeOption (napi_env env, napi_value opts, const char* name) {
257+
static std::optional<std::string> RangeOption (napi_env env, napi_value opts, const char* name) {
257258
if (HasProperty(env, opts, name)) {
258259
napi_value value = GetProperty(env, opts, name);
259260

260261
if (StringOrBufferLength(env, value) >= 0) {
261262
LD_STRING_OR_BUFFER_TO_COPY(env, value, to);
262-
std::string* result = new std::string(toCh_, toSz_);
263-
delete [] toCh_;
264-
return result;
263+
return std::string(toCh_, toSz_);
265264
}
266265
}
267266

268-
return NULL;
267+
return {};
269268
}
270269

271270
/**
@@ -625,20 +624,20 @@ struct PriorityWorker : public BaseWorker {
625624
struct BaseIterator {
626625
BaseIterator(Database* database,
627626
const bool reverse,
628-
std::string* lt,
629-
std::string* lte,
630-
std::string* gt,
631-
std::string* gte,
627+
std::optional<std::string> lt,
628+
std::optional<std::string> lte,
629+
std::optional<std::string> gt,
630+
std::optional<std::string> gte,
632631
const int limit,
633632
const bool fillCache)
634633
: database_(database),
635634
hasClosed_(false),
636635
didSeek_(false),
637636
reverse_(reverse),
638-
lt_(lt),
639-
lte_(lte),
640-
gt_(gt),
641-
gte_(gte),
637+
lt_(std::move(lt)),
638+
lte_(std::move(lte)),
639+
gt_(std::move(gt)),
640+
gte_(std::move(gte)),
642641
limit_(limit),
643642
count_(0) {
644643
options_ = new leveldb::ReadOptions();
@@ -650,11 +649,6 @@ struct BaseIterator {
650649
virtual ~BaseIterator () {
651650
assert(hasClosed_);
652651

653-
if (lt_ != NULL) delete lt_;
654-
if (gt_ != NULL) delete gt_;
655-
if (lte_ != NULL) delete lte_;
656-
if (gte_ != NULL) delete gte_;
657-
658652
delete options_;
659653
}
660654

@@ -668,23 +662,23 @@ struct BaseIterator {
668662
void SeekToRange () {
669663
didSeek_ = true;
670664

671-
if (!reverse_ && gte_ != NULL) {
665+
if (!reverse_ && gte_) {
672666
dbIterator_->Seek(*gte_);
673-
} else if (!reverse_ && gt_ != NULL) {
667+
} else if (!reverse_ && gt_) {
674668
dbIterator_->Seek(*gt_);
675669

676670
if (dbIterator_->Valid() && dbIterator_->key().compare(*gt_) == 0) {
677671
dbIterator_->Next();
678672
}
679-
} else if (reverse_ && lte_ != NULL) {
673+
} else if (reverse_ && lte_) {
680674
dbIterator_->Seek(*lte_);
681675

682676
if (!dbIterator_->Valid()) {
683677
dbIterator_->SeekToLast();
684678
} else if (dbIterator_->key().compare(*lte_) > 0) {
685679
dbIterator_->Prev();
686680
}
687-
} else if (reverse_ && lt_ != NULL) {
681+
} else if (reverse_ && lt_) {
688682
dbIterator_->Seek(*lt_);
689683

690684
if (!dbIterator_->Valid()) {
@@ -784,15 +778,15 @@ struct BaseIterator {
784778
// }
785779

786780
// The lte and gte options take precedence over lt and gt respectively
787-
if (lte_ != NULL) {
781+
if (lte_) {
788782
if (target.compare(*lte_) > 0) return true;
789-
} else if (lt_ != NULL) {
783+
} else if (lt_) {
790784
if (target.compare(*lt_) >= 0) return true;
791785
}
792786

793-
if (gte_ != NULL) {
787+
if (gte_) {
794788
if (target.compare(*gte_) < 0) return true;
795-
} else if (gt_ != NULL) {
789+
} else if (gt_) {
796790
if (target.compare(*gt_) <= 0) return true;
797791
}
798792

@@ -806,10 +800,10 @@ struct BaseIterator {
806800
leveldb::Iterator* dbIterator_;
807801
bool didSeek_;
808802
const bool reverse_;
809-
std::string* lt_;
810-
std::string* lte_;
811-
std::string* gt_;
812-
std::string* gte_;
803+
std::optional<std::string> lt_;
804+
std::optional<std::string> lte_;
805+
std::optional<std::string> gt_;
806+
std::optional<std::string> gte_;
813807
const int limit_;
814808
int count_;
815809
leveldb::ReadOptions* options_;
@@ -825,15 +819,15 @@ struct Iterator final : public BaseIterator {
825819
const bool keys,
826820
const bool values,
827821
const int limit,
828-
std::string* lt,
829-
std::string* lte,
830-
std::string* gt,
831-
std::string* gte,
822+
std::optional<std::string> lt,
823+
std::optional<std::string> lte,
824+
std::optional<std::string> gt,
825+
std::optional<std::string> gte,
832826
const bool fillCache,
833827
const bool keyAsBuffer,
834828
const bool valueAsBuffer,
835829
const uint32_t highWaterMarkBytes)
836-
: BaseIterator(database, reverse, lt, lte, gt, gte, limit, fillCache),
830+
: BaseIterator(database, reverse, std::move(lt), std::move(lte), std::move(gt), std::move(gte), limit, fillCache),
837831
id_(id),
838832
keys_(keys),
839833
values_(values),
@@ -1345,12 +1339,12 @@ struct ClearWorker final : public PriorityWorker {
13451339
napi_value callback,
13461340
const bool reverse,
13471341
const int limit,
1348-
std::string* lt,
1349-
std::string* lte,
1350-
std::string* gt,
1351-
std::string* gte)
1342+
std::optional<std::string> lt,
1343+
std::optional<std::string> lte,
1344+
std::optional<std::string> gt,
1345+
std::optional<std::string> gte)
13521346
: PriorityWorker(env, database, callback, "classic_level.db.clear") {
1353-
iterator_ = new BaseIterator(database, reverse, lt, lte, gt, gte, limit, false);
1347+
iterator_ = new BaseIterator(database, reverse, std::move(lt), std::move(lte), std::move(gt), std::move(gte), limit, false);
13541348
writeOptions_ = new leveldb::WriteOptions();
13551349
writeOptions_->sync = false;
13561350
}
@@ -1409,12 +1403,12 @@ NAPI_METHOD(db_clear) {
14091403
const bool reverse = BooleanProperty(env, options, "reverse", false);
14101404
const int limit = Int32Property(env, options, "limit", -1);
14111405

1412-
std::string* lt = RangeOption(env, options, "lt");
1413-
std::string* lte = RangeOption(env, options, "lte");
1414-
std::string* gt = RangeOption(env, options, "gt");
1415-
std::string* gte = RangeOption(env, options, "gte");
1406+
const auto lt = RangeOption(env, options, "lt");
1407+
const auto lte = RangeOption(env, options, "lte");
1408+
const auto gt = RangeOption(env, options, "gt");
1409+
const auto gte = RangeOption(env, options, "gte");
14161410

1417-
ClearWorker* worker = new ClearWorker(env, database, callback, reverse, limit, lt, lte, gt, gte);
1411+
ClearWorker* worker = new ClearWorker(env, database, callback, reverse, limit, std::move(lt), std::move(lte), std::move(gt), std::move(gte));
14181412
worker->Queue(env);
14191413

14201414
NAPI_RETURN_UNDEFINED();
@@ -1635,14 +1629,14 @@ NAPI_METHOD(iterator_init) {
16351629
const int limit = Int32Property(env, options, "limit", -1);
16361630
const uint32_t highWaterMarkBytes = Uint32Property(env, options, "highWaterMarkBytes", 16 * 1024);
16371631

1638-
std::string* lt = RangeOption(env, options, "lt");
1639-
std::string* lte = RangeOption(env, options, "lte");
1640-
std::string* gt = RangeOption(env, options, "gt");
1641-
std::string* gte = RangeOption(env, options, "gte");
1632+
auto lt = RangeOption(env, options, "lt");
1633+
auto lte = RangeOption(env, options, "lte");
1634+
auto gt = RangeOption(env, options, "gt");
1635+
auto gte = RangeOption(env, options, "gte");
16421636

16431637
const uint32_t id = database->currentIteratorId_++;
16441638
Iterator* iterator = new Iterator(database, id, reverse, keys,
1645-
values, limit, lt, lte, gt, gte, fillCache,
1639+
values, limit, std::move(lt), std::move(lte), std::move(gt), std::move(gte), fillCache,
16461640
keyAsBuffer, valueAsBuffer, highWaterMarkBytes);
16471641
napi_value result;
16481642

0 commit comments

Comments
 (0)