[WIP] Add 'fluent interface' for visitor construction
Following an idea originally proposed by @smuething long time ago
this allows to create a visitor using lambda expressions.
In contrast to providing plain callbacks to a global
traversal function (as in forEachNode()
) this has some benefits:
- It simplifies the
Visitor
approach instead of creating a new traversal mechanism. Hence you still have the full flexibility of theVisitor
approach. - Instead of positional arguments you have named arguments which is far less error prone.
- You can use the existing mechanism to switch traversal types.
- You can easily extend existing visitors.
- You can chain several callbacks for a single purpose (e.g. pre(...)).
As far as I remember this design pattern is called 'fluent design'. Here we use the piping syntax which was popularized by range adaptors from C++20 (and its prototype in range-V3). Usage example:
int counter = 0;
applyToTree(tree, makeVisitor() // create default doing nothig
| pre([&](auto&&...) { ++counter; }) // extend by pre callback
| leaf([&](auto&&...) { ++counter; }) // extend by leaf callback
); // traverse tree and count all nodes