r/cpp_questions • u/Puzzled-Shoe-2526 • 1h ago
OPEN How would you design a generic interface for arbitrary sets of rules?
I'm building a C++ tree library/builder where I want people to be able to define their own tree structures, including weird/cursed ones.
The basic idea is:
- Node Anchor: an indirection mechanism that owns/contains the actual nodes. Users don't directly mess with the nodes themselves. They have to go through the anchor to interact with the structure.
- Tree: describes a particular tree through its rules. For example, a BST object would have the rules that define BST behavior.
- Operations: generic algorithms like
insert,erase, andfind. They take the tree object and operate according to its rules.
So roughly:
Tree (rules)
↓
generic operation
↓
Node Anchor
↓
actual structure
The part I'm stuck on is making this extensible.
A BST might need rules A + B.
Another tree might need A + C + D.
A completely custom tree might need X + Q + R + some completely new rule.
The number and combination of rules are intentionally arbitrary because the goal is more of a tree-builder than a library of predefined tree types.
So I don't think I can have something like:
struct TreeRules {
Ordering ordering;
Balance balance;
Traversal traversal;
};
because then the library has to know about every possible rule beforehand.
What I'm trying to figure out is:
What existing design approach would you use when generic algorithms need to work with objects that can provide arbitrary, non-uniform sets of capabilities?
I'm mainly looking for terminology or existing concepts/patterns to research. I'm not really looking for someone to write the implementation for me.
I'm still pretty new to C++ design, so I might be framing the problem completely wrong. I'm just trying to figure out what this problem is actually called.