SofaBuffers C++ 0.12.0
Pure-C++20 SofaBuffers — API Documentation
Loading...
Searching...
No Matches
sofab::InlineVector< T, N > Class Template Reference

Fixed-capacity, heap-free sequence of up to N elements of type T. More...

#include <sofab.hpp>

Public Types

using value_type = T
 Element type (mirrors std::vector).
 
using size_type = std::size_t
 Size type (mirrors std::vector).
 

Public Member Functions

 InlineVector () noexcept=default
 Construct an empty sequence.
 
 InlineVector (std::initializer_list< T > init) noexcept
 Construct from a brace-enclosed list of elements (clamped to N; extra elements are dropped).
 
InlineVector & operator= (std::initializer_list< T > init) noexcept
 Replace the contents from a brace-enclosed list (clamped to N; extra elements are dropped).
 
InlineVector & assign (std::initializer_list< T > init) noexcept
 Replace the contents from a brace-enclosed list (clamped to N; extra elements are dropped).
 
std::size_t size () const noexcept
 Number of elements currently stored.
 
bool empty () const noexcept
 True if the sequence is empty.
 
void reserve (std::size_t) noexcept
 No-op (inline storage never reallocates); present for API parity.
 
void clear () noexcept
 Reset to an empty sequence (logical length only).
 
void resize (std::size_t n) noexcept
 Set the logical length to n, value-initializing what changes.
 
T & emplace_back () noexcept
 Append a default-constructed element and return a reference to it.
 
void push_back (const T &v) noexcept
 Append a copy of v; once at capacity N, v is dropped.
 
void push_back (T &&v) noexcept
 Append v by move; dropped once at capacity N (see above).
 
T & back () noexcept
 Reference to the last element.
 
const T & back () const noexcept
 Const reference to the last element.
 
T & operator[] (std::size_t i) noexcept
 Access the element at i (no bounds checking).
 
const T & operator[] (std::size_t i) const noexcept
 Access the element at i (no bounds checking).
 
T * data () noexcept
 Mutable pointer to the underlying storage.
 
const T * data () const noexcept
 Const pointer to the underlying storage.
 
T * begin () noexcept
 Iterator to the first element.
 
T * end () noexcept
 Iterator past the last element.
 
const T * begin () const noexcept
 Const iterator to the first element.
 
const T * end () const noexcept
 Const iterator past the last element.
 
bool operator== (const InlineVector &o) const noexcept
 Content equality (same logical length and elements).
 
bool operator!= (const InlineVector &o) const noexcept
 Negated operator==.
 

Static Public Member Functions

static constexpr std::size_t capacity () noexcept
 Maximum number of elements (the template parameter N).
 
static constexpr std::size_t max_size () noexcept
 Alias of capacity.
 

Detailed Description

template<typename T, std::size_t N>
class sofab::InlineVector< T, N >

Fixed-capacity, heap-free sequence of up to N elements of type T.

The embedded-friendly counterpart of std::vector<T> for every array a schema bounds with a count: native scalars as well as strings, blobs, structs/unions and nested arrays. Elements live in an inline std::array, so the storage never reallocates and an element being filled across IStreamImpl::feed chunks stays address-stable — strictly safer than a std::vector + reserve.

resize is what marks this type as a resizable destination to IStreamImpl::readArray, which sizes it to the wire element count. That count IS the array's length (MESSAGE_SPEC §3); N is the schema count, a capacity that bounds it and never adds to it.

A logical length (size, <= N) is tracked separately from the capacity N: an array shorter than its schema count holds only size elements. This is why the type is neither a plain std::array<T,N> (always length N) nor a heap-backed std::vector.

Warning
Historically this was an aggregate with a public, default-zero length, so a natural brace-init such as v = {a, b, c} silently filled the storage while leaving the logical length at 0 — the field then encoded as empty. The initializer_list constructor/assignment below make the type a non-aggregate, so that brace-init now sets size correctly instead of corrupting the wire.
Template Parameters
TElement type.
NMaximum number of elements.

Definition at line 1158 of file sofab.hpp.

Member Typedef Documentation

◆ size_type

