From 326cf6feb8598aa03a35fd6f678e4f536f168149 Mon Sep 17 00:00:00 2001 From: Uwe Hermann Date: Fri, 10 Mar 2017 21:32:43 +0100 Subject: [PATCH 1/1] Use emplace_back() where possible. This patch was generated using clang-tidy: clang-tidy -checks="-*,modernize-use-emplace" -fix Using emplace_back() has multiple advantages: - It's usually shorter and easier to read. - It's more efficient. V1: v.push_back("foo"); V2: v.emplace_back("foo"); V1 will construct a temporary std::string from the string literal "foo", another copy of that temporary object will be constructed and placed into the vector 'v', then the temporary object's destructor will be called. V2 will simply create a std::string directly in the vector 'v', i.e. there's only one construction (not 2) and no destructor needs to be called. --- pv/data/decoderstack.cpp | 4 ++-- pv/data/logicsegment.cpp | 8 ++++---- pv/session.cpp | 20 ++++++++++---------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/pv/data/decoderstack.cpp b/pv/data/decoderstack.cpp index 11b2b70b..1f158c23 100644 --- a/pv/data/decoderstack.cpp +++ b/pv/data/decoderstack.cpp @@ -145,14 +145,14 @@ std::vector DecoderStack::get_visible_rows() const // Add a row for the decoder if it doesn't have a row list if (!decc->annotation_rows) - rows.push_back(Row(decc)); + rows.emplace_back(decc); // Add the decoder rows for (const GSList *l = decc->annotation_rows; l; l = l->next) { const srd_decoder_annotation_row *const ann_row = (srd_decoder_annotation_row *)l->data; assert(ann_row); - rows.push_back(Row(decc, ann_row)); + rows.emplace_back(decc, ann_row); } } diff --git a/pv/data/logicsegment.cpp b/pv/data/logicsegment.cpp index bdfc9816..5191a3f0 100644 --- a/pv/data/logicsegment.cpp +++ b/pv/data/logicsegment.cpp @@ -322,7 +322,7 @@ void LogicSegment::get_subsampled_edges( // Store the initial state last_sample = (get_unpacked_sample(start) & sig_mask) != 0; - edges.push_back(pair(index++, last_sample)); + edges.emplace_back(index++, last_sample); while (index + block_length <= end) { //----- Continue to search -----// @@ -458,7 +458,7 @@ void LogicSegment::get_subsampled_edges( // Store the final state const bool final_sample = (get_unpacked_sample(final_index - 1) & sig_mask) != 0; - edges.push_back(pair(index, final_sample)); + edges.emplace_back(index, final_sample); index = final_index; last_sample = final_sample; @@ -467,8 +467,8 @@ void LogicSegment::get_subsampled_edges( // Add the final state const bool end_sample = get_unpacked_sample(end) & sig_mask; if (last_sample != end_sample) - edges.push_back(pair(end, end_sample)); - edges.push_back(pair(end + 1, end_sample)); + edges.emplace_back(end, end_sample); + edges.emplace_back(end + 1, end_sample); } uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const diff --git a/pv/session.cpp b/pv/session.cpp index ef2f39bb..e5bf1f6a 100644 --- a/pv/session.cpp +++ b/pv/session.cpp @@ -193,11 +193,11 @@ void Session::save_settings(QSettings &settings) const settings.setValue("device_type", "hardware"); settings.beginGroup("device"); - key_list.push_back("vendor"); - key_list.push_back("model"); - key_list.push_back("version"); - key_list.push_back("serial_num"); - key_list.push_back("connection_id"); + key_list.emplace_back("vendor"); + key_list.emplace_back("model"); + key_list.emplace_back("version"); + key_list.emplace_back("serial_num"); + key_list.emplace_back("connection_id"); dev_info = device_manager_.get_device_info(device_); @@ -277,11 +277,11 @@ void Session::restore_settings(QSettings &settings) // Re-select last used device if possible but only if it's not demo settings.beginGroup("device"); - key_list.push_back("vendor"); - key_list.push_back("model"); - key_list.push_back("version"); - key_list.push_back("serial_num"); - key_list.push_back("connection_id"); + key_list.emplace_back("vendor"); + key_list.emplace_back("model"); + key_list.emplace_back("version"); + key_list.emplace_back("serial_num"); + key_list.emplace_back("connection_id"); for (string key : key_list) { const QString k = QString::fromStdString(key); -- 2.30.2