Skip to content

Commit

Permalink
fix: use move instead of const &
Browse files Browse the repository at this point in the history
Prevents possible copying of the 1st argument into the object
  • Loading branch information
aminya committed Nov 8, 2020
1 parent 8dabebd commit 5ea324e
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ struct CandidateObject {
const size_t level = 0;
const size_t index = 0;

CandidateObject(const CandidateString &data_, const size_t level_, const size_t index_) noexcept
: data{ data_ }, level{ level_ }, index{ index_ } {}
CandidateObject(CandidateString &&data_, const size_t level_, const size_t index_) noexcept
: data{ move(data_) }, level{ level_ }, index{ index_ } {}
};

template<typename T>
Expand All @@ -53,9 +53,15 @@ struct Tree {

/** 1st argument is a single object */
void makeEntriesArray(const Napi::Object &jsTree, const size_t level, const size_t iEntry) {
// get the current data
const auto data = jsTree.Get(dataKey).ToString().Utf8Value();
entriesArray.emplace_back(CandidateObject(data, level, iEntry));
// finally emplace it back
entriesArray.emplace_back(
// then make the CandidateObject
CandidateObject(
jsTree.Get(dataKey).ToString().Utf8Value(),// first, get the current data
level,
iEntry)

);

// add children if any
auto mayChildren = getChildren(jsTree, childrenKey);
Expand Down

0 comments on commit 5ea324e

Please sign in to comment.