SofaBuffers C/C++ 0.12.0
API Documentation
Loading...
Searching...
No Matches
sofab::FixedString< N > Class Template Reference

Fixed-capacity, heap-free string of up to N characters. More...

#include <sofab.hpp>

Public Types

using value_type = char
 Character type (mirrors std::string).
 
using size_type = std::size_t
 Size type (mirrors std::string).
 

Public Member Functions

 FixedString () noexcept=default
 Construct an empty string.
 
 FixedString (const char *s) noexcept
 Construct from a NUL-terminated C string (clamped to N, see assign).
 
 FixedString (std::string_view sv) noexcept
 Construct from a string view (clamped to N, see assign).
 
FixedString & operator= (const char *s) noexcept
 Assign from a NUL-terminated C string (clamped to N, see assign).
 
FixedString & operator= (std::string_view sv) noexcept
 Assign from a string view (clamped to N, see assign).
 
FixedString & assign (std::string_view sv) noexcept
 Replace the contents with sv, clamped to N bytes.
 
void set_len (std::size_t n) noexcept
 Decode hook: set the logical length and (re)place the trailing NUL.
 
char * data () noexcept
 Mutable pointer to the character buffer (decode target).
 
const char * data () const noexcept
 Const pointer to the character buffer.
 
const char * c_str () const noexcept
 NUL-terminated view of the contents.
 
std::size_t size () const noexcept
 Number of characters currently stored.
 
std::size_t length () const noexcept
 Alias of size.
 
bool empty () const noexcept
 True if the string is empty.
 
char & operator[] (std::size_t i) noexcept
 Access the character at i (no bounds checking).
 
const char & operator[] (std::size_t i) const noexcept
 Access the character at i (no bounds checking).
 
char * begin () noexcept
 Iterator to the first character.
 
char * end () noexcept
 Iterator past the last character.
 
const char * begin () const noexcept
 Const iterator to the first character.
 
const char * end () const noexcept
 Const iterator past the last character.
 
void clear () noexcept
 Reset to an empty string.
 
std::string_view view () const noexcept
 Non-owning view over the current characters.
 
 operator std::string_view () const noexcept
 Implicit conversion to std::string_view.
 
bool operator== (std::string_view rhs) const noexcept
 Equality against any string view-like operand.
 
bool operator!= (std::string_view rhs) const noexcept
 Inequality against any string view-like operand.
 

Static Public Member Functions

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

Detailed Description

template<std::size_t N>
class sofab::FixedString< N >

Fixed-capacity, heap-free string of up to N characters.

A drop-in, embedded-friendly stand-in for std::string on both the encode and decode paths. The characters live in an inline std::array, so an instance allocates nothing, never throws (an over-long value is clamped to N bytes at a UTF-8 character boundary, see assign), and compiles cleanly under -fno-exceptions / -fno-rtti. Because the buffer never moves, an instance is a valid address-stable decode target for the deferred IStreamImpl decoder (bytes bound now, filled by a later IStreamImpl::feed pass).

The storage is N+1 bytes: one extra slot always holds a trailing NUL so c_str and the std::string_view encode path remain valid even at full length N. The buffer is zero-initialised, so the NUL is present from construction and is re-placed by every length-changing operation.

Generator integration contract
Generated code mirrors the std::string path exactly, so this type keeps a stable surface for it:
  • decode emits s.set_len(_size); if (_size) is.read(s); — set_len fixes the logical length (and terminating NUL) before the read binds the buffer, and IStreamImpl::read fills data() over size() bytes;
  • encode emits os.write(id, s); — the implicit operator std::string_view routes it through the existing string encode branch, byte-for-byte identical to the same-content std::string.
Template Parameters
NMaximum number of characters (excluding the reserved NUL slot).

Definition at line 309 of file sofab.hpp.

Member Typedef Documentation

◆ size_type

template<std::size_t N>
using sofab::FixedString< N >::size_type = std::size_t

Size type (mirrors std::string).

Definition at line 318 of file sofab.hpp.

◆ value_type

template<std::size_t N>
using sofab::FixedString< N >::value_type = char

Character type (mirrors std::string).

Definition at line 316 of file sofab.hpp.

Constructor & Destructor Documentation

◆ FixedString() [1/3]

template<std::size_t N>
sofab::FixedString< N >::FixedString ( )
defaultnoexcept

Construct an empty string.

◆ FixedString() [2/3]

template<std::size_t N>
sofab::FixedString< N >::FixedString ( const char *  s)
inlinenoexcept

Construct from a NUL-terminated C string (clamped to N, see assign).

Parameters
sSource string, or nullptr for an empty string.

Definition at line 327 of file sofab.hpp.

Here is the call graph for this function:

◆ FixedString() [3/3]

template<std::size_t N>
sofab::FixedString< N >::FixedString ( std::string_view  sv)
inlinenoexcept

Construct from a string view (clamped to N, see assign).

Parameters
svSource characters (may contain embedded NULs).

Definition at line 336 of file sofab.hpp.

Here is the call graph for this function:

Member Function Documentation

◆ assign()

template<std::size_t N>
FixedString & sofab::FixedString< N >::assign ( std::string_view  sv)
inlinenoexcept

Replace the contents with sv, clamped to N bytes.

