Modify observer_set to only accept and pass const ref arguments
This commit is contained in:
parent
09b635f217
commit
968092351d
1 changed files with 10 additions and 6 deletions
|
|
@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue