4.6.0
Freundlich's C++ toolkit
|
A special buffer class for uninitialized memory.
Ordinary arrays in C++ are not default-initialized, which is mostly for performance reasons. This helps in situations where you have to call functions that write into a buffer handed to them, e.g. when reading from a file. If you need a dynamically sized array instead, you could use a std::vector
. However, std::vector
default-initializes its values.
fcppt::container::buffer::object instead manages a partially uninitialized block of memory: It contains
Consider the following example in which the user wants to read 1024 integers from a file stream, using binary I/O. We have to hand a raw buffer to the ifstream::read
function that counts sizeof(int) * 1024
bytes. After reading has been done, we check if it succeeded and if it has, we increase the read area size appropriately.
A buffer can be converted into an fcppt::container::raw_vector::object using fcppt::container::buffer::to_raw_vector. A raw vector acts more like a standard container and is similar to std::vector
.
Classes | |
class | fcppt::container::buffer::object< T, A > |
A contiguous container for uninitialized data that contains a write and a read area. More... | |
Functions | |
template<typename T , typename A , typename Function > | |
fcppt::container::buffer::object< T, A > | fcppt::container::buffer::append_from (fcppt::container::buffer::object< T, A > &&_buffer, typename fcppt::container::buffer::object< T, A >::size_type const _size, Function const &_function) |
Appends to a buffer using a function. | |
template<typename T , typename A , typename Function > | |
fcppt::optional::object< fcppt::container::buffer::object< T, A > > | fcppt::container::buffer::append_from_opt (fcppt::container::buffer::object< T, A > &&_buffer, typename fcppt::container::buffer::object< T, A >::size_type const _size, Function const &_function) |
Appends to a buffer using a function which may fail. | |
template<typename Buffer , typename Function > | |
Buffer | fcppt::container::buffer::read_from (typename Buffer::size_type const _size, Function const &_function) |
Reads into a buffer using a function. | |
template<typename Buffer , typename Function > | |
fcppt::optional::object< Buffer > | fcppt::container::buffer::read_from_opt (typename Buffer::size_type const _size, Function const &_function) |
Reads into a buffer using a function which may fail. | |
template<typename T , typename A > | |
fcppt::container::raw_vector::object< T, A > | fcppt::container::buffer::to_raw_vector (fcppt::container::buffer::object< T, A > &&_buffer) noexcept |
Convert a buffer into a raw_vector. | |
fcppt::container::buffer::object< T, A > fcppt::container::buffer::append_from | ( | fcppt::container::buffer::object< T, A > && | _buffer, |
typename fcppt::container::buffer::object< T, A >::size_type const | _size, | ||
Function const & | _function ) |
Appends to a buffer using a function.
Allocates a write area for _buffer of size _size and then calls _function(_buffer.write_data(),_size)
. The result of the function is used to expand _buffer
's read area size.
Function | A function callable as size_type (pointer, size_type) . |
fcppt::optional::object< fcppt::container::buffer::object< T, A > > fcppt::container::buffer::append_from_opt | ( | fcppt::container::buffer::object< T, A > && | _buffer, |
typename fcppt::container::buffer::object< T, A >::size_type const | _size, | ||
Function const & | _function ) |
Appends to a buffer using a function which may fail.
Allocates a buffer buf
of size _size and then calls _function(buf.write_data(),_size)
. If the result of the function is nothing, then nothing is returned. Otherwise, The result of the function is used to set buf
's read area size.
Function | A function callable as fcppt::optional::object<size_type> (pointer, size_type) . |
|
inline |
Reads into a buffer using a function.
Allocates a buffer buf
of size _size and then calls _function(buf.write_data(),_size)
. The result of the function is used to set buf
's read area size.
Buffer | An fcppt::container::buffer::object. |
Function | A function callable as size_type (pointer, size_type) . |
fcppt::optional::object< Buffer > fcppt::container::buffer::read_from_opt | ( | typename Buffer::size_type const | _size, |
Function const & | _function ) |
Reads into a buffer using a function which may fail.
Allocates a buffer buf
of size _size and then calls _function(buf.write_data(),_size)
. If the result of the function is nothing, then nothing is returned. Otherwise, The result of the function is used to set buf
's read area size.
Buffer | An fcppt::container::buffer::object. |
Function | A function callable as fcppt::optional::object<size_type> (pointer, size_type) . |
|
noexcept |
Convert a buffer into a raw_vector.