template<typename T , std::size_t N>
using sofab::InlineVector< T, N >::size_type = std::size_t

Size type (mirrors std::vector).

Definition at line 1167 of file sofab.hpp.

◆ value_type

template<typename T , std::size_t N>
using sofab::InlineVector< T, N >::value_type = T

Element type (mirrors std::vector).

Definition at line 1165 of file sofab.hpp.

Constructor & Destructor Documentation

◆ InlineVector() [1/2]

template<typename T , std::size_t N>
sofab::InlineVector< T, N >::InlineVector ( )
defaultnoexcept

Construct an empty sequence.

◆ InlineVector() [2/2]

template<typename T , std::size_t N>
sofab::InlineVector< T, N >::InlineVector ( std::initializer_list< T >  init)
inlinenoexcept

Construct from a brace-enclosed list of elements (clamped to N; extra elements are dropped).

The presence of this constructor makes InlineVector a non-aggregate: v = {a, b, c} routes here and sets size, instead of aggregate brace-init filling the storage while leaving the length at 0.

Parameters
initSource elements (excess beyond N is dropped).

Definition at line 1181 of file sofab.hpp.

Member Function Documentation

◆ assign()

template<typename T , std::size_t N>
InlineVector & sofab::InlineVector< T, N >::assign ( std::initializer_list< T >  init)
inlinenoexcept

Replace the contents from a brace-enclosed list (clamped to N; extra elements are dropped).

Definition at line 1193 of file sofab.hpp.

◆ back() [1/2]

template<typename T , std::size_t N>
const T & sofab::InlineVector< T, N >::back ( ) const
inlinenoexcept

Const reference to the last element.

Definition at line 1289 of file sofab.hpp.

◆ back() [2/2]

template<typename T , std::size_t N>
T & sofab::InlineVector< T, N >::back ( )
inlinenoexcept

Reference to the last element.

Definition at line 1287 of file sofab.hpp.

◆ begin() [1/2]

template<typename T , std::size_t N>
const T * sofab::InlineVector< T, N >::begin ( ) const
inlinenoexcept

Const iterator to the first element.

Definition at line 1306 of file sofab.hpp.

◆ begin() [2/2]

template<typename T , std::size_t N>
T * sofab::InlineVector< T, N >::begin ( )
inlinenoexcept

Iterator to the first element.

Definition at line 1302 of file sofab.hpp.

◆ capacity()

template<typename T , std::size_t N>
static constexpr std::size_t sofab::InlineVector< T, N >::capacity ( )
inlinestaticconstexprnoexcept

Maximum number of elements (the template parameter N).

Definition at line 1212 of file sofab.hpp.

◆ clear()

template<typename T , std::size_t N>
void sofab::InlineVector< T, N >::clear ( )
inlinenoexcept

Reset to an empty sequence (logical length only).

Definition at line 1219 of file sofab.hpp.

◆ data() [1/2]

template<typename T , std::size_t N>
const T * sofab::InlineVector< T, N >::data ( ) const
inlinenoexcept

Const pointer to the underlying storage.

Definition at line 1299 of file sofab.hpp.

◆ data() [2/2]

template<typename T , std::size_t N>
T * sofab::InlineVector< T, N >::data ( )
inlinenoexcept

Mutable pointer to the underlying storage.

Definition at line 1297 of file sofab.hpp.

◆ emplace_back()

template<typename T , std::size_t N>
T & sofab::InlineVector< T, N >::emplace_back ( )
inlinenoexcept

Append a default-constructed element and return a reference to it.

The next inline slot is (re)set to T{} and bound; once at capacity N the last slot is reused so a decode never writes out of bounds.

Returns
Reference to the newly active element.

Definition at line 1267 of file sofab.hpp.

◆ empty()

template<typename T , std::size_t N>
bool sofab::InlineVector< T, N >::empty ( ) const
inlinenoexcept

True if the sequence is empty.

Definition at line 1210 of file sofab.hpp.

◆ end() [1/2]

template<typename T , std::size_t N>
const T * sofab::InlineVector< T, N >::end ( ) const
inlinenoexcept

Const iterator past the last element.

