Kylin/Nng/Buffer.h
2024-11-10 18:02:28 +08:00

60 lines
1.1 KiB
C++

#ifndef __BUFFER_H__
#define __BUFFER_H__
#include <cstdint>
#include <string_view>
namespace Nng {
namespace Asio {
class Socket;
};
class Buffer {
friend class Socket;
friend class Asio::Socket;
public:
Buffer() = default;
Buffer(std::size_t size);
Buffer(const Buffer &rhs);
~Buffer();
Buffer(Buffer &&rhs) noexcept {
m_buffer = rhs.m_buffer;
m_size = rhs.m_size;
rhs.m_buffer = nullptr;
rhs.m_size = 0;
}
Buffer &operator=(const Buffer &rhs);
Buffer &operator=(Buffer &&rhs) noexcept;
explicit operator bool() const noexcept {
return (m_buffer != nullptr) && (m_size > 0);
}
template <typename T = void>
T *data() const noexcept {
return reinterpret_cast<T *>(m_buffer);
}
std::string_view data() const noexcept;
std::size_t size() const noexcept {
return m_size;
}
protected:
template <typename T>
Buffer(T *data, std::size_t size) : m_buffer(data), m_size(size) {
}
void *release();
private:
void *m_buffer = nullptr;
std::size_t m_size = 0;
};
} // namespace Nng
#endif // __BUFFER_H__