31 using Subscriber = std::function<void(const T&)>;
37 unique_lock<mutex> guard(lock_);
40 for (
int i = 0; i < subscribers_.size(); ++i)
41 if (!subscribers_[i]) {
42 subscribers_[i] = subscriber;
46 subscribers_.push_back(subscriber);
47 return int(subscribers_.size() - 1);
53 unique_lock<mutex> guard(lock_);
54 CHECK(subscriber_index >= 0 && subscriber_index < subscribers_.size());
55 CHECK(subscribers_[subscriber_index]);
56 subscribers_[subscriber_index] =
nullptr;
65 vector<Subscriber> subscribers_snapshot;
68 unique_lock<mutex> guard(lock_);
69 subscribers_snapshot = subscribers_;
72 for (
const auto& subscriber : subscribers_snapshot) {
79 vector<Subscriber> subscribers_;
Generic utilities.
Definition: exception.h:24
void publish(const T &value) const
Publish a new value.
Definition: pubsub.h:64
A type-safe, non-buffered & synchronous publisher-subscriber channel.
Definition: pubsub.h:30
int subscribe(const Subscriber &subscriber)
Add a subscriber callback.
Definition: pubsub.h:36
void unsubscribe(int subscriber_index)
Removes a subscriber.
Definition: pubsub.h:52