Kill the awkward automatic subtitle format (de)registration

This commit is contained in:
Thomas Goyne 2014-03-09 14:17:56 -07:00
parent e0d34bfb01
commit 64f51c917f
3 changed files with 19 additions and 32 deletions

View file

@ -291,7 +291,6 @@ int AegisubApp::OnExit() {
wxTheClipboard->Close(); wxTheClipboard->Close();
} }
SubtitleFormat::DestroyFormats();
delete config::opt; delete config::opt;
delete config::mru; delete config::mru;
hotkey::clear(); hotkey::clear();

View file

@ -53,10 +53,10 @@
#include "subtitle_format_transtation.h" #include "subtitle_format_transtation.h"
#include "subtitle_format_ttxt.h" #include "subtitle_format_ttxt.h"
#include "subtitle_format_txt.h" #include "subtitle_format_txt.h"
#include "utils.h"
#include "video_context.h" #include "video_context.h"
#include <libaegisub/fs.h> #include <libaegisub/fs.h>
#include <libaegisub/util.h>
#include <algorithm> #include <algorithm>
#include <boost/algorithm/string/join.hpp> #include <boost/algorithm/string/join.hpp>
@ -65,15 +65,16 @@
using namespace std::placeholders; using namespace std::placeholders;
namespace {
std::vector<std::unique_ptr<SubtitleFormat>> formats;
}
SubtitleFormat::SubtitleFormat(std::string name) SubtitleFormat::SubtitleFormat(std::string name)
: name(std::move(name)) : name(std::move(name))
{ {
formats.push_back(this);
} }
SubtitleFormat::~SubtitleFormat() { SubtitleFormat::~SubtitleFormat() { }
formats.erase(remove(begin(formats), end(formats), this));
}
bool SubtitleFormat::CanReadFile(agi::fs::path const& filename, std::string const&) const { bool SubtitleFormat::CanReadFile(agi::fs::path const& filename, std::string const&) const {
auto wildcards = GetReadWildcards(); auto wildcards = GetReadWildcards();
@ -289,33 +290,27 @@ void SubtitleFormat::MergeIdentical(AssFile &file) {
} }
} }
std::vector<SubtitleFormat*> SubtitleFormat::formats;
void SubtitleFormat::LoadFormats() { void SubtitleFormat::LoadFormats() {
if (formats.empty()) { if (formats.empty()) {
new AssSubtitleFormat; formats.emplace_back(agi::util::make_unique<AssSubtitleFormat>());
new Ebu3264SubtitleFormat; formats.emplace_back(agi::util::make_unique<Ebu3264SubtitleFormat>());
new EncoreSubtitleFormat; formats.emplace_back(agi::util::make_unique<EncoreSubtitleFormat>());
new MKVSubtitleFormat; formats.emplace_back(agi::util::make_unique<MKVSubtitleFormat>());
new MicroDVDSubtitleFormat; formats.emplace_back(agi::util::make_unique<MicroDVDSubtitleFormat>());
new SRTSubtitleFormat; formats.emplace_back(agi::util::make_unique<SRTSubtitleFormat>());
new TTXTSubtitleFormat; formats.emplace_back(agi::util::make_unique<TTXTSubtitleFormat>());
new TXTSubtitleFormat; formats.emplace_back(agi::util::make_unique<TXTSubtitleFormat>());
new TranStationSubtitleFormat; formats.emplace_back(agi::util::make_unique<TranStationSubtitleFormat>());
} }
} }
void SubtitleFormat::DestroyFormats() {
while (!formats.empty())
delete formats.back();
}
template<class Cont, class Pred> template<class Cont, class Pred>
SubtitleFormat *find_or_throw(Cont &container, Pred pred) { SubtitleFormat *find_or_throw(Cont &container, Pred pred) {
auto it = find_if(container.begin(), container.end(), pred); auto it = find_if(container.begin(), container.end(), pred);
if (it == container.end()) if (it == container.end())
throw UnknownSubtitleFormatError("Subtitle format for extension not found", nullptr); throw UnknownSubtitleFormatError("Subtitle format for extension not found", nullptr);
return *it; return it->get();
} }
const SubtitleFormat *SubtitleFormat::GetReader(agi::fs::path const& filename, std::string const& encoding) { const SubtitleFormat *SubtitleFormat::GetReader(agi::fs::path const& filename, std::string const& encoding) {
@ -334,11 +329,11 @@ std::string SubtitleFormat::GetWildcards(int mode) {
std::vector<std::string> all; std::vector<std::string> all;
std::string final; std::string final;
for (auto format : formats) { for (auto const& format : formats) {
std::vector<std::string> cur = mode == 0 ? format->GetReadWildcards() : format->GetWriteWildcards(); auto cur = mode == 0 ? format->GetReadWildcards() : format->GetWriteWildcards();
if (cur.empty()) continue; if (cur.empty()) continue;
for_each(cur.begin(), cur.end(), [](std::string &str) { str.insert(0, "*."); }); for (auto& str : cur) str.insert(0, "*.");
all.insert(all.end(), begin(cur), end(cur)); all.insert(all.end(), begin(cur), end(cur));
final += "|" + format->GetName() + " (" + boost::join(cur, ",") + ")|" + boost::join(cur, ";"); final += "|" + format->GetName() + " (" + boost::join(cur, ",") + ")|" + boost::join(cur, ";");
} }

View file

@ -51,9 +51,6 @@ class SubtitleFormat {
/// Get this format's wildcards for a save dialog /// Get this format's wildcards for a save dialog
virtual std::vector<std::string> GetWriteWildcards() const { return {}; } virtual std::vector<std::string> GetWriteWildcards() const { return {}; }
/// List of loaded subtitle formats
static std::vector<SubtitleFormat*> formats;
public: public:
/// Strip override tags /// Strip override tags
static void StripTags(AssFile &file); static void StripTags(AssFile &file);
@ -77,10 +74,8 @@ public:
/// Constructor /// Constructor
/// @param Subtitle format name /// @param Subtitle format name
/// @note Automatically registers the format
SubtitleFormat(std::string name); SubtitleFormat(std::string name);
/// Destructor /// Destructor
/// @note Automatically unregisters the format
virtual ~SubtitleFormat(); virtual ~SubtitleFormat();
/// Get this format's name /// Get this format's name
@ -126,8 +121,6 @@ public:
static const SubtitleFormat *GetWriter(agi::fs::path const& filename); static const SubtitleFormat *GetWriter(agi::fs::path const& filename);
/// Initialize subtitle formats /// Initialize subtitle formats
static void LoadFormats(); static void LoadFormats();
/// Deinitialize subtitle formats
static void DestroyFormats();
}; };
DEFINE_SIMPLE_EXCEPTION(SubtitleFormatParseError, agi::InvalidInputException, "subtitle_io/parse/generic") DEFINE_SIMPLE_EXCEPTION(SubtitleFormatParseError, agi::InvalidInputException, "subtitle_io/parse/generic")