Template Class Buffer
Defined in File buffer.hpp
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 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(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(Buffer &&other) noexcept
Move constructor — the moved-from buffer is left in a valid, empty state.
-
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 const_reference operator[](size_t pos) const
-
inline const_reference at(size_t pos) const
-
inline const_reference front() const
-
inline const_reference back() const
-
inline const_pointer data() const
-
inline const_iterator begin() const
-
inline const_iterator cbegin() const
-
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 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 pop_back()
-
template<typename ...Args>
inline iterator emplace(const_iterator pos, Args&&... args)
-
inline iterator erase(const_iterator pos)
-
inline iterator erase(const_iterator first, const_iterator last)
-
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 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 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.
-
inline Buffer()