Skip to content

Commit

Permalink
fix: use move_const in Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Dec 7, 2020
1 parent 12e2fa6 commit 40cfc66
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <iostream>
#include <utility>
#include <cassert>
#include <move_const.h>

#include <napi.h>

Expand Down
25 changes: 13 additions & 12 deletions src/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#include "common.h"

/** Get the children of a jsTree (Napi::Object) */
inline std::optional<Napi::Array> getChildren(const Napi::Object &jsTree, const string &childrenKey) {
inline std::optional<Napi::Array> getChildren(const Napi::Object &&jsTree, const string &&childrenKey) {
Napi::Array childrenArray;

// determine if it has children
auto hasChildren = false;
if (jsTree.HasOwnProperty(childrenKey)) {
const auto childrenRaw = jsTree.Get(childrenKey);
const auto childrenRaw = jsTree.Get(move_const(childrenKey));
if (childrenRaw.IsArray()) {
childrenArray = childrenRaw.As<Napi::Array>();
if (childrenArray.Length() != 0) {
Expand All @@ -31,8 +31,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 {
Expand All @@ -43,7 +43,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++) {
Expand All @@ -52,7 +52,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
Expand All @@ -62,10 +62,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);
}
}

Expand All @@ -74,11 +74,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_

0 comments on commit 40cfc66

Please sign in to comment.