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

Use const references for message-passing #5

Merged
merged 1 commit into from
Feb 15, 2024
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
2 changes: 1 addition & 1 deletion examples/Simple/Simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ int main()
const auto message = DummyMessage{ .important_value = 100 };

bus.subscribe<DummyMessage>(
[](const DummyMessage message)
[](const DummyMessage& message)
{
std::cout << "Important value: " << message.important_value << "\n";
}
Expand Down
8 changes: 4 additions & 4 deletions include/PubBus/MessageBus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ namespace pub
{
public:
template<typename M>
SubscriberHandle subscribe(std::function<void(M)> subscriber);
SubscriberHandle subscribe(std::function<void(const M&)> subscriber);
void unsubscribe(SubscriberHandle& subscriber_handle);
template<typename M>
void publish(M message);
void publish(const M& message);
template<typename M>
[[nodiscard]] bool validate(SubscriberHandle handle) const;

Expand All @@ -26,7 +26,7 @@ namespace pub
};

template<typename M>
SubscriberHandle MessageBus::subscribe(std::function<void(M)> subscriber)
SubscriberHandle MessageBus::subscribe(std::function<void(const M&)> subscriber)
{
auto repo = m_repository.find(Message::id<M>());
auto valid = true;
Expand Down Expand Up @@ -58,7 +58,7 @@ namespace pub
}

template<typename M>
void MessageBus::publish(M message)
void MessageBus::publish(const M& message)
{
auto repo = m_repository.find(Message::id<M>());

Expand Down
10 changes: 5 additions & 5 deletions include/PubBus/MessageContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ namespace pub
void remove(std::size_t index) override final;
[[nodiscard]] bool validate(std::size_t index) const override final;

std::size_t add(std::function<void(M)> subscriber);
void publish(M message);
std::size_t add(std::function<void(const M&)> subscriber);
void publish(const M& message);

private:
std::size_t m_index = 0;
std::map<std::size_t, std::function<void(M)>> m_subscribers;
std::map<std::size_t, std::function<void(const M&)>> m_subscribers;
};

template<typename M>
Expand All @@ -53,15 +53,15 @@ namespace pub
}

template<typename M>
std::size_t MessageContainer<M>::add(std::function<void(M)> subscriber)
std::size_t MessageContainer<M>::add(std::function<void(const M&)> subscriber)
{
m_subscribers.insert({ m_index, subscriber });
++m_index;
return m_index - 1u;
}

template<typename M>
void MessageContainer<M>::publish(M message)
void MessageContainer<M>::publish(const M& message)
{
for (auto& subscriber : m_subscribers)
{
Expand Down