diff --git a/src/common.h b/src/common.h index abb8ecc9..d6222caf 100644 --- a/src/common.h +++ b/src/common.h @@ -10,6 +10,7 @@ #include #include #include +#include #include diff --git a/src/tree.h b/src/tree.h index be1ff45c..8cd0e755 100644 --- a/src/tree.h +++ b/src/tree.h @@ -5,10 +5,10 @@ #include "common.h" /** Get the children of a jsTree (Napi::Object) */ -inline std::optional getChildren(const Napi::Object &jsTree, const string &childrenKey) { +inline std::optional getChildren(const Napi::Object &&jsTree, const string &&childrenKey) { // determine if it has children if (jsTree.HasOwnProperty(childrenKey)) { - const auto childrenRaw = jsTree.Get(childrenKey); + const auto childrenRaw = jsTree.Get(move_const(childrenKey)); if (childrenRaw.IsArray()) { const auto childrenArray = childrenRaw.As(); if (childrenArray.Length() != 0) { @@ -25,8 +25,8 @@ struct CandidateObject { const size_t level = 0; const size_t index = 0; - explicit CandidateObject(CandidateString &&data_, const size_t level_, const size_t index_) noexcept - : data{ move(data_) }, level{ level_ }, index{ index_ } {} + explicit CandidateObject(const CandidateString &&data_, const size_t level_, const size_t index_) noexcept + : data{ move_const(data_) }, level{ level_ }, index{ index_ } {} }; struct Tree { @@ -37,7 +37,7 @@ struct Tree { /** Recursive function that fills the entriesArray from the given jsTreeArray */ - void makeEntriesArray(const Napi::Array &jsTreeArray, const size_t level) { + void makeEntriesArray(const Napi::Array &&jsTreeArray, const size_t level) { const auto entriesArrayLength = jsTreeArray.Length(); entriesArray.reserve(entriesArrayLength);// reserve enough space for (auto iEntry = 0u; iEntry < entriesArrayLength; iEntry++) { @@ -46,7 +46,7 @@ struct Tree { } /** 1st argument is a single object */ - void makeEntriesArray(const Napi::Object &jsTree, const size_t level, const size_t iEntry) { + void makeEntriesArray(const Napi::Object &&jsTree, const size_t level, const size_t iEntry) { // make the CandidateObject and push it back entriesArray.emplace_back( jsTree.Get(dataKey).ToString().Utf8Value(),// first, get the current data @@ -56,10 +56,10 @@ struct Tree { ); // add children if any - auto mayChildren = getChildren(jsTree, childrenKey); + const auto mayChildren = getChildren(move_const(jsTree), move_const(childrenKey)); if (mayChildren.has_value()) { // recurse - makeEntriesArray(mayChildren.value(), level + 1); + makeEntriesArray(move_const(mayChildren.value()), level + 1); } } @@ -68,11 +68,12 @@ struct Tree { /** create a Tree object and make an entries array */ // NOTE: this is made to only accept Napi::Array because we cannot export templates to JavaScript - explicit Tree(const Napi::Array &jsTreeArrayOrObject_, const string &dataKey_, const string &childrenKey_) - : dataKey{ dataKey_ }, - childrenKey{ childrenKey_ } { - makeEntriesArray(jsTreeArrayOrObject_, 0); + explicit Tree(const Napi::Array &&jsTreeArrayOrObject_, const string &&dataKey_, const string &&childrenKey_) + : dataKey{ move_const(dataKey_) }, + childrenKey{ move_const(childrenKey_) } { + makeEntriesArray(move_const(jsTreeArrayOrObject_), 0); } + }; #endif// Fuzzaldrin_tree_h_