From 176493c539112ceb1caafddc988db72dc5f2ab69 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 26 Aug 2019 03:25:35 -0400 Subject: [PATCH] General: Use emplace_back where applicable Same thing, less reading. --- Editor/LayersEditor.cpp | 25 ++++++++++++++++--------- Editor/SongGroupEditor.cpp | 16 ++++++++++------ Editor/SoundGroupEditor.cpp | 2 +- lib/AudioGroupSampleDirectory.cpp | 2 +- lib/Common.cpp | 5 +++-- 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/Editor/LayersEditor.cpp b/Editor/LayersEditor.cpp index 4931acf..52f0923 100644 --- a/Editor/LayersEditor.cpp +++ b/Editor/LayersEditor.cpp @@ -505,13 +505,17 @@ amuse::LayerMapping LayersModel::_removeRow(int row) { LayersModel::LayersModel(QObject* parent) : QAbstractTableModel(parent) {} void LayersTableView::deleteSelection() { - QModelIndexList list = selectionModel()->selectedRows(); - if (list.isEmpty()) + const QModelIndexList list = selectionModel()->selectedRows(); + if (list.isEmpty()) { return; + } + std::vector> data; data.reserve(list.size()); - for (QModelIndex idx : list) - data.push_back(std::make_pair(amuse::LayerMapping{}, idx.row())); + for (const QModelIndex idx : list) { + data.emplace_back(amuse::LayerMapping{}, idx.row()); + } + std::sort(data.begin(), data.end(), [](const auto& a, const auto& b) { return a.second < b.second; }); g_MainWindow->pushUndoCommand(new LayerRowDelUndoCommand(static_cast(model())->m_node.get(), data.size() > 1 ? tr("Delete Layers") : tr("Delete Layer"), @@ -582,12 +586,15 @@ void LayersEditor::rowsMoved(const QModelIndex& parent, int start, int end, cons } void LayersEditor::doAdd() { - QModelIndex idx = m_tableView.selectionModel()->currentIndex(); + const QModelIndex idx = m_tableView.selectionModel()->currentIndex(); std::vector> data; - if (!idx.isValid()) - data.push_back(std::make_pair(amuse::LayerMapping{}, m_model.rowCount() - 1)); - else - data.push_back(std::make_pair(amuse::LayerMapping{}, idx.row())); + + if (idx.isValid()) { + data.emplace_back(amuse::LayerMapping{}, idx.row()); + } else { + data.emplace_back(amuse::LayerMapping{}, m_model.rowCount() - 1); + } + g_MainWindow->pushUndoCommand( new LayerRowAddUndoCommand(m_model.m_node.get(), tr("Add Layer"), &m_tableView, std::move(data))); } diff --git a/Editor/SongGroupEditor.cpp b/Editor/SongGroupEditor.cpp index 5bc971e..54f99ae 100644 --- a/Editor/SongGroupEditor.cpp +++ b/Editor/SongGroupEditor.cpp @@ -1152,13 +1152,17 @@ Qt::ItemFlags SetupModel::flags(const QModelIndex& index) const { return Qt::Ite SetupModel::SetupModel(QObject* parent) : QAbstractTableModel(parent) {} void PageTableView::deleteSelection() { - QModelIndexList list = selectionModel()->selectedRows(); - if (list.isEmpty()) + const QModelIndexList list = selectionModel()->selectedRows(); + if (list.isEmpty()) { return; + } + std::vector> data; data.reserve(list.size()); - for (QModelIndex idx : list) - data.push_back(std::make_pair(model()->data(idx).toInt() - 1, amuse::SongGroupIndex::PageEntry{})); + for (const QModelIndex idx : list) { + data.emplace_back(model()->data(idx).toInt() - 1, amuse::SongGroupIndex::PageEntry{}); + } + std::sort(data.begin(), data.end(), [](const auto& a, const auto& b) { return a.first < b.first; }); g_MainWindow->pushUndoCommand(new PageRowDelUndoCommand( static_cast(model())->m_node.get(), @@ -1413,7 +1417,7 @@ void SongGroupEditor::doAdd() { } std::vector> data; - data.push_back(std::make_pair(prog, amuse::SongGroupIndex::PageEntry{})); + data.emplace_back(prog, amuse::SongGroupIndex::PageEntry{}); g_MainWindow->pushUndoCommand( new PageRowAddUndoCommand(model->m_node.get(), tr("Add Page Entry"), table, std::move(data))); @@ -1423,7 +1427,7 @@ void SongGroupEditor::doAdd() { g_MainWindow->projectModel()->setIdDatabases(model->m_node.get()); std::vector>> data; auto songId = g_MainWindow->projectModel()->bootstrapSongId(); - data.push_back(std::make_tuple(songId.first, songId.second, std::array{})); + data.emplace_back(songId.first, songId.second, std::array{}); g_MainWindow->pushUndoCommand( new SetupRowAddUndoCommand(model->m_node.get(), tr("Add Setup Entry"), table, std::move(data))); } diff --git a/Editor/SoundGroupEditor.cpp b/Editor/SoundGroupEditor.cpp index 72de4cb..95238b9 100644 --- a/Editor/SoundGroupEditor.cpp +++ b/Editor/SoundGroupEditor.cpp @@ -581,7 +581,7 @@ void SoundGroupEditor::doAdd() { std::vector> data; amuse::SFXId sfxId = amuse::SFXId::CurNameDB->generateId(amuse::NameDB::Type::SFX); std::string sfxName = amuse::SFXId::CurNameDB->generateName(sfxId, amuse::NameDB::Type::SFX); - data.push_back(std::make_tuple(sfxId, sfxName, amuse::SFXGroupIndex::SFXEntry{})); + data.emplace_back(sfxId, sfxName, amuse::SFXGroupIndex::SFXEntry{}); g_MainWindow->pushUndoCommand( new SFXRowAddUndoCommand(m_sfxs.m_node.get(), tr("Add SFX Entry"), m_sfxTable, std::move(data))); } diff --git a/lib/AudioGroupSampleDirectory.cpp b/lib/AudioGroupSampleDirectory.cpp index 1d3685e..099833d 100644 --- a/lib/AudioGroupSampleDirectory.cpp +++ b/lib/AudioGroupSampleDirectory.cpp @@ -933,7 +933,7 @@ AudioGroupSampleDirectory::toGCNData(const AudioGroupDatabase& group) const { entryDNA.m_sampleOff = sampleOffset; sampleOffset += ROUND_UP_32(dataLen); entryDNA.binarySize(adpcmOffset); - entries.push_back(std::make_pair(entryDNA, adpcmParms)); + entries.emplace_back(entryDNA, adpcmParms); } } adpcmOffset += 4; diff --git a/lib/Common.cpp b/lib/Common.cpp index 821a888..2822589 100644 --- a/lib/Common.cpp +++ b/lib/Common.cpp @@ -330,8 +330,9 @@ std::string NameDB::generateName(ObjectId id, Type tp) { std::string NameDB::generateDefaultName(Type tp) const { return generateName(generateId(tp), tp); } std::string_view NameDB::registerPair(std::string_view str, ObjectId id) { - m_stringToId[std::string(str)] = id; - return m_idToString.insert(std::make_pair(id, str)).first->second; + auto string = std::string(str); + m_stringToId.insert_or_assign(string, id); + return m_idToString.emplace(id, std::move(string)).first->second; } std::string_view NameDB::resolveNameFromId(ObjectId id) const {