I have a class that is created from an enum that can be tested at any point in the future returning a bool whether it passes, for example:
ConditionObject cnd(CondIsTuesday);
door.setOpenCondition(cnd);
Then the door has a condition that it can use to determine its state.
However I wish to now overload the && and || operators for this condition class such that they can be chained together and work in the way expected with normal boolean logic. For example, making the following possible:
ConditionObject cnd(ConditionObject(CondIsTuesday) || (ConditionObject(CondIsThursday) && ConditionObject(CondIsAfterEight)));
door.openCondition(cnd);
Now the the door will be open on tuesdays or thursdays after eight.
I wasn’t sure of the correct way to describe the problem which also made it hard to search for a solution if one already existed. Thanks for any help with the problem!
[Edit]
I don’t think I explained it well enough, I don’t want the ConditionObject to be evaluated at the time of the creation of the object but instead to store the entire logic into the final ConditionObject such that it can be evaulated lazily.
Using the second example above, If I created the cnd object on a monday and never give the door anouther condition, it will open come tuesday because the condition it was given started returning true.
“Expression Templates” might be of use to you. This seems to be a reasonably thorough take on the subject, but there are others. I’m not overly familiar with the subject, but that should be a good googling point.
The basic idea is that you want a tree, with the leafs being an actual condition, and the branches between them the operators that define how they interact. The expresion template is this tree, and you overload operators to build the tree.
Then, when you actually want to know if your door needs to open, you walk the tree, checking each condition in turn until you have your answer. Short-circuit evaluation [like Tony mentioned] should come naturally, since you ultimately fall back on the built in operators.