diff --git a/protocol/osc/sender.cpp b/protocol/osc/sender.cpp index 641e82d..54352af 100644 --- a/protocol/osc/sender.cpp +++ b/protocol/osc/sender.cpp @@ -39,4 +39,34 @@ Sender::~Sender() } + +/** + * @brief Sender::send + * @param msg + */ +void Sender::send(std::shared_ptr msg) const +{ + auto buffer = std::vector(msg->streamSize()); + auto stream = std::make_shared(buffer.data(), buffer.size()); + msg->oStream(stream); + if (auto sp = sender_.lock()) // the owner is still holding the token + (*sp)(stream); +} + + +/** + * @brief Sender::setSender + * @param cb + * @return + */ +std::shared_ptr Sender::setSender(const std::function)> cb) +{ + // wrap the callback with a shared pointer + auto sp = std::make_shared)>>(std::move(cb)); + // store sender function (as a weak pointer) + sender_ = sp; + // return token that caller must keep throughout it's scope + return sp; +} + } // namespace OSC diff --git a/protocol/osc/sender.h b/protocol/osc/sender.h index 76ec9c2..e3abc01 100644 --- a/protocol/osc/sender.h +++ b/protocol/osc/sender.h @@ -23,6 +23,8 @@ */ #pragma once +#include +#include #include #include "message.h" @@ -40,10 +42,12 @@ public: explicit Sender(); virtual ~Sender(); - /** - * @brief send - */ - virtual void send(std::shared_ptr) = 0; + void send(std::shared_ptr) const; + std::shared_ptr setSender(const std::function)>); + + +private: + std::weak_ptr)>> sender_; }; } // namespace OSC