From 53acb7bfcbed943d7592cb868d427d8b48dd1bcb Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 12 Feb 2018 23:55:28 +0100 Subject: [PATCH] test/run_decder: RAII-style global initialization --- test/run_decoder.cxx | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/test/run_decoder.cxx b/test/run_decoder.cxx index 5bc00199c..90f93420e 100644 --- a/test/run_decoder.cxx +++ b/test/run_decoder.cxx @@ -35,6 +35,23 @@ #include #include +class GlobalInit { + EventThread io_thread; + +public: + GlobalInit() { + io_thread.Start(); + + input_stream_global_init(io_thread.GetEventLoop()); + decoder_plugin_init_all(); + } + + ~GlobalInit() { + decoder_plugin_deinit_all(); + input_stream_global_finish(); + } +}; + int main(int argc, char **argv) try { if (argc != 3) { @@ -42,16 +59,10 @@ try { return EXIT_FAILURE; } - FakeDecoder decoder; const char *const decoder_name = argv[1]; const char *const uri = argv[2]; - EventThread io_thread; - io_thread.Start(); - - input_stream_global_init(io_thread.GetEventLoop()); - - decoder_plugin_init_all(); + const GlobalInit init; const DecoderPlugin *plugin = decoder_plugin_from_name(decoder_name); if (plugin == nullptr) { @@ -59,6 +70,7 @@ try { return EXIT_FAILURE; } + FakeDecoder decoder; if (plugin->file_decode != nullptr) { plugin->FileDecode(decoder, Path::FromFS(uri)); } else if (plugin->stream_decode != nullptr) { @@ -70,9 +82,6 @@ try { return EXIT_FAILURE; } - decoder_plugin_deinit_all(); - input_stream_global_finish(); - if (!decoder.initialized) { fprintf(stderr, "Decoding failed\n"); return EXIT_FAILURE;