diff --git a/src/filter/FilterConfig.cxx b/src/filter/FilterConfig.cxx index 61a23797a..380a65020 100644 --- a/src/filter/FilterConfig.cxx +++ b/src/filter/FilterConfig.cxx @@ -19,8 +19,9 @@ #include "config.h" #include "FilterConfig.hxx" -#include "plugins/ChainFilterPlugin.hxx" #include "FilterPlugin.hxx" +#include "FilterInternal.hxx" +#include "plugins/ChainFilterPlugin.hxx" #include "config/Param.hxx" #include "config/ConfigOption.hxx" #include "config/ConfigGlobal.hxx" @@ -42,11 +43,11 @@ filter_chain_append_new(PreparedFilter &chain, const char *template_name) template_name); // Instantiate one of those filter plugins with the template name as a hint - PreparedFilter *f = filter_configured_new(*cfg); + auto f = filter_configured_new(*cfg); const char *plugin_name = cfg->GetBlockValue("plugin", "unknown"); - filter_chain_append(chain, plugin_name, f); + filter_chain_append(chain, plugin_name, std::move(f)); } void diff --git a/src/filter/FilterPlugin.cxx b/src/filter/FilterPlugin.cxx index 11326e09f..5f5ad5d8c 100644 --- a/src/filter/FilterPlugin.cxx +++ b/src/filter/FilterPlugin.cxx @@ -20,13 +20,14 @@ #include "config.h" #include "FilterPlugin.hxx" #include "FilterRegistry.hxx" +#include "FilterInternal.hxx" #include "config/Block.hxx" #include "config/ConfigError.hxx" #include "util/RuntimeError.hxx" #include -PreparedFilter * +std::unique_ptr filter_new(const FilterPlugin *plugin, const ConfigBlock &block) { assert(plugin != nullptr); @@ -34,7 +35,7 @@ filter_new(const FilterPlugin *plugin, const ConfigBlock &block) return plugin->init(block); } -PreparedFilter * +std::unique_ptr filter_configured_new(const ConfigBlock &block) { const char *plugin_name = block.GetBlockValue("plugin"); diff --git a/src/filter/FilterPlugin.hxx b/src/filter/FilterPlugin.hxx index 37d45f3ef..cb7f82476 100644 --- a/src/filter/FilterPlugin.hxx +++ b/src/filter/FilterPlugin.hxx @@ -26,6 +26,8 @@ #ifndef MPD_FILTER_PLUGIN_HXX #define MPD_FILTER_PLUGIN_HXX +#include + struct ConfigBlock; class PreparedFilter; @@ -35,7 +37,7 @@ struct FilterPlugin { /** * Allocates and configures a filter. */ - PreparedFilter *(*init)(const ConfigBlock &block); + std::unique_ptr (*init)(const ConfigBlock &block); }; /** @@ -46,7 +48,7 @@ struct FilterPlugin { * @param plugin the filter plugin * @param block configuration section */ -PreparedFilter * +std::unique_ptr filter_new(const FilterPlugin *plugin, const ConfigBlock &block); @@ -58,7 +60,7 @@ filter_new(const FilterPlugin *plugin, * * @param block the configuration section */ -PreparedFilter * +std::unique_ptr filter_configured_new(const ConfigBlock &block); #endif diff --git a/src/filter/Observer.cxx b/src/filter/Observer.cxx index 5d6602d46..706abd8b7 100644 --- a/src/filter/Observer.cxx +++ b/src/filter/Observer.cxx @@ -27,21 +27,20 @@ class FilterObserver::PreparedProxy final : public PreparedFilter { FilterObserver &observer; - PreparedFilter *const prepared_filter; + std::unique_ptr prepared_filter; Proxy *child = nullptr; public: PreparedProxy(FilterObserver &_observer, - PreparedFilter *_prepared_filter) + std::unique_ptr _prepared_filter) :observer(_observer), - prepared_filter(_prepared_filter) {} + prepared_filter(std::move(_prepared_filter)) {} ~PreparedProxy() { assert(child == nullptr); assert(observer.proxy == this); observer.proxy = nullptr; - delete prepared_filter; } void Clear(gcc_unused Proxy *_child) { @@ -95,12 +94,14 @@ FilterObserver::PreparedProxy::Open(AudioFormat &af) return child = new Proxy(*this, f); } -PreparedFilter * -FilterObserver::Set(PreparedFilter *pf) +std::unique_ptr +FilterObserver::Set(std::unique_ptr pf) { assert(proxy == nullptr); - return proxy = new PreparedProxy(*this, pf); + auto p = std::make_unique(*this, std::move(pf)); + proxy = p.get(); + return p; } Filter * diff --git a/src/filter/Observer.hxx b/src/filter/Observer.hxx index 276012bd8..b2114d1d0 100644 --- a/src/filter/Observer.hxx +++ b/src/filter/Observer.hxx @@ -22,6 +22,8 @@ #include "check.h" +#include + class PreparedFilter; class Filter; @@ -39,7 +41,7 @@ public: /** * @return a proxy object */ - PreparedFilter *Set(PreparedFilter *pf); + std::unique_ptr Set(std::unique_ptr pf); Filter *Get(); }; diff --git a/src/filter/plugins/AutoConvertFilterPlugin.cxx b/src/filter/plugins/AutoConvertFilterPlugin.cxx index 890018243..f805b6183 100644 --- a/src/filter/plugins/AutoConvertFilterPlugin.cxx +++ b/src/filter/plugins/AutoConvertFilterPlugin.cxx @@ -62,13 +62,11 @@ class PreparedAutoConvertFilter final : public PreparedFilter { /** * The underlying filter. */ - PreparedFilter *const filter; + std::unique_ptr filter; public: - PreparedAutoConvertFilter(PreparedFilter *_filter):filter(_filter) {} - ~PreparedAutoConvertFilter() { - delete filter; - } + PreparedAutoConvertFilter(std::unique_ptr _filter) noexcept + :filter(std::move(_filter)) {} Filter *Open(AudioFormat &af) override; }; @@ -106,8 +104,8 @@ AutoConvertFilter::FilterPCM(ConstBuffer src) return filter->FilterPCM(src); } -PreparedFilter * -autoconvert_filter_new(PreparedFilter *filter) +std::unique_ptr +autoconvert_filter_new(std::unique_ptr filter) noexcept { - return new PreparedAutoConvertFilter(filter); + return std::make_unique(std::move(filter)); } diff --git a/src/filter/plugins/AutoConvertFilterPlugin.hxx b/src/filter/plugins/AutoConvertFilterPlugin.hxx index e2bd6917e..de8e87509 100644 --- a/src/filter/plugins/AutoConvertFilterPlugin.hxx +++ b/src/filter/plugins/AutoConvertFilterPlugin.hxx @@ -20,6 +20,8 @@ #ifndef MPD_AUTOCONVERT_FILTER_PLUGIN_HXX #define MPD_AUTOCONVERT_FILTER_PLUGIN_HXX +#include + class PreparedFilter; /** @@ -28,7 +30,7 @@ class PreparedFilter; * requests a different format, it automatically creates a * convert_filter. */ -PreparedFilter * -autoconvert_filter_new(PreparedFilter *filter); +std::unique_ptr +autoconvert_filter_new(std::unique_ptr filter) noexcept; #endif diff --git a/src/filter/plugins/ChainFilterPlugin.cxx b/src/filter/plugins/ChainFilterPlugin.cxx index b3ac2fdbf..0349301b0 100644 --- a/src/filter/plugins/ChainFilterPlugin.cxx +++ b/src/filter/plugins/ChainFilterPlugin.cxx @@ -67,13 +67,11 @@ public: class PreparedChainFilter final : public PreparedFilter { struct Child { const char *name; - PreparedFilter *filter; + std::unique_ptr filter; - Child(const char *_name, PreparedFilter *_filter) - :name(_name), filter(_filter) {} - ~Child() { - delete filter; - } + Child(const char *_name, + std::unique_ptr _filter) + :name(_name), filter(std::move(_filter)) {} Child(const Child &) = delete; Child &operator=(const Child &) = delete; @@ -84,8 +82,9 @@ class PreparedChainFilter final : public PreparedFilter { std::list children; public: - void Append(const char *name, PreparedFilter *filter) { - children.emplace_back(name, filter); + void Append(const char *name, + std::unique_ptr filter) noexcept { + children.emplace_back(name, std::move(filter)); } /* virtual methods from class PreparedFilter */ @@ -143,17 +142,17 @@ ChainFilter::FilterPCM(ConstBuffer src) return src; } -PreparedFilter * -filter_chain_new(void) +std::unique_ptr +filter_chain_new() noexcept { - return new PreparedChainFilter(); + return std::make_unique(); } void filter_chain_append(PreparedFilter &_chain, const char *name, - PreparedFilter *filter) + std::unique_ptr filter) noexcept { PreparedChainFilter &chain = (PreparedChainFilter &)_chain; - chain.Append(name, filter); + chain.Append(name, std::move(filter)); } diff --git a/src/filter/plugins/ChainFilterPlugin.hxx b/src/filter/plugins/ChainFilterPlugin.hxx index 15814281d..c5b4c810d 100644 --- a/src/filter/plugins/ChainFilterPlugin.hxx +++ b/src/filter/plugins/ChainFilterPlugin.hxx @@ -27,13 +27,15 @@ #ifndef MPD_FILTER_CHAIN_HXX #define MPD_FILTER_CHAIN_HXX +#include + class PreparedFilter; /** * Creates a new filter chain. */ -PreparedFilter * -filter_chain_new(); +std::unique_ptr +filter_chain_new() noexcept; /** * Appends a new filter at the end of the filter chain. You must call @@ -44,6 +46,6 @@ filter_chain_new(); */ void filter_chain_append(PreparedFilter &chain, const char *name, - PreparedFilter *filter); + std::unique_ptr filter) noexcept; #endif diff --git a/src/filter/plugins/ConvertFilterPlugin.cxx b/src/filter/plugins/ConvertFilterPlugin.cxx index dc6fedf82..571f68e6e 100644 --- a/src/filter/plugins/ConvertFilterPlugin.cxx +++ b/src/filter/plugins/ConvertFilterPlugin.cxx @@ -119,10 +119,10 @@ ConvertFilter::FilterPCM(ConstBuffer src) return state.Convert(src); } -PreparedFilter * +std::unique_ptr convert_filter_prepare() noexcept { - return new PreparedConvertFilter(); + return std::make_unique(); } Filter * diff --git a/src/filter/plugins/ConvertFilterPlugin.hxx b/src/filter/plugins/ConvertFilterPlugin.hxx index 469271c93..26e472099 100644 --- a/src/filter/plugins/ConvertFilterPlugin.hxx +++ b/src/filter/plugins/ConvertFilterPlugin.hxx @@ -20,11 +20,13 @@ #ifndef MPD_CONVERT_FILTER_PLUGIN_HXX #define MPD_CONVERT_FILTER_PLUGIN_HXX +#include + class PreparedFilter; class Filter; struct AudioFormat; -PreparedFilter * +std::unique_ptr convert_filter_prepare() noexcept; Filter * diff --git a/src/filter/plugins/NormalizeFilterPlugin.cxx b/src/filter/plugins/NormalizeFilterPlugin.cxx index 4cd68b074..16119086b 100644 --- a/src/filter/plugins/NormalizeFilterPlugin.cxx +++ b/src/filter/plugins/NormalizeFilterPlugin.cxx @@ -53,10 +53,10 @@ public: Filter *Open(AudioFormat &af) override; }; -static PreparedFilter * +static std::unique_ptr normalize_filter_init(gcc_unused const ConfigBlock &block) { - return new PreparedNormalizeFilter(); + return std::make_unique(); } Filter * @@ -82,8 +82,8 @@ const FilterPlugin normalize_filter_plugin = { normalize_filter_init, }; -PreparedFilter * +std::unique_ptr normalize_filter_prepare() noexcept { - return new PreparedNormalizeFilter(); + return std::make_unique(); } diff --git a/src/filter/plugins/NormalizeFilterPlugin.hxx b/src/filter/plugins/NormalizeFilterPlugin.hxx index fb62575d8..3c9101341 100644 --- a/src/filter/plugins/NormalizeFilterPlugin.hxx +++ b/src/filter/plugins/NormalizeFilterPlugin.hxx @@ -20,11 +20,13 @@ #ifndef MPD_NORMALIZE_FILTER_PLUGIN_HXX #define MPD_NORMALIZE_FILTER_PLUGIN_HXX +#include + class PreparedFilter; class Filter; struct AudioFormat; -PreparedFilter * +std::unique_ptr normalize_filter_prepare() noexcept; #endif diff --git a/src/filter/plugins/NullFilterPlugin.cxx b/src/filter/plugins/NullFilterPlugin.cxx index 09599d156..7856494bd 100644 --- a/src/filter/plugins/NullFilterPlugin.cxx +++ b/src/filter/plugins/NullFilterPlugin.cxx @@ -48,10 +48,10 @@ public: } }; -static PreparedFilter * +static std::unique_ptr null_filter_init(gcc_unused const ConfigBlock &block) { - return new PreparedNullFilter(); + return std::make_unique(); } const FilterPlugin null_filter_plugin = { diff --git a/src/filter/plugins/ReplayGainFilterPlugin.cxx b/src/filter/plugins/ReplayGainFilterPlugin.cxx index 1a77d44dc..0a74bb13c 100644 --- a/src/filter/plugins/ReplayGainFilterPlugin.cxx +++ b/src/filter/plugins/ReplayGainFilterPlugin.cxx @@ -171,10 +171,10 @@ ReplayGainFilter::Update() pv.SetVolume(volume); } -PreparedFilter * -NewReplayGainFilter(const ReplayGainConfig &config) +std::unique_ptr +NewReplayGainFilter(const ReplayGainConfig &config) noexcept { - return new PreparedReplayGainFilter(config); + return std::make_unique(config); } Filter * diff --git a/src/filter/plugins/ReplayGainFilterPlugin.hxx b/src/filter/plugins/ReplayGainFilterPlugin.hxx index 5a295491d..2177537b3 100644 --- a/src/filter/plugins/ReplayGainFilterPlugin.hxx +++ b/src/filter/plugins/ReplayGainFilterPlugin.hxx @@ -22,14 +22,16 @@ #include "ReplayGainMode.hxx" +#include + class Filter; class PreparedFilter; class Mixer; struct ReplayGainConfig; struct ReplayGainInfo; -PreparedFilter * -NewReplayGainFilter(const ReplayGainConfig &config); +std::unique_ptr +NewReplayGainFilter(const ReplayGainConfig &config) noexcept; /** * Enables or disables the hardware mixer for applying replay gain. diff --git a/src/filter/plugins/RouteFilterPlugin.cxx b/src/filter/plugins/RouteFilterPlugin.cxx index 682dd06a3..98181800a 100644 --- a/src/filter/plugins/RouteFilterPlugin.cxx +++ b/src/filter/plugins/RouteFilterPlugin.cxx @@ -196,10 +196,10 @@ PreparedRouteFilter::PreparedRouteFilter(const ConfigBlock &block) } } -static PreparedFilter * +static std::unique_ptr route_filter_init(const ConfigBlock &block) { - return new PreparedRouteFilter(block); + return std::make_unique(block); } RouteFilter::RouteFilter(const AudioFormat &audio_format, diff --git a/src/filter/plugins/VolumeFilterPlugin.cxx b/src/filter/plugins/VolumeFilterPlugin.cxx index 81282dcc1..2c7addbd1 100644 --- a/src/filter/plugins/VolumeFilterPlugin.cxx +++ b/src/filter/plugins/VolumeFilterPlugin.cxx @@ -65,10 +65,10 @@ VolumeFilter::FilterPCM(ConstBuffer src) return pv.Apply(src); } -PreparedFilter * +std::unique_ptr volume_filter_prepare() noexcept { - return new PreparedVolumeFilter(); + return std::make_unique(); } unsigned diff --git a/src/filter/plugins/VolumeFilterPlugin.hxx b/src/filter/plugins/VolumeFilterPlugin.hxx index 1f7280ab9..6091663f7 100644 --- a/src/filter/plugins/VolumeFilterPlugin.hxx +++ b/src/filter/plugins/VolumeFilterPlugin.hxx @@ -20,10 +20,12 @@ #ifndef MPD_VOLUME_FILTER_PLUGIN_HXX #define MPD_VOLUME_FILTER_PLUGIN_HXX +#include + class PreparedFilter; class Filter; -PreparedFilter * +std::unique_ptr volume_filter_prepare() noexcept; unsigned diff --git a/src/output/Filtered.hxx b/src/output/Filtered.hxx index 34ac4de38..59ab78e3f 100644 --- a/src/output/Filtered.hxx +++ b/src/output/Filtered.hxx @@ -93,7 +93,7 @@ public: * The filter object of this audio output. This is an * instance of chain_filter_plugin. */ - PreparedFilter *prepared_filter = nullptr; + std::unique_ptr prepared_filter; /** * The #VolumeFilter instance of this audio output. It is @@ -105,14 +105,14 @@ public: * The replay_gain_filter_plugin instance of this audio * output. */ - PreparedFilter *prepared_replay_gain_filter = nullptr; + std::unique_ptr prepared_replay_gain_filter; /** * The replay_gain_filter_plugin instance of this audio * output, to be applied to the second chunk during * cross-fading. */ - PreparedFilter *prepared_other_replay_gain_filter = nullptr; + std::unique_ptr prepared_other_replay_gain_filter; /** * The convert_filter_plugin instance of this audio output. diff --git a/src/output/Finish.cxx b/src/output/Finish.cxx index 9e1c4eb46..a3714586f 100644 --- a/src/output/Finish.cxx +++ b/src/output/Finish.cxx @@ -27,10 +27,6 @@ FilteredAudioOutput::~FilteredAudioOutput() { if (mixer != nullptr) mixer_free(mixer); - - delete prepared_replay_gain_filter; - delete prepared_other_replay_gain_filter; - delete prepared_filter; } void diff --git a/src/output/Init.cxx b/src/output/Init.cxx index 70bb960a3..9dbd14ef6 100644 --- a/src/output/Init.cxx +++ b/src/output/Init.cxx @@ -22,12 +22,13 @@ #include "Registry.hxx" #include "Domain.hxx" #include "OutputAPI.hxx" -#include "filter/FilterConfig.hxx" #include "AudioParser.hxx" #include "mixer/MixerList.hxx" #include "mixer/MixerType.hxx" #include "mixer/MixerControl.hxx" #include "mixer/plugins/SoftwareMixerPlugin.hxx" +#include "filter/FilterConfig.hxx" +#include "filter/FilterInternal.hxx" #include "filter/plugins/AutoConvertFilterPlugin.hxx" #include "filter/plugins/ConvertFilterPlugin.hxx" #include "filter/plugins/ReplayGainFilterPlugin.hxx" diff --git a/src/output/Thread.cxx b/src/output/Thread.cxx index fe2e42512..2ff81c8e3 100644 --- a/src/output/Thread.cxx +++ b/src/output/Thread.cxx @@ -146,8 +146,8 @@ AudioOutputControl::InternalOpen(const AudioFormat in_audio_format, try { try { f = source.Open(in_audio_format, pipe, - output->prepared_replay_gain_filter, - output->prepared_other_replay_gain_filter, + output->prepared_replay_gain_filter.get(), + output->prepared_other_replay_gain_filter.get(), *output->prepared_filter); } catch (...) { std::throw_with_nested(FormatRuntimeError("Failed to open filter for %s", diff --git a/test/run_filter.cxx b/test/run_filter.cxx index 2251cf1c4..bad10169e 100644 --- a/test/run_filter.cxx +++ b/test/run_filter.cxx @@ -48,7 +48,7 @@ mixer_set_volume(gcc_unused Mixer *mixer, { } -static PreparedFilter * +static std::unique_ptr load_filter(const char *name) { const auto *param = config_find_block(ConfigBlockOption::AUDIO_FILTER, @@ -86,7 +86,7 @@ try { /* initialize the filter */ - std::unique_ptr prepared_filter(load_filter(argv[2])); + auto prepared_filter = load_filter(argv[2]); if (!prepared_filter) return EXIT_FAILURE;