1 #ifndef SUBSCRIPTION_HPP 2 #define SUBSCRIPTION_HPP 58 timer_->setPeriod(period_seconds);
60 else if(period_seconds > 0.0) {
74 timer_->setPeriod(1.0 / rate_hz);
76 else if(rate_hz > 0.0) {
85 std::unique_ptr<PeriodicTimer>
timer_;
91 typedef std::function<void(const msp::Message&)>
CallbackM;
105 Subscription(
const CallbackT& recv_callback,
const CallbackM& send_callback,
106 std::unique_ptr<T>&& io_object,
const double& period = 0.0) :
107 recv_callback_(recv_callback),
108 send_callback_(send_callback),
109 io_object_(std::move(io_object)) {
122 io_object_->decode(data);
123 recv_callback_(*io_object_);
131 io_object_ = std::move(obj);
153 recv_callback_ = recv_callback;
160 if(recv_callback_) recv_callback_(*io_object_);
168 send_callback_ = send_callback;
175 if(send_callback_) send_callback_(*io_object_);
std::unique_ptr< T > io_object_
void setReceiveCallback(const CallbackT &recv_callback) const
Sets the callback to be executed on success.
Subscription()
Subscription constructor.
std::function< void(const msp::Message &)> CallbackM
bool start() const
Start the timer for automatic execution.
virtual ~SubscriptionBase()
const T & getIoObject() const
Gets a reference to the IO object.
std::unique_ptr< PeriodicTimer > timer_
void setTimerFrequency(const double &rate_hz)
setTimerFrequency change the update rate of timer
bool hasTimer() const
Checks to see if the timer has been created.
virtual void makeRequest() const override
Calls the send callback if it exists.
void setIoObject(std::unique_ptr< T > &&obj) const
Sets the object used for packing and unpacking data.
virtual void makeRequest() const =0
bool stop() const
Stop the timer's automatic execution.
bool isAutomatic() const
Checks to see if the subscription fires automatically.
virtual void decode(msp::ByteVector &data) const =0
virtual void handleResponse() const =0
Subscription(const CallbackT &recv_callback, const CallbackM &send_callback, std::unique_ptr< T > &&io_object, const double &period=0.0)
Subscription constructor setting all parameters.
void setTimerPeriod(const double &period_seconds)
setTimerPeriod change the period of the timer
virtual const msp::Message & getMsgObject() const =0
std::function< void(const T &)> CallbackT
void setSendCallback(const CallbackM &send_callback) const
Sets the callback used to send the request.
virtual const msp::Message & getMsgObject() const override
Gets a reference to the internal IO object as a Message.
virtual void decode(msp::ByteVector &data) const override
Virtual method for decoding received data.
virtual void handleResponse() const override
Calls the receive callback if it exists.