From fc86b22ac53f5d8645a1f21cde70af358cd541ff Mon Sep 17 00:00:00 2001 From: Jussi Kuokkanen Date: Sat, 30 Sep 2023 16:09:10 +0300 Subject: [PATCH] construct trees with TreeNode instead of T This allows constructing some subtrees imperatively, instead of having to construct everything declaratively. Consider we want to construct the following tree: Fans| |0|Read | |Write | |1|Read |Write The leaf nodes might be hard to construct declaratively, since they can't easily know which fan index they should use, since they only get the amount of fans. --- src/plugins/TreeConstructor.hpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/plugins/TreeConstructor.hpp b/src/plugins/TreeConstructor.hpp index b337c43..698547c 100644 --- a/src/plugins/TreeConstructor.hpp +++ b/src/plugins/TreeConstructor.hpp @@ -4,9 +4,9 @@ #include // Allows declarative and conditional tree construction -// More precisely, TreeConstructor a b -> TreeNode b, where there is a function a -> [b] +// More precisely, TreeConstructor a b -> TreeNode b, where there is a function a -> [TreeNode b] template struct TreeConstructor { - std::function(In)> nodesToAttach; + std::function>(In)> nodesToAttach; std::vector> children; }; @@ -14,8 +14,7 @@ template void constructTree(TreeConstructor consNode, TuxClocker::TreeNode &node, In in) { for (auto &newNode : consNode.nodesToAttach(in)) { // Attach wanted child - auto treeNode = TuxClocker::TreeNode{newNode}; - node.appendChild(treeNode); + node.appendChild(newNode); for (auto &child : consNode.children) // We need pointer since node.children() just gives a copy constructTree(child, node.childrenPtr()->back(), in);