Template Class Buffer

Class Documentation

template<typename T, typename Allocator = std::allocator<T>>
class Buffer

Buffer<T> provides a drop-in replacement for std::vector<T> with support for vendor-specific memory backends (CPU, GPU, etc.).

For CPU backends, it provides implicit conversion to std::vector<T>& for seamless backward compatibility. For non-CPU backends, direct access throws exceptions, requiring explicit conversion via to_vector().

Public Types

using value_type = T
using allocator_type = Allocator
using size_type = size_t
using difference_type = std::ptrdiff_t
using reference = T&
using const_reference = const T&
using pointer = typename std::vector<T, Allocator>::pointer
using const_pointer = typename std::vector<T, Allocator>::const_pointer
using iterator = typename std::vector<T, Allocator>::iterator
using const_iterator = typename std::vector<T, Allocator>::const_iterator
using reverse_iterator = typename std::vector<T, Allocator>::reverse_iterator
using const_reverse_iterator = typename std::vector<T, Allocator>::const_reverse_iterator

Public Functions

inline Buffer()

Default constructor creates CPU buffer.

inline explicit Buffer(std::unique_ptr<BufferImplBase<T>> impl)

Construct with a backend implementation (set once at construction). This is the only way to set a non-CPU backend; there is no post- construction setter, which avoids race conditions with concurrent reads.

inline explicit Buffer(size_t count)

Construct with initial size (CPU backend)

inline Buffer(size_t count, const T &value)

Construct with initial size and value (CPU backend)

inline Buffer(const std::vector<T, Allocator> &vec)

Construct from std::vector (copy) - for backward compatibility.

inline Buffer(std::vector<T, Allocator> &&vec)

Construct from std::vector (move) - for backward compatibility.

inline Buffer(std::initializer_list<T> init)

Construct from initializer list - for backward compatibility.

inline Buffer(const Buffer &other)

Copy constructor (deep copy via clone())

inline Buffer(Buffer &&other) noexcept

Move constructor — the moved-from buffer is left in a valid, empty state.

inline Buffer &operator=(const Buffer &other)

Copy assignment (deep copy via clone())

inline Buffer &operator=(Buffer &&other) noexcept

Move assignment — the moved-from buffer is left in a valid, empty state.

inline Buffer &operator=(std::initializer_list<T> init)

Assignment from initializer list - for backward compatibility This must come before vector assignment to resolve ambiguity with {{…}} syntax

template<typename U = std::vector<T, Allocator>, typename std::enable_if<std::is_same<U, std::vector<T, Allocator>>::value, int>::type = 0>
inline Buffer &operator=(const U &vec)

Assignment from std::vector (copy) - for backward compatibility Uses SFINAE to avoid ambiguity with initializer lists

template<typename U = std::vector<T, Allocator>, typename std::enable_if<std::is_same<U, std::vector<T, Allocator>>::value, int>::type = 0>
inline Buffer &operator=(U &&vec)

Assignment from std::vector (move) - for backward compatibility Uses SFINAE to avoid ambiguity with initializer lists

inline reference operator[](size_t pos)

Access element at position (CPU only)

inline const_reference operator[](size_t pos) const
inline reference at(size_t pos)

Access element with bounds checking (CPU only)

inline const_reference at(size_t pos) const
inline reference front()

Access first element (CPU only)

inline const_reference front() const
inline reference back()

Access last element (CPU only)

inline const_reference back() const
inline pointer data()

Get pointer to data (CPU only)

inline const_pointer data() const
inline iterator begin()
inline const_iterator begin() const
inline const_iterator cbegin() const
inline iterator end()
inline const_iterator end() const
inline const_iterator cend() const
inline reverse_iterator rbegin()
inline const_reverse_iterator rbegin() const
inline const_reverse_iterator crbegin() const
inline reverse_iterator rend()
inline const_reverse_iterator rend() const
inline const_reverse_iterator crend() const
inline bool empty() const

Works for all backends (delegates to BufferImplBase::size()).

inline size_t size() const

Works for all backends (delegates to BufferImplBase::size()).

inline size_t max_size() const
inline void reserve(size_t new_cap)
inline size_t capacity() const
inline void shrink_to_fit()
inline allocator_type get_allocator() const
inline void assign(size_t count, const T &value)
template<typename InputIt>
inline void assign(InputIt first, InputIt last)
inline void assign(std::initializer_list<T> ilist)
inline iterator insert(const_iterator pos, const T &value)
inline iterator insert(const_iterator pos, T &&value)
inline iterator insert(const_iterator pos, size_t count, const T &value)
template<typename InputIt>
inline iterator insert(const_iterator pos, InputIt first, InputIt last)
inline iterator insert(const_iterator pos, std::initializer_list<T> ilist)
inline void clear()
inline void resize(size_t n)
inline void resize(size_t n, const T &value)
inline void push_back(const T &value)
inline void push_back(T &&value)
inline void pop_back()
template<typename ...Args>
inline iterator emplace(const_iterator pos, Args&&... args)
template<typename ...Args>
inline reference emplace_back(Args&&... args)
inline iterator erase(const_iterator pos)
inline iterator erase(const_iterator first, const_iterator last)
inline void swap(Buffer &other) noexcept
inline void swap(std::vector<T, Allocator> &vec) noexcept
inline operator std::vector<T, Allocator>&()

Implicit conversion to std::vector<T, Allocator>& (CPU only). Provides backward compatibility with existing code.

Throws:

std::runtime_error – if backend is not CPU.

inline operator const std::vector<T, Allocator>&() const
inline std::vector<T, Allocator> to_vector() const

Escape hatch: Explicit conversion to std::vector<T, Allocator> (all backends). For non-CPU backends, this triggers a copy to CPU memory.

Returns:

A std::vector containing a copy of the buffer data.

inline bool operator==(const Buffer &other) const
inline bool operator!=(const Buffer &other) const
inline std::string get_backend_type() const

Get the backend type identifier (e.g., “cpu”, “cuda”). Delegates to the underlying implementation — the impl is the single source of truth for its own backend type.

inline const BufferImplBase<T> *get_impl() const

Get the implementation pointer (read-only).

inline BufferImplBase<T> *get_impl()

Get the implementation pointer (mutable, e.g. for descriptor creation).

inline void throw_if_not_cpu_backend() const

Throw exception if not CPU backend.

Throws:

std::runtime_error – if backend is not CPU.