Moved the factory to unique pointers

This commit is contained in:
Paul Fd 2020-03-05 16:53:12 +01:00
parent 28c12f32f3
commit 7532291545
5 changed files with 13 additions and 14 deletions

View file

@ -29,7 +29,7 @@ void EffectFactory::registerEffectType(absl::string_view name, Effect::MakeInsta
_entries.push_back(std::move(ent)); _entries.push_back(std::move(ent));
} }
Effect* EffectFactory::makeEffect(absl::Span<const Opcode> members) std::unique_ptr<Effect> EffectFactory::makeEffect(absl::Span<const Opcode> members)
{ {
const Opcode* opcode = nullptr; const Opcode* opcode = nullptr;
@ -40,7 +40,7 @@ Effect* EffectFactory::makeEffect(absl::Span<const Opcode> members)
if (!opcode) { if (!opcode) {
DBG("The effect does not specify a type"); DBG("The effect does not specify a type");
return new sfz::fx::Nothing; return std::make_unique<sfz::fx::Nothing>();
} }
absl::string_view type = opcode->value; absl::string_view type = opcode->value;
@ -52,13 +52,13 @@ Effect* EffectFactory::makeEffect(absl::Span<const Opcode> members)
if (it == end) { if (it == end) {
DBG("Unsupported effect type: " << type); DBG("Unsupported effect type: " << type);
return new sfz::fx::Nothing; return std::make_unique<sfz::fx::Nothing>();
} }
Effect* fx = it->make(members); auto fx = std::unique_ptr<Effect>(it->make(members));
if (!fx) { if (!fx) {
DBG("Could not instantiate effect of type: " << type); DBG("Could not instantiate effect of type: " << type);
return new sfz::fx::Nothing; return std::make_unique<sfz::fx::Nothing>();
} }
return fx; return fx;

View file

@ -46,7 +46,7 @@ public:
@brief Type of the factory function used to instantiate an effect given @brief Type of the factory function used to instantiate an effect given
the contents of the <effect> block the contents of the <effect> block
*/ */
typedef Effect* (MakeInstance)(absl::Span<const Opcode> members); typedef std::unique_ptr<Effect> (MakeInstance)(absl::Span<const Opcode> members);
}; };
/** /**
@ -67,7 +67,7 @@ public:
/** /**
@brief Instantiates an effect given the contents of the <effect> block. @brief Instantiates an effect given the contents of the <effect> block.
*/ */
Effect* makeEffect(absl::Span<const Opcode> members); std::unique_ptr<Effect> makeEffect(absl::Span<const Opcode> members);
private: private:
struct FactoryEntry { struct FactoryEntry {

View file

@ -256,10 +256,9 @@ void sfz::Synth::handleEffectOpcodes(const std::vector<Opcode>& members)
// create the effect and add it // create the effect and add it
EffectBus& bus = getOrCreateBus(busIndex); EffectBus& bus = getOrCreateBus(busIndex);
Effect* fx = effectFactory.makeEffect(members); auto fx = effectFactory.makeEffect(members);
bus.addEffect(std::unique_ptr<Effect>(fx));
fx->init(sampleRate); fx->init(sampleRate);
bus.addEffect(std::move(fx));
} }
void addEndpointsToVelocityCurve(sfz::Region& region) void addEndpointsToVelocityCurve(sfz::Region& region)

View file

@ -72,9 +72,9 @@ namespace fx {
} }
} }
Effect* Lofi::makeInstance(absl::Span<const Opcode> members) std::unique_ptr<Effect> Lofi::makeInstance(absl::Span<const Opcode> members)
{ {
std::unique_ptr<Lofi> fx { new Lofi }; auto fx = std::make_unique<Lofi>();
for (const Opcode& opcode : members) { for (const Opcode& opcode : members) {
switch (opcode.lettersOnlyHash) { switch (opcode.lettersOnlyHash) {
@ -87,7 +87,7 @@ namespace fx {
} }
} }
return fx.release(); return fx;
} }
/// ///

View file

@ -34,7 +34,7 @@ namespace fx {
/** /**
* @brief Instantiates given the contents of the <effect> block. * @brief Instantiates given the contents of the <effect> block.
*/ */
static Effect* makeInstance(absl::Span<const Opcode> members); static std::unique_ptr<Effect> makeInstance(absl::Span<const Opcode> members);
private: private:
float _bitred_depth = 0; float _bitred_depth = 0;