Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize render updates and use of thread mutexes in Sensors system #1938

Merged
merged 7 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions include/gz/sim/EventManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,31 @@ namespace gz
}
}

/// \brief Get connection count for a particular event
/// Connection count for the event
public: template <typename E>
unsigned int
ConnectionCount()
{
if (this->events.find(typeid(E)) == this->events.end())
{
return 0u;
}

E *eventPtr = dynamic_cast<E *>(this->events[typeid(E)].get());
// All values in the map should be derived from Event,
// so this shouldn't be an issue, but it doesn't hurt to check.
if (eventPtr != nullptr)
{
return eventPtr->ConnectionCount();
}
else
{
gzerr << "Failed to get connection count for event: "
<< typeid(E).name() << std::endl;
return 0u;
}
}

/// \brief Convenience type for storing typeinfo references.
private: using TypeInfoRef = std::reference_wrapper<const std::type_info>;
Expand Down
9 changes: 9 additions & 0 deletions src/EventManager_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ TEST(EventManager, EmitConnectTest)
{
EventManager eventManager;

EXPECT_EQ(0u, eventManager.ConnectionCount<events::Pause>());

bool paused1 = false;
auto connection1 = eventManager.Connect<events::Pause>(
[&paused1](bool _paused) {
paused1 = _paused;
});

EXPECT_EQ(1u, eventManager.ConnectionCount<events::Pause>());

// Emitting events causes connection callbacks to be fired.
eventManager.Emit<events::Pause>(true);
EXPECT_EQ(true, paused1);
Expand All @@ -47,6 +51,8 @@ TEST(EventManager, EmitConnectTest)
paused2 = _paused;
});

EXPECT_EQ(2u, eventManager.ConnectionCount<events::Pause>());

// Multiple connections should each be fired.
eventManager.Emit<events::Pause>(true);
EXPECT_EQ(true, paused1);
Expand All @@ -58,9 +64,12 @@ TEST(EventManager, EmitConnectTest)

// Clearing the ConnectionPtr will cause it to no longer fire.
connection1.reset();

eventManager.Emit<events::Pause>(true);
EXPECT_EQ(false, paused1);
EXPECT_EQ(true, paused2);

EXPECT_EQ(1u, eventManager.ConnectionCount<events::Pause>());
}

/// Test that we are able to connect arbitrary events and signal them.
Expand Down
Loading