From 76afcdafa1ae9b363213c4cf241620acf367fdd9 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Fri, 26 Dec 2014 08:51:14 -0800 Subject: [PATCH] Eliminate uses of dynamic_cast on everything but GUI widgets --- .../include/libaegisub/of_type_adaptor.h | 8 ++++---- src/ass_karaoke.cpp | 18 +++++++++++------- src/auto4_lua.cpp | 10 +++++----- src/auto4_lua_assfile.cpp | 13 +++++++++---- src/font_file_lister.cpp | 17 +++++++++++------ src/subtitle_format_srt.cpp | 16 ++++++++++------ src/visual_tool.cpp | 3 ++- 7 files changed, 52 insertions(+), 33 deletions(-) diff --git a/libaegisub/include/libaegisub/of_type_adaptor.h b/libaegisub/include/libaegisub/of_type_adaptor.h index 9b684dd06..bccbefcf8 100644 --- a/libaegisub/include/libaegisub/of_type_adaptor.h +++ b/libaegisub/include/libaegisub/of_type_adaptor.h @@ -30,19 +30,19 @@ namespace agi { typedef Type *result_type; template Type *operator()(InType &ptr) const { - return dynamic_cast(&ptr); + return typeid(ptr) == typeid(Type) ? static_cast(&ptr) : nullptr; } template Type *operator()(std::unique_ptr& ptr) const { - return dynamic_cast(ptr.get()); + return (*this)(*ptr); } template Type *operator()(std::unique_ptr const& ptr) const { - return dynamic_cast(ptr.get()); + return (*this)(*ptr); } template Type *operator()(InType *ptr) const { - return dynamic_cast(ptr); + return (*this)(*ptr); } }; diff --git a/src/ass_karaoke.cpp b/src/ass_karaoke.cpp index fab0621ca..a06a81bd3 100644 --- a/src/ass_karaoke.cpp +++ b/src/ass_karaoke.cpp @@ -91,15 +91,18 @@ void AssKaraoke::ParseSyllables(AssDialogue *line, Syllable &syl) { for (auto& block : line->ParseTags()) { std::string text = block->GetText(); - if (dynamic_cast(block.get())) + switch (block->GetType()) { + case AssBlockType::PLAIN: syl.text += text; - else if (dynamic_cast(block.get())) + break; + case AssBlockType::COMMENT: + // drawings aren't override tags but they shouldn't show up in the + // stripped text so pretend they are + case AssBlockType::DRAWING: syl.ovr_tags[syl.text.size()] += text; - else if (dynamic_cast(block.get())) - // drawings aren't override tags but they shouldn't show up in the - // stripped text so pretend they are - syl.ovr_tags[syl.text.size()] += text; - else if (AssDialogueBlockOverride *ovr = dynamic_cast(block.get())) { + break; + case AssBlockType::OVERRIDE: + auto ovr = static_cast(block.get()); bool in_tag = false; for (auto& tag : ovr->Tags) { if (tag.IsValid() && boost::istarts_with(tag.Name, "\\k")) { @@ -137,6 +140,7 @@ void AssKaraoke::ParseSyllables(AssDialogue *line, Syllable &syl) { if (in_tag) syl.ovr_tags[syl.text.size()] += "}"; + break; } } diff --git a/src/auto4_lua.cpp b/src/auto4_lua.cpp index 1b6a5f318..e2816d4d1 100644 --- a/src/auto4_lua.cpp +++ b/src/auto4_lua.cpp @@ -237,13 +237,13 @@ namespace { lua_pushvalue(L, 1); std::unique_ptr et(Automation4::LuaAssFile::LuaToAssEntry(L)); - auto st = dynamic_cast(et.get()); lua_pop(L, 1); - if (!st) + if (typeid(*et) != typeid(AssStyle)) return error(L, "Not a style entry"); double width, height, descent, extlead; - if (!Automation4::CalculateTextExtents(st, check_string(L, 2), width, height, descent, extlead)) + if (!Automation4::CalculateTextExtents(static_cast(et.get()), + check_string(L, 2), width, height, descent, extlead)) return error(L, "Some internal error occurred calculating text_extents"); push_value(L, width); @@ -788,12 +788,12 @@ namespace { throw LuaForEachBreak(); } - auto diag = dynamic_cast(lines[cur - 1]); - if (!diag) { + if (typeid(*lines[cur - 1]) != typeid(AssDialogue)) { wxLogError("Selected row %d is not a dialogue line", cur); throw LuaForEachBreak(); } + auto diag = static_cast(lines[cur - 1]); sel.insert(diag); if (!active_line || active_idx == cur) active_line = diag; diff --git a/src/auto4_lua_assfile.cpp b/src/auto4_lua_assfile.cpp index 9a73aee2b..9d5bb25ab 100644 --- a/src/auto4_lua_assfile.cpp +++ b/src/auto4_lua_assfile.cpp @@ -123,6 +123,11 @@ namespace { default: return AssFile::COMMIT_SCRIPTINFO; } } + + template + T check_cast(U value) { + return typeid(T) == typeid(value) ? static_cast(value) : nullptr; + } } namespace Automation4 { @@ -150,13 +155,13 @@ namespace Automation4 { set_field(L, "section", e->GroupHeader()); - if (auto info = dynamic_cast(e)) { + if (auto info = check_cast(e)) { set_field(L, "raw", info->GetEntryData()); set_field(L, "key", info->Key()); set_field(L, "value", info->Value()); set_field(L, "class", "info"); } - else if (auto dia = dynamic_cast(e)) { + else if (auto dia = check_cast(e)) { set_field(L, "raw", dia->GetEntryData()); set_field(L, "comment", dia->Comment); @@ -187,7 +192,7 @@ namespace Automation4 { set_field(L, "class", "dialogue"); } - else if (auto sty = dynamic_cast(e)) { + else if (auto sty = check_cast(e)) { set_field(L, "raw", sty->GetEntryData()); set_field(L, "name", sty->name); @@ -618,7 +623,7 @@ namespace Automation4 { int LuaAssFile::LuaParseKaraokeData(lua_State *L) { auto e = LuaToAssEntry(L, ass); - auto dia = dynamic_cast(e.get()); + auto dia = check_cast(e.get()); argcheck(L, !!dia, 1, "Subtitle line must be a dialogue line"); int idx = 0; diff --git a/src/font_file_lister.cpp b/src/font_file_lister.cpp index de8203165..813e2ca96 100644 --- a/src/font_file_lister.cpp +++ b/src/font_file_lister.cpp @@ -75,8 +75,9 @@ void FontCollector::ProcessDialogueLine(const AssDialogue *line, int index) { bool overriden = false; for (auto& block : line->ParseTags()) { - if (auto ovr = dynamic_cast(block.get())) { - for (auto const& tag : ovr->Tags) { + switch (block->GetType()) { + case AssBlockType::OVERRIDE: + for (auto const& tag : static_cast(*block).Tags) { if (tag.Name == "\\r") { style = styles[tag.Params[0].Get(line->Style.get())]; overriden = false; @@ -94,9 +95,9 @@ void FontCollector::ProcessDialogueLine(const AssDialogue *line, int index) { overriden = true; } } - } - else if (auto txt = dynamic_cast(block.get())) { - auto text = txt->GetText(); + break; + case AssBlockType::PLAIN: { + auto text = block->GetText(); if (text.empty()) continue; @@ -135,8 +136,12 @@ void FontCollector::ProcessDialogueLine(const AssDialogue *line, int index) { sort(begin(chars), end(chars)); chars.erase(unique(chars.begin(), chars.end()), chars.end()); + break; + } + case AssBlockType::DRAWING: + case AssBlockType::COMMENT: + break; } - // Do nothing with drawing and comment blocks } } diff --git a/src/subtitle_format_srt.cpp b/src/subtitle_format_srt.cpp index 530cca805..981137a6d 100644 --- a/src/subtitle_format_srt.cpp +++ b/src/subtitle_format_srt.cpp @@ -485,9 +485,9 @@ std::string SRTSubtitleFormat::ConvertTags(const AssDialogue *diag) const { std::string final; for (auto& block : diag->ParseTags()) { - if (AssDialogueBlockOverride* ovr = dynamic_cast(block.get())) { - // Iterate through overrides - for (auto const& tag : ovr->Tags) { + switch (block->GetType()) { + case AssBlockType::OVERRIDE: + for (auto const& tag : static_cast(*block).Tags) { if (!tag.IsValid() || tag.Name.size() != 2) continue; for (auto& state : tag_states) { @@ -501,10 +501,14 @@ std::string SRTSubtitleFormat::ConvertTags(const AssDialogue *diag) const { state.value = temp; } } + break; + case AssBlockType::PLAIN: + final += block->GetText(); + break; + case AssBlockType::DRAWING: + case AssBlockType::COMMENT: + break; } - // Plain text - else if (AssDialogueBlockPlain *plain = dynamic_cast(block.get())) - final += plain->GetText(); } // Ensure all tags are closed diff --git a/src/visual_tool.cpp b/src/visual_tool.cpp index bec098dd5..bdbc4e813 100644 --- a/src/visual_tool.cpp +++ b/src/visual_tool.cpp @@ -534,7 +534,8 @@ void VisualToolBase::SetOverride(AssDialogue* line, std::string const& tag, std: auto blocks = line->ParseTags(); AssDialogueBlock *block = blocks.front().get(); - if (AssDialogueBlockOverride *ovr = dynamic_cast(block)) { + if (block->GetType() == AssBlockType::OVERRIDE) { + auto ovr = static_cast(block); // Remove old of same for (size_t i = 0; i < ovr->Tags.size(); i++) { std::string const& name = ovr->Tags[i].Name;