Modify observer_set to only accept and pass const ref arguments

This commit is contained in:
Piotr Wójcik 2024-10-27 11:20:58 +01:00
commit 968092351d

View file

@ -12,21 +12,25 @@ template <typename... T>
class observer_set final
{
public:
void add (std::function<void (T...)> const & observer_a)
using observer_type = std::function<void (T const &...)>;
public:
void add (observer_type observer)
{
nano::lock_guard<nano::mutex> lock{ mutex };
observers.push_back (observer_a);
observers.push_back (observer);
}
void notify (T... args) const
void notify (T const &... args) const
{
// Make observers copy to allow parallel notifications
nano::unique_lock<nano::mutex> lock{ mutex };
auto observers_copy = observers;
lock.unlock ();
for (auto & i : observers_copy)
for (auto const & observer : observers_copy)
{
i (args...);
observer (args...);
}
}
@ -53,7 +57,7 @@ public:
private:
mutable nano::mutex mutex{ mutex_identifier (mutexes::observer_set) };
std::vector<std::function<void (T...)>> observers;
std::vector<observer_type> observers;
};
}