From d2046de1932039643493a6de3bec79c17406fe0a Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 3 Jan 2017 12:22:14 +0100 Subject: [PATCH] test/run_input, ...: RAII-style global initialization --- test/dump_text_file.cxx | 55 +++++++++++++++++++---------------------- test/run_input.cxx | 54 +++++++++++++++++++--------------------- test/visit_archive.cxx | 39 +++++++++++++++++------------ 3 files changed, 75 insertions(+), 73 deletions(-) diff --git a/test/dump_text_file.cxx b/test/dump_text_file.cxx index b25761dd3..8fa64c5f7 100644 --- a/test/dump_text_file.cxx +++ b/test/dump_text_file.cxx @@ -36,6 +36,27 @@ #include #include +class GlobalInit { + const ScopeIOThread io_thread; + +public: + GlobalInit() { + config_global_init(); +#ifdef ENABLE_ARCHIVE + archive_plugin_init_all(); +#endif + input_stream_global_init(); + } + + ~GlobalInit() { + input_stream_global_finish(); +#ifdef ENABLE_ARCHIVE + archive_plugin_deinit_all(); +#endif + config_global_finish(); + } +}; + static void dump_text_file(TextInputStream &is) { @@ -60,8 +81,6 @@ dump_input_stream(InputStreamPtr &&is) int main(int argc, char **argv) try { - int ret; - if (argc != 2) { fprintf(stderr, "Usage: run_input URI\n"); return EXIT_FAILURE; @@ -69,37 +88,15 @@ try { /* initialize MPD */ - config_global_init(); - - const ScopeIOThread io_thread; - -#ifdef ENABLE_ARCHIVE - archive_plugin_init_all(); -#endif - - input_stream_global_init(); + const GlobalInit init; /* open the stream and dump it */ - { - Mutex mutex; - Cond cond; + Mutex mutex; + Cond cond; - auto is = InputStream::OpenReady(argv[1], mutex, cond); - ret = dump_input_stream(std::move(is)); - } - - /* deinitialize everything */ - - input_stream_global_finish(); - -#ifdef ENABLE_ARCHIVE - archive_plugin_deinit_all(); -#endif - - config_global_finish(); - - return ret; + auto is = InputStream::OpenReady(argv[1], mutex, cond); + return dump_input_stream(std::move(is)); } catch (const std::exception &e) { LogError(e); return EXIT_FAILURE; diff --git a/test/run_input.cxx b/test/run_input.cxx index 0e519cd0e..691189148 100644 --- a/test/run_input.cxx +++ b/test/run_input.cxx @@ -38,6 +38,27 @@ #include #include +class GlobalInit { + const ScopeIOThread io_thread; + +public: + GlobalInit() { + config_global_init(); +#ifdef ENABLE_ARCHIVE + archive_plugin_init_all(); +#endif + input_stream_global_init(); + } + + ~GlobalInit() { + input_stream_global_finish(); +#ifdef ENABLE_ARCHIVE + archive_plugin_deinit_all(); +#endif + config_global_finish(); + } +}; + static void tag_save(FILE *file, const Tag &tag) { @@ -91,37 +112,14 @@ try { /* initialize MPD */ - config_global_init(); - - const ScopeIOThread io_thread; - -#ifdef ENABLE_ARCHIVE - archive_plugin_init_all(); -#endif - - input_stream_global_init(); + const GlobalInit init; /* open the stream and dump it */ - int ret; - { - Mutex mutex; - Cond cond; - auto is = InputStream::OpenReady(argv[1], mutex, cond); - ret = dump_input_stream(is.get()); - } - - /* deinitialize everything */ - - input_stream_global_finish(); - -#ifdef ENABLE_ARCHIVE - archive_plugin_deinit_all(); -#endif - - config_global_finish(); - - return ret; + Mutex mutex; + Cond cond; + auto is = InputStream::OpenReady(argv[1], mutex, cond); + return dump_input_stream(is.get()); } catch (const std::exception &e) { LogError(e); return EXIT_FAILURE; diff --git a/test/visit_archive.cxx b/test/visit_archive.cxx index 28d5dda35..dd5b2d3b3 100644 --- a/test/visit_archive.cxx +++ b/test/visit_archive.cxx @@ -35,8 +35,29 @@ #include #include +class GlobalInit { + const ScopeIOThread io_thread; + +public: + GlobalInit() { + config_global_init(); +#ifdef ENABLE_ARCHIVE + archive_plugin_init_all(); +#endif + input_stream_global_init(); + } + + ~GlobalInit() { + input_stream_global_finish(); +#ifdef ENABLE_ARCHIVE + archive_plugin_deinit_all(); +#endif + config_global_finish(); + } +}; + class MyArchiveVisitor final : public ArchiveVisitor { - public: +public: virtual void VisitArchiveEntry(const char *path_utf8) override { printf("%s\n", path_utf8); } @@ -55,13 +76,7 @@ try { /* initialize MPD */ - config_global_init(); - - const ScopeIOThread io_thread; - - archive_plugin_init_all(); - - input_stream_global_init(); + const GlobalInit init; /* open the archive and dump it */ @@ -79,14 +94,6 @@ try { file->Visit(visitor); file->Close(); - /* deinitialize everything */ - - input_stream_global_finish(); - - archive_plugin_deinit_all(); - - config_global_finish(); - return result; } catch (const std::exception &e) { LogError(e);