[Lldb-commits] [lldb] ec77d1f - [lldb] Simplify predicates of find_if in BroadcastManager
Alex Langford via lldb-commits
lldb-commits at lists.llvm.org
Tue May 9 10:00:11 PDT 2023
Author: Alex Langford
Date: 2023-05-09T10:00:02-07:00
New Revision: ec77d1f3d9fcf7105b6bda25fb4d0e5ed5afd0c5
URL: https://github.com/llvm/llvm-project/commit/ec77d1f3d9fcf7105b6bda25fb4d0e5ed5afd0c5
DIFF: https://github.com/llvm/llvm-project/commit/ec77d1f3d9fcf7105b6bda25fb4d0e5ed5afd0c5.diff
LOG: [lldb] Simplify predicates of find_if in BroadcastManager
We had some custom classes that were used as the predicate for
`std::find_if`. It would be a lot simpler if we used lambdas instead.
Differential Revision: https://reviews.llvm.org/D150168
Added:
Modified:
lldb/include/lldb/Utility/Broadcaster.h
lldb/source/Utility/Broadcaster.cpp
Removed:
################################################################################
diff --git a/lldb/include/lldb/Utility/Broadcaster.h b/lldb/include/lldb/Utility/Broadcaster.h
index 85e2468a111f5..081e6ee5c883f 100644
--- a/lldb/include/lldb/Utility/Broadcaster.h
+++ b/lldb/include/lldb/Utility/Broadcaster.h
@@ -112,103 +112,6 @@ class BroadcasterManager
listener_collection m_listeners;
mutable std::recursive_mutex m_manager_mutex;
-
- // A couple of comparator classes for find_if:
-
- class BroadcasterClassMatches {
- public:
- BroadcasterClassMatches(const ConstString &broadcaster_class)
- : m_broadcaster_class(broadcaster_class) {}
-
- ~BroadcasterClassMatches() = default;
-
- bool operator()(const event_listener_key &input) const {
- return (input.first.GetBroadcasterClass() == m_broadcaster_class);
- }
-
- private:
- ConstString m_broadcaster_class;
- };
-
- class BroadcastEventSpecMatches {
- public:
- BroadcastEventSpecMatches(const BroadcastEventSpec &broadcaster_spec)
- : m_broadcaster_spec(broadcaster_spec) {}
-
- ~BroadcastEventSpecMatches() = default;
-
- bool operator()(const event_listener_key &input) const {
- return (input.first.IsContainedIn(m_broadcaster_spec));
- }
-
- private:
- BroadcastEventSpec m_broadcaster_spec;
- };
-
- class ListenerMatchesAndSharedBits {
- public:
- explicit ListenerMatchesAndSharedBits(
- const BroadcastEventSpec &broadcaster_spec,
- const lldb::ListenerSP &listener_sp)
- : m_broadcaster_spec(broadcaster_spec), m_listener_sp(listener_sp) {}
-
- ~ListenerMatchesAndSharedBits() = default;
-
- bool operator()(const event_listener_key &input) const {
- return (input.first.GetBroadcasterClass() ==
- m_broadcaster_spec.GetBroadcasterClass() &&
- (input.first.GetEventBits() &
- m_broadcaster_spec.GetEventBits()) != 0 &&
- input.second == m_listener_sp);
- }
-
- private:
- BroadcastEventSpec m_broadcaster_spec;
- const lldb::ListenerSP m_listener_sp;
- };
-
- class ListenerMatches {
- public:
- explicit ListenerMatches(const lldb::ListenerSP &in_listener_sp)
- : m_listener_sp(in_listener_sp) {}
-
- ~ListenerMatches() = default;
-
- bool operator()(const event_listener_key &input) const {
- if (input.second == m_listener_sp)
- return true;
-
- return false;
- }
-
- private:
- const lldb::ListenerSP m_listener_sp;
- };
-
- class ListenerMatchesPointer {
- public:
- ListenerMatchesPointer(const Listener *in_listener)
- : m_listener(in_listener) {}
-
- ~ListenerMatchesPointer() = default;
-
- bool operator()(const event_listener_key &input) const {
- if (input.second.get() == m_listener)
- return true;
-
- return false;
- }
-
- bool operator()(const lldb::ListenerSP &input) const {
- if (input.get() == m_listener)
- return true;
-
- return false;
- }
-
- private:
- const Listener *m_listener;
- };
};
/// \class Broadcaster Broadcaster.h "lldb/Utility/Broadcaster.h" An event
diff --git a/lldb/source/Utility/Broadcaster.cpp b/lldb/source/Utility/Broadcaster.cpp
index 4e6710e1108b3..66c78978571fa 100644
--- a/lldb/source/Utility/Broadcaster.cpp
+++ b/lldb/source/Utility/Broadcaster.cpp
@@ -336,10 +336,13 @@ uint32_t BroadcasterManager::RegisterListenerForEvents(
collection::iterator iter = m_event_map.begin(), end_iter = m_event_map.end();
uint32_t available_bits = event_spec.GetEventBits();
+ auto class_matches = [&event_spec](const event_listener_key &input) -> bool {
+ return input.first.GetBroadcasterClass() ==
+ event_spec.GetBroadcasterClass();
+ };
+
while (iter != end_iter &&
- (iter = find_if(iter, end_iter,
- BroadcasterClassMatches(
- event_spec.GetBroadcasterClass()))) != end_iter) {
+ (iter = find_if(iter, end_iter, class_matches)) != end_iter) {
available_bits &= ~((*iter).first.GetEventBits());
iter++;
}
@@ -362,7 +365,13 @@ bool BroadcasterManager::UnregisterListenerForEvents(
if (m_listeners.erase(listener_sp) == 0)
return false;
- ListenerMatchesAndSharedBits predicate(event_spec, listener_sp);
+ auto listener_matches_and_shared_bits =
+ [&listener_sp, &event_spec](const event_listener_key &input) -> bool {
+ return input.first.GetBroadcasterClass() ==
+ event_spec.GetBroadcasterClass() &&
+ (input.first.GetEventBits() & event_spec.GetEventBits()) != 0 &&
+ input.second == listener_sp;
+ };
std::vector<BroadcastEventSpec> to_be_readded;
uint32_t event_bits_to_remove = event_spec.GetEventBits();
@@ -370,7 +379,8 @@ bool BroadcasterManager::UnregisterListenerForEvents(
// matches that weren't exact to re-add:
while (true) {
collection::iterator iter, end_iter = m_event_map.end();
- iter = find_if(m_event_map.begin(), end_iter, predicate);
+ iter = find_if(m_event_map.begin(), end_iter,
+ listener_matches_and_shared_bits);
if (iter == end_iter) {
break;
}
@@ -397,9 +407,13 @@ ListenerSP BroadcasterManager::GetListenerForEventSpec(
const BroadcastEventSpec &event_spec) const {
std::lock_guard<std::recursive_mutex> guard(m_manager_mutex);
+ auto event_spec_matches =
+ [&event_spec](const event_listener_key &input) -> bool {
+ return input.first.IsContainedIn(event_spec);
+ };
+
collection::const_iterator iter, end_iter = m_event_map.end();
- iter = find_if(m_event_map.begin(), end_iter,
- BroadcastEventSpecMatches(event_spec));
+ iter = find_if(m_event_map.begin(), end_iter, event_spec_matches);
if (iter != end_iter)
return (*iter).second;
@@ -408,17 +422,25 @@ ListenerSP BroadcasterManager::GetListenerForEventSpec(
void BroadcasterManager::RemoveListener(Listener *listener) {
std::lock_guard<std::recursive_mutex> guard(m_manager_mutex);
- ListenerMatchesPointer predicate(listener);
+ auto listeners_predicate =
+ [&listener](const lldb::ListenerSP &input) -> bool {
+ return input.get() == listener;
+ };
+
listener_collection::iterator iter = m_listeners.begin(),
end_iter = m_listeners.end();
- iter = std::find_if(iter, end_iter, predicate);
+ iter = std::find_if(iter, end_iter, listeners_predicate);
if (iter != end_iter)
m_listeners.erase(iter);
while (true) {
+ auto events_predicate =
+ [&listener](const event_listener_key &input) -> bool {
+ return input.second.get() == listener;
+ };
collection::iterator iter, end_iter = m_event_map.end();
- iter = find_if(m_event_map.begin(), end_iter, predicate);
+ iter = find_if(m_event_map.begin(), end_iter, events_predicate);
if (iter == end_iter)
break;
@@ -428,14 +450,18 @@ void BroadcasterManager::RemoveListener(Listener *listener) {
void BroadcasterManager::RemoveListener(const lldb::ListenerSP &listener_sp) {
std::lock_guard<std::recursive_mutex> guard(m_manager_mutex);
- ListenerMatches predicate(listener_sp);
+
+ auto listener_matches =
+ [&listener_sp](const event_listener_key &input) -> bool {
+ return input.second == listener_sp;
+ };
if (m_listeners.erase(listener_sp) == 0)
return;
while (true) {
collection::iterator iter, end_iter = m_event_map.end();
- iter = find_if(m_event_map.begin(), end_iter, predicate);
+ iter = find_if(m_event_map.begin(), end_iter, listener_matches);
if (iter == end_iter)
break;
@@ -449,10 +475,13 @@ void BroadcasterManager::SignUpListenersForBroadcaster(
collection::iterator iter = m_event_map.begin(), end_iter = m_event_map.end();
+ auto class_matches = [&broadcaster](const event_listener_key &input) -> bool {
+ return input.first.GetBroadcasterClass() ==
+ broadcaster.GetBroadcasterClass();
+ };
+
while (iter != end_iter &&
- (iter = find_if(iter, end_iter,
- BroadcasterClassMatches(
- broadcaster.GetBroadcasterClass()))) != end_iter) {
+ (iter = find_if(iter, end_iter, class_matches)) != end_iter) {
(*iter).second->StartListeningForEvents(&broadcaster,
(*iter).first.GetEventBits());
iter++;
More information about the lldb-commits
mailing list