22 cras::HasLogger(logHelper), connectFn(
std::move(connectFn)), disconnectFn(
std::move(disconnectFn))
28 ConditionalSubscriber(
std::move(connectFn), [](
ros::Subscriber&
sub) {
sub.shutdown(); }, logHelper)
34 std::lock_guard<std::mutex> lock(this->connectMutex);
36 this->disconnectNoLock();
46 std::lock_guard<std::mutex> lock(this->connectMutex);
48 if (lazy == this->lazy)
54 CRAS_DEBUG(
"Switching to lazy subscription mode");
56 CRAS_DEBUG(
"Switching to non-lazy subscription mode");
58 this->updateSubscriptionNoLock();
63 std::lock_guard<std::mutex> lock(this->connectMutex);
64 return this->subscribed;
69 std::lock_guard<std::mutex> lock(this->connectMutex);
70 this->updateSubscriptionNoLock();
75 const auto shouldBeSubscribed = !this->lazy || this->shouldBeSubscribed();
76 if (this->subscribed && !shouldBeSubscribed)
78 this->disconnectNoLock();
80 else if (!this->subscribed && shouldBeSubscribed)
82 this->connectNoLock();
88 this->connectFn(this->
sub);
89 this->subscribed =
true;
95 CRAS_DEBUG(
"Disconnecting from topic " + this->
sub.getTopic());
96 this->disconnectFn(this->
sub);
97 this->subscribed =
false;
ConditionalSubscriber(ConnectFn connectFn, DisconnectFn disconnectFn, const ::cras::LogHelperPtr &logHelper=::std::make_shared<::cras::NodeLogHelper >())
Create the conditional subscriber.
void connectNoLock()
Connect the subscriber to its input.
void updateSubscriptionNoLock()
The callback called when the condition might have changed.
bool isSubscribed() const
Whether the subscriber is currently subscribed to its topic or not.
std::unique_ptr< SubscriberType > sub
The lazy subscriber.
void disconnectNoLock()
Disconnect the subscriber from its input.
virtual ~ConditionalSubscriber()
Destroy this object and unsubscribe the subscriber if it was subscribed.
void setLazy(bool lazy)
Set whether the subscriber behaves in the lazy way.
Lazy subscriber that subscribes only when a paired publisher has subscribers.
bool isLazy() const
Tell whether this subscriber has the lazy behavior enabled.
::cras::LogHelper::Ptr LogHelperPtr
void updateSubscription()
The callback called when the condition might have changed.