Commit 12613356 authored by Max Kellermann's avatar Max Kellermann

filter/Chain: use std::forward_list instead of GSList

parent 3894450b
...@@ -27,17 +27,24 @@ ...@@ -27,17 +27,24 @@
#include <glib.h> #include <glib.h>
#include <list>
#include <assert.h> #include <assert.h>
struct ChainFilter { struct ChainFilter {
/** the base class */ /** the base class */
struct filter base; struct filter base;
GSList *children; std::list<struct filter *> children;
ChainFilter():children(nullptr) { ChainFilter() {
filter_init(&base, &chain_filter_plugin); filter_init(&base, &chain_filter_plugin);
} }
~ChainFilter() {
for (auto i : children)
filter_free(i);
}
}; };
static inline GQuark static inline GQuark
...@@ -56,21 +63,10 @@ chain_filter_init(G_GNUC_UNUSED const struct config_param *param, ...@@ -56,21 +63,10 @@ chain_filter_init(G_GNUC_UNUSED const struct config_param *param,
} }
static void static void
chain_free_child(gpointer data, G_GNUC_UNUSED gpointer user_data)
{
struct filter *filter = (struct filter *)data;
filter_free(filter);
}
static void
chain_filter_finish(struct filter *_filter) chain_filter_finish(struct filter *_filter)
{ {
ChainFilter *chain = (ChainFilter *)_filter; ChainFilter *chain = (ChainFilter *)_filter;
g_slist_foreach(chain->children, chain_free_child, NULL);
g_slist_free(chain->children);
delete chain; delete chain;
} }
...@@ -81,23 +77,17 @@ chain_filter_finish(struct filter *_filter) ...@@ -81,23 +77,17 @@ chain_filter_finish(struct filter *_filter)
static void static void
chain_close_until(ChainFilter *chain, const struct filter *until) chain_close_until(ChainFilter *chain, const struct filter *until)
{ {
GSList *i = chain->children; for (auto filter : chain->children) {
if (filter == until)
while (true) {
/* this assertion fails if #until does not exist
(anymore) */
assert(i != NULL);
if (i->data == until)
/* don't close this filter */ /* don't close this filter */
break; return;
/* close this filter */ /* close this filter */
struct filter *filter = (struct filter *)i->data;
filter_close(filter); filter_close(filter);
i = g_slist_next(i);
} }
/* this assertion fails if #until does not exist (anymore) */
assert(false);
} }
static const struct audio_format * static const struct audio_format *
...@@ -133,9 +123,7 @@ chain_filter_open(struct filter *_filter, struct audio_format *in_audio_format, ...@@ -133,9 +123,7 @@ chain_filter_open(struct filter *_filter, struct audio_format *in_audio_format,
ChainFilter *chain = (ChainFilter *)_filter; ChainFilter *chain = (ChainFilter *)_filter;
const struct audio_format *audio_format = in_audio_format; const struct audio_format *audio_format = in_audio_format;
for (GSList *i = chain->children; i != NULL; i = g_slist_next(i)) { for (auto filter : chain->children) {
struct filter *filter = (struct filter *)i->data;
audio_format = chain_open_child(filter, audio_format, error_r); audio_format = chain_open_child(filter, audio_format, error_r);
if (audio_format == NULL) { if (audio_format == NULL) {
/* rollback, close all children */ /* rollback, close all children */
...@@ -149,19 +137,12 @@ chain_filter_open(struct filter *_filter, struct audio_format *in_audio_format, ...@@ -149,19 +137,12 @@ chain_filter_open(struct filter *_filter, struct audio_format *in_audio_format,
} }
static void static void
chain_close_child(gpointer data, G_GNUC_UNUSED gpointer user_data)
{
struct filter *filter = (struct filter *)data;
filter_close(filter);
}
static void
chain_filter_close(struct filter *_filter) chain_filter_close(struct filter *_filter)
{ {
ChainFilter *chain = (ChainFilter *)_filter; ChainFilter *chain = (ChainFilter *)_filter;
g_slist_foreach(chain->children, chain_close_child, NULL); for (auto filter : chain->children)
filter_close(filter);
} }
static const void * static const void *
...@@ -171,9 +152,7 @@ chain_filter_filter(struct filter *_filter, ...@@ -171,9 +152,7 @@ chain_filter_filter(struct filter *_filter,
{ {
ChainFilter *chain = (ChainFilter *)_filter; ChainFilter *chain = (ChainFilter *)_filter;
for (GSList *i = chain->children; i != NULL; i = g_slist_next(i)) { for (auto filter : chain->children) {
struct filter *filter = (struct filter *)i->data;
/* feed the output of the previous filter as input /* feed the output of the previous filter as input
into the current one */ into the current one */
src = filter_filter(filter, src, src_size, &src_size, error_r); src = filter_filter(filter, src, src_size, &src_size, error_r);
...@@ -210,5 +189,5 @@ filter_chain_append(struct filter *_chain, struct filter *filter) ...@@ -210,5 +189,5 @@ filter_chain_append(struct filter *_chain, struct filter *filter)
{ {
ChainFilter *chain = (ChainFilter *)_chain; ChainFilter *chain = (ChainFilter *)_chain;
chain->children = g_slist_append(chain->children, filter); chain->children.push_back(filter);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment