Template Class buffer

Class Documentation

template<class T, std::size_t N = 16, class Allocator = std::allocator<T>>
class util::buffer

A buffer class with a fixed-size base storage and dynamic memory for additional storage.

The buffer class is a container for values with a base fixed-size memory part and additional dynamic part. This means that a number of values less than the fixed-size parameter are stored on the stack and a number of values greater than the fixed-size parameter are stored on the heap can can grow dynamically.

tparam T

the type of values used in the buffer

tparam N

the base part fixed size of the buffer

tparam Allocator

the allocator for the dynamic part of the buffer

Public Types

using allocator_type = Allocator
using difference_type = std::ptrdiff_t
using size_type = std::size_t
using value_type = T
using iterator = detail::buffer_iterator<false, T, N, Allocator>
using const_iterator = detail::buffer_iterator<true, T, N, Allocator>
using reference = value_type&
using const_reference = const value_type&
using pointer = value_type*
using const_pointer = const value_type*

Public Functions

buffer() = default
~buffer() = default
buffer(const buffer&) = default
buffer(buffer&&) noexcept = default
auto operator=(const buffer&) -> buffer& = default
auto operator=(buffer&&) noexcept -> buffer& = default
buffer(const std::initializer_list<T> &list)

Constructs a buffer object from an initializer list.

const util::buffer<int> numbers{1, 2, 3};

Parameters

list – A initializer list containing the initial elements for this buffer

auto at(size_type pos) -> reference

Returns the element at the given position with boundary checking.

util::buffer<int> numbers{1, 2, 3};
assert(numbers.at(2) == 3);

Parameters

pos – the requested element’s position

Throws

out_of_range – if pos >= size()

Returns

a reference to the requested element

auto at(size_type pos) const -> const_reference

See

auto buffer<T, N, Allocator>::at(size_type pos) -> reference

const util::buffer<int> numbers{1, 2, 3};
assert(numbers.at(2) == 3);

auto operator[](size_type pos) -> reference

Returns the element at the given position without boundary checking.

util::buffer<int> numbers{1, 2, 3};
assert(numbers[2] == 3);

Parameters

pos – the requested element’s position

Returns

a reference to the requested element

auto operator[](size_type pos) const -> const_reference

See

auto buffer<T, N, Allocator>::operator(size_type pos) -> reference

const util::buffer<int> numbers{1, 2, 3};
assert(numbers[2] == 3);

auto front() -> reference

Returns the first element from the buffer. Undefined behaviour if the buffer is empty.

util::buffer<int> numbers{1, 2, 3};
assert(numbers.front() == 1);

Returns

a reference to the first element in the buffer

auto front() const -> const_reference

See

auto buffer<T, N, Allocator>::front -> reference

const util::buffer<int> numbers{1, 2, 3};
assert(numbers.front() == 1);

auto back() -> reference

Returns the last element from the buffer. Undefined behaviour if the buffer is empty.

util::buffer<int> numbers{1, 2, 3};
assert(numbers.back() == 3);

Returns

a reference to the last element in the buffer

auto back() const -> const_reference

See

auto buffer<T, N, Allocator>::back -> reference

const util::buffer<int> numbers{1, 2, 3};
assert(numbers.back() == 3);

auto stack_data() noexcept -> pointer

Returns a direct pointer to the memory used internally by the buffer to store its stack elements.

util::buffer<int> numbers{1, 2, 3};
assert(*numbers.stack_data() == 1);

Returns

a pointer to the first stack element in the array used internally by the buffer

auto stack_data() const noexcept -> const_pointer

See

auto buffer<T, N, Allocator>::stack_data -> pointer

const util::buffer<int> numbers{1, 2, 3};
assert(*numbers.stack_data() == 1);

auto heap_data() noexcept -> pointer

Returns a direct pointer to the memory used internally by the buffer to store its heap elements.

util::buffer<int, 2> numbers{1, 2, 3};
assert(*numbers.heap_data() == 3);

Returns

a pointer to the first heap element in the array used internally by the buffer

auto heap_data() const noexcept -> const_pointer

See

auto buffer<T, N, Allocator>::heap_data -> pointer

const util::buffer<int, 2> numbers{1, 2, 3};
assert(*numbers.heap_data() == 3);

auto begin() noexcept -> iterator

Returns an iterator to the first element of the buffer. If the buffer is empty, the returned iterator will be equal to end().


Returns

an iterator to the first element

auto begin() const noexcept -> const_iterator

See

auto buffer<T, N, Allocator>::begin -> iterator

const util::buffer<int> numbers{1, 2, 3};
assert(*numbers.begin() == 1);

auto cbegin() const noexcept -> const_iterator

Returns a const_iterator to the first element of the buffer.

util::buffer<int> numbers{1, 2, 3};
assert(*numbers.cbegin() == 1);

Returns

a const_iterator to the first element

auto end() noexcept -> iterator

Returns an iterator to the element past the last element of the buffer.

util::buffer<int> numbers{1, 2, 3};
assert(numbers.begin() != numbers.end());

Returns

an iterator to the position past the last element

auto end() const noexcept -> const_iterator

See

auto buffer<T, N, Allocator>::end -> iterator

const util::buffer<int> numbers{1, 2, 3};
assert(numbers.begin() != numbers.end());

auto cend() const noexcept -> const_iterator

Returns a const_iterator to the element past the last element of the buffer.

util::buffer<int> numbers{1, 2, 3};
assert(numbers.cbegin() != numbers.cend());

Returns

an iterator to the position past the last element

auto rbegin() noexcept -> iterator
auto rbegin() const noexcept -> const_iterator
auto crbegin() const noexcept -> const_iterator
auto rend() noexcept -> iterator
auto rend() const noexcept -> const_iterator
auto crend() const noexcept -> const_iterator
auto empty() const noexcept -> bool

Checks if the buffer has no elements.

const util::buffer<int> numbers;
assert(numbers.empty());

Returns

true if the buffer is empty, false otherwise

auto size() const noexcept -> size_type

Returns the current number of elements in the buffer (stack and heap).

const util::buffer<int> numbers{1, 2, 3, 4, 5};
assert(numbers.size() == 5);

Returns

the current number of elements in the buffer

auto max_size() const noexcept -> size_type

Returns the maximum number of elements the container is able to hold.

const util::buffer<int> numbers{1, 2, 3, 4, 5};
assert(numbers.size() == 5);

Returns

the current number of elements in the buffer

void resize(size_type n, const value_type &val)
auto capacity() const noexcept -> size_type