From dbe9bcfdad8de01263baef9d4d71ba2d90291483 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Mon, 24 Mar 2014 07:44:20 -0700 Subject: [PATCH] Extract some duplicated code --- src/audio_provider.cpp | 28 ++++++---------------------- src/factory_manager.h | 34 ++++++++++++++++++++++++++++++++++ src/video_provider_manager.cpp | 28 ++++++---------------------- 3 files changed, 46 insertions(+), 44 deletions(-) diff --git a/src/audio_provider.cpp b/src/audio_provider.cpp index 577917bb5..50c717749 100644 --- a/src/audio_provider.cpp +++ b/src/audio_provider.cpp @@ -38,6 +38,7 @@ #include "audio_controller.h" #include "dialog_progress.h" +#include "factory_manager.h" #include "frame_main.h" #include "main.h" #include "options.h" @@ -47,6 +48,8 @@ #include #include +#include + void AudioProvider::GetAudioWithVolume(void *buf, int64_t start, int64_t count, double volume) const { GetAudio(buf, start, count); @@ -112,10 +115,9 @@ std::unique_ptr CreateHDAudioProvider(std::unique_ptr CreateRAMAudioProvider(std::unique_ptr source_provider, agi::BackgroundRunner *br); namespace { - using factory_fn = std::unique_ptr (*)(agi::fs::path const&); struct factory { const char *name; - factory_fn create; + std::unique_ptr (*create)(agi::fs::path const&); bool hidden; }; @@ -132,30 +134,12 @@ namespace { } std::vector AudioProviderFactory::GetClasses() { - std::vector list; - for (auto const& provider : providers) { - if (!provider.hidden) - list.push_back(provider.name); - } - return list; + return ::GetClasses(boost::make_iterator_range(std::begin(providers), std::end(providers))); } std::unique_ptr AudioProviderFactory::GetProvider(agi::fs::path const& filename) { auto preferred = OPT_GET("Audio/Provider")->GetString(); - std::vector sorted; - auto preferred_insertion_point = sorted.end(); - for (auto const& provider : providers) { - if (provider.hidden) - sorted.push_back(&provider); - else if (preferred_insertion_point == sorted.end()) { - sorted.push_back(&provider); - preferred_insertion_point = prev(sorted.end()); - } - else if (preferred == provider.name) - sorted.insert(preferred_insertion_point, &provider); - else - sorted.push_back(&provider); - } + auto sorted = GetSorted(boost::make_iterator_range(std::begin(providers), std::end(providers)), preferred); std::unique_ptr provider; bool found_file = false; diff --git a/src/factory_manager.h b/src/factory_manager.h index fcfd01f82..deb977377 100644 --- a/src/factory_manager.h +++ b/src/factory_manager.h @@ -73,3 +73,37 @@ public: } }; +namespace { +template +std::vector GetClasses(Container const& c) { + std::vector list; + for (auto const& provider : c) { + if (!provider.hidden) + list.push_back(provider.name); + } + return list; +} + +template +auto GetSorted(Container const& c, std::string const& preferred) -> std::vector { + std::vector sorted; + sorted.reserve(std::distance(c.begin(), c.end())); + size_t end_of_hidden = 0; + bool any_hidden = false; + for (auto const& provider : c) { + if (provider.hidden) { + sorted.push_back(&provider); + any_hidden = true; + } + else if (any_hidden && end_of_hidden == 0) { + end_of_hidden = sorted.size(); + sorted.push_back(&provider); + } + else if (preferred == provider.name) + sorted.insert(sorted.begin() + end_of_hidden, &provider); + else + sorted.push_back(&provider); + } + return sorted; +} +} diff --git a/src/video_provider_manager.cpp b/src/video_provider_manager.cpp index b74697350..73550b50f 100644 --- a/src/video_provider_manager.cpp +++ b/src/video_provider_manager.cpp @@ -18,6 +18,7 @@ #include "video_provider_manager.h" +#include "factory_manager.h" #include "include/aegisub/video_provider.h" #include "options.h" @@ -25,6 +26,8 @@ #include #include +#include + std::unique_ptr CreateDummyVideoProvider(agi::fs::path const&, std::string const&); std::unique_ptr CreateYUV4MPEGVideoProvider(agi::fs::path const&, std::string const&); std::unique_ptr CreateFFmpegSourceVideoProvider(agi::fs::path const&, std::string const&); @@ -33,10 +36,9 @@ std::unique_ptr CreateAvisynthVideoProvider(agi::fs::path const&, std::unique_ptr CreateCacheVideoProvider(std::unique_ptr); namespace { - using factory_fn = std::unique_ptr (*)(agi::fs::path const&, std::string const&); struct factory { const char *name; - factory_fn create; + std::unique_ptr (*create)(agi::fs::path const&, std::string const&); bool hidden; }; @@ -53,30 +55,12 @@ namespace { } std::vector VideoProviderFactory::GetClasses() { - std::vector list; - for (auto const& provider : providers) { - if (!provider.hidden) - list.push_back(provider.name); - } - return list; + return ::GetClasses(boost::make_iterator_range(std::begin(providers), std::end(providers))); } std::unique_ptr VideoProviderFactory::GetProvider(agi::fs::path const& filename, std::string const& colormatrix) { auto preferred = OPT_GET("Video/Provider")->GetString(); - std::vector sorted; - auto preferred_insertion_point = sorted.end(); - for (auto const& provider : providers) { - if (provider.hidden) - sorted.push_back(&provider); - else if (preferred_insertion_point == sorted.end()) { - sorted.push_back(&provider); - preferred_insertion_point = prev(sorted.end()); - } - else if (preferred == provider.name) - sorted.insert(preferred_insertion_point, &provider); - else - sorted.push_back(&provider); - } + auto sorted = GetSorted(boost::make_iterator_range(std::begin(providers), std::end(providers)), preferred); bool found = false; bool supported = false;