2024-10-29 21:51:37 +08:00
|
|
|
#ifndef __BUFFER_H__
|
|
|
|
#define __BUFFER_H__
|
|
|
|
|
|
|
|
#include <cstdint>
|
2024-11-10 18:02:28 +08:00
|
|
|
#include <string_view>
|
2024-10-29 21:51:37 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-11-10 18:02:28 +08:00
|
|
|
std::string_view data() const noexcept;
|
|
|
|
|
2024-10-29 21:51:37 +08:00
|
|
|
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__
|