mirror of
https://github.com/Lurkki14/tuxclocker.git
synced 2025-02-25 18:55:24 -06:00
add a way to construct device trees declaratively and conditionally
This commit is contained in:
23
src/plugins/TreeConstructor.hpp
Normal file
23
src/plugins/TreeConstructor.hpp
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <Tree.hpp>
|
||||||
|
|
||||||
|
// Allows declarative and conditional tree construction
|
||||||
|
// More precisely, TreeConstructor a b -> TreeNode b, where there is a function a -> [b]
|
||||||
|
template <typename In, typename Out> struct TreeConstructor {
|
||||||
|
std::function<std::vector<Out>(In)> nodesToAttach;
|
||||||
|
std::vector<TreeConstructor<In, Out>> children;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename In, typename Out>
|
||||||
|
void constructTree(TreeConstructor<In, Out> consNode, TuxClocker::TreeNode<Out> &node, In in) {
|
||||||
|
for (auto &newNode : consNode.nodesToAttach(in)) {
|
||||||
|
// Attach wanted child
|
||||||
|
auto treeNode = TuxClocker::TreeNode<Out>{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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user