Skip to content

Commit

Permalink
[lldb] Simplify predicates of find_if in BroadcastManager
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bulbazord committed May 9, 2023
1 parent aa32655 commit ec77d1f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 112 deletions.
97 changes: 0 additions & 97 deletions lldb/include/lldb/Utility/Broadcaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 44 additions & 15 deletions lldb/source/Utility/Broadcaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand All @@ -362,15 +365,22 @@ 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();

// Go through the map and delete the exact matches, and build a list of
// 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;
}
Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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++;
Expand Down

0 comments on commit ec77d1f

Please sign in to comment.