Functors#
SAMSON uses SBCFunctor-based functors to represent small pieces of reusable logic such as predicates for selection and conditions used during traversal.
In practice, you will encounter functors most often when querying the data graph.
Mental model#
A functor lets you describe what you want without manually writing the traversal and filtering logic at each call site.
That makes code easier to read and easier to combine. Instead of writing nested if statements in a custom loop, you can often compose predicates directly.
Selection functors#
Selection functors are predicates that can be combined with logical operators. They are commonly passed to node-collection APIs.
Example:
// Find all atoms with a partial charge larger than 0.4f
SBNodeIndexer nodeIndexer;
SAMSON::getActiveDocument()->getNodes(
nodeIndexer,
SBNode::IsType(SBNode::Atom) &&
SBAtom::HasPartialCharge() &&
(SBAtom::GetPartialCharge() >= 0.4f));
This style is useful because the intent stays visible:
- restrict to atoms
- require a partial charge
- apply a numerical threshold
When functors are useful#
Use functors when:
- you need concise node filtering logic
- the selection condition should be composable
- the same predicate may be reused in several places
If the selection logic becomes a first-class reusable object with parameters and lifecycle of its own, a selector may be a better abstraction.
What to read next#
- Getting nodes for more practical node-query examples
- Selectors for reusable selection objects rather than inline predicate composition