From 2e63352fe8893a65e45690402d8681644b572e12 Mon Sep 17 00:00:00 2001 From: Patrick Venture Date: Sat, 13 Oct 2018 13:51:06 -0700 Subject: cleanup: transition to find_if [utils.cpp:81]: (style) Consider using std::find_if algorithm instead of a raw loop. [storageaddsel.cpp:170]: (style) Consider using std::find_if algorithm instead of a raw loop. Change-Id: I7ebbb6428fbbae9196912837d2ac9820420b77df Signed-off-by: Patrick Venture --- storageaddsel.cpp | 16 +++++++--------- utils.cpp | 22 +++++++++------------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/storageaddsel.cpp b/storageaddsel.cpp index 41f813c..4ed22fc 100644 --- a/storageaddsel.cpp +++ b/storageaddsel.cpp @@ -151,11 +151,9 @@ Entry::Level create_esel_severity(const uint8_t* buffer) int create_esel_association(const uint8_t* buffer, std::string& inventoryPath) { - uint8_t sensor; - auto p = reinterpret_cast(buffer); - sensor = p->sensornumber; + uint8_t sensor = p->sensornumber; inventoryPath = {}; @@ -163,13 +161,13 @@ int create_esel_association(const uint8_t* buffer, std::string& inventoryPath) * Search the sensor number to inventory path mapping to figure out the * inventory associated with the ESEL. */ - for (auto const& iter : invSensors) + auto found = std::find_if(invSensors.begin(), invSensors.end(), + [&sensor](const auto& iter) { + return (iter.second.sensorID == sensor); + }); + if (found != invSensors.end()) { - if (iter.second.sensorID == sensor) - { - inventoryPath = iter.first; - break; - } + inventoryPath = found->first; } return 0; diff --git a/utils.cpp b/utils.cpp index 225b1cc..06c26e8 100644 --- a/utils.cpp +++ b/utils.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -74,25 +75,20 @@ DbusObjectInfo getDbusObject(sdbusplus::bus::bus& bus, } // else search the match string in the object path - auto objectFound = false; - for (auto& object : objectTree) - { - if (object.first.find(match) != std::string::npos) - { - objectFound = true; - objectInfo = make_pair(object.first, - std::move(object.second.begin()->first)); - break; - } - } + auto found = std::find_if( + objectTree.begin(), objectTree.end(), [&match](const auto& object) { + return (object.first.find(match) != std::string::npos); + }); - if (!objectFound) + if (found == objectTree.end()) { log("Failed to find object which matches", entry("MATCH=%s", match.c_str())); elog(); + // elog<> throws an exception. } - return objectInfo; + + return make_pair(found->first, std::move(found->second.begin()->first)); } DbusObjectInfo getIPObject(sdbusplus::bus::bus& bus, -- cgit v1.2.1