| 735 | |
| 736 | |
| 737 | void Engine::addModule(Module* module) { |
| 738 | std::lock_guard<SharedMutex> lock(internal->mutex); |
| 739 | DISTRHO_SAFE_ASSERT_RETURN(module != nullptr,); |
| 740 | // Check that the module is not already added |
| 741 | auto it = std::find(internal->modules.begin(), internal->modules.end(), module); |
| 742 | DISTRHO_SAFE_ASSERT_RETURN(it == internal->modules.end(),); |
| 743 | auto tit = std::find(internal->terminalModules.begin(), internal->terminalModules.end(), module); |
| 744 | DISTRHO_SAFE_ASSERT_RETURN(tit == internal->terminalModules.end(),); |
| 745 | // Set ID if unset or collides with an existing ID |
| 746 | while (module->id < 0 || internal->modulesCache.find(module->id) != internal->modulesCache.end()) { |
| 747 | // Randomly generate ID |
| 748 | module->id = random::u64() % (1ull << 53); |
| 749 | } |
| 750 | // Add module |
| 751 | if (TerminalModule* const terminalModule = asTerminalModule(module)) |
| 752 | internal->terminalModules.push_back(terminalModule); |
| 753 | else |
| 754 | internal->modules.push_back(module); |
| 755 | internal->modulesCache[module->id] = module; |
| 756 | // Dispatch AddEvent |
| 757 | Module::AddEvent eAdd; |
| 758 | module->onAdd(eAdd); |
| 759 | // Dispatch SampleRateChangeEvent |
| 760 | Module::SampleRateChangeEvent eSrc; |
| 761 | eSrc.sampleRate = internal->sampleRate; |
| 762 | eSrc.sampleTime = internal->sampleTime; |
| 763 | module->onSampleRateChange(eSrc); |
| 764 | // Update ParamHandles' module pointers |
| 765 | for (ParamHandle* paramHandle : internal->paramHandles) { |
| 766 | if (paramHandle->moduleId == module->id) |
| 767 | paramHandle->module = module; |
| 768 | } |
| 769 | #if DEBUG_ORDERED_MODULES |
| 770 | printf("New module: %s - %ld\n", module->model->getFullName().c_str(), module->id); |
| 771 | #endif |
| 772 | } |
| 773 | |
| 774 | |
| 775 | void Engine::removeModule(Module* module) { |
no test coverage detected