LCOV - code coverage report
Current view: top level - src/audio/effects - effect_factory.h (source / functions) Coverage Total Hit
Test: merged.info Lines: 100.0 % 39 39
Test Date: 2026-06-01 11:15:25 Functions: 100.0 % 62 62

            Line data    Source code
       1              : #pragma once
       2              : 
       3              : #include "audio/effects/effect.h"
       4              : #include <functional>
       5              : #include <stdexcept>
       6              : #include <unordered_map>
       7              : #include <string>
       8              : 
       9              : namespace Amplitron {
      10              : 
      11              : /**
      12              :  * Registry-based effect factory.
      13              :  * Replaces the if-chain in PresetManager::create_effect().
      14              :  * All effect types self-register via EffectRegistrar.
      15              :  */
      16              : class EffectFactory {
      17              : public:
      18              :     using Creator = std::function<std::shared_ptr<Effect>()>;
      19              : 
      20          666 :     static EffectFactory& instance() {
      21          669 :         static EffectFactory factory;
      22          666 :         return factory;
      23              :     }
      24              : 
      25          111 :     void register_effect(const std::string& type_name, Creator creator) {
      26          111 :         if (creators_.count(type_name) > 0) {
      27            4 :             throw std::runtime_error("Duplicate effect registration: " + type_name);
      28              :         }
      29          108 :         creators_.emplace(type_name, std::move(creator));
      30          109 :     }
      31              : 
      32          549 :     std::shared_ptr<Effect> create(const std::string& type_name) const {
      33          549 :         auto it = creators_.find(type_name);
      34          549 :         if (it != creators_.end()) {
      35          465 :             return it->second();
      36              :         }
      37           84 :         return nullptr;
      38          183 :     }
      39              : 
      40            6 :     std::vector<std::string> registered_types() const {
      41            6 :         std::vector<std::string> types;
      42            6 :         types.reserve(creators_.size());
      43          114 :         for (auto& [name, _] : creators_) {
      44          108 :             types.push_back(name);
      45              :         }
      46            6 :         return types;
      47            2 :     }
      48              : 
      49           57 :     std::shared_ptr<Effect> create_from_type(const std::string& type_name) const {
      50           57 :         return create(type_name);
      51              :     }
      52              : 
      53            6 :     std::vector<std::string> get_all_type_names() const {
      54            6 :         return registered_types();
      55              :     }
      56              : 
      57              : private:
      58            8 :     EffectFactory() = default;
      59              :     std::unordered_map<std::string, Creator> creators_;
      60              : };
      61              : 
      62              : /**
      63              :  * RAII helper that registers an effect type at static-init time.
      64              :  * Usage (in .cpp file):
      65              :  *   static EffectRegistrar<MyEffect> reg("My Effect");
      66              :  */
      67              : template <typename T>
      68              : struct EffectRegistrar {
      69          144 :     explicit EffectRegistrar(const std::string& type_name) {
      70          418 :         EffectFactory::instance().register_effect(type_name, []() {
      71          310 :             return std::make_shared<T>();
      72              :         });
      73          144 :     }
      74              : };
      75              : 
      76            3 : inline std::shared_ptr<Effect> Effect::clone() const {
      77            4 :     auto new_effect = EffectFactory::instance().create(type_id());
      78            3 :     if (new_effect) {
      79            3 :         new_effect->set_params(get_params());
      80            3 :         new_effect->set_enabled(enabled_);
      81            3 :         new_effect->set_mix(mix_);
      82            1 :     }
      83            3 :     return new_effect;
      84            1 : }
      85              : 
      86              : } // namespace Amplitron
        

Generated by: LCOV version 2.0-1