Use std::erase_if to simplify removal of items
This function makes it simpler to remove items from collections. Update
our code to make it more readable.
Bug: 198472780
Test: presubmit
Change-Id: I3bc81bf729e9b22ced16691272187870783c1728
diff --git a/services/inputflinger/Android.bp b/services/inputflinger/Android.bp
index 8cdb706..469c9e6 100644
--- a/services/inputflinger/Android.bp
+++ b/services/inputflinger/Android.bp
@@ -24,6 +24,7 @@
cc_defaults {
name: "inputflinger_defaults",
+ cpp_std: "c++20",
cflags: [
"-Wall",
"-Wextra",
diff --git a/services/inputflinger/BlockingQueue.h b/services/inputflinger/BlockingQueue.h
index b612ca7..8300e8a 100644
--- a/services/inputflinger/BlockingQueue.h
+++ b/services/inputflinger/BlockingQueue.h
@@ -71,8 +71,7 @@
void erase(const std::function<bool(const T&)>& lambda) {
std::scoped_lock lock(mLock);
- mQueue.erase(std::remove_if(mQueue.begin(), mQueue.end(),
- [&lambda](const T& t) { return lambda(t); }), mQueue.end());
+ std::erase_if(mQueue, [&lambda](const auto& t) { return lambda(t); });
}
/**
diff --git a/services/inputflinger/dispatcher/TouchState.cpp b/services/inputflinger/dispatcher/TouchState.cpp
index d624e99..08c7826 100644
--- a/services/inputflinger/dispatcher/TouchState.cpp
+++ b/services/inputflinger/dispatcher/TouchState.cpp
@@ -106,10 +106,8 @@
}
void TouchState::filterWindowsExcept(const sp<IBinder>& token) {
- auto it = std::remove_if(windows.begin(), windows.end(), [&token](const TouchedWindow& w) {
- return w.windowHandle->getToken() != token;
- });
- windows.erase(it, windows.end());
+ std::erase_if(windows,
+ [&token](const TouchedWindow& w) { return w.windowHandle->getToken() != token; });
}
sp<WindowInfoHandle> TouchState::getFirstForegroundWindowHandle() const {
diff --git a/services/inputflinger/reader/EventHub.cpp b/services/inputflinger/reader/EventHub.cpp
index d10f8b6..269bdab 100644
--- a/services/inputflinger/reader/EventHub.cpp
+++ b/services/inputflinger/reader/EventHub.cpp
@@ -2344,13 +2344,10 @@
return;
}
}
- mUnattachedVideoDevices
- .erase(std::remove_if(mUnattachedVideoDevices.begin(), mUnattachedVideoDevices.end(),
- [&devicePath](
- const std::unique_ptr<TouchVideoDevice>& videoDevice) {
- return videoDevice->getPath() == devicePath;
- }),
- mUnattachedVideoDevices.end());
+ std::erase_if(mUnattachedVideoDevices,
+ [&devicePath](const std::unique_ptr<TouchVideoDevice>& videoDevice) {
+ return videoDevice->getPath() == devicePath;
+ });
}
void EventHub::closeAllDevicesLocked() {
diff --git a/services/inputflinger/reader/InputReader.cpp b/services/inputflinger/reader/InputReader.cpp
index 7704a84..564db56 100644
--- a/services/inputflinger/reader/InputReader.cpp
+++ b/services/inputflinger/reader/InputReader.cpp
@@ -237,9 +237,7 @@
auto mapIt = mDeviceToEventHubIdsMap.find(device);
if (mapIt != mDeviceToEventHubIdsMap.end()) {
std::vector<int32_t>& eventHubIds = mapIt->second;
- eventHubIds.erase(std::remove_if(eventHubIds.begin(), eventHubIds.end(),
- [eventHubId](int32_t eId) { return eId == eventHubId; }),
- eventHubIds.end());
+ std::erase_if(eventHubIds, [eventHubId](int32_t eId) { return eId == eventHubId; });
if (eventHubIds.size() == 0) {
mDeviceToEventHubIdsMap.erase(mapIt);
}