From e4d6b8661b6471729beaa4ab8f6094f5d690a4bb Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Mon, 5 Nov 2012 08:20:58 -0800 Subject: [PATCH] Add range adaptor for filtering to a specific subclass from a heterogeneous range This makes looping over the subtitle file and parsed dialogue lines less awkward in many cases. --- aegisub/build/libaegisub/libaegisub.vcxproj | 1 + .../libaegisub/libaegisub.vcxproj.filters | 3 + .../include/libaegisub/of_type_adaptor.h | 69 +++++++++++++++++++ aegisub/src/ass_file.cpp | 17 +++-- aegisub/src/base_grid.cpp | 12 ++-- aegisub/src/command/edit.cpp | 26 +++---- aegisub/src/command/time.cpp | 7 +- aegisub/src/dialog_attachments.cpp | 16 ++--- aegisub/src/dialog_resample.cpp | 8 +-- aegisub/src/dialog_search_replace.cpp | 8 +-- aegisub/src/dialog_selection.cpp | 6 +- aegisub/src/dialog_shift_times.cpp | 5 +- aegisub/src/dialog_style_editor.cpp | 7 +- aegisub/src/dialog_style_manager.cpp | 10 +-- aegisub/src/export_fixstyle.cpp | 13 ++-- aegisub/src/export_framerate.cpp | 7 +- aegisub/src/subs_edit_box.cpp | 8 +-- aegisub/src/subtitle_format.cpp | 23 +++---- aegisub/src/subtitle_format_ebu3264.cpp | 6 +- aegisub/src/subtitle_format_encore.cpp | 10 +-- aegisub/src/subtitle_format_microdvd.cpp | 12 ++-- aegisub/src/subtitle_format_srt.cpp | 21 +++--- aegisub/src/subtitle_format_transtation.cpp | 11 ++- aegisub/src/subtitle_format_ttxt.cpp | 22 +++--- aegisub/src/subtitle_format_txt.cpp | 18 ++--- aegisub/src/visual_tool.cpp | 23 +++---- aegisub/src/visual_tool_drag.cpp | 12 ++-- 27 files changed, 210 insertions(+), 171 deletions(-) create mode 100644 aegisub/libaegisub/include/libaegisub/of_type_adaptor.h diff --git a/aegisub/build/libaegisub/libaegisub.vcxproj b/aegisub/build/libaegisub/libaegisub.vcxproj index 35402e0d2..870e9ffdf 100644 --- a/aegisub/build/libaegisub/libaegisub.vcxproj +++ b/aegisub/build/libaegisub/libaegisub.vcxproj @@ -92,6 +92,7 @@ + diff --git a/aegisub/build/libaegisub/libaegisub.vcxproj.filters b/aegisub/build/libaegisub/libaegisub.vcxproj.filters index 63a41bc87..04d67508d 100644 --- a/aegisub/build/libaegisub/libaegisub.vcxproj.filters +++ b/aegisub/build/libaegisub/libaegisub.vcxproj.filters @@ -134,6 +134,9 @@ Header Files + + Header Files + diff --git a/aegisub/libaegisub/include/libaegisub/of_type_adaptor.h b/aegisub/libaegisub/include/libaegisub/of_type_adaptor.h new file mode 100644 index 000000000..46545a606 --- /dev/null +++ b/aegisub/libaegisub/include/libaegisub/of_type_adaptor.h @@ -0,0 +1,69 @@ +// Copyright (c) 2011, Thomas Goyne +// +// Permission to use, copy, modify, and distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +// +// Aegisub Project http://www.aegisub.org/ + +#include +#include + +namespace agi { + namespace of_type_detail { + using namespace boost::adaptors; + + /// Tag type returned from of_type() to select the operator| overload + template struct of_type_tag {}; + + /// Take the address of a value and dynamic_cast it to Type* + template + struct cast_to { + typedef Type *result_type; + + template Type *operator()(InType &ptr) const { + return dynamic_cast(&ptr); + } + + template Type *operator()(InType *ptr) const { + return dynamic_cast(ptr); + } + }; + + template + inline bool not_null(Type *ptr) { + return !!ptr; + } + + // Defined here for ADL reasons, since we don't want the tag type in + // the top-level agi namespace (and it lets us get away with the using + // namespace above) + template + inline auto operator|(Rng& r, of_type_tag) + -> decltype(r | transformed(cast_to()) | filtered(not_null)) + { + return r | transformed(cast_to()) | filtered(not_null); + } + + // const overload of the above + template + inline auto operator|(Rng const& r, of_type_tag) + -> decltype(r | transformed(cast_to()) | filtered(not_null)) + { + return r | transformed(cast_to()) | filtered(not_null); + } + } + + template + inline of_type_detail::of_type_tag of_type() { + return of_type_detail::of_type_tag(); + } +} diff --git a/aegisub/src/ass_file.cpp b/aegisub/src/ass_file.cpp index 2091ed532..bf985021a 100644 --- a/aegisub/src/ass_file.cpp +++ b/aegisub/src/ass_file.cpp @@ -58,6 +58,8 @@ #include "text_file_writer.h" #include "utils.h" +#include + namespace std { template<> void swap(AssFile &lft, AssFile &rgt) { @@ -399,20 +401,17 @@ void AssFile::GetResolution(int &sw,int &sh) const { wxArrayString AssFile::GetStyles() const { wxArrayString styles; - for (auto const& line : Line) { - if (const AssStyle *curstyle = dynamic_cast(&line)) - styles.Add(curstyle->name); - } + for (auto style : Line | agi::of_type()) + styles.push_back(style->name); return styles; } AssStyle *AssFile::GetStyle(wxString const& name) { - for (auto& line : Line) { - AssStyle *curstyle = dynamic_cast(&line); - if (curstyle && curstyle->name == name) - return curstyle; + for (auto style : Line | agi::of_type()) { + if (style->name == name) + return style; } - return NULL; + return nullptr; } void AssFile::AddToRecent(wxString const& file) const { diff --git a/aegisub/src/base_grid.cpp b/aegisub/src/base_grid.cpp index 3c21b1f29..b8b320864 100644 --- a/aegisub/src/base_grid.cpp +++ b/aegisub/src/base_grid.cpp @@ -63,6 +63,8 @@ #include "video_context.h" #include "video_slider.h" +#include + enum { GRID_SCROLLBAR = 1730, MENU_SHOW_COL = 1250 // Needs 15 IDs after this @@ -278,12 +280,10 @@ void BaseGrid::UpdateMaps(bool preserve_selected_rows) { index_line_map.clear(); line_index_map.clear(); - for (auto& line : context->ass->Line) { - if (AssDialogue *curdiag = dynamic_cast(&line)) { - line_index_map[curdiag] = (int)index_line_map.size(); - index_line_map.push_back(curdiag); - } - } + for (auto curdiag : context->ass->Line | agi::of_type()) { + line_index_map[curdiag] = (int)index_line_map.size(); + index_line_map.push_back(curdiag); +} if (preserve_selected_rows) { Selection sel; diff --git a/aegisub/src/command/edit.cpp b/aegisub/src/command/edit.cpp index efe985287..5085eb21a 100644 --- a/aegisub/src/command/edit.cpp +++ b/aegisub/src/command/edit.cpp @@ -62,6 +62,10 @@ #include "../utils.h" #include "../video_context.h" +#include + +#include + namespace { using cmd::Command; /// @defgroup cmd-edit Editing commands. @@ -163,14 +167,10 @@ void paste_lines(agi::Context *c, bool paste_over) { template T get_value(AssDialogue const& line, int blockn, T initial, wxString const& tag, wxString alt = wxString()) { - for (int i = blockn; i >= 0; i--) { - AssDialogueBlockOverride *ovr = dynamic_cast(line.Blocks[i]); - if (!ovr) continue; - - for (int j = (int)ovr->Tags.size() - 1; j >= 0; j--) { - if (ovr->Tags[j]->Name == tag || ovr->Tags[j]->Name == alt) { - return ovr->Tags[j]->Params[0]->Get(initial); - } + for (auto ovr : line.Blocks | boost::adaptors::reversed | agi::of_type()) { + for (auto tag : ovr->Tags | boost::adaptors::reversed) { + if (tag->Name == tag || tag->Name == alt) + return tag->Params[0]->Get(initial); } } return initial; @@ -485,9 +485,8 @@ struct edit_find_replace : public Command { static void copy_lines(agi::Context *c) { wxString data; SubtitleSelection sel = c->selectionController->GetSelectedSet(); - for (auto& line : c->ass->Line) { - AssDialogue *diag = dynamic_cast(&line); - if (diag && sel.count(diag)) { + for (auto diag : c->ass->Line | agi::of_type()) { + if (sel.count(diag)) { if (!data.empty()) data += "\r\n"; data += diag->GetEntryData(); @@ -505,10 +504,7 @@ static void delete_lines(agi::Context *c, wxString const& commit_message) { AssDialogue *new_active = 0; bool hit_active = false; - for (auto& line : c->ass->Line) { - AssDialogue *diag = dynamic_cast(&line); - if (!diag) continue; - + for (auto diag : c->ass->Line | agi::of_type()) { if (diag == active) { hit_active = true; if (new_active) break; diff --git a/aegisub/src/command/time.cpp b/aegisub/src/command/time.cpp index 3d6f7f21e..e2adb1f00 100644 --- a/aegisub/src/command/time.cpp +++ b/aegisub/src/command/time.cpp @@ -54,6 +54,8 @@ #include "../subs_grid.h" #include "../video_context.h" +#include + namespace { using cmd::Command; @@ -71,10 +73,7 @@ namespace { if (sel.size() < 2) return false; size_t found = 0; - for (auto& line : c->ass->Line) { - AssDialogue *diag = dynamic_cast(&line); - if (!diag) continue; - + for (auto diag : c->ass->Line | agi::of_type()) { if (sel.count(diag)) { ++found; if (found == sel.size()) diff --git a/aegisub/src/dialog_attachments.cpp b/aegisub/src/dialog_attachments.cpp index 56359084f..e9fe5b5b5 100644 --- a/aegisub/src/dialog_attachments.cpp +++ b/aegisub/src/dialog_attachments.cpp @@ -53,6 +53,8 @@ #include "main.h" #include "utils.h" +#include + enum { BUTTON_ATTACH_FONT = 1300, BUTTON_ATTACH_GRAPHICS, @@ -102,14 +104,12 @@ void DialogAttachments::UpdateList() { listView->InsertColumn(2, _("Group"), wxLIST_FORMAT_LEFT, 100); // Fill list - for (auto& line : ass->Line) { - if (AssAttachment *attach = dynamic_cast(&line)) { - int row = listView->GetItemCount(); - listView->InsertItem(row,attach->GetFileName(true)); - listView->SetItem(row,1,PrettySize(attach->GetSize())); - listView->SetItem(row,2,attach->group); - listView->SetItemPtrData(row,wxPtrToUInt(attach)); - } + for (auto attach : ass->Line | agi::of_type()) { + int row = listView->GetItemCount(); + listView->InsertItem(row,attach->GetFileName(true)); + listView->SetItem(row,1,PrettySize(attach->GetSize())); + listView->SetItem(row,2,attach->group); + listView->SetItemPtrData(row,wxPtrToUInt(attach)); } } diff --git a/aegisub/src/dialog_resample.cpp b/aegisub/src/dialog_resample.cpp index 5b3486f6e..72ba309b0 100644 --- a/aegisub/src/dialog_resample.cpp +++ b/aegisub/src/dialog_resample.cpp @@ -42,6 +42,8 @@ #include "libresrc/libresrc.h" #include "video_context.h" +#include + enum { LEFT = 0, RIGHT = 1, @@ -206,10 +208,8 @@ namespace { diag->ParseAssTags(); diag->ProcessParameters(resample_tags, state); - for (auto block : diag->Blocks) { - if (AssDialogueBlockDrawing *drawing = dynamic_cast(block)) - drawing->TransformCoords(state->margin[LEFT], state->margin[TOP], state->rx, state->ry); - } + for (auto drawing : diag->Blocks | agi::of_type()) + drawing->TransformCoords(state->margin[LEFT], state->margin[TOP], state->rx, state->ry); for (size_t i = 0; i < 3; ++i) diag->Margin[i] = int((diag->Margin[i] + state->margin[i]) * (i < 2 ? state->rx : state->ry) + 0.5); diff --git a/aegisub/src/dialog_search_replace.cpp b/aegisub/src/dialog_search_replace.cpp index b9de1d985..52b5482b3 100644 --- a/aegisub/src/dialog_search_replace.cpp +++ b/aegisub/src/dialog_search_replace.cpp @@ -54,6 +54,8 @@ #include "subs_grid.h" #include "video_context.h" +#include + enum { BUTTON_FIND_NEXT, BUTTON_REPLACE_NEXT, @@ -378,11 +380,7 @@ void SearchReplaceEngine::ReplaceAll() { bool hasSelection = !sel.empty(); bool inSel = affect == 1; - // Scan - for (auto& line : context->ass->Line) { - AssDialogue *diag = dynamic_cast(&line); - if (!diag) continue; - + for (auto diag : context->ass->Line | agi::of_type()) { // Check if row is selected if (inSel && hasSelection && !sel.count(diag)) continue; diff --git a/aegisub/src/dialog_selection.cpp b/aegisub/src/dialog_selection.cpp index 97a2e0e7a..6fad9c762 100644 --- a/aegisub/src/dialog_selection.cpp +++ b/aegisub/src/dialog_selection.cpp @@ -45,6 +45,8 @@ #include "main.h" #include "selection_controller.h" +#include + enum { ACTION_SET = 0, ACTION_ADD, @@ -113,9 +115,7 @@ static std::set process(wxString match_text, bool match_case, int std::function pred = get_predicate(mode, &re, match_case, match_text); std::set matches; - for (auto& line : ass->Line) { - AssDialogue *diag = dynamic_cast(&line); - if (!diag) continue; + for (auto diag : ass->Line | agi::of_type()) { if (diag->Comment && !comments) continue; if (!diag->Comment && !dialogue) continue; diff --git a/aegisub/src/dialog_shift_times.cpp b/aegisub/src/dialog_shift_times.cpp index 18522e45f..e5f0bb236 100644 --- a/aegisub/src/dialog_shift_times.cpp +++ b/aegisub/src/dialog_shift_times.cpp @@ -38,6 +38,7 @@ #include #include +#include #include #include @@ -352,9 +353,7 @@ void DialogShiftTimes::Process(wxCommandEvent &) { int block_start = 0; json::Array shifted_blocks; - for (auto& entry : context->ass->Line) { - AssDialogue *line = dynamic_cast(&entry); - if (!line) continue; + for (auto line : context->ass->Line | agi::of_type()) { ++row_number; if (!sel.count(line)) { diff --git a/aegisub/src/dialog_style_editor.cpp b/aegisub/src/dialog_style_editor.cpp index aa0338f9e..8f41c95da 100644 --- a/aegisub/src/dialog_style_editor.cpp +++ b/aegisub/src/dialog_style_editor.cpp @@ -63,6 +63,8 @@ #include "utils.h" #include "validators.h" +#include + /// Style rename helper that walks a file searching for a style and optionally /// updating references to it class StyleRenamer { @@ -87,10 +89,7 @@ class StyleRenamer { found_any = false; do_replace = replace; - for (auto& line : c->ass->Line) { - AssDialogue *diag = dynamic_cast(&line); - if (!diag) continue; - + for (auto diag : c->ass->Line | agi::of_type()) { if (diag->Style == source_name) { if (replace) diag->Style = new_name; diff --git a/aegisub/src/dialog_style_manager.cpp b/aegisub/src/dialog_style_manager.cpp index f591dd869..c5e15cc5b 100644 --- a/aegisub/src/dialog_style_manager.cpp +++ b/aegisub/src/dialog_style_manager.cpp @@ -67,6 +67,8 @@ #include "subtitle_format.h" #include "utils.h" +#include + using std::placeholders::_1; namespace { @@ -274,11 +276,9 @@ void DialogStyleManager::LoadCurrentStyles(int commit_type) { CurrentList->Clear(); styleMap.clear(); - for (auto& line : c->ass->Line) { - if (AssStyle *style = dynamic_cast(&line)) { - CurrentList->Append(style->name); - styleMap.push_back(style); - } + for (auto style : c->ass->Line | agi::of_type()) { + CurrentList->Append(style->name); + styleMap.push_back(style); } } diff --git a/aegisub/src/export_fixstyle.cpp b/aegisub/src/export_fixstyle.cpp index c29ee0625..ca0b39fba 100644 --- a/aegisub/src/export_fixstyle.cpp +++ b/aegisub/src/export_fixstyle.cpp @@ -34,15 +34,18 @@ #include "config.h" +#include "export_fixstyle.h" + #ifndef AGI_PRE #include #include #endif -#include "export_fixstyle.h" #include "ass_file.h" #include "ass_dialogue.h" +#include + AssFixStylesFilter::AssFixStylesFilter() : AssExportFilter(_("Fix Styles"), _("Fixes styles by replacing any style that isn't available on file with Default."), -5000, true) { @@ -53,10 +56,8 @@ void AssFixStylesFilter::ProcessSubs(AssFile *subs, wxWindow *) { for_each(styles.begin(), styles.end(), std::mem_fun_ref(&wxString::MakeLower)); styles.Sort(); - for (auto& line : subs->Line) { - if (AssDialogue *diag = dynamic_cast(&line)) { - if (!std::binary_search(styles.begin(), styles.end(), diag->Style.Lower())) - diag->Style = "Default"; - } + for (auto diag : subs->Line | agi::of_type()) { + if (!std::binary_search(styles.begin(), styles.end(), diag->Style.Lower())) + diag->Style = "Default"; } } diff --git a/aegisub/src/export_framerate.cpp b/aegisub/src/export_framerate.cpp index f650557ab..14affc37a 100644 --- a/aegisub/src/export_framerate.cpp +++ b/aegisub/src/export_framerate.cpp @@ -54,6 +54,8 @@ #include "utils.h" #include "video_context.h" +#include + AssTransformFramerateFilter::AssTransformFramerateFilter() : AssExportFilter(_("Transform Framerate"), _("Transform subtitle times, including those in override tags, from an input framerate to an output framerate.\n\nThis is useful for converting regular time subtitles to VFRaC time subtitles for hardsubbing.\nIt can also be used to convert subtitles to a different speed video, such as NTSC to PAL speedup."), 1000) , c(0) @@ -208,10 +210,7 @@ void AssTransformFramerateFilter::TransformTimeTags(wxString name,int n,AssOverr void AssTransformFramerateFilter::TransformFrameRate(AssFile *subs) { if (!Input->IsLoaded() || !Output->IsLoaded()) return; - for (auto& entry : subs->Line) { - AssDialogue *curDialogue = dynamic_cast(&entry); - if (!curDialogue) continue; - + for (auto curDialogue : subs->Line | agi::of_type()) { line = curDialogue; newK = 0; oldK = 0; diff --git a/aegisub/src/subs_edit_box.cpp b/aegisub/src/subs_edit_box.cpp index e0906e917..f2fa73c74 100644 --- a/aegisub/src/subs_edit_box.cpp +++ b/aegisub/src/subs_edit_box.cpp @@ -68,6 +68,8 @@ #include "validators.h" #include "video_context.h" +#include + namespace { template struct field_setter : public std::binary_function { @@ -315,10 +317,8 @@ void SubsEditBox::PopulateList(wxComboBox *combo, wxString AssDialogue::*field) wxEventBlocker blocker(this); std::set values; - for (auto& line : c->ass->Line) { - if (AssDialogue *diag = dynamic_cast(&line)) - values.insert(diag->*field); - } + for (auto diag : c->ass->Line | agi::of_type()) + values.insert(diag->*field); values.erase(""); wxArrayString arrstr; arrstr.reserve(values.size()); diff --git a/aegisub/src/subtitle_format.cpp b/aegisub/src/subtitle_format.cpp index 7658e2b5a..51eaf1d23 100644 --- a/aegisub/src/subtitle_format.cpp +++ b/aegisub/src/subtitle_format.cpp @@ -57,6 +57,8 @@ #include "utils.h" #include "video_context.h" +#include + using namespace std::placeholders; SubtitleFormat::SubtitleFormat(wxString const& name) @@ -164,22 +166,17 @@ agi::vfr::Framerate SubtitleFormat::AskForFPS(bool allow_vfr, bool show_smpte) { } void SubtitleFormat::StripTags(AssFile &file) { - for (auto &line : file.Line) { - if (AssDialogue *current = dynamic_cast(&line)) { - current->StripTags(); - } - } + for (auto current : file.Line | agi::of_type()) + current->StripTags(); } void SubtitleFormat::ConvertNewlines(AssFile &file, wxString const& newline, bool mergeLineBreaks) { - for (auto &line : file.Line) { - if (AssDialogue *current = dynamic_cast(&line)) { - current->Text.Replace("\\h", " "); - current->Text.Replace("\\n", newline); - current->Text.Replace("\\N", newline); - if (mergeLineBreaks) { - while (current->Text.Replace(newline+newline, newline)); - } + for (auto current : file.Line | agi::of_type()) { + current->Text.Replace("\\h", " "); + current->Text.Replace("\\n", newline); + current->Text.Replace("\\N", newline); + if (mergeLineBreaks) { + while (current->Text.Replace(newline+newline, newline)); } } } diff --git a/aegisub/src/subtitle_format_ebu3264.cpp b/aegisub/src/subtitle_format_ebu3264.cpp index 5f98a80c7..5e781f9ca 100644 --- a/aegisub/src/subtitle_format_ebu3264.cpp +++ b/aegisub/src/subtitle_format_ebu3264.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include "aegisub_endian.h" @@ -384,11 +385,8 @@ namespace subs_list.reserve(copy.Line.size()); // convert to intermediate format - for (auto& orgline : copy.Line) + for (auto line : copy.Line | agi::of_type()) { - AssDialogue *line = dynamic_cast(&orgline); - if (!line) continue; - // add a new subtitle and work on it subs_list.push_back(EbuSubtitle()); EbuSubtitle &imline = subs_list.back(); diff --git a/aegisub/src/subtitle_format_encore.cpp b/aegisub/src/subtitle_format_encore.cpp index 2c4264293..634a213af 100644 --- a/aegisub/src/subtitle_format_encore.cpp +++ b/aegisub/src/subtitle_format_encore.cpp @@ -40,6 +40,8 @@ #include "ass_file.h" #include "text_file_writer.h" +#include + EncoreSubtitleFormat::EncoreSubtitleFormat() : SubtitleFormat("Adobe Encore") { @@ -77,10 +79,8 @@ void EncoreSubtitleFormat::WriteFile(const AssFile *src, wxString const& filenam // Write lines int i = 0; TextFileWriter file(filename, "UTF-8"); - for (auto const& line : copy.Line) { - if (const AssDialogue *current = dynamic_cast(&line)) { - ++i; - file.WriteLineToFile(wxString::Format("%i %s %s %s", i, ft.ToSMPTE(current->Start), ft.ToSMPTE(current->End), current->Text)); - } + for (auto current : copy.Line | agi::of_type()) { + ++i; + file.WriteLineToFile(wxString::Format("%i %s %s %s", i, ft.ToSMPTE(current->Start), ft.ToSMPTE(current->End), current->Text)); } } diff --git a/aegisub/src/subtitle_format_microdvd.cpp b/aegisub/src/subtitle_format_microdvd.cpp index 7495261fa..bff4458c9 100644 --- a/aegisub/src/subtitle_format_microdvd.cpp +++ b/aegisub/src/subtitle_format_microdvd.cpp @@ -47,6 +47,8 @@ #include "text_file_writer.h" #include "video_context.h" +#include + MicroDVDSubtitleFormat::MicroDVDSubtitleFormat() : SubtitleFormat("MicroDVD") { @@ -142,12 +144,10 @@ void MicroDVDSubtitleFormat::WriteFile(const AssFile *src, wxString const& filen } // Write lines - for (auto const& line : copy.Line) { - if (const AssDialogue *current = dynamic_cast(&line)) { - int start = fps.FrameAtTime(current->Start, agi::vfr::START); - int end = fps.FrameAtTime(current->End, agi::vfr::END); + for (auto current : copy.Line | agi::of_type()) { + int start = fps.FrameAtTime(current->Start, agi::vfr::START); + int end = fps.FrameAtTime(current->End, agi::vfr::END); - file.WriteLineToFile(wxString::Format("{%i}{%i}%s", start, end, current->Text)); - } + file.WriteLineToFile(wxString::Format("{%i}{%i}%s", start, end, current->Text)); } } diff --git a/aegisub/src/subtitle_format_srt.cpp b/aegisub/src/subtitle_format_srt.cpp index 24a26a4c2..c429412e0 100644 --- a/aegisub/src/subtitle_format_srt.cpp +++ b/aegisub/src/subtitle_format_srt.cpp @@ -51,6 +51,8 @@ #include "text_file_reader.h" #include "text_file_writer.h" +#include + DEFINE_SIMPLE_EXCEPTION(SRTParseError, SubtitleFormatParseError, "subtitle_io/parse/srt") namespace { @@ -508,14 +510,12 @@ void SRTSubtitleFormat::WriteFile(const AssFile *src, wxString const& filename, #endif // Write lines - int i=1; - for (auto const& line : copy.Line) { - if (const AssDialogue *current = dynamic_cast(&line)) { - file.WriteLineToFile(wxString::Format("%d", i++)); - file.WriteLineToFile(WriteSRTTime(current->Start) + " --> " + WriteSRTTime(current->End)); - file.WriteLineToFile(ConvertTags(current)); - file.WriteLineToFile(""); - } + int i=0; + for (auto current : copy.Line | agi::of_type()) { + file.WriteLineToFile(wxString::Format("%d", ++i)); + file.WriteLineToFile(WriteSRTTime(current->Start) + " --> " + WriteSRTTime(current->End)); + file.WriteLineToFile(ConvertTags(current)); + file.WriteLineToFile(""); } } @@ -536,10 +536,7 @@ bool SRTSubtitleFormat::CanSave(const AssFile *file) const { // Check dialogue if (const AssDialogue *curdiag = dynamic_cast(&line)) { std::vector blocks = curdiag->ParseTags(); - for (auto block : blocks) { - AssDialogueBlockOverride *ovr = dynamic_cast(block); - if (!ovr) continue; - + for (auto ovr : blocks | agi::of_type()) { // Verify that all overrides used are supported for (auto tag : ovr->Tags) { if (!std::binary_search(supported_tags, std::end(supported_tags), tag->Name)) { diff --git a/aegisub/src/subtitle_format_transtation.cpp b/aegisub/src/subtitle_format_transtation.cpp index 709119c7e..b46d1aec6 100644 --- a/aegisub/src/subtitle_format_transtation.cpp +++ b/aegisub/src/subtitle_format_transtation.cpp @@ -46,6 +46,8 @@ #include "ass_time.h" #include "text_file_writer.h" +#include + TranStationSubtitleFormat::TranStationSubtitleFormat() : SubtitleFormat("TranStation") { @@ -75,16 +77,13 @@ void TranStationSubtitleFormat::WriteFile(const AssFile *src, wxString const& fi SmpteFormatter ft(fps); TextFileWriter file(filename, encoding); AssDialogue *prev = 0; - for (auto& line : copy.Line) { - AssDialogue *cur = dynamic_cast(&line); - - if (prev && cur) { + for (auto cur : copy.Line | agi::of_type()) { + if (prev) { file.WriteLineToFile(ConvertLine(©, prev, fps, ft, cur->Start)); file.WriteLineToFile(""); } - if (cur) - prev = cur; + prev = cur; } // flush last line diff --git a/aegisub/src/subtitle_format_ttxt.cpp b/aegisub/src/subtitle_format_ttxt.cpp index 4176b4f0c..2f0b7141e 100644 --- a/aegisub/src/subtitle_format_ttxt.cpp +++ b/aegisub/src/subtitle_format_ttxt.cpp @@ -46,6 +46,9 @@ #include "compat.h" #include "main.h" +#include +#include + DEFINE_SIMPLE_EXCEPTION(TTXTParseError, SubtitleFormatParseError, "subtitle_io/parse/ttxt") TTXTSubtitleFormat::TTXTSubtitleFormat() @@ -176,14 +179,9 @@ void TTXTSubtitleFormat::WriteFile(const AssFile *src, wxString const& filename, // Create lines const AssDialogue *prev = 0; - for (auto const& line : copy.Line) { - const AssDialogue *current = dynamic_cast(&line); - if (current && !current->Comment) { - WriteLine(root, prev, current); - prev = current; - } - else - throw TTXTParseError("Unexpected line type in TTXT file", 0); + for (auto current : copy.Line | agi::of_type()) { + WriteLine(root, prev, current); + prev = current; } // Save XML @@ -268,11 +266,9 @@ void TTXTSubtitleFormat::ConvertToTTXT(AssFile &file) const { // Find last line AssTime lastTime; - for (EntryList::reverse_iterator cur = file.Line.rbegin(); cur != file.Line.rend(); ++cur) { - if (AssDialogue *prev = dynamic_cast(&*cur)) { - lastTime = prev->End; - break; - } + for (auto line : file.Line | boost::adaptors::reversed | agi::of_type()) { + lastTime = line->End; + break; } // Insert blank line at the end diff --git a/aegisub/src/subtitle_format_txt.cpp b/aegisub/src/subtitle_format_txt.cpp index 768b0009e..e66896e68 100644 --- a/aegisub/src/subtitle_format_txt.cpp +++ b/aegisub/src/subtitle_format_txt.cpp @@ -46,6 +46,8 @@ #include "utils.h" #include "version.h" +#include + TXTSubtitleFormat::TXTSubtitleFormat() : SubtitleFormat("Plain-Text") { @@ -129,9 +131,8 @@ void TXTSubtitleFormat::WriteFile(const AssFile *src, wxString const& filename, size_t num_actor_names = 0, num_dialogue_lines = 0; // Detect number of lines with Actor field filled out - for (auto const& line : src->Line) { - const AssDialogue *dia = dynamic_cast(&line); - if (dia && !dia->Comment) { + for (auto dia : src->Line | agi::of_type()) { + if (!dia->Comment) { num_dialogue_lines++; if (!dia->Actor.empty()) num_actor_names++; @@ -146,10 +147,7 @@ void TXTSubtitleFormat::WriteFile(const AssFile *src, wxString const& filename, file.WriteLineToFile(wxString("# Exported by Aegisub ") + GetAegisubShortVersionString()); // Write the file - for (auto const& line : src->Line) { - const AssDialogue *dia = dynamic_cast(&line); - if (!dia) continue; - + for (auto dia : src->Line | agi::of_type()) { wxString out_line; if (dia->Comment) @@ -161,10 +159,8 @@ void TXTSubtitleFormat::WriteFile(const AssFile *src, wxString const& filename, wxString out_text; if (strip_formatting) { std::vector blocks = dia->ParseTags(); - for (auto block : blocks) { - if (block->GetType() == BLOCK_PLAIN) - out_text += block->GetText(); - } + for (auto block : blocks | agi::of_type()) + out_text += block->GetText(); delete_clear(blocks); } else { diff --git a/aegisub/src/visual_tool.cpp b/aegisub/src/visual_tool.cpp index 42f1d023a..67c8142d1 100644 --- a/aegisub/src/visual_tool.cpp +++ b/aegisub/src/visual_tool.cpp @@ -42,10 +42,7 @@ #include "visual_tool_drag.h" #include "visual_tool_vector_clip.h" -template -static void for_each(C &range, F func) { - std::for_each(range.begin(), range.end(), func); -} +#include using std::placeholders::_1; @@ -218,9 +215,10 @@ void VisualTool::OnMouseEvent(wxMouseEvent &event) { if (dragging) { // continue drag if (event.LeftIsDown()) { - for_each(sel_features, std::bind(&FeatureType::UpdateDrag, _1, - mouse_pos - drag_start, shift_down)); - for_each(sel_features, std::bind(&VisualTool::UpdateDrag, this, _1)); + for (auto sel : sel_features) + sel->UpdateDrag(mouse_pos - drag_start, shift_down); + for (auto sel : sel_features) + UpdateDrag(sel); Commit(); need_render = true; } @@ -273,7 +271,7 @@ void VisualTool::OnMouseEvent(wxMouseEvent &event) { c->selectionController->SetActiveLine(active_feature->line); if (InitializeDrag(active_feature)) { - for_each(sel_features, std::bind(&VisualDraggableFeature::StartDrag, _1)); + for (auto sel : sel_features) sel->StartDrag(); dragging = true; parent->CaptureMouse(); } @@ -370,10 +368,7 @@ struct scoped_tag_parse { // Find a tag's parameters in a line or return NULL if it's not found static param_vec find_tag(const AssDialogue *line, wxString tag_name) { - for (auto block : line->Blocks) { - const AssDialogueBlockOverride *ovr = dynamic_cast(block); - if (!ovr) continue; - + for (auto ovr : line->Blocks | agi::of_type()) { for (auto tag : ovr->Tags) { if (tag->Name == tag_name) return &tag->Params; @@ -552,8 +547,8 @@ wxString VisualToolBase::GetLineVectorClip(AssDialogue *diag, int &scale, bool & } void VisualToolBase::SetSelectedOverride(wxString const& tag, wxString const& value) { - for_each(c->selectionController->GetSelectedSet(), - std::bind(&VisualToolBase::SetOverride, this, _1, tag, value)); + for (auto line : c->selectionController->GetSelectedSet()) + SetOverride(line, tag, value); } void VisualToolBase::SetOverride(AssDialogue* line, wxString const& tag, wxString const& value) { diff --git a/aegisub/src/visual_tool_drag.cpp b/aegisub/src/visual_tool_drag.cpp index 217c44eed..ec59d7c81 100644 --- a/aegisub/src/visual_tool_drag.cpp +++ b/aegisub/src/visual_tool_drag.cpp @@ -39,6 +39,8 @@ #include "video_context.h" #include "video_display.h" +#include + static const DraggableFeatureType DRAG_ORIGIN = DRAG_BIG_TRIANGLE; static const DraggableFeatureType DRAG_START = DRAG_BIG_SQUARE; static const DraggableFeatureType DRAG_END = DRAG_BIG_CIRCLE; @@ -114,9 +116,8 @@ void VisualToolDrag::OnFileChanged() { primary = 0; active_feature = features.end(); - for (auto& line : c->ass->Line) { - AssDialogue *diag = dynamic_cast(&line); - if (diag && IsDisplayed(diag)) + for (auto diag : c->ass->Line | agi::of_type()) { + if (IsDisplayed(diag)) MakeFeatures(diag); } @@ -130,10 +131,7 @@ void VisualToolDrag::OnFrameChanged() { feature_iterator feat = features.begin(); feature_iterator end = features.end(); - for (auto& line : c->ass->Line) { - AssDialogue *diag = dynamic_cast(&line); - if (!diag) continue; - + for (auto diag : c->ass->Line | agi::of_type()) { if (IsDisplayed(diag)) { // Features don't exist and should if (feat == end || feat->line != diag)