Use ranges
C++20 brought us std::ranges for a lot of algorithms. Most of these
conversions were done using comby, similar to:
```
comby -verbose 'std::lower_bound(:[a].begin(),:[b].end(),:[c])' 'std::ranges::lower_bound(:[a], :[c])' $(git ls-files | grep "\.[hc]\(pp\)\?$") -in-place
```
Change-Id: I0c99c04e9368312555c08147d474ca93a5959e8d
Signed-off-by: Ed Tanous <edtanous@google.com>
diff --git a/redfish-core/lib/log_services.hpp b/redfish-core/lib/log_services.hpp
index e17169a..3cf6599 100644
--- a/redfish-core/lib/log_services.hpp
+++ b/redfish-core/lib/log_services.hpp
@@ -50,6 +50,7 @@
#include <charconv>
#include <filesystem>
#include <optional>
+#include <ranges>
#include <span>
#include <string_view>
#include <variant>
@@ -302,7 +303,7 @@
// As the log files rotate, they are appended with a ".#" that is higher for
// the older logs. Since we don't expect more than 10 log files, we
// can just sort the list to get them in order from newest to oldest
- std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
+ std::ranges::sort(redfishLogFiles);
return !redfishLogFiles.empty();
}
@@ -492,14 +493,13 @@
asyncResp->res.jsonValue["Description"] = "Collection of " + dumpType +
" Dump Entries";
- nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
- entriesArray = nlohmann::json::array();
+ nlohmann::json::array_t entriesArray;
std::string dumpEntryPath =
"/xyz/openbmc_project/dump/" +
std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
dbus::utility::ManagedObjectType resp(objects);
- std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
+ std::ranges::sort(resp, [](const auto& l, const auto& r) {
return AlphanumLess<std::string>()(l.first.filename(),
r.first.filename());
});
@@ -568,6 +568,7 @@
entriesArray.emplace_back(std::move(thisEntry));
}
asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
+ asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
});
}
@@ -1588,8 +1589,7 @@
messages::internalError(asyncResp->res);
return;
}
- nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
- entriesArray = nlohmann::json::array();
+ nlohmann::json::array_t entriesArray;
for (const auto& objectPath : resp)
{
const uint32_t* id = nullptr;
@@ -1690,8 +1690,7 @@
{
continue;
}
- entriesArray.push_back({});
- nlohmann::json& thisEntry = entriesArray.back();
+ nlohmann::json& thisEntry = entriesArray.emplace_back();
thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
thisEntry["@odata.id"] = boost::urls::format(
"/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
@@ -1724,13 +1723,13 @@
std::to_string(*id) + "/attachment";
}
}
- std::sort(
- entriesArray.begin(), entriesArray.end(),
- [](const nlohmann::json& left, const nlohmann::json& right) {
+ std::ranges::sort(entriesArray, [](const nlohmann::json& left,
+ const nlohmann::json& right) {
return (left["Id"] <= right["Id"]);
- });
+ });
asyncResp->res.jsonValue["Members@odata.count"] =
entriesArray.size();
+ asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
});
});
}