diff --git a/src/plugins/TreeConstructor.hpp b/src/plugins/TreeConstructor.hpp new file mode 100644 index 0000000..b337c43 --- /dev/null +++ b/src/plugins/TreeConstructor.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include +#include + +// Allows declarative and conditional tree construction +// More precisely, TreeConstructor a b -> TreeNode b, where there is a function a -> [b] +template struct TreeConstructor { + std::function(In)> nodesToAttach; + std::vector> children; +}; + +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); + for (auto &child : consNode.children) + // We need pointer since node.children() just gives a copy + constructTree(child, node.childrenPtr()->back(), in); + } +}