right now i have to pass around a reference to my processor to all of my editor components and dsp components, and i feel like i’m doing something wrong.
is there a better idiom to access processor globally? i know i can get it from my top level editor, but i’m not sure of a straight-forward way to access that (and that doesn’t solve the issue from the dsp side).
There is a way, but the way you are doing it at the moment gives you most control, where you want to expose internals.
You can leverage the fact, that every Component is a child or grandchild of the editor.
Add an accessor method for the processor to your editor and use findParentComponentOfClass()
MyProcessor& MyEditor::getProcessor()
{
return processor;
}
// in any subcomponent:
if (auto* editor = findParentComponentOfClass<MyEditor>())
{
auto& processor = editor->getProcessor();
// use the processor
}
else
{
// component was not a child of MyEditor
jassertfalse;
}
findParentComponentOfClass costs incurs a loop of dynamic_cast calls. It seems like burning electricity … probably 4ns for the pointer solution about 300ns for the dynamic_cast loop mind you so it’s not long in real terms, but I’d feel guilty making the computer do it very often.