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/include/event_service_manager.hpp b/redfish-core/include/event_service_manager.hpp
index 00d77c8..67f1800 100644
--- a/redfish-core/include/event_service_manager.hpp
+++ b/redfish-core/include/event_service_manager.hpp
@@ -40,6 +40,7 @@
#include <ctime>
#include <fstream>
#include <memory>
+#include <ranges>
#include <span>
namespace redfish
@@ -83,10 +84,9 @@
getMsgFromRegistry(const std::string& messageKey,
const std::span<const MessageEntry>& registry)
{
- std::span<const MessageEntry>::iterator messageIt =
- std::find_if(registry.begin(), registry.end(),
- [&messageKey](const MessageEntry& messageEntry) {
- return messageKey == messageEntry.first;
+ std::span<const MessageEntry>::iterator messageIt = std::ranges::find_if(
+ registry, [&messageKey](const MessageEntry& messageEntry) {
+ return messageKey == messageEntry.first;
});
if (messageIt != registry.end())
{
@@ -278,9 +278,8 @@
std::vector<std::string>& registryPrefixes,
std::vector<std::string>& metricReportDefinitions)
{
- sseFilter.erase(std::remove_if(sseFilter.begin(), sseFilter.end(),
- isFilterQuerySpecialChar),
- sseFilter.end());
+ auto remove = std::ranges::remove_if(sseFilter, isFilterQuerySpecialChar);
+ sseFilter.erase(std::ranges::begin(remove), sseFilter.end());
std::vector<std::string> result;
@@ -447,8 +446,7 @@
// send everything.
if (!registryPrefixes.empty())
{
- auto obj = std::find(registryPrefixes.begin(),
- registryPrefixes.end(), registryName);
+ auto obj = std::ranges::find(registryPrefixes, registryName);
if (obj == registryPrefixes.end())
{
continue;
@@ -459,8 +457,7 @@
// send everything.
if (!registryMsgIds.empty())
{
- auto obj = std::find(registryMsgIds.begin(),
- registryMsgIds.end(), messageKey);
+ auto obj = std::ranges::find(registryMsgIds, messageKey);
if (obj == registryMsgIds.end())
{
continue;
@@ -509,9 +506,8 @@
// Empty list means no filter. Send everything.
if (!metricReportDefinitions.empty())
{
- if (std::find(metricReportDefinitions.begin(),
- metricReportDefinitions.end(),
- mrdUri.buffer()) == metricReportDefinitions.end())
+ if (std::ranges::find(metricReportDefinitions, mrdUri.buffer()) ==
+ metricReportDefinitions.end())
{
return;
}
@@ -997,8 +993,8 @@
size_t getNumberOfSSESubscriptions() const
{
- auto size = std::count_if(
- subscriptionsMap.begin(), subscriptionsMap.end(),
+ auto size = std::ranges::count_if(
+ subscriptionsMap,
[](const std::pair<std::string, std::shared_ptr<Subscription>>&
entry) {
return (entry.second->subscriptionType == subscriptionTypeSSE);
@@ -1369,9 +1365,8 @@
std::vector<std::string> invalidProps;
msg.read(interface, props, invalidProps);
- auto found =
- std::find_if(props.begin(), props.end(),
- [](const auto& x) { return x.first == "Readings"; });
+ auto found = std::ranges::find_if(
+ props, [](const auto& x) { return x.first == "Readings"; });
if (found == props.end())
{
BMCWEB_LOG_INFO("Failed to get Readings from Report properties");
diff --git a/redfish-core/include/redfish_aggregator.hpp b/redfish-core/include/redfish_aggregator.hpp
index 0cdf081..2514fae 100644
--- a/redfish-core/include/redfish_aggregator.hpp
+++ b/redfish-core/include/redfish_aggregator.hpp
@@ -9,6 +9,8 @@
#include <boost/algorithm/string/predicate.hpp>
#include <array>
+#include <ranges>
+#include <string_view>
namespace redfish
{
@@ -100,9 +102,8 @@
return (searchType == SearchType::ContainsSubordinate) ||
(searchType == SearchType::CollOrCon);
}
-
- const auto* it = std::lower_bound(
- topCollections.begin(), topCollections.end(), parsedUrl->buffer());
+ std::string_view url = parsedUrl->buffer();
+ const auto* it = std::ranges::lower_bound(topCollections, url);
if (it == topCollections.end())
{
// parsedUrl is alphabetically after the last entry in the array so it
diff --git a/redfish-core/include/utils/collection.hpp b/redfish-core/include/utils/collection.hpp
index 862be90..0801cd5 100644
--- a/redfish-core/include/utils/collection.hpp
+++ b/redfish-core/include/utils/collection.hpp
@@ -9,6 +9,7 @@
#include <boost/url/url.hpp>
#include <nlohmann/json.hpp>
+#include <ranges>
#include <span>
#include <string>
#include <string_view>
@@ -68,8 +69,7 @@
}
pathNames.push_back(leaf);
}
- std::sort(pathNames.begin(), pathNames.end(),
- AlphanumLess<std::string>());
+ std::ranges::sort(pathNames, AlphanumLess<std::string>());
nlohmann::json& members = asyncResp->res.jsonValue["Members"];
members = nlohmann::json::array();
diff --git a/redfish-core/include/utils/json_utils.hpp b/redfish-core/include/utils/json_utils.hpp
index 5b4ad9a..f4e5385 100644
--- a/redfish-core/include/utils/json_utils.hpp
+++ b/redfish-core/include/utils/json_utils.hpp
@@ -32,6 +32,7 @@
#include <limits>
#include <map>
#include <optional>
+#include <ranges>
#include <span>
#include <string>
#include <string_view>
@@ -670,7 +671,7 @@
// those whose |element[key]| is string.
inline void sortJsonArrayByOData(nlohmann::json::array_t& array)
{
- std::sort(array.begin(), array.end(), ODataObjectLess());
+ std::ranges::sort(array, ODataObjectLess());
}
// Returns the estimated size of the JSON value
diff --git a/redfish-core/include/utils/query_param.hpp b/redfish-core/include/utils/query_param.hpp
index 696e323..5fa4852 100644
--- a/redfish-core/include/utils/query_param.hpp
+++ b/redfish-core/include/utils/query_param.hpp
@@ -30,6 +30,7 @@
#include <map>
#include <memory>
#include <optional>
+#include <ranges>
#include <string>
#include <string_view>
#include <system_error>
@@ -960,9 +961,8 @@
constexpr std::array<std::string_view, 5> reservedProperties = {
"@odata.id", "@odata.type", "@odata.context", "@odata.etag",
"error"};
- bool reserved = std::find(reservedProperties.begin(),
- reservedProperties.end(),
- it.key()) != reservedProperties.end();
+ bool reserved = std::ranges::find(reservedProperties, it.key()) !=
+ reservedProperties.end();
if (reserved || (nextNode != nullptr && nextNode->isSelected()))
{
it = nextIt;
diff --git a/redfish-core/include/utils/sw_utils.hpp b/redfish-core/include/utils/sw_utils.hpp
index cffc637..4e1e066 100644
--- a/redfish-core/include/utils/sw_utils.hpp
+++ b/redfish-core/include/utils/sw_utils.hpp
@@ -13,6 +13,7 @@
#include <algorithm>
#include <array>
+#include <ranges>
#include <string>
#include <string_view>
#include <vector>
@@ -125,8 +126,8 @@
// Look at Ids from
// /xyz/openbmc_project/software/functional
// to determine if this is a running image
- if (std::find(functionalSwIds.begin(), functionalSwIds.end(),
- swId) != functionalSwIds.end())
+ if (std::ranges::find(functionalSwIds, swId) !=
+ functionalSwIds.end())
{
runningImage = true;
}
@@ -367,8 +368,7 @@
}
std::string reqSwObjPath = "/xyz/openbmc_project/software/" + *swId;
- if (std::find(objPaths.begin(), objPaths.end(), reqSwObjPath) !=
- objPaths.end())
+ if (std::ranges::find(objPaths, reqSwObjPath) != objPaths.end())
{
asyncResp->res.jsonValue["Updateable"] = true;
return;