Switch from boost::ptr_vector to a vector of unique_ptr

ptr_vector hasn't been updated for C++11, so despite being specifically
designed to store pointers to objects it's less safe and not really any
easier to use than a regular vector of unique_ptrs
This commit is contained in:
Thomas Goyne 2014-04-14 10:58:46 -07:00
parent f53d840520
commit 7fc78d40ab
15 changed files with 74 additions and 74 deletions

View File

@ -33,6 +33,14 @@ namespace agi {
return dynamic_cast<Type *>(&ptr);
}
template<class InType> Type *operator()(std::unique_ptr<InType>& ptr) const {
return dynamic_cast<Type *>(ptr.get());
}
template<class InType> Type *operator()(std::unique_ptr<InType> const& ptr) const {
return dynamic_cast<Type *>(ptr.get());
}
template<class InType> Type *operator()(InType *ptr) const {
return dynamic_cast<Type *>(ptr);
}

View File

@ -70,7 +70,6 @@
#endif
#include <boost/flyweight.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/indirected.hpp>
#include <boost/range/adaptor/reversed.hpp>

View File

@ -168,13 +168,13 @@ std::string AssDialogue::GetData(bool ssa) const {
return str;
}
std::auto_ptr<boost::ptr_vector<AssDialogueBlock>> AssDialogue::ParseTags() const {
boost::ptr_vector<AssDialogueBlock> Blocks;
std::vector<std::unique_ptr<AssDialogueBlock>> AssDialogue::ParseTags() const {
std::vector<std::unique_ptr<AssDialogueBlock>> Blocks;
// Empty line, make an empty block
if (Text.get().empty()) {
Blocks.push_back(new AssDialogueBlockPlain);
return Blocks.release();
Blocks.push_back(agi::util::make_unique<AssDialogueBlockPlain>());
return Blocks;
}
int drawingLevel = 0;
@ -198,19 +198,19 @@ std::auto_ptr<boost::ptr_vector<AssDialogueBlock>> AssDialogue::ParseTags() cons
if (work.size() && work.find('\\') == std::string::npos) {
//We've found an override block with no backslashes
//We're going to assume it's a comment and not consider it an override block
Blocks.push_back(new AssDialogueBlockComment(work));
Blocks.push_back(agi::util::make_unique<AssDialogueBlockComment>(work));
}
else {
// Create block
auto block = new AssDialogueBlockOverride(work);
auto block = agi::util::make_unique<AssDialogueBlockOverride>(work);
block->ParseTags();
Blocks.push_back(block);
// Look for \p in block
for (auto const& tag : block->Tags) {
if (tag.Name == "\\p")
drawingLevel = tag.Params[0].Get<int>(0);
}
Blocks.push_back(std::move(block));
}
continue;
@ -230,20 +230,20 @@ plain:
}
if (drawingLevel == 0)
Blocks.push_back(new AssDialogueBlockPlain(work));
Blocks.push_back(agi::util::make_unique<AssDialogueBlockPlain>(work));
else
Blocks.push_back(new AssDialogueBlockDrawing(work, drawingLevel));
Blocks.push_back(agi::util::make_unique<AssDialogueBlockDrawing>(work, drawingLevel));
}
return Blocks.release();
return Blocks;
}
void AssDialogue::StripTags() {
Text = GetStrippedText();
}
static std::string get_text(AssDialogueBlock &d) { return d.GetText(); }
void AssDialogue::UpdateText(boost::ptr_vector<AssDialogueBlock>& blocks) {
static std::string get_text(std::unique_ptr<AssDialogueBlock> &d) { return d->GetText(); }
void AssDialogue::UpdateText(std::vector<std::unique_ptr<AssDialogueBlock>>& blocks) {
if (blocks.empty()) return;
Text = join(blocks | transformed(get_text), "");
}
@ -255,6 +255,6 @@ bool AssDialogue::CollidesWith(const AssDialogue *target) const {
static std::string get_text_p(AssDialogueBlock *d) { return d->GetText(); }
std::string AssDialogue::GetStrippedText() const {
boost::ptr_vector<AssDialogueBlock> blocks(ParseTags());
auto blocks = ParseTags();
return join(blocks | agi::of_type<AssDialogueBlockPlain>() | transformed(get_text_p), "");
}

View File

@ -40,7 +40,6 @@
#include <array>
#include <boost/flyweight.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <vector>
enum class AssBlockType {
@ -162,7 +161,7 @@ public:
AssEntryGroup Group() const override { return AssEntryGroup::DIALOGUE; }
/// Parse text as ASS and return block information
std::auto_ptr<boost::ptr_vector<AssDialogueBlock>> ParseTags() const;
std::vector<std::unique_ptr<AssDialogueBlock>> ParseTags() const;
/// Strip all ASS tags from the text
void StripTags();
@ -171,7 +170,7 @@ public:
std::string GetStrippedText() const;
/// Update the text of the line from parsed blocks
void UpdateText(boost::ptr_vector<AssDialogueBlock>& blocks);
void UpdateText(std::vector<std::unique_ptr<AssDialogueBlock>>& blocks);
std::string GetEntryData() const { return GetData(false); }
/// Get the line as SSA rather than ASS

View File

@ -99,20 +99,18 @@ void AssKaraoke::SetLine(AssDialogue *line, bool auto_split, bool normalize) {
}
void AssKaraoke::ParseSyllables(AssDialogue *line, Syllable &syl) {
boost::ptr_vector<AssDialogueBlock> blocks(line->ParseTags());
for (auto& block : line->ParseTags()) {
std::string text = block->GetText();
for (auto& block : blocks) {
std::string text = block.GetText();
if (dynamic_cast<AssDialogueBlockPlain*>(&block))
if (dynamic_cast<AssDialogueBlockPlain*>(block.get()))
syl.text += text;
else if (dynamic_cast<AssDialogueBlockComment*>(&block))
else if (dynamic_cast<AssDialogueBlockComment*>(block.get()))
syl.ovr_tags[syl.text.size()] += text;
else if (dynamic_cast<AssDialogueBlockDrawing*>(&block))
else if (dynamic_cast<AssDialogueBlockDrawing*>(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<AssDialogueBlockOverride*>(&block)) {
else if (AssDialogueBlockOverride *ovr = dynamic_cast<AssDialogueBlockOverride*>(block.get())) {
bool in_tag = false;
for (auto& tag : ovr->Tags) {
if (tag.IsValid() && boost::istarts_with(tag.Name, "\\k")) {

View File

@ -62,6 +62,7 @@
#include <boost/format.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/indirected.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/range/adaptor/sliced.hpp>
#include <boost/range/adaptor/transformed.hpp>
@ -157,7 +158,7 @@ AssDialogue *paste_over(wxWindow *parent, std::vector<bool>& pasteOverOptions, A
}
template<typename T>
T get_value(boost::ptr_vector<AssDialogueBlock> const& blocks, int blockn, T initial, std::string const& tag_name, std::string alt = "") {
T get_value(std::vector<std::unique_ptr<AssDialogueBlock>> const& blocks, int blockn, T initial, std::string const& tag_name, std::string alt = "") {
for (auto ovr : blocks | sliced(0, blockn + 1) | reversed | agi::of_type<AssDialogueBlockOverride>()) {
for (auto const& tag : ovr->Tags | reversed) {
if (tag.Name == tag_name || tag.Name == alt)
@ -197,7 +198,7 @@ int block_at_pos(std::string const& text, int pos) {
return n - in_block;
}
void set_tag(AssDialogue *line, boost::ptr_vector<AssDialogueBlock> &blocks, std::string const& tag, std::string const& value, int &sel_start, int &sel_end, bool at_end = false) {
void set_tag(AssDialogue *line, std::vector<std::unique_ptr<AssDialogueBlock>> &blocks, std::string const& tag, std::string const& value, int &sel_start, int &sel_end, bool at_end = false) {
if (blocks.empty())
blocks = line->ParseTags();
@ -207,7 +208,7 @@ void set_tag(AssDialogue *line, boost::ptr_vector<AssDialogueBlock> &blocks, std
AssDialogueBlockPlain *plain = nullptr;
AssDialogueBlockOverride *ovr = nullptr;
while (blockn >= 0) {
AssDialogueBlock *block = &blocks[blockn];
AssDialogueBlock *block = blocks[blockn].get();
if (dynamic_cast<AssDialogueBlockDrawing*>(block))
--blockn;
else if (dynamic_cast<AssDialogueBlockComment*>(block)) {
@ -286,7 +287,7 @@ void toggle_override_tag(const agi::Context *c, bool (AssStyle::*field), const c
AssStyle const* const style = c->ass->GetStyle(line->Style);
bool state = style ? style->*field : AssStyle().*field;
boost::ptr_vector<AssDialogueBlock> blocks(line->ParseTags());
auto blocks = line->ParseTags();
int sel_start = c->textSelectionController->GetSelectionStart();
int sel_end = c->textSelectionController->GetSelectionEnd();
int blockn = block_at_pos(line->Text, sel_start);
@ -305,7 +306,7 @@ void show_color_picker(const agi::Context *c, agi::Color (AssStyle::*field), con
AssStyle const* const style = c->ass->GetStyle(line->Style);
agi::Color color = (style ? style->*field : AssStyle().*field);
boost::ptr_vector<AssDialogueBlock> blocks(line->ParseTags());
auto blocks = line->ParseTags();
int sel_start = c->textSelectionController->GetSelectionStart();
int sel_end = c->textSelectionController->GetSelectionEnd();
int blockn = block_at_pos(line->Text, sel_start);
@ -436,7 +437,7 @@ struct edit_font final : public Command {
void operator()(agi::Context *c) override {
AssDialogue *const line = c->selectionController->GetActiveLine();
boost::ptr_vector<AssDialogueBlock> blocks(line->ParseTags());
auto blocks = line->ParseTags();
const int blockn = block_at_pos(line->Text, c->textSelectionController->GetInsertionPoint());
const AssStyle *style = c->ass->GetStyle(line->Style);
@ -1150,8 +1151,9 @@ struct edit_clear_text final : public Command {
void operator()(agi::Context *c) override {
AssDialogue *line = c->selectionController->GetActiveLine();
boost::ptr_vector<AssDialogueBlock> blocks(line->ParseTags());
auto blocks = line->ParseTags();
line->Text = join(blocks
| indirected
| filtered([](AssDialogueBlock const& b) { return b.GetType() != AssBlockType::PLAIN; })
| transformed(get_text),
"");

View File

@ -94,7 +94,7 @@ class StyleRenamer {
found_any = true;
}
boost::ptr_vector<AssDialogueBlock> blocks(diag.ParseTags());
auto blocks = diag.ParseTags();
for (auto block : blocks | agi::of_type<AssDialogueBlockOverride>())
block->ProcessParameters(&StyleRenamer::ProcessTag, this);
if (replace)

View File

@ -56,8 +56,8 @@ static void add_hotkey(wxSizer *sizer, wxWindow *parent, const char *command, wx
}
// Skip over override blocks, comments, and whitespace between blocks
static bool bad_block(AssDialogueBlock &block) {
return block.GetType() != AssBlockType::PLAIN || boost::all(block.GetText(), boost::is_space());
static bool bad_block(std::unique_ptr<AssDialogueBlock> &block) {
return block->GetType() != AssBlockType::PLAIN || boost::all(block->GetText(), boost::is_space());
}
DialogTranslation::DialogTranslation(agi::Context *c)
@ -245,17 +245,17 @@ void DialogTranslation::UpdateDisplay() {
size_t i = 0;
for (auto& block : blocks) {
if (block.GetType() == AssBlockType::PLAIN) {
if (block->GetType() == AssBlockType::PLAIN) {
int initial_pos = original_text->GetLength();
original_text->AppendTextRaw(block.GetText().c_str());
original_text->AppendTextRaw(block->GetText().c_str());
if (i == cur_block) {
int cur_size = original_text->GetReverseUnicodePosition(initial_pos);
original_text->StartUnicodeStyling(cur_size);
original_text->SetUnicodeStyling(cur_size, block.GetText().size(), 1);
original_text->SetUnicodeStyling(cur_size, block->GetText().size(), 1);
}
}
else
original_text->AppendTextRaw(block.GetText().c_str());
original_text->AppendTextRaw(block->GetText().c_str());
++i;
}
@ -272,7 +272,7 @@ void DialogTranslation::Commit(bool next) {
new_value.Replace("\r\n", "\\N");
new_value.Replace("\r", "\\N");
new_value.Replace("\n", "\\N");
blocks[cur_block] = AssDialogueBlockPlain(from_wx(new_value));
*blocks[cur_block] = AssDialogueBlockPlain(from_wx(new_value));
active_line->UpdateText(blocks);
file_change_connection.Block();
@ -291,7 +291,7 @@ void DialogTranslation::Commit(bool next) {
}
void DialogTranslation::InsertOriginal() {
translated_text->AddText(to_wx(blocks[cur_block].GetText()));
translated_text->AddText(to_wx(blocks[cur_block]->GetText()));
}

View File

@ -18,13 +18,12 @@
/// @see dialog_translation.cpp
/// @ingroup tools_ui
#include <wx/dialog.h>
#include <libaegisub/exception.h>
#include <libaegisub/signal.h>
#include <boost/ptr_container/ptr_vector.hpp>
#include <memory>
#include <vector>
#include <wx/dialog.h>
namespace agi { struct Context; }
class AssDialogue;
@ -45,7 +44,7 @@ class DialogTranslation final : public wxDialog {
/// The active line
AssDialogue *active_line;
/// The parsed dialogue blocks for the active line
boost::ptr_vector<AssDialogueBlock> blocks;
std::vector<std::unique_ptr<AssDialogueBlock>> blocks;
/// Which dialogue block in the active line is currently being translated
size_t cur_block = 0;

View File

@ -209,7 +209,7 @@ void AssTransformFramerateFilter::TransformFrameRate(AssFile *subs) {
newEnd = trunc_cs(ConvertTime(curDialogue.End) + 9);
// Process stuff
boost::ptr_vector<AssDialogueBlock> blocks;
auto blocks = line->ParseTags();
for (auto block : blocks | agi::of_type<AssDialogueBlockOverride>())
block->ProcessParameters(TransformTimeTags, this);
curDialogue.Start = newStart;

View File

@ -73,14 +73,13 @@ void FontCollector::ProcessDialogueLine(const AssDialogue *line, int index) {
return;
}
boost::ptr_vector<AssDialogueBlock> blocks(line->ParseTags());
StyleInfo style = style_it->second;
StyleInfo initial = style;
bool overriden = false;
for (auto& block : blocks) {
if (AssDialogueBlockOverride *ovr = dynamic_cast<AssDialogueBlockOverride *>(&block)) {
for (auto& block : line->ParseTags()) {
if (AssDialogueBlockOverride *ovr = dynamic_cast<AssDialogueBlockOverride *>(block.get())) {
for (auto const& tag : ovr->Tags) {
std::string const& name = tag.Name;
@ -102,7 +101,7 @@ void FontCollector::ProcessDialogueLine(const AssDialogue *line, int index) {
}
}
}
else if (AssDialogueBlockPlain *txt = dynamic_cast<AssDialogueBlockPlain *>(&block)) {
else if (AssDialogueBlockPlain *txt = dynamic_cast<AssDialogueBlockPlain *>(block.get())) {
wxString text(to_wx(txt->GetText()));
if (text.empty())

View File

@ -129,7 +129,7 @@ namespace {
if (diag.Comment && (boost::starts_with(diag.Effect.get(), "template") || boost::starts_with(diag.Effect.get(), "code")))
return;
boost::ptr_vector<AssDialogueBlock> blocks(diag.ParseTags());
auto blocks = diag.ParseTags();
for (auto block : blocks | agi::of_type<AssDialogueBlockOverride>())
block->ProcessParameters(resample_tags, state);

View File

@ -259,8 +259,6 @@ namespace
void SetTextFromAss(AssDialogue *line, bool style_underline, bool style_italic, int align, int wrap_mode)
{
boost::ptr_vector<AssDialogueBlock> blocks(line->ParseTags());
text_rows.clear();
text_rows.emplace_back();
@ -272,14 +270,14 @@ namespace
bool underline = style_underline, italic = style_italic;
for (auto& b : blocks)
for (auto& b : line->ParseTags())
{
switch (b.GetType())
switch (b->GetType())
{
case AssBlockType::PLAIN:
// find special characters and convert them
{
std::string text = b.GetText();
std::string text = b->GetText();
boost::replace_all(text, "\\t", " ");
@ -321,7 +319,7 @@ namespace
case AssBlockType::OVERRIDE:
// find relevant tags and process them
{
AssDialogueBlockOverride *ob = static_cast<AssDialogueBlockOverride*>(&b);
AssDialogueBlockOverride *ob = static_cast<AssDialogueBlockOverride*>(b.get());
ob->ParseTags();
ProcessOverrides(ob, underline, italic, align, style_underline, style_italic);

View File

@ -501,7 +501,7 @@ bool SRTSubtitleFormat::CanSave(const AssFile *file) const {
}
for (auto const& line : file->Events) {
boost::ptr_vector<AssDialogueBlock> blocks(line.ParseTags());
auto blocks = line.ParseTags();
for (auto ovr : blocks | agi::of_type<AssDialogueBlockOverride>()) {
// Verify that all overrides used are supported
for (auto const& tag : ovr->Tags) {
@ -522,10 +522,8 @@ std::string SRTSubtitleFormat::ConvertTags(const AssDialogue *diag) const {
tag_states['u'] = false;
tag_states['s'] = false;
boost::ptr_vector<AssDialogueBlock> blocks(diag->ParseTags());
for (auto& block : blocks) {
if (AssDialogueBlockOverride* ovr = dynamic_cast<AssDialogueBlockOverride*>(&block)) {
for (auto& block : diag->ParseTags()) {
if (AssDialogueBlockOverride* ovr = dynamic_cast<AssDialogueBlockOverride*>(block.get())) {
// Iterate through overrides
for (auto const& tag : ovr->Tags) {
if (tag.IsValid() && tag.Name.size() == 2) {
@ -542,7 +540,7 @@ std::string SRTSubtitleFormat::ConvertTags(const AssDialogue *diag) const {
}
}
// Plain text
else if (AssDialogueBlockPlain *plain = dynamic_cast<AssDialogueBlockPlain*>(&block))
else if (AssDialogueBlockPlain *plain = dynamic_cast<AssDialogueBlockPlain*>(block.get()))
final += plain->GetText();
}

View File

@ -331,7 +331,7 @@ void VisualTool<FeatureType>::RemoveSelection(FeatureType *feat) {
typedef const std::vector<AssOverrideParameter> * param_vec;
// Find a tag's parameters in a line or return nullptr if it's not found
static param_vec find_tag(boost::ptr_vector<AssDialogueBlock>& blocks, std::string const& tag_name) {
static param_vec find_tag(std::vector<std::unique_ptr<AssDialogueBlock>>& blocks, std::string const& tag_name) {
for (auto ovr : blocks | agi::of_type<AssDialogueBlockOverride>()) {
for (auto const& tag : ovr->Tags) {
if (tag.Name == tag_name)
@ -354,7 +354,7 @@ static Vector2D vec_or_bad(param_vec tag, size_t x_idx, size_t y_idx) {
}
Vector2D VisualToolBase::GetLinePosition(AssDialogue *diag) {
boost::ptr_vector<AssDialogueBlock> blocks(diag->ParseTags());
auto blocks = diag->ParseTags();
if (Vector2D ret = vec_or_bad(find_tag(blocks, "\\pos"), 0, 1)) return ret;
if (Vector2D ret = vec_or_bad(find_tag(blocks, "\\move"), 0, 1)) return ret;
@ -405,12 +405,12 @@ Vector2D VisualToolBase::GetLinePosition(AssDialogue *diag) {
}
Vector2D VisualToolBase::GetLineOrigin(AssDialogue *diag) {
boost::ptr_vector<AssDialogueBlock> blocks(diag->ParseTags());
auto blocks = diag->ParseTags();
return vec_or_bad(find_tag(blocks, "\\org"), 0, 1);
}
bool VisualToolBase::GetLineMove(AssDialogue *diag, Vector2D &p1, Vector2D &p2, int &t1, int &t2) {
boost::ptr_vector<AssDialogueBlock> blocks(diag->ParseTags());
auto blocks = diag->ParseTags();
param_vec tag = find_tag(blocks, "\\move");
if (!tag)
@ -431,7 +431,7 @@ void VisualToolBase::GetLineRotation(AssDialogue *diag, float &rx, float &ry, fl
if (AssStyle *style = c->ass->GetStyle(diag->Style))
rz = style->angle;
boost::ptr_vector<AssDialogueBlock> blocks(diag->ParseTags());
auto blocks = diag->ParseTags();
if (param_vec tag = find_tag(blocks, "\\frx"))
rx = tag->front().Get(rx);
@ -446,7 +446,7 @@ void VisualToolBase::GetLineRotation(AssDialogue *diag, float &rx, float &ry, fl
void VisualToolBase::GetLineShear(AssDialogue *diag, float& fax, float& fay) {
fax = fay = 0.f;
boost::ptr_vector<AssDialogueBlock> blocks(diag->ParseTags());
auto blocks = diag->ParseTags();
if (param_vec tag = find_tag(blocks, "\\fax"))
fax = tag->front().Get(fax);
@ -462,7 +462,7 @@ void VisualToolBase::GetLineScale(AssDialogue *diag, Vector2D &scale) {
y = style->scaley;
}
boost::ptr_vector<AssDialogueBlock> blocks(diag->ParseTags());
auto blocks = diag->ParseTags();
if (param_vec tag = find_tag(blocks, "\\fscx"))
x = tag->front().Get(x);
@ -475,7 +475,7 @@ void VisualToolBase::GetLineScale(AssDialogue *diag, Vector2D &scale) {
void VisualToolBase::GetLineClip(AssDialogue *diag, Vector2D &p1, Vector2D &p2, bool &inverse) {
inverse = false;
boost::ptr_vector<AssDialogueBlock> blocks(diag->ParseTags());
auto blocks = diag->ParseTags();
param_vec tag = find_tag(blocks, "\\iclip");
if (tag)
inverse = true;
@ -493,7 +493,7 @@ void VisualToolBase::GetLineClip(AssDialogue *diag, Vector2D &p1, Vector2D &p2,
}
std::string VisualToolBase::GetLineVectorClip(AssDialogue *diag, int &scale, bool &inverse) {
boost::ptr_vector<AssDialogueBlock> blocks(diag->ParseTags());
auto blocks = diag->ParseTags();
scale = 1;
inverse = false;
@ -536,8 +536,8 @@ void VisualToolBase::SetOverride(AssDialogue* line, std::string const& tag, std:
else if (tag == "\\iclip") removeTag = "\\clip";
// Get block at start
boost::ptr_vector<AssDialogueBlock> blocks(line->ParseTags());
AssDialogueBlock *block = &blocks.front();
auto blocks = line->ParseTags();
AssDialogueBlock *block = blocks.front().get();
if (AssDialogueBlockOverride *ovr = dynamic_cast<AssDialogueBlockOverride*>(block)) {
// Remove old of same