rcpp_framework/libs/brynet/net/SendableMsg.hpp

48 lines
1.0 KiB
C++
Raw Normal View History

2021-04-30 16:10:14 +02:00
#include <memory>
#include <string>
2021-05-14 17:16:45 +02:00
class SendableMsg {
2021-04-30 16:10:14 +02:00
public:
2021-05-14 17:16:45 +02:00
using Ptr = std::shared_ptr<SendableMsg>;
2021-04-30 16:10:14 +02:00
2021-05-14 17:16:45 +02:00
virtual ~SendableMsg() = default;
2021-04-30 16:10:14 +02:00
2021-05-14 17:16:45 +02:00
virtual const void *data() = 0;
virtual size_t size() = 0;
2021-04-30 16:10:14 +02:00
};
2021-05-14 17:16:45 +02:00
class StringSendMsg : public SendableMsg {
2021-04-30 16:10:14 +02:00
public:
2021-05-14 17:16:45 +02:00
explicit StringSendMsg(const char *buffer, size_t len) :
mMsg(buffer, len) {}
explicit StringSendMsg(const std::string &buffer) :
mMsg(buffer) {}
explicit StringSendMsg(std::string &&buffer) :
mMsg(std::move(buffer)) {}
const void *data() override {
return static_cast<const void *>(mMsg.data());
}
size_t size() override {
return mMsg.size();
}
2021-04-30 16:10:14 +02:00
private:
2021-05-14 17:16:45 +02:00
std::string mMsg;
2021-04-30 16:10:14 +02:00
};
2021-05-14 17:16:45 +02:00
static SendableMsg::Ptr MakeStringMsg(const char *buffer, size_t len) {
return std::make_shared<StringSendMsg>(buffer, len);
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
static SendableMsg::Ptr MakeStringMsg(const std::string &buffer) {
return std::make_shared<StringSendMsg>(buffer);
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
static SendableMsg::Ptr MakeStringMsg(std::string &&buffer) {
return std::make_shared<StringSendMsg>(std::move(buffer));
2021-04-30 16:10:14 +02:00
}