Idiom for accessing processor

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).

Nope - you’re doing it right … there’s no magic bullet!

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;
}
1 Like

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.

1 Like

I agree 100%, I stated my preference towards your solution (maybe not strong enough).

Just wanted to point out there is a possibility, but I also wouldn’t use it.

  • Once because of the reason you mentioned (unnecessary overhead), and
  • because each child calling that method will need knowledge about the Editor (to call the getProcessor()), which is a weak design.

I thought you were agreeing. Just wanted to try and help modosc by making it extra clear.

Separation of Concerns

1 Like