Skip to content

Commit db4c853

Browse files
committed
fix: use move_const in Tree
1 parent 34d5929 commit db4c853

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <iostream>
1111
#include <utility>
1212
#include <cassert>
13+
#include <move_const.h>
1314

1415
#include <napi.h>
1516

src/tree.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#include "common.h"
66

77
/** Get the children of a jsTree (Napi::Object) */
8-
inline std::optional<Napi::Array> getChildren(const Napi::Object &jsTree, const string &childrenKey) {
8+
inline std::optional<Napi::Array> getChildren(const Napi::Object &&jsTree, const string &&childrenKey) {
99
// determine if it has children
1010
if (jsTree.HasOwnProperty(childrenKey)) {
11-
const auto childrenRaw = jsTree.Get(childrenKey);
11+
const auto childrenRaw = jsTree.Get(move_const(childrenKey));
1212
if (childrenRaw.IsArray()) {
1313
const auto childrenArray = childrenRaw.As<Napi::Array>();
1414
if (childrenArray.Length() != 0) {
@@ -25,8 +25,8 @@ struct CandidateObject {
2525
const size_t level = 0;
2626
const size_t index = 0;
2727

28-
explicit CandidateObject(CandidateString &&data_, const size_t level_, const size_t index_) noexcept
29-
: data{ move(data_) }, level{ level_ }, index{ index_ } {}
28+
explicit CandidateObject(const CandidateString &&data_, const size_t level_, const size_t index_) noexcept
29+
: data{ move_const(data_) }, level{ level_ }, index{ index_ } {}
3030
};
3131

3232
struct Tree {
@@ -37,7 +37,7 @@ struct Tree {
3737

3838

3939
/** Recursive function that fills the entriesArray from the given jsTreeArray */
40-
void makeEntriesArray(const Napi::Array &jsTreeArray, const size_t level) {
40+
void makeEntriesArray(const Napi::Array &&jsTreeArray, const size_t level) {
4141
const auto entriesArrayLength = jsTreeArray.Length();
4242
entriesArray.reserve(entriesArrayLength);// reserve enough space
4343
for (auto iEntry = 0u; iEntry < entriesArrayLength; iEntry++) {
@@ -46,7 +46,7 @@ struct Tree {
4646
}
4747

4848
/** 1st argument is a single object */
49-
void makeEntriesArray(const Napi::Object &jsTree, const size_t level, const size_t iEntry) {
49+
void makeEntriesArray(const Napi::Object &&jsTree, const size_t level, const size_t iEntry) {
5050
// make the CandidateObject and push it back
5151
entriesArray.emplace_back(
5252
jsTree.Get(dataKey).ToString().Utf8Value(),// first, get the current data
@@ -56,10 +56,10 @@ struct Tree {
5656
);
5757

5858
// add children if any
59-
auto mayChildren = getChildren(jsTree, childrenKey);
59+
const auto mayChildren = getChildren(move_const(jsTree), move_const(childrenKey));
6060
if (mayChildren.has_value()) {
6161
// recurse
62-
makeEntriesArray(mayChildren.value(), level + 1);
62+
makeEntriesArray(move_const(mayChildren.value()), level + 1);
6363
}
6464
}
6565

@@ -68,11 +68,12 @@ struct Tree {
6868

6969
/** create a Tree object and make an entries array */
7070
// NOTE: this is made to only accept Napi::Array because we cannot export templates to JavaScript
71-
explicit Tree(const Napi::Array &jsTreeArrayOrObject_, const string &dataKey_, const string &childrenKey_)
72-
: dataKey{ dataKey_ },
73-
childrenKey{ childrenKey_ } {
74-
makeEntriesArray(jsTreeArrayOrObject_, 0);
71+
explicit Tree(const Napi::Array &&jsTreeArrayOrObject_, const string &&dataKey_, const string &&childrenKey_)
72+
: dataKey{ move_const(dataKey_) },
73+
childrenKey{ move_const(childrenKey_) } {
74+
makeEntriesArray(move_const(jsTreeArrayOrObject_), 0);
7575
}
76+
7677
};
7778

7879
#endif// Fuzzaldrin_tree_h_

0 commit comments

Comments
 (0)