The assignment-time clamp is a documented contract of the static storage (the generator's allow_dynamic: false): a value longer than N is cut, not refused, before any encoder sees it. The cut never lands inside a UTF-8 character – it moves back to the start of the character that would not fit whole, so the kept prefix is valid UTF-8 whenever sv is.

Parameters
svSource characters (may contain embedded NULs).
Returns
Reference to *this.

Definition at line 391 of file sofab.hpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ begin() [1/2]

template<std::size_t N>
const char * sofab::FixedString< N >::begin ( ) const
inlinenoexcept

Const iterator to the first character.

Definition at line 448 of file sofab.hpp.

◆ begin() [2/2]

template<std::size_t N>
char * sofab::FixedString< N >::begin ( )
inlinenoexcept

Iterator to the first character.

Definition at line 444 of file sofab.hpp.

◆ c_str()

template<std::size_t N>
const char * sofab::FixedString< N >::c_str ( ) const
inlinenoexcept

NUL-terminated view of the contents.

Definition at line 425 of file sofab.hpp.

◆ capacity()

template<std::size_t N>
static constexpr std::size_t sofab::FixedString< N >::capacity ( )
inlinestaticconstexprnoexcept

Maximum number of characters (the template parameter N).

Definition at line 434 of file sofab.hpp.

Here is the call graph for this function:

◆ clear()

template<std::size_t N>
void sofab::FixedString< N >::clear ( )
inlinenoexcept

Reset to an empty string.

Definition at line 453 of file sofab.hpp.

◆ data() [1/2]

template<std::size_t N>
const char * sofab::FixedString< N >::data ( ) const
inlinenoexcept

Const pointer to the character buffer.

Definition at line 423 of file sofab.hpp.

◆ data() [2/2]

template<std::size_t N>
char * sofab::FixedString< N >::data ( )
inlinenoexcept

Mutable pointer to the character buffer (decode target).

Definition at line 421 of file sofab.hpp.

◆ empty()

template<std::size_t N>
bool sofab::FixedString< N >::empty ( ) const
inlinenoexcept

True if the string is empty.

Definition at line 432 of file sofab.hpp.

◆ end() [1/2]

template<std::size_t N>
const char * sofab::FixedString< N >::end ( ) const
inlinenoexcept

Const iterator past the last character.

Definition at line 450 of file sofab.hpp.

◆ end() [2/2]

template<std::size_t N>
char * sofab::FixedString< N >::end ( )
inlinenoexcept

Iterator past the last character.

Definition at line 446 of file sofab.hpp.

◆ length()

template<std::size_t N>
std::size_t sofab::FixedString< N >::length ( ) const
inlinenoexcept

Alias of size.

Definition at line 430 of file sofab.hpp.

◆ max_size()

template<std::size_t N>
static constexpr std::size_t sofab::FixedString< N >::max_size ( )
inlinestaticconstexprnoexcept

Alias of capacity.

Definition at line 436 of file sofab.hpp.

Here is the call graph for this function:

◆ operator std::string_view()

template<std::size_t N>
sofab::FixedString< N >::operator std::string_view ( ) const
inlinenoexcept

Implicit conversion to std::string_view.

Gives a cheap non-owning view and makes the existing string encode branch (OStreamImpl::write) match a FixedString automatically.

Definition at line 471 of file sofab.hpp.

Here is the call graph for this function:

◆ operator!=()

template<std::size_t N>
bool sofab::FixedString< N >::operator!= ( std::string_view  rhs) const
inlinenoexcept

Inequality against any string view-like operand.

Definition at line 492 of file sofab.hpp.

Here is the call graph for this function:

◆ operator=() [1/2]

template<std::size_t N>
FixedString & sofab::FixedString< N >::operator= ( const char *  s)
inlinenoexcept

Assign from a NUL-terminated C string (clamped to N, see assign).

Definition at line 356 of file sofab.hpp.

Here is the call graph for this function:

◆ operator=() [2/2]

template<std::size_t N>
FixedString & sofab::FixedString< N >::operator= ( std::string_view  sv)
inlinenoexcept

Assign from a string view (clamped to N, see assign).

Definition at line 363 of file sofab.hpp.

Here is the call graph for this function:

◆ operator==()

template<std::size_t N>
bool sofab::FixedString< N >::operator== ( std::string_view  rhs) const
inlinenoexcept

Equality against any string view-like operand.

Definition at line 486 of file sofab.hpp.

Here is the call graph for this function:

◆ operator[]() [1/2]

template<std::size_t N>
const char & sofab::FixedString< N >::operator[] ( std::size_t  i) const
inlinenoexcept

Access the character at i (no bounds checking).

Definition at line 441 of file sofab.hpp.

Here is the call graph for this function:

◆ operator[]() [2/2]

template<std::size_t N>
char & sofab::FixedString< N >::operator[] ( std::size_t  i)
inlinenoexcept

Access the character at i (no bounds checking).

Definition at line 439 of file sofab.hpp.

Here is the call graph for this function:

◆ set_len()

template<std::size_t N>
void sofab::FixedString< N >::set_len ( std::size_t  n)
inlinenoexcept

Decode hook: set the logical length and (re)place the trailing NUL.

Called by generated decode before binding the buffer: it fixes size() to the field length (clamped to N) and writes the NUL at buf_[len_]. The subsequent IStreamImpl::read binds exactly size() bytes via sofab_istream_read_string_noterm, which fills [0, size()) and never touches buf_[len_], so the NUL survives and c_str stays valid. Re-decoding a shorter value re-terminates here.

Parameters
nRequested logical length (clamped to N).

Definition at line 414 of file sofab.hpp.

Here is the call graph for this function:

◆ size()

template<std::size_t N>
std::size_t sofab::FixedString< N >::size ( ) const
inlinenoexcept

Number of characters currently stored.

Definition at line 428 of file sofab.hpp.

Here is the caller graph for this function:

◆ view()

template<std::size_t N>
std::string_view sofab::FixedString< N >::view ( ) const
inlinenoexcept

Non-owning view over the current characters.

Definition at line 460 of file sofab.hpp.

Here is the caller graph for this function:

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