Skip to content

Commit

Permalink
Use const references
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Nielsen authored and eXpl0it3r committed Feb 15, 2024
1 parent 14f2db3 commit 341a034
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
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

0 comments on commit 341a034

Please sign in to comment.