Common#
-
class Buffer#
Buffer, use to encapsulate the continuous data type of any other data area into a unified data transfer connection; support for packaging objects in managed and unmanaged ways. Among them, packaging an object in a managed manner requires that the shared smart pointer of the wrapped object be passed in, Buffer will try to automatically deconstruct the data pointer when destructing; packaging in an unmanaged manner will not automatically destruct the data pointer, and requires the user to control the release of the data. packaging an object in a managed manner could be like this:
packaging in an unmanaged manner could be like below:std::vector<int>* vec = new std::vector<int>(10); Buffer buffer(vec->data(), vec->size(), std::shared_ptr(vec)); // Use the buffer // No need to delete vec manually.
If resizer set, you can resize the buffer like below:std::vector<int>* vec = new std::vector<int>(10); Buffer buffer(vec->data(), vec->size()); // Use the buffer delete vec; // delete to release vector
std::string my_str; my_str.resize(100); Buffer buffer_1(my_str.data(), my_str.size()); // buffer_1.resize(500); // May encounter an exception Buffer buffer_2(my_str.data(), my_str.size(), {}, // unmanaged [&my_str](std::shared_ptr<void>, size_t size) { my_str.resize(size); return my_str.data(); }); buffer_2.resize(500); std::cout << buffer_2.size(); // 500
Public Types
Public Functions
-
explicit Buffer(size_t size = 0)#
Construct a new buffer with given data, default size is 0, which means the buffer is empty.
- Parameters:
size – buffer size.
Construct a new Buffer object with given data pointer and size.
- Parameters:
data – data pointer.
size – data size.
owner – whether the data pointer is managed by this buffer. If owner shared_pointer is not empty, the data pointer will be deleted when this buffer is destructed.
resizer – resizer function, if resizer is not empty, the buffer will be resized when the buffer size is changed. otherwise, the buffer will be a fixed size buffer and can not be resized.
-
Buffer(const Buffer &other) = default#
Copy constructor, only copy the data pointer and size, not the data. If you want to copy the data, you should use Buffer::clone_data().
- Parameters:
other – other buffer.
-
Buffer &operator=(const Buffer &other) = default#
Assign operator, only copy the data pointer and size, not the data. If you want to copy the data, you should use Buffer::clone_data().
- Parameters:
other – other buffer.
-
Buffer(Buffer &&rvalue) noexcept = default#
Move constructor.
- Parameters:
rvalue – other rvalue buffer.
-
bool operator==(const Buffer &other) const#
Check whether the buffer data is equal to the given buffer.
- Parameters:
other – other buffer.
- Returns:
true if the buffer data is equal to the given buffer.
- Returns:
false if the buffer data is not equal to the given buffer.
-
void *resize(size_t size)#
Resize the buffer with given size.
Exception will be thrown if the buffer is not resizable, so you’d better to check the buffer is resizable before resize by call Buffer::is_resizable().
- Parameters:
size – new size.
- Throws:
excepts::LogicError – if the buffer is not resizable.
- Returns:
void* new data pointer.
-
void append(const void *data, size_t size)#
Append the given data at the end of the buffer.
- Parameters:
data – data pointer.
size – data size.
- Throws:
excepts::LogicError – if append data out of the buffer reserved size and the buffer is not resizable.
-
void *reserve(size_t size)#
Reserve the buffer with given size.
- Parameters:
size – new size.
- Returns:
void* new data pointer.
-
void *data()#
Get the data pointer.
- Returns:
void* data pointer.
-
const void *data() const#
Get the const data pointer.
- Returns:
void const* const data pointer.
-
Buffer &from_file(const std::string &path)#
Read buffer content from the given file path.
See also
- Parameters:
path – file path, should encoded in UTF-8.
- Returns:
Buffer& The current buffer object.
-
Buffer &from_hex(const std::string &hex_str)#
Read buffer content from the given hex string.
See also
- Parameters:
hex_str – hex string, should only contains 0-9, a-f, A-F.
- Returns:
Buffer& The current buffer object.
-
Buffer &from_base32(const std::string &base32_str)#
Read buffer content from the given base32 string.
- Parameters:
base32_str – the base32(clockwork_base32) string, should only contains 0-9, A-Z(exclude I, L, O, U).
- Returns:
Buffer& The current buffer object decode from base32 string.
-
Buffer &from_base64(const std::string &base64_str)#
Read buffer content from the given base64 string.
See also
- Parameters:
base64_str – base64 string, should only contains 0-9, a-z, A-Z, +, /, =.
- Returns:
Buffer& The current buffer object.
-
bool to_file(const std::string &path) const#
Save the buffer content into the given file path with binary mode.
- Parameters:
path – file path, should encoded in UTF-8.
- Returns:
true if the buffer content is saved successfully.
- Returns:
false if the buffer content is not saved successfully.
-
std::string to_hex() const#
Convert the buffer content to a hex string.
- Returns:
std::string hex string.
-
std::string to_base32() const#
Encode the buffer data into base32 string.
- Returns:
std::string the encode base32 string.
-
std::string to_base64() const#
Convert the buffer content to a base64 string.
- Returns:
std::string base64 string.
-
inline std::string to_string() const#
Copy data in Buffer into std::string.
Warning
Not all data in Buffer can be converted into a string. The developer who calls this function needs to make sure that the content in the Buffer is a string, otherwise the data may not be displayed normally. If you have a Buffer with binary data, we recommend that you use the to_hex() or to_base64().
- Returns:
std::string which contains copied data in Buffer.
-
Buffer clone_data() const#
Clone data in buffer, Only promise that the cloned data is consistent with the original data, and the managed objects are not guaranteed to be consistent.
- Returns:
Buffer buffer with cloned data.
-
bool is_managed() const#
Check if the buffer data is managed by this buffer.
- Returns:
true if the buffer data is managed by this buffer.
- Returns:
false if the buffer data is not managed by this buffer.
-
bool is_resizable() const#
Check if the buffer data is resizable.
- Returns:
true if the buffer data is resizable.
- Returns:
false if the buffer data is not resizable.
-
size_t size() const#
Get the buffer size.
- Returns:
size_t buffer size.
-
size_t capacity() const#
Get the buffer reserved size.
- Returns:
size_t buffer reserved size.
Public Static Functions
-
static Buffer FromFile(const std::string &path)#
Read buffer from a file.
- Parameters:
path – file path, should encoded in UTF-8.
- Returns:
Buffer buffer object.
-
static Buffer FromHex(const std::string &hex_str)#
Decode a hex string to a buffer.
- Parameters:
hex_str – hex string, should only contains 0-9, A-F.
- Returns:
Buffer Decoded buffer object.
-
explicit Buffer(size_t size = 0)#