#ifndef MESSAGE_H #define MESSAGE_H #include "ErrorCode.h" #include #include namespace ZeroMQ { class Message { public: Message() noexcept; ~Message() noexcept; Message(Message &&rhs) noexcept; Message &operator=(Message &&rhs) noexcept; bool operator==(const Message &other) const noexcept; bool operator!=(const Message &other) const noexcept; Message(size_t size) { int rc = zmq_msg_init_size(&m_message, size); if (rc != 0) throw makeErrorCode(); } Message(const void *dataSrc, size_t size); Message(std::string_view string) : Message(string.data(), string.size()) { } void *data() noexcept { return zmq_msg_data(&m_message); } const void *data() const noexcept { return zmq_msg_data(const_cast(&m_message)); } template T const *data() const noexcept { return static_cast(data()); } size_t size() const noexcept { return zmq_msg_size(const_cast(&m_message)); } zmq_msg_t *handle() noexcept { return &m_message; } const zmq_msg_t *handle() const noexcept { return &m_message; } bool more() const noexcept { int rc = zmq_msg_more(const_cast(&m_message)); return rc != 0; } /** * @brief copy from message */ void copy(Message &message); /** * @brief zeromq的字符串不是以\0结束的,所以调用std::string_view::data()可能会导致错误 * * @return std::string_view */ std::string_view toStringView() const noexcept { return std::string_view(static_cast(data()), size()); } std::string dump() const; protected: Message(const Message &) = delete; void operator=(const Message &) = delete; private: // The underlying message zmq_msg_t m_message; }; } // namespace ZeroMQ namespace std { ostream &operator<<(ostream &stream, const ZeroMQ::Message &message); } #endif // MESSAGE_H