Definition at line 1308 of file sofab.hpp.

◆ end() [2/2]

template<typename T , std::size_t N>
T * sofab::InlineVector< T, N >::end ( )
inlinenoexcept

Iterator past the last element.

Definition at line 1304 of file sofab.hpp.

◆ max_size()

template<typename T , std::size_t N>
static constexpr std::size_t sofab::InlineVector< T, N >::max_size ( )
inlinestaticconstexprnoexcept

Alias of capacity.

Definition at line 1214 of file sofab.hpp.

◆ operator!=()

template<typename T , std::size_t N>
bool sofab::InlineVector< T, N >::operator!= ( const InlineVector< T, N > &  o) const
inlinenoexcept

Negated operator==.

Definition at line 1328 of file sofab.hpp.

◆ operator=()

template<typename T , std::size_t N>
InlineVector & sofab::InlineVector< T, N >::operator= ( std::initializer_list< T >  init)
inlinenoexcept

Replace the contents from a brace-enclosed list (clamped to N; extra elements are dropped).

Definition at line 1187 of file sofab.hpp.

◆ operator==()

template<typename T , std::size_t N>
bool sofab::InlineVector< T, N >::operator== ( const InlineVector< T, N > &  o) const
inlinenoexcept

Content equality (same logical length and elements).

Definition at line 1311 of file sofab.hpp.

◆ operator[]() [1/2]

template<typename T , std::size_t N>
const T & sofab::InlineVector< T, N >::operator[] ( std::size_t  i) const
inlinenoexcept

Access the element at i (no bounds checking).

Definition at line 1294 of file sofab.hpp.

◆ operator[]() [2/2]

template<typename T , std::size_t N>
T & sofab::InlineVector< T, N >::operator[] ( std::size_t  i)
inlinenoexcept

Access the element at i (no bounds checking).

Definition at line 1292 of file sofab.hpp.

◆ push_back() [1/2]

template<typename T , std::size_t N>
void sofab::InlineVector< T, N >::push_back ( const T &  v)
inlinenoexcept

Append a copy of v; once at capacity N, v is dropped.

The assignment-time clamp of the static storage: elements past N are left out and the ones already held are kept, so a filled container holds the first N values it was given. (emplace_back, the decode-side binder, reuses the last slot instead.)

Definition at line 1282 of file sofab.hpp.

◆ push_back() [2/2]

template<typename T , std::size_t N>
void sofab::InlineVector< T, N >::push_back ( T &&  v)
inlinenoexcept

Append v by move; dropped once at capacity N (see above).

Definition at line 1284 of file sofab.hpp.

◆ reserve()

template<typename T , std::size_t N>
void sofab::InlineVector< T, N >::reserve ( std::size_t  )
inlinenoexcept

No-op (inline storage never reallocates); present for API parity.

Definition at line 1217 of file sofab.hpp.

◆ resize()

template<typename T , std::size_t N>
void sofab::InlineVector< T, N >::resize ( std::size_t  n)
inlinenoexcept

Set the logical length to n, value-initializing what changes.

The std::vector member of the container API this type mirrors, and the one IStreamImpl::readArray and the wrapper-array collectors probe for: readArray resizes a resizable destination and value-initializes a fixed-extent one, and without this method an InlineVector matched neither — it fell to the fixed-extent branch, which assigned a default-constructed container and so set the logical length to 0. The decode then bound an empty span and dropped the array silently. With resize present, readArray keeps ownership of the tag / bound / reset / bind order it documents, for inline storage too.

Slots that enter or leave the logical range are set to T{}, so the elements a shorter value no longer covers cannot be observed through a later grow. n above the capacity N is clamped to N — the callers that can reject an over-capacity count do so before resizing (readArray checks the schema count first), and a heap-free container has nowhere to put the excess.

Parameters
nNew logical length.

Definition at line 1243 of file sofab.hpp.

◆ size()

template<typename T , std::size_t N>
std::size_t sofab::InlineVector< T, N >::size ( ) const
inlinenoexcept

Number of elements currently stored.

Definition at line 1208 of file sofab.hpp.


The documentation for this class was generated from the following file: