Extract some duplicated code

This commit is contained in:
Thomas Goyne 2014-03-24 07:44:20 -07:00
parent 470f85d365
commit dbe9bcfdad
3 changed files with 46 additions and 44 deletions

View file

@ -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 <libaegisub/log.h>
#include <libaegisub/util.h>
#include <boost/range/iterator_range.hpp>
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<AudioProvider> CreateHDAudioProvider(std::unique_ptr<AudioProvid
std::unique_ptr<AudioProvider> CreateRAMAudioProvider(std::unique_ptr<AudioProvider> source_provider, agi::BackgroundRunner *br);
namespace {
using factory_fn = std::unique_ptr<AudioProvider> (*)(agi::fs::path const&);
struct factory {
const char *name;
factory_fn create;
std::unique_ptr<AudioProvider> (*create)(agi::fs::path const&);
bool hidden;
};
@ -132,30 +134,12 @@ namespace {
}
std::vector<std::string> AudioProviderFactory::GetClasses() {
std::vector<std::string> 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<AudioProvider> AudioProviderFactory::GetProvider(agi::fs::path const& filename) {
auto preferred = OPT_GET("Audio/Provider")->GetString();
std::vector<const factory *> 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<AudioProvider> provider;
bool found_file = false;

View file

@ -73,3 +73,37 @@ public:
}
};
namespace {
template<typename Container>
std::vector<std::string> GetClasses(Container const& c) {
std::vector<std::string> list;
for (auto const& provider : c) {
if (!provider.hidden)
list.push_back(provider.name);
}
return list;
}
template<typename Container>
auto GetSorted(Container const& c, std::string const& preferred) -> std::vector<decltype(&*c.begin())> {
std::vector<decltype(&*c.begin())> 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;
}
}

View file

@ -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 <libaegisub/log.h>
#include <libaegisub/util.h>
#include <boost/range/iterator_range.hpp>
std::unique_ptr<VideoProvider> CreateDummyVideoProvider(agi::fs::path const&, std::string const&);
std::unique_ptr<VideoProvider> CreateYUV4MPEGVideoProvider(agi::fs::path const&, std::string const&);
std::unique_ptr<VideoProvider> CreateFFmpegSourceVideoProvider(agi::fs::path const&, std::string const&);
@ -33,10 +36,9 @@ std::unique_ptr<VideoProvider> CreateAvisynthVideoProvider(agi::fs::path const&,
std::unique_ptr<VideoProvider> CreateCacheVideoProvider(std::unique_ptr<VideoProvider>);
namespace {
using factory_fn = std::unique_ptr<VideoProvider> (*)(agi::fs::path const&, std::string const&);
struct factory {
const char *name;
factory_fn create;
std::unique_ptr<VideoProvider> (*create)(agi::fs::path const&, std::string const&);
bool hidden;
};
@ -53,30 +55,12 @@ namespace {
}
std::vector<std::string> VideoProviderFactory::GetClasses() {
std::vector<std::string> 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<VideoProvider> VideoProviderFactory::GetProvider(agi::fs::path const& filename, std::string const& colormatrix) {
auto preferred = OPT_GET("Video/Provider")->GetString();
std::vector<const factory *> 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;